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;
|
2011-10-23 07:26:43 +02:00
|
|
|
|
using NzbDrone.Common;
|
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
|
|
|
|
{
|
2011-10-13 04:24:30 +02:00
|
|
|
|
private static readonly Logger Logger = LogManager.GetLogger("Host.Router");
|
|
|
|
|
|
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;
|
2011-10-14 03:22:51 +02:00
|
|
|
|
private readonly EnviromentProvider _enviromentProvider;
|
2011-10-09 04:16:11 +02:00
|
|
|
|
|
2011-10-14 03:22:51 +02:00
|
|
|
|
public Router(ApplicationServer applicationServer, ServiceProvider serviceProvider, ConsoleProvider consoleProvider, EnviromentProvider enviromentProvider)
|
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;
|
2011-10-14 03:22:51 +02:00
|
|
|
|
_enviromentProvider = enviromentProvider;
|
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)
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Application mode: {0}", applicationMode);
|
|
|
|
|
|
2011-10-15 02:41:09 +02:00
|
|
|
|
if (!_enviromentProvider.IsUserInteractive)
|
2011-10-14 03:22:51 +02:00
|
|
|
|
{
|
2011-10-15 02:41:09 +02:00
|
|
|
|
_serviceProvider.Run(_applicationServer);
|
2011-10-09 04:16:11 +02:00
|
|
|
|
}
|
2011-10-14 03:22:51 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
switch (applicationMode)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
case ApplicationMode.Console:
|
|
|
|
|
{
|
|
|
|
|
_applicationServer.Start();
|
|
|
|
|
_consoleProvider.WaitForClose();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ApplicationMode.InstallService:
|
|
|
|
|
{
|
|
|
|
|
if (_serviceProvider.ServiceExist(ServiceProvider.NzbDroneServiceName))
|
|
|
|
|
{
|
|
|
|
|
_consoleProvider.PrintServiceAlreadyExist();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_serviceProvider.Install();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ApplicationMode.UninstallService:
|
|
|
|
|
{
|
2011-10-15 02:41:09 +02:00
|
|
|
|
if (!_serviceProvider.ServiceExist(ServiceProvider.NzbDroneServiceName))
|
|
|
|
|
{
|
|
|
|
|
_consoleProvider.PrintServiceDoestExist();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_serviceProvider.UnInstall();
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-14 03:22:51 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
_consoleProvider.PrintHelp();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
return ApplicationMode.Help;
|
2011-10-09 04:16:11 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|