2013-07-19 03:47:55 +00:00
|
|
|
|
using NLog;
|
2011-10-23 05:26:43 +00:00
|
|
|
|
using NzbDrone.Common;
|
2013-06-28 00:04:52 +00:00
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2013-05-04 21:09:25 +00:00
|
|
|
|
using NzbDrone.SysTray;
|
2013-05-10 23:53:50 +00:00
|
|
|
|
using IServiceProvider = NzbDrone.Common.IServiceProvider;
|
2011-10-09 02:16:11 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone
|
|
|
|
|
{
|
2011-10-13 02:24:30 +00:00
|
|
|
|
public class Router
|
2011-10-09 02:16:11 +00:00
|
|
|
|
{
|
2013-04-16 04:52:41 +00:00
|
|
|
|
private readonly INzbDroneServiceFactory _nzbDroneServiceFactory;
|
2013-05-10 23:53:50 +00:00
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
2013-07-09 00:47:09 +00:00
|
|
|
|
private readonly StartupArguments _startupArguments;
|
2013-04-16 04:52:41 +00:00
|
|
|
|
private readonly IConsoleService _consoleService;
|
2013-06-28 00:04:52 +00:00
|
|
|
|
private readonly IRuntimeInfo _runtimeInfo;
|
2013-05-10 23:53:50 +00:00
|
|
|
|
private readonly ISystemTrayApp _systemTrayProvider;
|
2013-03-01 00:50:50 +00:00
|
|
|
|
private readonly Logger _logger;
|
2011-10-09 02:16:11 +00:00
|
|
|
|
|
2013-07-09 00:47:09 +00:00
|
|
|
|
public Router(INzbDroneServiceFactory nzbDroneServiceFactory, IServiceProvider serviceProvider, StartupArguments startupArguments,
|
2013-06-28 00:04:52 +00:00
|
|
|
|
IConsoleService consoleService, IRuntimeInfo runtimeInfo, ISystemTrayApp systemTrayProvider, Logger logger)
|
2011-10-09 02:16:11 +00:00
|
|
|
|
{
|
2013-04-16 04:52:41 +00:00
|
|
|
|
_nzbDroneServiceFactory = nzbDroneServiceFactory;
|
2011-10-09 02:16:11 +00:00
|
|
|
|
_serviceProvider = serviceProvider;
|
2013-07-09 00:47:09 +00:00
|
|
|
|
_startupArguments = startupArguments;
|
2013-04-16 04:52:41 +00:00
|
|
|
|
_consoleService = consoleService;
|
2013-06-28 00:04:52 +00:00
|
|
|
|
_runtimeInfo = runtimeInfo;
|
2013-05-10 23:53:50 +00:00
|
|
|
|
_systemTrayProvider = systemTrayProvider;
|
2013-03-01 00:50:50 +00:00
|
|
|
|
_logger = logger;
|
2011-10-09 02:16:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-09 00:47:09 +00:00
|
|
|
|
public void Route()
|
2011-10-09 02:16:11 +00:00
|
|
|
|
{
|
2013-07-09 00:47:09 +00:00
|
|
|
|
var appMode = GetApplicationMode();
|
|
|
|
|
Route(appMode);
|
2011-10-14 01:22:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-01 00:50:50 +00:00
|
|
|
|
public void Route(ApplicationModes applicationModes)
|
2011-10-14 01:22:51 +00:00
|
|
|
|
{
|
2013-06-19 21:08:56 +00:00
|
|
|
|
if (!_runtimeInfo.IsUserInteractive && !OsInfo.IsLinux)
|
2011-10-14 01:22:51 +00:00
|
|
|
|
{
|
2013-03-01 00:50:50 +00:00
|
|
|
|
applicationModes = ApplicationModes.Service;
|
2011-10-09 02:16:11 +00:00
|
|
|
|
}
|
2013-01-06 08:11:14 +00:00
|
|
|
|
|
2013-03-01 00:50:50 +00:00
|
|
|
|
_logger.Info("Application mode: {0}", applicationModes);
|
2013-01-15 23:17:12 +00:00
|
|
|
|
|
2013-03-01 00:50:50 +00:00
|
|
|
|
switch (applicationModes)
|
2013-01-06 08:11:14 +00:00
|
|
|
|
{
|
2013-03-01 00:50:50 +00:00
|
|
|
|
case ApplicationModes.Service:
|
2013-01-06 08:11:14 +00:00
|
|
|
|
{
|
2013-03-01 00:50:50 +00:00
|
|
|
|
_logger.Trace("Service selected");
|
2013-04-16 04:52:41 +00:00
|
|
|
|
_serviceProvider.Run(_nzbDroneServiceFactory.Build());
|
2013-01-06 08:11:14 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2011-10-14 01:22:51 +00:00
|
|
|
|
|
2013-03-01 00:50:50 +00:00
|
|
|
|
case ApplicationModes.Console:
|
2013-01-06 08:11:14 +00:00
|
|
|
|
{
|
2013-03-01 00:50:50 +00:00
|
|
|
|
_logger.Trace("Console selected");
|
2013-04-16 04:52:41 +00:00
|
|
|
|
_nzbDroneServiceFactory.Start();
|
|
|
|
|
if (_consoleService.IsConsoleApplication)
|
|
|
|
|
{
|
|
|
|
|
_consoleService.WaitForClose();
|
|
|
|
|
}
|
2013-01-30 02:21:45 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2013-05-10 23:53:50 +00:00
|
|
|
|
_systemTrayProvider.Start();
|
2013-01-30 02:21:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-01-06 08:11:14 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2013-03-01 00:50:50 +00:00
|
|
|
|
case ApplicationModes.InstallService:
|
2013-01-06 08:11:14 +00:00
|
|
|
|
{
|
2013-03-01 00:50:50 +00:00
|
|
|
|
_logger.Trace("Install Service selected");
|
2013-01-06 08:11:14 +00:00
|
|
|
|
if (_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
|
2011-10-14 01:22:51 +00:00
|
|
|
|
{
|
2013-04-16 04:52:41 +00:00
|
|
|
|
_consoleService.PrintServiceAlreadyExist();
|
2011-10-14 01:22:51 +00:00
|
|
|
|
}
|
2013-01-06 08:11:14 +00:00
|
|
|
|
else
|
2011-10-14 01:22:51 +00:00
|
|
|
|
{
|
2013-01-06 08:11:14 +00:00
|
|
|
|
_serviceProvider.Install(ServiceProvider.NZBDRONE_SERVICE_NAME);
|
|
|
|
|
_serviceProvider.Start(ServiceProvider.NZBDRONE_SERVICE_NAME);
|
2011-10-14 01:22:51 +00:00
|
|
|
|
}
|
2013-01-06 08:11:14 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2013-03-01 00:50:50 +00:00
|
|
|
|
case ApplicationModes.UninstallService:
|
2013-01-06 08:11:14 +00:00
|
|
|
|
{
|
2013-03-01 00:50:50 +00:00
|
|
|
|
_logger.Trace("Uninstall Service selected");
|
2013-01-06 08:11:14 +00:00
|
|
|
|
if (!_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
|
2011-10-14 01:22:51 +00:00
|
|
|
|
{
|
2013-07-09 00:47:09 +00:00
|
|
|
|
_consoleService.PrintServiceDoesNotExist();
|
2011-10-14 01:22:51 +00:00
|
|
|
|
}
|
2013-01-06 08:11:14 +00:00
|
|
|
|
else
|
2011-10-14 01:22:51 +00:00
|
|
|
|
{
|
2013-01-06 08:11:14 +00:00
|
|
|
|
_serviceProvider.UnInstall(ServiceProvider.NZBDRONE_SERVICE_NAME);
|
2011-10-14 01:22:51 +00:00
|
|
|
|
}
|
2013-01-06 08:11:14 +00:00
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
{
|
2013-04-16 04:52:41 +00:00
|
|
|
|
_consoleService.PrintHelp();
|
2013-01-06 08:11:14 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2011-10-14 01:22:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-09 00:47:09 +00:00
|
|
|
|
private ApplicationModes GetApplicationMode()
|
2011-10-14 01:22:51 +00:00
|
|
|
|
{
|
2013-07-09 00:47:09 +00:00
|
|
|
|
if (_startupArguments.Flags.Contains(StartupArguments.HELP))
|
|
|
|
|
{
|
|
|
|
|
return ApplicationModes.Help;
|
|
|
|
|
}
|
2011-10-14 01:22:51 +00:00
|
|
|
|
|
2013-07-09 00:47:09 +00:00
|
|
|
|
if (_startupArguments.Flags.Contains(StartupArguments.INSTALL_SERVICE))
|
|
|
|
|
{
|
|
|
|
|
return ApplicationModes.InstallService;
|
|
|
|
|
}
|
2011-10-14 01:22:51 +00:00
|
|
|
|
|
2013-07-09 00:47:09 +00:00
|
|
|
|
if (_startupArguments.Flags.Contains(StartupArguments.UNINSTALL_SERVICE))
|
|
|
|
|
{
|
|
|
|
|
return ApplicationModes.UninstallService;
|
|
|
|
|
}
|
2011-10-14 01:22:51 +00:00
|
|
|
|
|
2013-07-09 00:47:09 +00:00
|
|
|
|
return ApplicationModes.Console;
|
2011-10-09 02:16:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|