From 08d9004d8f361aaf13756cab70fc659e5fbb775c Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 3 Jun 2013 14:15:35 -0400 Subject: [PATCH] a little more kernel consolidation --- MediaBrowser.Api/Images/ImageService.cs | 2 +- .../Drawing/ImageManager.cs | 11 ++- MediaBrowser.Controller/Dto/DtoBuilder.cs | 2 +- .../IServerApplicationHost.cs | 12 ++++ MediaBrowser.Controller/Kernel.cs | 47 +------------ .../Localization/LocalizedStrings.cs | 11 ++- .../ServerManager/ServerManager.cs | 15 ++--- .../Sqlite/SQLiteRepository.cs | 6 +- MediaBrowser.ServerApplication/App.xaml.cs | 7 +- .../ApplicationHost.cs | 67 ++++++++++++------- .../EntryPoints/StartupWizard.cs | 8 +-- .../MainWindow.xaml.cs | 12 ++-- 12 files changed, 98 insertions(+), 102 deletions(-) diff --git a/MediaBrowser.Api/Images/ImageService.cs b/MediaBrowser.Api/Images/ImageService.cs index 4b4aeb83f..68e7d0686 100644 --- a/MediaBrowser.Api/Images/ImageService.cs +++ b/MediaBrowser.Api/Images/ImageService.cs @@ -658,7 +658,7 @@ namespace MediaBrowser.Api.Images // See if we can avoid a file system lookup by looking for the file in ResolveArgs var originalFileImageDateModified = kernel.ImageManager.GetImageDateModified(item, request.Type, index); - var supportedImageEnhancers = kernel.ImageEnhancers.Where(i => + var supportedImageEnhancers = kernel.ImageManager.ImageEnhancers.Where(i => { try { diff --git a/MediaBrowser.Controller/Drawing/ImageManager.cs b/MediaBrowser.Controller/Drawing/ImageManager.cs index 807c2d1e0..b728fe71f 100644 --- a/MediaBrowser.Controller/Drawing/ImageManager.cs +++ b/MediaBrowser.Controller/Drawing/ImageManager.cs @@ -25,6 +25,13 @@ namespace MediaBrowser.Controller.Drawing /// public class ImageManager { + /// + /// Gets the list of currently registered image processors + /// Image processors are specialized metadata providers that run after the normal ones + /// + /// The image enhancers. + public IEnumerable ImageEnhancers { get; set; } + /// /// Gets the image size cache. /// @@ -120,7 +127,7 @@ namespace MediaBrowser.Controller.Drawing originalImagePath = await GetCroppedImage(originalImagePath, dateModified).ConfigureAwait(false); } - var supportedEnhancers = _kernel.ImageEnhancers.Where(i => + var supportedEnhancers = ImageEnhancers.Where(i => { try { @@ -621,7 +628,7 @@ namespace MediaBrowser.Controller.Drawing var dateModified = GetImageDateModified(item, imagePath); - var supportedEnhancers = _kernel.ImageEnhancers.Where(i => + var supportedEnhancers = ImageEnhancers.Where(i => { try { diff --git a/MediaBrowser.Controller/Dto/DtoBuilder.cs b/MediaBrowser.Controller/Dto/DtoBuilder.cs index 549248595..412b911f4 100644 --- a/MediaBrowser.Controller/Dto/DtoBuilder.cs +++ b/MediaBrowser.Controller/Dto/DtoBuilder.cs @@ -182,7 +182,7 @@ namespace MediaBrowser.Controller.Dto return; } - var supportedEnhancers = Kernel.Instance.ImageEnhancers.Where(i => + var supportedEnhancers = Kernel.Instance.ImageManager.ImageEnhancers.Where(i => { try { diff --git a/MediaBrowser.Controller/IServerApplicationHost.cs b/MediaBrowser.Controller/IServerApplicationHost.cs index 042ef30ec..f96c2536e 100644 --- a/MediaBrowser.Controller/IServerApplicationHost.cs +++ b/MediaBrowser.Controller/IServerApplicationHost.cs @@ -13,5 +13,17 @@ namespace MediaBrowser.Controller /// /// SystemInfo. SystemInfo GetSystemInfo(); + + /// + /// Gets the name of the web application. + /// + /// The name of the web application. + string WebApplicationName { get; } + + /// + /// Gets the HTTP server URL prefix. + /// + /// The HTTP server URL prefix. + string HttpServerUrlPrefix { get; } } } diff --git a/MediaBrowser.Controller/Kernel.cs b/MediaBrowser.Controller/Kernel.cs index 3e1c21a50..d7b24a0cc 100644 --- a/MediaBrowser.Controller/Kernel.cs +++ b/MediaBrowser.Controller/Kernel.cs @@ -1,8 +1,5 @@ -using MediaBrowser.Controller.Configuration; -using MediaBrowser.Controller.Drawing; -using MediaBrowser.Controller.Localization; +using MediaBrowser.Controller.Drawing; using MediaBrowser.Controller.MediaInfo; -using MediaBrowser.Controller.Providers; using MediaBrowser.Controller.Weather; using System.Collections.Generic; @@ -31,58 +28,18 @@ namespace MediaBrowser.Controller /// The FFMPEG controller. public FFMpegManager FFMpegManager { get; set; } - /// - /// Gets the name of the web application that can be used for url building. - /// All api urls will be of the form {protocol}://{host}:{port}/{appname}/... - /// - /// The name of the web application. - public string WebApplicationName - { - get { return "mediabrowser"; } - } - - /// - /// Gets the HTTP server URL prefix. - /// - /// The HTTP server URL prefix. - public virtual string HttpServerUrlPrefix - { - get - { - return "http://+:" + _configurationManager.Configuration.HttpServerPortNumber + "/" + WebApplicationName + "/"; - } - } - - /// - /// Gets the list of Localized string files - /// - /// The string files. - public IEnumerable StringFiles { get; set; } - /// /// Gets the list of currently registered weather prvoiders /// /// The weather providers. public IEnumerable WeatherProviders { get; set; } - /// - /// Gets the list of currently registered image processors - /// Image processors are specialized metadata providers that run after the normal ones - /// - /// The image enhancers. - public IEnumerable ImageEnhancers { get; set; } - - private readonly IServerConfigurationManager _configurationManager; - /// /// Creates a kernel based on a Data path, which is akin to our current programdata path /// - /// The configuration manager. - public Kernel(IServerConfigurationManager configurationManager) + public Kernel() { Instance = this; - - _configurationManager = configurationManager; } } } diff --git a/MediaBrowser.Controller/Localization/LocalizedStrings.cs b/MediaBrowser.Controller/Localization/LocalizedStrings.cs index 31dcb2e9f..bd82d39ea 100644 --- a/MediaBrowser.Controller/Localization/LocalizedStrings.cs +++ b/MediaBrowser.Controller/Localization/LocalizedStrings.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Concurrent; +using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; @@ -13,7 +14,13 @@ namespace MediaBrowser.Controller.Localization public class LocalizedStrings { public static IServerApplicationPaths ApplicationPaths; - + + /// + /// Gets the list of Localized string files + /// + /// The string files. + public static IEnumerable StringFiles { get; set; } + /// /// The base prefix /// @@ -42,7 +49,7 @@ namespace MediaBrowser.Controller.Localization { _appPaths = appPaths; - foreach (var stringObject in Kernel.Instance.StringFiles) + foreach (var stringObject in StringFiles) { AddStringData(LoadFromFile(GetFileName(stringObject),stringObject.GetType())); } diff --git a/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs b/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs index f8e47434e..80b6a0f7d 100644 --- a/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs +++ b/MediaBrowser.Server.Implementations/ServerManager/ServerManager.cs @@ -59,7 +59,7 @@ namespace MediaBrowser.Server.Implementations.ServerManager /// /// The _application host /// - private readonly IApplicationHost _applicationHost; + private readonly IServerApplicationHost _applicationHost; /// /// Gets or sets the configuration manager. @@ -91,11 +91,6 @@ namespace MediaBrowser.Server.Implementations.ServerManager /// The web socket listeners. private readonly List _webSocketListeners = new List(); - /// - /// The _kernel - /// - private readonly Kernel _kernel; - /// /// Initializes a new instance of the class. /// @@ -103,9 +98,8 @@ namespace MediaBrowser.Server.Implementations.ServerManager /// The json serializer. /// The logger. /// The configuration manager. - /// The kernel. /// applicationHost - public ServerManager(IApplicationHost applicationHost, IJsonSerializer jsonSerializer, ILogger logger, IServerConfigurationManager configurationManager, Kernel kernel) + public ServerManager(IServerApplicationHost applicationHost, IJsonSerializer jsonSerializer, ILogger logger, IServerConfigurationManager configurationManager) { if (applicationHost == null) { @@ -124,7 +118,6 @@ namespace MediaBrowser.Server.Implementations.ServerManager _jsonSerializer = jsonSerializer; _applicationHost = applicationHost; ConfigurationManager = configurationManager; - _kernel = kernel; ConfigurationManager.ConfigurationUpdated += ConfigurationUpdated; } @@ -161,7 +154,7 @@ namespace MediaBrowser.Server.Implementations.ServerManager private void ReloadHttpServer() { // Only reload if the port has changed, so that we don't disconnect any active users - if (HttpServer != null && HttpServer.UrlPrefix.Equals(_kernel.HttpServerUrlPrefix, StringComparison.OrdinalIgnoreCase)) + if (HttpServer != null && HttpServer.UrlPrefix.Equals(_applicationHost.HttpServerUrlPrefix, StringComparison.OrdinalIgnoreCase)) { return; } @@ -174,7 +167,7 @@ namespace MediaBrowser.Server.Implementations.ServerManager { HttpServer = _applicationHost.Resolve(); HttpServer.EnableHttpRequestLogging = ConfigurationManager.Configuration.EnableHttpLevelLogging; - HttpServer.Start(_kernel.HttpServerUrlPrefix); + HttpServer.Start(_applicationHost.HttpServerUrlPrefix); } catch (HttpListenerException ex) { diff --git a/MediaBrowser.Server.Implementations/Sqlite/SQLiteRepository.cs b/MediaBrowser.Server.Implementations/Sqlite/SQLiteRepository.cs index 297033cd6..6cd816e51 100644 --- a/MediaBrowser.Server.Implementations/Sqlite/SQLiteRepository.cs +++ b/MediaBrowser.Server.Implementations/Sqlite/SQLiteRepository.cs @@ -49,7 +49,7 @@ namespace MediaBrowser.Server.Implementations.Sqlite /// The db path. /// Task{System.Boolean}. /// dbPath - protected async Task ConnectToDb(string dbPath) + protected Task ConnectToDb(string dbPath) { if (string.IsNullOrEmpty(dbPath)) { @@ -68,7 +68,7 @@ namespace MediaBrowser.Server.Implementations.Sqlite Connection = new SQLiteConnection(connectionstr.ConnectionString); - await Connection.OpenAsync().ConfigureAwait(false); + return Connection.OpenAsync(); } /// @@ -164,7 +164,7 @@ namespace MediaBrowser.Server.Implementations.Sqlite { throw new ArgumentNullException("reader"); } - + var memoryStream = new MemoryStream(); var num = 0L; var array = new byte[4096]; diff --git a/MediaBrowser.ServerApplication/App.xaml.cs b/MediaBrowser.ServerApplication/App.xaml.cs index 5a1d7505c..260081416 100644 --- a/MediaBrowser.ServerApplication/App.xaml.cs +++ b/MediaBrowser.ServerApplication/App.xaml.cs @@ -226,10 +226,13 @@ namespace MediaBrowser.ServerApplication /// Opens the dashboard page. /// /// The page. - public static void OpenDashboardPage(string page, User loggedInUser, IServerConfigurationManager configurationManager) + /// The logged in user. + /// The configuration manager. + /// The app host. + public static void OpenDashboardPage(string page, User loggedInUser, IServerConfigurationManager configurationManager, IServerApplicationHost appHost) { var url = "http://localhost:" + configurationManager.Configuration.HttpServerPortNumber + "/" + - Kernel.Instance.WebApplicationName + "/dashboard/" + page; + appHost.WebApplicationName + "/dashboard/" + page; OpenUrl(url); } diff --git a/MediaBrowser.ServerApplication/ApplicationHost.cs b/MediaBrowser.ServerApplication/ApplicationHost.cs index 17ee2d597..9e9ab7ff3 100644 --- a/MediaBrowser.ServerApplication/ApplicationHost.cs +++ b/MediaBrowser.ServerApplication/ApplicationHost.cs @@ -40,7 +40,6 @@ using MediaBrowser.Server.Implementations.Providers; using MediaBrowser.Server.Implementations.ServerManager; using MediaBrowser.Server.Implementations.Session; using MediaBrowser.Server.Implementations.Sqlite; -using MediaBrowser.Server.Implementations.Udp; using MediaBrowser.Server.Implementations.Updates; using MediaBrowser.Server.Implementations.WebSocket; using MediaBrowser.ServerApplication.Implementations; @@ -50,7 +49,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; -using System.Net.Sockets; using System.Reflection; using System.Threading; using System.Threading.Tasks; @@ -88,6 +86,28 @@ namespace MediaBrowser.ServerApplication get { return "Server"; } } + /// + /// Gets the name of the web application that can be used for url building. + /// All api urls will be of the form {protocol}://{host}:{port}/{appname}/... + /// + /// The name of the web application. + public string WebApplicationName + { + get { return "mediabrowser"; } + } + + /// + /// Gets the HTTP server URL prefix. + /// + /// The HTTP server URL prefix. + public string HttpServerUrlPrefix + { + get + { + return "http://+:" + ServerConfigurationManager.Configuration.HttpServerPortNumber + "/" + WebApplicationName + "/"; + } + } + /// /// Gets the configuration manager. /// @@ -200,7 +220,7 @@ namespace MediaBrowser.ServerApplication /// Task. protected override async Task RegisterResources() { - ServerKernel = new Kernel(ServerConfigurationManager); + ServerKernel = new Kernel(); await base.RegisterResources().ConfigureAwait(false); @@ -261,7 +281,7 @@ namespace MediaBrowser.ServerApplication HttpServer = await _httpServerCreationTask.ConfigureAwait(false); RegisterSingleInstance(HttpServer, false); - ServerManager = new ServerManager(this, JsonSerializer, Logger, ServerConfigurationManager, ServerKernel); + ServerManager = new ServerManager(this, JsonSerializer, Logger, ServerConfigurationManager); RegisterSingleInstance(ServerManager); var displayPreferencesTask = Task.Run(async () => await ConfigureDisplayPreferencesRepositories().ConfigureAwait(false)); @@ -279,14 +299,15 @@ namespace MediaBrowser.ServerApplication /// private void SetKernelProperties() { + ServerKernel.ImageManager = new ImageManager(ServerKernel, LogManager.GetLogger("ImageManager"), + ApplicationPaths); Parallel.Invoke( - () => ServerKernel.FFMpegManager = new FFMpegManager(ApplicationPaths, MediaEncoder, LibraryManager, Logger), - () => ServerKernel.ImageManager = new ImageManager(ServerKernel, LogManager.GetLogger("ImageManager"), ApplicationPaths), - () => ServerKernel.WeatherProviders = GetExports(), - () => ServerKernel.ImageEnhancers = GetExports().OrderBy(e => e.Priority).ToArray(), - () => ServerKernel.StringFiles = GetExports(), - SetStaticProperties - ); + () => ServerKernel.FFMpegManager = new FFMpegManager(ApplicationPaths, MediaEncoder, LibraryManager, Logger), + () => ServerKernel.WeatherProviders = GetExports(), + () => ServerKernel.ImageManager.ImageEnhancers = GetExports().OrderBy(e => e.Priority).ToArray(), + () => LocalizedStrings.StringFiles = GetExports(), + SetStaticProperties + ); } /// @@ -364,20 +385,16 @@ namespace MediaBrowser.ServerApplication ServerManager.AddWebSocketListeners(GetExports(false)); StartServer(true); - - Parallel.Invoke( - () => LibraryManager.AddParts(GetExports(), - GetExports(), - GetExports(), - GetExports(), - GetExports(), - GetExports(), - GetExports()), + LibraryManager.AddParts(GetExports(), + GetExports(), + GetExports(), + GetExports(), + GetExports(), + GetExports(), + GetExports()); - () => ProviderManager.AddMetadataProviders(GetExports().ToArray()) - - ); + ProviderManager.AddMetadataProviders(GetExports().ToArray()); } /// @@ -414,7 +431,7 @@ namespace MediaBrowser.ServerApplication { base.OnConfigurationUpdated(sender, e); - if (!string.Equals(HttpServer.UrlPrefix, ServerKernel.HttpServerUrlPrefix, StringComparison.OrdinalIgnoreCase)) + if (!string.Equals(HttpServer.UrlPrefix, HttpServerUrlPrefix, StringComparison.OrdinalIgnoreCase)) { NotifyPendingRestart(); } @@ -553,7 +570,7 @@ namespace MediaBrowser.ServerApplication FileName = tmpFile, Arguments = string.Format("{0} {1} {2} {3}", ServerConfigurationManager.Configuration.HttpServerPortNumber, - ServerKernel.HttpServerUrlPrefix, + HttpServerUrlPrefix, UdpServerPort, ServerConfigurationManager.Configuration.LegacyWebSocketPortNumber), diff --git a/MediaBrowser.ServerApplication/EntryPoints/StartupWizard.cs b/MediaBrowser.ServerApplication/EntryPoints/StartupWizard.cs index 22ec16b02..cbdd92c71 100644 --- a/MediaBrowser.ServerApplication/EntryPoints/StartupWizard.cs +++ b/MediaBrowser.ServerApplication/EntryPoints/StartupWizard.cs @@ -1,4 +1,4 @@ -using MediaBrowser.Common; +using MediaBrowser.Controller; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Plugins; @@ -17,7 +17,7 @@ namespace MediaBrowser.ServerApplication.EntryPoints /// /// The _app host /// - private readonly IApplicationHost _appHost; + private readonly IServerApplicationHost _appHost; /// /// The _user manager /// @@ -31,7 +31,7 @@ namespace MediaBrowser.ServerApplication.EntryPoints /// /// The app host. /// The user manager. - public StartupWizard(IApplicationHost appHost, IUserManager userManager, IServerConfigurationManager configurationManager) + public StartupWizard(IServerApplicationHost appHost, IUserManager userManager, IServerConfigurationManager configurationManager) { _appHost = appHost; _userManager = userManager; @@ -58,7 +58,7 @@ namespace MediaBrowser.ServerApplication.EntryPoints try { - App.OpenDashboardPage("wizardStart.html", user, _configurationManager); + App.OpenDashboardPage("wizardStart.html", user, _configurationManager, _appHost); } catch (Win32Exception ex) { diff --git a/MediaBrowser.ServerApplication/MainWindow.xaml.cs b/MediaBrowser.ServerApplication/MainWindow.xaml.cs index 0c9307394..f23016d7b 100644 --- a/MediaBrowser.ServerApplication/MainWindow.xaml.cs +++ b/MediaBrowser.ServerApplication/MainWindow.xaml.cs @@ -29,7 +29,7 @@ namespace MediaBrowser.ServerApplication /// /// The _app host /// - private readonly IApplicationHost _appHost; + private readonly IServerApplicationHost _appHost; /// /// The _log manager @@ -57,7 +57,7 @@ namespace MediaBrowser.ServerApplication /// The json serializer. /// The display preferences manager. /// logger - public MainWindow(ILogManager logManager, IApplicationHost appHost, IServerConfigurationManager configurationManager, IUserManager userManager, ILibraryManager libraryManager, IJsonSerializer jsonSerializer, IDisplayPreferencesManager displayPreferencesManager) + public MainWindow(ILogManager logManager, IServerApplicationHost appHost, IServerConfigurationManager configurationManager, IUserManager userManager, ILibraryManager libraryManager, IJsonSerializer jsonSerializer, IDisplayPreferencesManager displayPreferencesManager) { if (logManager == null) { @@ -189,13 +189,13 @@ namespace MediaBrowser.ServerApplication void cmdApiDocs_Click(object sender, EventArgs e) { App.OpenUrl("http://localhost:" + _configurationManager.Configuration.HttpServerPortNumber + "/" + - Kernel.Instance.WebApplicationName + "/metadata"); + _appHost.WebApplicationName + "/metadata"); } void cmdSwaggerApiDocs_Click(object sender, EventArgs e) { App.OpenUrl("http://localhost:" + _configurationManager.Configuration.HttpServerPortNumber + "/" + - Kernel.Instance.WebApplicationName + "/swagger-ui/index.html"); + _appHost.WebApplicationName + "/swagger-ui/index.html"); } void cmdGithubWiki_Click(object sender, EventArgs e) @@ -254,7 +254,7 @@ namespace MediaBrowser.ServerApplication /// private void OpenDashboard(User loggedInUser) { - App.OpenDashboardPage("dashboard.html", loggedInUser, _configurationManager); + App.OpenDashboardPage("dashboard.html", loggedInUser, _configurationManager, _appHost); } /// @@ -275,7 +275,7 @@ namespace MediaBrowser.ServerApplication private void cmdBrowseLibrary_click(object sender, RoutedEventArgs e) { var user = _userManager.Users.FirstOrDefault(u => u.Configuration.IsAdministrator); - App.OpenDashboardPage("index.html", user, _configurationManager); + App.OpenDashboardPage("index.html", user, _configurationManager, _appHost); } ///