2011-10-08 19:16:11 -07:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2011-10-12 19:24:30 -07:00
|
|
|
using NLog;
|
2011-10-22 22:26:43 -07:00
|
|
|
using NzbDrone.Common;
|
2013-05-10 16:53:50 -07:00
|
|
|
using NzbDrone.Common.Composition;
|
2013-05-04 14:09:25 -07:00
|
|
|
using NzbDrone.SysTray;
|
2013-05-10 16:53:50 -07:00
|
|
|
using IServiceProvider = NzbDrone.Common.IServiceProvider;
|
2011-10-08 19:16:11 -07:00
|
|
|
|
|
|
|
namespace NzbDrone
|
|
|
|
{
|
2013-05-10 16:53:50 -07:00
|
|
|
[Singleton]
|
2011-10-12 19:24:30 -07:00
|
|
|
public class Router
|
2011-10-08 19:16:11 -07:00
|
|
|
{
|
2013-04-15 21:52:41 -07:00
|
|
|
private readonly INzbDroneServiceFactory _nzbDroneServiceFactory;
|
2013-05-10 16:53:50 -07:00
|
|
|
private readonly IServiceProvider _serviceProvider;
|
2013-04-15 21:52:41 -07:00
|
|
|
private readonly IConsoleService _consoleService;
|
2013-05-10 16:53:50 -07:00
|
|
|
private readonly IEnvironmentProvider _environmentProvider;
|
|
|
|
private readonly ISystemTrayApp _systemTrayProvider;
|
2013-02-28 16:50:50 -08:00
|
|
|
private readonly Logger _logger;
|
2011-10-08 19:16:11 -07:00
|
|
|
|
2013-05-10 16:53:50 -07:00
|
|
|
public Router(INzbDroneServiceFactory nzbDroneServiceFactory, IServiceProvider serviceProvider,
|
|
|
|
IConsoleService consoleService, IEnvironmentProvider environmentProvider, ISystemTrayApp systemTrayProvider, Logger logger)
|
2011-10-08 19:16:11 -07:00
|
|
|
{
|
2013-04-15 21:52:41 -07:00
|
|
|
_nzbDroneServiceFactory = nzbDroneServiceFactory;
|
2011-10-08 19:16:11 -07:00
|
|
|
_serviceProvider = serviceProvider;
|
2013-04-15 21:52:41 -07:00
|
|
|
_consoleService = consoleService;
|
2012-03-06 18:59:43 -08:00
|
|
|
_environmentProvider = environmentProvider;
|
2013-05-10 16:53:50 -07:00
|
|
|
_systemTrayProvider = systemTrayProvider;
|
2013-02-28 16:50:50 -08:00
|
|
|
_logger = logger;
|
2011-10-08 19:16:11 -07:00
|
|
|
}
|
|
|
|
|
2011-10-13 18:22:51 -07:00
|
|
|
public void Route(IEnumerable<string> args)
|
2011-10-08 19:16:11 -07:00
|
|
|
{
|
2011-10-13 18:22:51 -07:00
|
|
|
Route(GetApplicationMode(args));
|
|
|
|
}
|
|
|
|
|
2013-02-28 16:50:50 -08:00
|
|
|
public void Route(ApplicationModes applicationModes)
|
2011-10-13 18:22:51 -07:00
|
|
|
{
|
2013-02-17 11:19:38 -08:00
|
|
|
if (!_environmentProvider.IsUserInteractive)
|
2011-10-13 18:22:51 -07:00
|
|
|
{
|
2013-02-28 16:50:50 -08:00
|
|
|
applicationModes = ApplicationModes.Service;
|
2011-10-08 19:16:11 -07:00
|
|
|
}
|
2013-01-06 00:11:14 -08:00
|
|
|
|
2013-02-28 16:50:50 -08:00
|
|
|
_logger.Info("Application mode: {0}", applicationModes);
|
2013-01-15 15:17:12 -08:00
|
|
|
|
2013-02-28 16:50:50 -08:00
|
|
|
switch (applicationModes)
|
2013-01-06 00:11:14 -08:00
|
|
|
{
|
2013-02-28 16:50:50 -08:00
|
|
|
case ApplicationModes.Service:
|
2013-01-06 00:11:14 -08:00
|
|
|
{
|
2013-02-28 16:50:50 -08:00
|
|
|
_logger.Trace("Service selected");
|
2013-04-15 21:52:41 -07:00
|
|
|
_serviceProvider.Run(_nzbDroneServiceFactory.Build());
|
2013-01-06 00:11:14 -08:00
|
|
|
break;
|
|
|
|
}
|
2011-10-13 18:22:51 -07:00
|
|
|
|
2013-02-28 16:50:50 -08:00
|
|
|
case ApplicationModes.Console:
|
2013-01-06 00:11:14 -08:00
|
|
|
{
|
2013-02-28 16:50:50 -08:00
|
|
|
_logger.Trace("Console selected");
|
2013-04-15 21:52:41 -07:00
|
|
|
_nzbDroneServiceFactory.Start();
|
|
|
|
if (_consoleService.IsConsoleApplication)
|
|
|
|
{
|
|
|
|
_consoleService.WaitForClose();
|
|
|
|
}
|
2013-01-29 18:21:45 -08:00
|
|
|
else
|
|
|
|
{
|
2013-05-10 16:53:50 -07:00
|
|
|
_systemTrayProvider.Start();
|
2013-01-29 18:21:45 -08:00
|
|
|
}
|
|
|
|
|
2013-01-06 00:11:14 -08:00
|
|
|
break;
|
|
|
|
}
|
2013-02-28 16:50:50 -08:00
|
|
|
case ApplicationModes.InstallService:
|
2013-01-06 00:11:14 -08:00
|
|
|
{
|
2013-02-28 16:50:50 -08:00
|
|
|
_logger.Trace("Install Service selected");
|
2013-01-06 00:11:14 -08:00
|
|
|
if (_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
|
2011-10-13 18:22:51 -07:00
|
|
|
{
|
2013-04-15 21:52:41 -07:00
|
|
|
_consoleService.PrintServiceAlreadyExist();
|
2011-10-13 18:22:51 -07:00
|
|
|
}
|
2013-01-06 00:11:14 -08:00
|
|
|
else
|
2011-10-13 18:22:51 -07:00
|
|
|
{
|
2013-01-06 00:11:14 -08:00
|
|
|
_serviceProvider.Install(ServiceProvider.NZBDRONE_SERVICE_NAME);
|
|
|
|
_serviceProvider.Start(ServiceProvider.NZBDRONE_SERVICE_NAME);
|
2011-10-13 18:22:51 -07:00
|
|
|
}
|
2013-01-06 00:11:14 -08:00
|
|
|
break;
|
|
|
|
}
|
2013-02-28 16:50:50 -08:00
|
|
|
case ApplicationModes.UninstallService:
|
2013-01-06 00:11:14 -08:00
|
|
|
{
|
2013-02-28 16:50:50 -08:00
|
|
|
_logger.Trace("Uninstall Service selected");
|
2013-01-06 00:11:14 -08:00
|
|
|
if (!_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
|
2011-10-13 18:22:51 -07:00
|
|
|
{
|
2013-04-15 21:52:41 -07:00
|
|
|
_consoleService.PrintServiceDoestExist();
|
2011-10-13 18:22:51 -07:00
|
|
|
}
|
2013-01-06 00:11:14 -08:00
|
|
|
else
|
2011-10-13 18:22:51 -07:00
|
|
|
{
|
2013-01-06 00:11:14 -08:00
|
|
|
_serviceProvider.UnInstall(ServiceProvider.NZBDRONE_SERVICE_NAME);
|
2011-10-13 18:22:51 -07:00
|
|
|
}
|
2013-01-06 00:11:14 -08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
2013-04-15 21:52:41 -07:00
|
|
|
_consoleService.PrintHelp();
|
2013-01-06 00:11:14 -08:00
|
|
|
break;
|
|
|
|
}
|
2011-10-13 18:22:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 16:50:50 -08:00
|
|
|
public static ApplicationModes GetApplicationMode(IEnumerable<string> args)
|
2011-10-13 18:22:51 -07:00
|
|
|
{
|
2013-02-28 16:50:50 -08:00
|
|
|
if (args == null) return ApplicationModes.Console;
|
2011-10-13 18:22:51 -07:00
|
|
|
|
|
|
|
var cleanArgs = args.Where(c => c != null && !String.IsNullOrWhiteSpace(c)).ToList();
|
2013-02-28 16:50:50 -08:00
|
|
|
if (cleanArgs.Count == 0) return ApplicationModes.Console;
|
|
|
|
if (cleanArgs.Count != 1) return ApplicationModes.Help;
|
2011-10-13 18:22:51 -07:00
|
|
|
|
|
|
|
var arg = cleanArgs.First().Trim('/', '\\', '-').ToLower();
|
|
|
|
|
2013-02-28 16:50:50 -08:00
|
|
|
if (arg == "i") return ApplicationModes.InstallService;
|
|
|
|
if (arg == "u") return ApplicationModes.UninstallService;
|
2011-10-13 18:22:51 -07:00
|
|
|
|
2013-02-28 16:50:50 -08:00
|
|
|
return ApplicationModes.Help;
|
2011-10-08 19:16:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|