diff --git a/NzbDrone.App.Test/CentralDispatchTests.cs b/NzbDrone.App.Test/CentralDispatchTests.cs new file mode 100644 index 000000000..67d1790c8 --- /dev/null +++ b/NzbDrone.App.Test/CentralDispatchTests.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using FluentAssertions; +using NUnit.Framework; +using Ninject; +using NzbDrone.Providers; + +namespace NzbDrone.App.Test +{ + [TestFixture] + public class CentralDispatchTests + { + [Test] + public void Kernel_can_get_kernel() + { + CentralDispatch.Kernel.Should().NotBeNull(); + } + + [Test] + public void Kernel_should_return_same_kernel() + { + var firstKernel = CentralDispatch.Kernel; + var secondKernel = CentralDispatch.Kernel; + + firstKernel.Should().BeSameAs(secondKernel); + } + + [Test] + public void Kernel_should_be_able_to_resolve_ApplicationServer() + { + var appServer = CentralDispatch.Kernel.Get(); + + appServer.Should().NotBeNull(); + } + + } +} diff --git a/NzbDrone.App.Test/NzbDrone.App.Test.csproj b/NzbDrone.App.Test/NzbDrone.App.Test.csproj index efcc045e1..12b6df60f 100644 --- a/NzbDrone.App.Test/NzbDrone.App.Test.csproj +++ b/NzbDrone.App.Test/NzbDrone.App.Test.csproj @@ -49,6 +49,7 @@ ..\packages\Moq.4.0.10827\lib\NET40\Moq.dll + ..\packages\NUnit.2.5.10.11092\lib\nunit.framework.dll @@ -72,6 +73,7 @@ + diff --git a/NzbDrone.App.Test/ProgramTest.cs b/NzbDrone.App.Test/ProgramTest.cs index 388a3ab0e..464fad9d4 100644 --- a/NzbDrone.App.Test/ProgramTest.cs +++ b/NzbDrone.App.Test/ProgramTest.cs @@ -29,7 +29,7 @@ namespace NzbDrone.App.Test [TestCase("-u", ApplicationMode.UninstallService)] public void GetApplicationMode_single_arg(string arg, ApplicationMode mode) { - Console.GetApplicationMode(new[] { arg }).Should().Be(mode); + NzbDroneConsole.GetApplicationMode(new[] { arg }).Should().Be(mode); } [TestCase("", "", ApplicationMode.Console)] @@ -37,7 +37,7 @@ namespace NzbDrone.App.Test [TestCase("i", "n", ApplicationMode.Help)] public void GetApplicationMode_two_args(string a, string b, ApplicationMode mode) { - Console.GetApplicationMode(new[] { a, b }).Should().Be(mode); + NzbDroneConsole.GetApplicationMode(new[] { a, b }).Should().Be(mode); } } } diff --git a/NzbDrone/Application.cs b/NzbDrone/ApplicationServer.cs similarity index 84% rename from NzbDrone/Application.cs rename to NzbDrone/ApplicationServer.cs index 13fe665cf..41665b52d 100644 --- a/NzbDrone/Application.cs +++ b/NzbDrone/ApplicationServer.cs @@ -7,7 +7,7 @@ using NzbDrone.Providers; namespace NzbDrone { - public class Application + public class ApplicationServer { private static readonly Logger Logger = LogManager.GetLogger("Host.App"); @@ -19,7 +19,7 @@ namespace NzbDrone private readonly WebClient _webClient; [Inject] - public Application(ConfigProvider configProvider, WebClient webClient, IISProvider iisProvider, + public ApplicationServer(ConfigProvider configProvider, WebClient webClient, IISProvider iisProvider, DebuggerProvider debuggerProvider, EnviromentProvider enviromentProvider, ProcessProvider processProvider) { @@ -29,14 +29,9 @@ namespace NzbDrone _debuggerProvider = debuggerProvider; _enviromentProvider = enviromentProvider; _processProvider = processProvider; - - _configProvider.ConfigureNlog(); - _configProvider.CreateDefaultConfigFile(); - Logger.Info("Starting NZBDrone. Start-up Path:'{0}'", _enviromentProvider.ApplicationPath); - Thread.CurrentThread.Name = "Host"; } - public Application() + public ApplicationServer() { } diff --git a/NzbDrone/CentralDispatch.cs b/NzbDrone/CentralDispatch.cs new file mode 100644 index 000000000..a0a55dfd4 --- /dev/null +++ b/NzbDrone/CentralDispatch.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using Ninject; +using NzbDrone.Model; +using NzbDrone.Providers; + +namespace NzbDrone +{ + public static class CentralDispatch + { + private static StandardKernel _kernel; + + static CentralDispatch() + { + _kernel = new StandardKernel(); + } + + public static ApplicationMode ApplicationMode { get; set; } + + public static StandardKernel Kernel + { + get + { + return _kernel; + } + } + + private static void BindKernel() + { + _kernel = new StandardKernel(); + _kernel.Bind().ToSelf().InSingletonScope(); + _kernel.Bind().ToSelf().InSingletonScope(); + _kernel.Bind().ToSelf().InSingletonScope(); + _kernel.Bind().ToSelf().InSingletonScope(); + _kernel.Bind().ToSelf().InSingletonScope(); + _kernel.Bind().ToSelf().InSingletonScope(); + _kernel.Bind().ToSelf().InSingletonScope(); + _kernel.Bind().ToSelf().InSingletonScope(); + _kernel.Bind().ToSelf().InSingletonScope(); + } + + private static void InitilizeApp() + { + _kernel.Get().ConfigureNlog(); + _kernel.Get().CreateDefaultConfigFile(); + Logger.Info("Starting NZBDrone. Start-up Path:'{0}'", _kernel.Get().ApplicationPath); + Thread.CurrentThread.Name = "Host"; + } + } +} diff --git a/NzbDrone/Console.cs b/NzbDrone/Console.cs deleted file mode 100644 index 8c32dea47..000000000 --- a/NzbDrone/Console.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System; -using System.Linq; -using System.Reflection; -using NLog; -using Ninject; -using NzbDrone.Model; -using NzbDrone.Providers; - -namespace NzbDrone -{ - public static class Console - { - private static readonly StandardKernel Kernel = new StandardKernel(); - - private static readonly Logger Logger = LogManager.GetLogger("Host.Main"); - - private static void Main(string[] args) - { - try - { - System.Console.WriteLine("Starting NzbDrone Console. Version " + Assembly.GetExecutingAssembly().GetName().Version); - - Kernel.Bind().ToSelf().InSingletonScope(); - Kernel.Bind().ToSelf().InSingletonScope(); - Kernel.Bind().ToSelf().InSingletonScope(); - Kernel.Bind().ToSelf().InSingletonScope(); - Kernel.Bind().ToSelf().InSingletonScope(); - Kernel.Bind().ToSelf().InSingletonScope(); - Kernel.Bind().ToSelf().InSingletonScope(); - Kernel.Bind().ToSelf().InSingletonScope(); - Kernel.Bind().ToSelf().InSingletonScope(); - - Kernel.Bind().ToConstant(GetApplicationMode(args)); - - Kernel.Get().Route(); - } - catch (Exception e) - { - System.Console.WriteLine(e.ToString()); - Logger.Fatal(e.ToString()); - } - - System.Console.WriteLine("Press enter to exit."); - System.Console.ReadLine(); - } - - public static ApplicationMode GetApplicationMode(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; - } - } -} \ No newline at end of file diff --git a/NzbDrone/NzbDrone.csproj b/NzbDrone/NzbDrone.csproj index d3a1b88be..191e1fd4c 100644 --- a/NzbDrone/NzbDrone.csproj +++ b/NzbDrone/NzbDrone.csproj @@ -85,7 +85,8 @@ - + + @@ -98,7 +99,7 @@ - + diff --git a/NzbDrone/NzbDroneConsole.cs b/NzbDrone/NzbDroneConsole.cs new file mode 100644 index 000000000..a0f6f1422 --- /dev/null +++ b/NzbDrone/NzbDroneConsole.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using NLog; +using Ninject; +using NzbDrone.Model; + +namespace NzbDrone +{ + public static class NzbDroneConsole + { + + + private static readonly Logger Logger = LogManager.GetLogger("Host.Main"); + + private static void Main(string[] args) + { + try + { + Console.WriteLine("Starting NzbDrone Console. Version " + Assembly.GetExecutingAssembly().GetName().Version); + + CentralDispatch.ApplicationMode = GetApplicationMode(args); + + CentralDispatch.Kernel.Get().Route(); + } + catch (Exception e) + { + Console.WriteLine(e.ToString()); + Logger.Fatal(e.ToString()); + } + + Console.WriteLine("Press enter to exit."); + Console.ReadLine(); + } + + public static ApplicationMode GetApplicationMode(IEnumerable 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; + } + } +} \ No newline at end of file diff --git a/NzbDrone/NzbDroneService.cs b/NzbDrone/NzbDroneService.cs index 3f1cdf35c..c2d41b092 100644 --- a/NzbDrone/NzbDroneService.cs +++ b/NzbDrone/NzbDroneService.cs @@ -1,4 +1,5 @@ using System.ServiceProcess; +using Ninject; namespace NzbDrone { @@ -6,12 +7,12 @@ namespace NzbDrone { protected override void OnStart(string[] args) { - base.OnStart(args); + CentralDispatch.Kernel.Get().Start(); } protected override void OnStop() { - base.OnStop(); - } + CentralDispatch.Kernel.Get().Stop(); + } } } \ No newline at end of file diff --git a/NzbDrone/Router.cs b/NzbDrone/Router.cs index a03b0c9a3..bb1065bc5 100644 --- a/NzbDrone/Router.cs +++ b/NzbDrone/Router.cs @@ -9,27 +9,24 @@ namespace NzbDrone { class Router { - private readonly Application _application; + private readonly ApplicationServer _applicationServer; private readonly ServiceProvider _serviceProvider; private readonly ConsoleProvider _consoleProvider; - private readonly ApplicationMode _applicationMode; - - public Router(Application application, ServiceProvider serviceProvider, ConsoleProvider consoleProvider, ApplicationMode applicationMode) + public Router(ApplicationServer applicationServer, ServiceProvider serviceProvider, ConsoleProvider consoleProvider) { - _application = application; + _applicationServer = applicationServer; _serviceProvider = serviceProvider; - _consoleProvider = consoleProvider; - _applicationMode = applicationMode; + _consoleProvider = consoleProvider; } public void Route() { - switch (_applicationMode) + switch (CentralDispatch.ApplicationMode) { case ApplicationMode.Console: { - _application.Start(); + _applicationServer.Start(); _consoleProvider.WaitForClose(); break; }