Sonarr/NzbDrone/Router.cs

120 lines
4.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
2011-10-13 02:24:30 +00:00
using NLog;
using NzbDrone.Common;
using NzbDrone.Common.SysTray;
namespace NzbDrone
{
2011-10-13 02:24:30 +00:00
public class Router
{
2011-10-11 07:11:05 +00:00
private readonly ApplicationServer _applicationServer;
private readonly ServiceProvider _serviceProvider;
private readonly ConsoleProvider _consoleProvider;
private readonly EnvironmentProvider _environmentProvider;
private readonly SysTrayProvider _sysTrayProvider;
2013-03-01 00:50:50 +00:00
private readonly Logger _logger;
public Router(ApplicationServer applicationServer, ServiceProvider serviceProvider,
2013-03-01 00:50:50 +00:00
ConsoleProvider consoleProvider, EnvironmentProvider environmentProvider, SysTrayProvider sysTrayProvider, Logger logger)
{
2011-10-11 07:11:05 +00:00
_applicationServer = applicationServer;
_serviceProvider = serviceProvider;
2011-10-13 02:24:30 +00:00
_consoleProvider = consoleProvider;
_environmentProvider = environmentProvider;
_sysTrayProvider = sysTrayProvider;
2013-03-01 00:50:50 +00:00
_logger = logger;
}
public void Route(IEnumerable<string> args)
{
Route(GetApplicationMode(args));
}
2013-03-01 00:50:50 +00:00
public void Route(ApplicationModes applicationModes)
{
if (!_environmentProvider.IsUserInteractive)
{
2013-03-01 00:50:50 +00:00
applicationModes = ApplicationModes.Service;
}
2013-03-01 00:50:50 +00:00
_logger.Info("Application mode: {0}", applicationModes);
2013-03-01 00:50:50 +00:00
switch (applicationModes)
{
2013-03-01 00:50:50 +00:00
case ApplicationModes.Service:
{
2013-03-01 00:50:50 +00:00
_logger.Trace("Service selected");
_serviceProvider.Run(_applicationServer);
break;
}
2013-03-01 00:50:50 +00:00
case ApplicationModes.Console:
{
2013-03-01 00:50:50 +00:00
_logger.Trace("Console selected");
_applicationServer.Start();
if (ConsoleProvider.IsConsoleApplication)
_consoleProvider.WaitForClose();
else
{
_sysTrayProvider.Start();
}
break;
}
2013-03-01 00:50:50 +00:00
case ApplicationModes.InstallService:
{
2013-03-01 00:50:50 +00:00
_logger.Trace("Install Service selected");
if (_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
{
_consoleProvider.PrintServiceAlreadyExist();
}
else
{
_serviceProvider.Install(ServiceProvider.NZBDRONE_SERVICE_NAME);
_serviceProvider.Start(ServiceProvider.NZBDRONE_SERVICE_NAME);
}
break;
}
2013-03-01 00:50:50 +00:00
case ApplicationModes.UninstallService:
{
2013-03-01 00:50:50 +00:00
_logger.Trace("Uninstall Service selected");
if (!_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
{
_consoleProvider.PrintServiceDoestExist();
}
else
{
_serviceProvider.UnInstall(ServiceProvider.NZBDRONE_SERVICE_NAME);
}
break;
}
default:
{
_consoleProvider.PrintHelp();
break;
}
}
}
2013-03-01 00:50:50 +00:00
public static ApplicationModes GetApplicationMode(IEnumerable<string> args)
{
2013-03-01 00:50:50 +00:00
if (args == null) return ApplicationModes.Console;
var cleanArgs = args.Where(c => c != null && !String.IsNullOrWhiteSpace(c)).ToList();
2013-03-01 00:50:50 +00:00
if (cleanArgs.Count == 0) return ApplicationModes.Console;
if (cleanArgs.Count != 1) return ApplicationModes.Help;
var arg = cleanArgs.First().Trim('/', '\\', '-').ToLower();
2013-03-01 00:50:50 +00:00
if (arg == "i") return ApplicationModes.InstallService;
if (arg == "u") return ApplicationModes.UninstallService;
2013-03-01 00:50:50 +00:00
return ApplicationModes.Help;
}
}
}