2011-10-09 02:16:11 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2011-10-13 02:24:30 +00:00
|
|
|
|
using NLog;
|
2011-10-23 05:26:43 +00:00
|
|
|
|
using NzbDrone.Common;
|
2013-01-30 02:21:45 +00:00
|
|
|
|
using NzbDrone.Common.SysTray;
|
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
|
|
|
|
{
|
2011-10-11 07:11:05 +00:00
|
|
|
|
private readonly ApplicationServer _applicationServer;
|
2011-10-09 02:16:11 +00:00
|
|
|
|
private readonly ServiceProvider _serviceProvider;
|
|
|
|
|
private readonly ConsoleProvider _consoleProvider;
|
2012-03-07 02:59:43 +00:00
|
|
|
|
private readonly EnvironmentProvider _environmentProvider;
|
2013-01-30 02:21:45 +00:00
|
|
|
|
private readonly SysTrayProvider _sysTrayProvider;
|
2013-03-01 00:50:50 +00:00
|
|
|
|
private readonly Logger _logger;
|
2011-10-09 02:16:11 +00:00
|
|
|
|
|
2013-01-06 08:11:14 +00:00
|
|
|
|
public Router(ApplicationServer applicationServer, ServiceProvider serviceProvider,
|
2013-03-01 00:50:50 +00:00
|
|
|
|
ConsoleProvider consoleProvider, EnvironmentProvider environmentProvider, SysTrayProvider sysTrayProvider, Logger logger)
|
2011-10-09 02:16:11 +00:00
|
|
|
|
{
|
2011-10-11 07:11:05 +00:00
|
|
|
|
_applicationServer = applicationServer;
|
2011-10-09 02:16:11 +00:00
|
|
|
|
_serviceProvider = serviceProvider;
|
2011-10-13 02:24:30 +00:00
|
|
|
|
_consoleProvider = consoleProvider;
|
2012-03-07 02:59:43 +00:00
|
|
|
|
_environmentProvider = environmentProvider;
|
2013-01-30 02:21:45 +00:00
|
|
|
|
_sysTrayProvider = sysTrayProvider;
|
2013-03-01 00:50:50 +00:00
|
|
|
|
_logger = logger;
|
2011-10-09 02:16:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-14 01:22:51 +00:00
|
|
|
|
public void Route(IEnumerable<string> args)
|
2011-10-09 02:16:11 +00:00
|
|
|
|
{
|
2011-10-14 01:22:51 +00:00
|
|
|
|
Route(GetApplicationMode(args));
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-01 00:50:50 +00:00
|
|
|
|
public void Route(ApplicationModes applicationModes)
|
2011-10-14 01:22:51 +00:00
|
|
|
|
{
|
2013-02-17 19:19:38 +00:00
|
|
|
|
if (!_environmentProvider.IsUserInteractive)
|
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-01-06 08:11:14 +00:00
|
|
|
|
_serviceProvider.Run(_applicationServer);
|
|
|
|
|
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-01-06 08:11:14 +00:00
|
|
|
|
_applicationServer.Start();
|
2013-02-17 19:19:38 +00:00
|
|
|
|
if (ConsoleProvider.IsConsoleApplication)
|
2013-01-30 02:21:45 +00:00
|
|
|
|
_consoleProvider.WaitForClose();
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_sysTrayProvider.Start();
|
|
|
|
|
}
|
|
|
|
|
|
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-01-06 08:11:14 +00:00
|
|
|
|
_consoleProvider.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-01-06 08:11:14 +00:00
|
|
|
|
_consoleProvider.PrintServiceDoestExist();
|
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:
|
|
|
|
|
{
|
|
|
|
|
_consoleProvider.PrintHelp();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2011-10-14 01:22:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-01 00:50:50 +00:00
|
|
|
|
public static ApplicationModes GetApplicationMode(IEnumerable<string> args)
|
2011-10-14 01:22:51 +00:00
|
|
|
|
{
|
2013-03-01 00:50:50 +00:00
|
|
|
|
if (args == null) return ApplicationModes.Console;
|
2011-10-14 01:22:51 +00:00
|
|
|
|
|
|
|
|
|
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;
|
2011-10-14 01:22:51 +00:00
|
|
|
|
|
|
|
|
|
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;
|
2011-10-14 01:22:51 +00:00
|
|
|
|
|
2013-03-01 00:50:50 +00:00
|
|
|
|
return ApplicationModes.Help;
|
2011-10-09 02:16:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|