1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-11-01 08:22:35 +01:00
Sonarr/NzbDrone/Router.cs

127 lines
4.5 KiB
C#
Raw Normal View History

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