2011-10-09 02:16:11 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2011-10-14 01:22:51 +00:00
|
|
|
|
using System.Diagnostics;
|
2011-10-09 02:16:11 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2011-10-14 01:22:51 +00:00
|
|
|
|
using System.Threading;
|
2011-10-13 02:24:30 +00:00
|
|
|
|
using NLog;
|
2011-10-09 17:45:08 +00:00
|
|
|
|
using NzbDrone.Model;
|
2011-10-09 02:16:11 +00:00
|
|
|
|
using NzbDrone.Providers;
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone
|
|
|
|
|
{
|
2011-10-13 02:24:30 +00:00
|
|
|
|
public class Router
|
2011-10-09 02:16:11 +00:00
|
|
|
|
{
|
2011-10-13 02:24:30 +00:00
|
|
|
|
private static readonly Logger Logger = LogManager.GetLogger("Host.Router");
|
|
|
|
|
|
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;
|
2011-10-14 01:22:51 +00:00
|
|
|
|
private readonly EnviromentProvider _enviromentProvider;
|
2011-10-09 02:16:11 +00:00
|
|
|
|
|
2011-10-14 01:22:51 +00:00
|
|
|
|
public Router(ApplicationServer applicationServer, ServiceProvider serviceProvider, ConsoleProvider consoleProvider, EnviromentProvider enviromentProvider)
|
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;
|
2011-10-14 01:22:51 +00:00
|
|
|
|
_enviromentProvider = enviromentProvider;
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Route(ApplicationMode applicationMode)
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Application mode: {0}", applicationMode);
|
|
|
|
|
|
|
|
|
|
_applicationServer.IsRunningAsService();
|
|
|
|
|
|
|
|
|
|
while (!Debugger.IsAttached)
|
2011-10-09 02:16:11 +00:00
|
|
|
|
{
|
2011-10-14 01:22:51 +00:00
|
|
|
|
Thread.Sleep(1000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (_enviromentProvider.IsRunningAsService)
|
|
|
|
|
{
|
|
|
|
|
_applicationServer.StartService();
|
2011-10-13 02:24:30 +00:00
|
|
|
|
|
2011-10-09 02:16:11 +00:00
|
|
|
|
}
|
2011-10-14 01:22:51 +00: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:
|
|
|
|
|
{
|
|
|
|
|
_serviceProvider.UnInstall();
|
|
|
|
|
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 02:16:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|