2011-10-09 04:16:11 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2011-10-13 04:24:30 +02:00
|
|
|
|
using NLog;
|
2013-02-17 20:19:38 +01:00
|
|
|
|
using Nancy.Hosting.Self;
|
|
|
|
|
using NzbDrone.Api;
|
2011-10-23 07:26:43 +02:00
|
|
|
|
using NzbDrone.Common;
|
2013-01-30 03:21:45 +01:00
|
|
|
|
using NzbDrone.Common.SysTray;
|
2011-10-09 19:45:08 +02:00
|
|
|
|
using NzbDrone.Model;
|
2011-10-09 04:16:11 +02:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone
|
|
|
|
|
{
|
2011-10-13 04:24:30 +02:00
|
|
|
|
public class Router
|
2011-10-09 04:16:11 +02:00
|
|
|
|
{
|
2012-02-11 01:48:20 +01:00
|
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
2011-10-13 04:24:30 +02:00
|
|
|
|
|
2011-10-11 09:11:05 +02:00
|
|
|
|
private readonly ApplicationServer _applicationServer;
|
2011-10-09 04:16:11 +02:00
|
|
|
|
private readonly ServiceProvider _serviceProvider;
|
|
|
|
|
private readonly ConsoleProvider _consoleProvider;
|
2012-03-07 03:59:43 +01:00
|
|
|
|
private readonly EnvironmentProvider _environmentProvider;
|
2013-01-30 03:21:45 +01:00
|
|
|
|
private readonly SysTrayProvider _sysTrayProvider;
|
2013-02-17 20:19:38 +01:00
|
|
|
|
private readonly ProcessProvider _processProvider;
|
2011-10-09 04:16:11 +02:00
|
|
|
|
|
2013-01-06 09:11:14 +01:00
|
|
|
|
public Router(ApplicationServer applicationServer, ServiceProvider serviceProvider,
|
2013-02-17 20:19:38 +01:00
|
|
|
|
ConsoleProvider consoleProvider, EnvironmentProvider environmentProvider, SysTrayProvider sysTrayProvider, ProcessProvider processProvider)
|
2011-10-09 04:16:11 +02:00
|
|
|
|
{
|
2011-10-11 09:11:05 +02:00
|
|
|
|
_applicationServer = applicationServer;
|
2011-10-09 04:16:11 +02:00
|
|
|
|
_serviceProvider = serviceProvider;
|
2011-10-13 04:24:30 +02:00
|
|
|
|
_consoleProvider = consoleProvider;
|
2012-03-07 03:59:43 +01:00
|
|
|
|
_environmentProvider = environmentProvider;
|
2013-01-30 03:21:45 +01:00
|
|
|
|
_sysTrayProvider = sysTrayProvider;
|
2013-02-17 20:19:38 +01:00
|
|
|
|
_processProvider = processProvider;
|
2011-10-09 04:16:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-14 03:22:51 +02:00
|
|
|
|
public void Route(IEnumerable<string> args)
|
2011-10-09 04:16:11 +02:00
|
|
|
|
{
|
2011-10-14 03:22:51 +02:00
|
|
|
|
Route(GetApplicationMode(args));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Route(ApplicationMode applicationMode)
|
|
|
|
|
{
|
2013-02-17 20:19:38 +01:00
|
|
|
|
if (!_environmentProvider.IsUserInteractive)
|
2011-10-14 03:22:51 +02:00
|
|
|
|
{
|
2013-01-06 09:11:14 +01:00
|
|
|
|
applicationMode = ApplicationMode.Service;
|
2011-10-09 04:16:11 +02:00
|
|
|
|
}
|
2013-01-06 09:11:14 +01:00
|
|
|
|
|
2013-01-16 00:17:12 +01:00
|
|
|
|
logger.Info("Application mode: {0}", applicationMode);
|
|
|
|
|
|
|
|
|
|
switch (applicationMode)
|
2013-01-06 09:11:14 +01:00
|
|
|
|
{
|
2013-02-17 20:19:38 +01:00
|
|
|
|
case ApplicationMode.Nancy:
|
|
|
|
|
{
|
|
|
|
|
|
2013-02-19 02:13:42 +01:00
|
|
|
|
var nancyHost = new NancyHost(new Uri("http://localhost:8282"), new NancyBootstrapper());
|
2013-02-17 20:19:38 +01:00
|
|
|
|
nancyHost.Start();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_processProvider.Start("http://localhost:8282");
|
|
|
|
|
|
|
|
|
|
_consoleProvider.WaitForClose();
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-06 09:11:14 +01:00
|
|
|
|
case ApplicationMode.Service:
|
|
|
|
|
{
|
2013-01-16 00:17:12 +01:00
|
|
|
|
logger.Trace("Service selected");
|
2013-01-06 09:11:14 +01:00
|
|
|
|
_serviceProvider.Run(_applicationServer);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2011-10-14 03:22:51 +02:00
|
|
|
|
|
2013-01-06 09:11:14 +01:00
|
|
|
|
case ApplicationMode.Console:
|
|
|
|
|
{
|
2013-01-16 00:17:12 +01:00
|
|
|
|
logger.Trace("Console selected");
|
2013-01-06 09:11:14 +01:00
|
|
|
|
_applicationServer.Start();
|
2013-02-17 20:19:38 +01:00
|
|
|
|
if (ConsoleProvider.IsConsoleApplication)
|
2013-01-30 03:21:45 +01:00
|
|
|
|
_consoleProvider.WaitForClose();
|
|
|
|
|
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_sysTrayProvider.Start();
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-06 09:11:14 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ApplicationMode.InstallService:
|
|
|
|
|
{
|
2013-01-16 00:17:12 +01:00
|
|
|
|
logger.Trace("Install Service selected");
|
2013-01-06 09:11:14 +01:00
|
|
|
|
if (_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
|
2011-10-14 03:22:51 +02:00
|
|
|
|
{
|
2013-01-06 09:11:14 +01:00
|
|
|
|
_consoleProvider.PrintServiceAlreadyExist();
|
2011-10-14 03:22:51 +02:00
|
|
|
|
}
|
2013-01-06 09:11:14 +01:00
|
|
|
|
else
|
2011-10-14 03:22:51 +02:00
|
|
|
|
{
|
2013-01-06 09:11:14 +01:00
|
|
|
|
_serviceProvider.Install(ServiceProvider.NZBDRONE_SERVICE_NAME);
|
|
|
|
|
_serviceProvider.Start(ServiceProvider.NZBDRONE_SERVICE_NAME);
|
2011-10-14 03:22:51 +02:00
|
|
|
|
}
|
2013-01-06 09:11:14 +01:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ApplicationMode.UninstallService:
|
|
|
|
|
{
|
2013-01-16 00:17:12 +01:00
|
|
|
|
logger.Trace("Uninstall Service selected");
|
2013-01-06 09:11:14 +01:00
|
|
|
|
if (!_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
|
2011-10-14 03:22:51 +02:00
|
|
|
|
{
|
2013-01-06 09:11:14 +01:00
|
|
|
|
_consoleProvider.PrintServiceDoestExist();
|
2011-10-14 03:22:51 +02:00
|
|
|
|
}
|
2013-01-06 09:11:14 +01:00
|
|
|
|
else
|
2011-10-14 03:22:51 +02:00
|
|
|
|
{
|
2013-01-06 09:11:14 +01:00
|
|
|
|
_serviceProvider.UnInstall(ServiceProvider.NZBDRONE_SERVICE_NAME);
|
2011-10-14 03:22:51 +02:00
|
|
|
|
}
|
2013-01-06 09:11:14 +01:00
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
_consoleProvider.PrintHelp();
|
|
|
|
|
break;
|
|
|
|
|
}
|
2011-10-14 03:22:51 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ApplicationMode GetApplicationMode(IEnumerable<string> args)
|
|
|
|
|
{
|
|
|
|
|
if (args == null) return ApplicationMode.Console;
|
|
|
|
|
|
|
|
|
|
var cleanArgs = args.Where(c => c != null && !String.IsNullOrWhiteSpace(c)).ToList();
|
|
|
|
|
if (cleanArgs.Count == 0) return ApplicationMode.Console;
|
|
|
|
|
if (cleanArgs.Count != 1) return ApplicationMode.Help;
|
|
|
|
|
|
|
|
|
|
var arg = cleanArgs.First().Trim('/', '\\', '-').ToLower();
|
|
|
|
|
|
|
|
|
|
if (arg == "i") return ApplicationMode.InstallService;
|
|
|
|
|
if (arg == "u") return ApplicationMode.UninstallService;
|
2013-02-17 20:19:38 +01:00
|
|
|
|
if (arg == "n") return ApplicationMode.Nancy;
|
2011-10-14 03:22:51 +02:00
|
|
|
|
|
|
|
|
|
return ApplicationMode.Help;
|
2011-10-09 04:16:11 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|