From da7af24fca3b2462b971dce595cfa5e548311cce Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 17 May 2013 11:29:22 -0400 Subject: [PATCH] add separate entry point for new item notifier --- MediaBrowser.Controller/Library/TVUtils.cs | 11 +- .../Providers/TV/RemoteEpisodeProvider.cs | 4 +- .../Providers/TV/RemoteSeasonProvider.cs | 2 +- .../Providers/TV/RemoteSeriesProvider.cs | 6 +- .../ApplicationHost.cs | 19 +- .../EntryPoints/LibraryChangedNotifier.cs | 210 ++++++++++++++++++ .../EntryPoints/WebSocketEvents.cs | 171 +------------- .../MediaBrowser.ServerApplication.csproj | 1 + 8 files changed, 231 insertions(+), 193 deletions(-) create mode 100644 MediaBrowser.ServerApplication/EntryPoints/LibraryChangedNotifier.cs diff --git a/MediaBrowser.Controller/Library/TVUtils.cs b/MediaBrowser.Controller/Library/TVUtils.cs index 43aef88f1..0b9972d0a 100644 --- a/MediaBrowser.Controller/Library/TVUtils.cs +++ b/MediaBrowser.Controller/Library/TVUtils.cs @@ -1,10 +1,9 @@ -using System.IO; -using MediaBrowser.Controller.IO; +using MediaBrowser.Controller.Resolvers; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text.RegularExpressions; -using MediaBrowser.Controller.Resolvers; namespace MediaBrowser.Controller.Library { @@ -16,7 +15,7 @@ namespace MediaBrowser.Controller.Library /// /// The TVDB API key /// - public static readonly string TVDBApiKey = "B89CE93890E9419B"; + public static readonly string TvdbApiKey = "B89CE93890E9419B"; /// /// The banner URL /// @@ -93,7 +92,7 @@ namespace MediaBrowser.Controller.Library // Look for one of the season folder names foreach (var name in SeasonFolderNames) { - int index = path.IndexOf(name, StringComparison.OrdinalIgnoreCase); + var index = path.IndexOf(name, StringComparison.OrdinalIgnoreCase); if (index != -1) { @@ -115,7 +114,7 @@ namespace MediaBrowser.Controller.Library int length = 0; // Find out where the numbers start, and then keep going until they end - for (int i = 0; i < path.Length; i++) + for (var i = 0; i < path.Length; i++) { if (char.IsNumber(path, i)) { diff --git a/MediaBrowser.Controller/Providers/TV/RemoteEpisodeProvider.cs b/MediaBrowser.Controller/Providers/TV/RemoteEpisodeProvider.cs index b494ad021..2b4173ed9 100644 --- a/MediaBrowser.Controller/Providers/TV/RemoteEpisodeProvider.cs +++ b/MediaBrowser.Controller/Providers/TV/RemoteEpisodeProvider.cs @@ -179,7 +179,7 @@ namespace MediaBrowser.Controller.Providers.TV seasonNumber = "0"; // Specials } - var url = string.Format(episodeQuery, TVUtils.TVDBApiKey, seriesId, seasonNumber, episodeNumber, ConfigurationManager.Configuration.PreferredMetadataLanguage); + var url = string.Format(episodeQuery, TVUtils.TvdbApiKey, seriesId, seasonNumber, episodeNumber, ConfigurationManager.Configuration.PreferredMetadataLanguage); var doc = new XmlDocument(); try @@ -205,7 +205,7 @@ namespace MediaBrowser.Controller.Providers.TV //this is basicly just for anime. if (!doc.HasChildNodes && Int32.Parse(seasonNumber) == 1) { - url = string.Format(absEpisodeQuery, TVUtils.TVDBApiKey, seriesId, episodeNumber, ConfigurationManager.Configuration.PreferredMetadataLanguage); + url = string.Format(absEpisodeQuery, TVUtils.TvdbApiKey, seriesId, episodeNumber, ConfigurationManager.Configuration.PreferredMetadataLanguage); try { diff --git a/MediaBrowser.Controller/Providers/TV/RemoteSeasonProvider.cs b/MediaBrowser.Controller/Providers/TV/RemoteSeasonProvider.cs index 79ea75f8e..70ac2d45c 100644 --- a/MediaBrowser.Controller/Providers/TV/RemoteSeasonProvider.cs +++ b/MediaBrowser.Controller/Providers/TV/RemoteSeasonProvider.cs @@ -158,7 +158,7 @@ namespace MediaBrowser.Controller.Providers.TV if ((season.PrimaryImagePath == null) || (!season.HasImage(ImageType.Banner)) || (season.BackdropImagePaths == null)) { var images = new XmlDocument(); - var url = string.Format("http://www.thetvdb.com/api/" + TVUtils.TVDBApiKey + "/series/{0}/banners.xml", seriesId); + var url = string.Format("http://www.thetvdb.com/api/" + TVUtils.TvdbApiKey + "/series/{0}/banners.xml", seriesId); using (var imgs = await HttpClient.Get(new HttpRequestOptions { diff --git a/MediaBrowser.Controller/Providers/TV/RemoteSeriesProvider.cs b/MediaBrowser.Controller/Providers/TV/RemoteSeriesProvider.cs index 9dd5aa418..eb689ed2f 100644 --- a/MediaBrowser.Controller/Providers/TV/RemoteSeriesProvider.cs +++ b/MediaBrowser.Controller/Providers/TV/RemoteSeriesProvider.cs @@ -197,7 +197,7 @@ namespace MediaBrowser.Controller.Providers.TV if (!string.IsNullOrEmpty(seriesId)) { - string url = string.Format(seriesGet, TVUtils.TVDBApiKey, seriesId, ConfigurationManager.Configuration.PreferredMetadataLanguage); + string url = string.Format(seriesGet, TVUtils.TvdbApiKey, seriesId, ConfigurationManager.Configuration.PreferredMetadataLanguage); var doc = new XmlDocument(); try @@ -296,7 +296,7 @@ namespace MediaBrowser.Controller.Providers.TV /// Task. private async Task FetchActors(Series series, string seriesId, XmlDocument doc, CancellationToken cancellationToken) { - string urlActors = string.Format(getActors, TVUtils.TVDBApiKey, seriesId); + string urlActors = string.Format(getActors, TVUtils.TvdbApiKey, seriesId); var docActors = new XmlDocument(); try @@ -377,7 +377,7 @@ namespace MediaBrowser.Controller.Providers.TV { if ((!string.IsNullOrEmpty(seriesId)) && ((series.PrimaryImagePath == null) || (series.BackdropImagePaths == null))) { - string url = string.Format("http://www.thetvdb.com/api/" + TVUtils.TVDBApiKey + "/series/{0}/banners.xml", seriesId); + string url = string.Format("http://www.thetvdb.com/api/" + TVUtils.TvdbApiKey + "/series/{0}/banners.xml", seriesId); var images = new XmlDocument(); try diff --git a/MediaBrowser.ServerApplication/ApplicationHost.cs b/MediaBrowser.ServerApplication/ApplicationHost.cs index a35ac44ea..4dd24d98f 100644 --- a/MediaBrowser.ServerApplication/ApplicationHost.cs +++ b/MediaBrowser.ServerApplication/ApplicationHost.cs @@ -378,19 +378,16 @@ namespace MediaBrowser.ServerApplication RegisterServerWithAdministratorAccess(); } + base.FindParts(); + + HttpServer.Init(GetExports(false)); + + ServerManager.AddWebSocketListeners(GetExports(false)); + + StartServer(true); + Parallel.Invoke( - () => base.FindParts(), - - () => - { - HttpServer.Init(GetExports(false)); - - ServerManager.AddWebSocketListeners(GetExports(false)); - - StartServer(true); - }, - () => LibraryManager.AddParts(GetExports(), GetExports(), GetExports(), GetExports(), GetExports()), () => ProviderManager.AddMetadataProviders(GetExports().ToArray()), diff --git a/MediaBrowser.ServerApplication/EntryPoints/LibraryChangedNotifier.cs b/MediaBrowser.ServerApplication/EntryPoints/LibraryChangedNotifier.cs new file mode 100644 index 000000000..62c1e17f9 --- /dev/null +++ b/MediaBrowser.ServerApplication/EntryPoints/LibraryChangedNotifier.cs @@ -0,0 +1,210 @@ +using MediaBrowser.Common.Net; +using MediaBrowser.Controller.Library; +using MediaBrowser.Controller.Plugins; +using MediaBrowser.Controller.Session; +using MediaBrowser.Model.Entities; +using System.Linq; +using System.Threading; + +namespace MediaBrowser.ServerApplication.EntryPoints +{ + public class LibraryChangedNotifier : IServerEntryPoint + { + /// + /// The _library manager + /// + private readonly ILibraryManager _libraryManager; + + private readonly ISessionManager _sessionManager; + private readonly IServerManager _serverManager; + + /// + /// The _library changed sync lock + /// + private readonly object _libraryChangedSyncLock = new object(); + + /// + /// Gets or sets the library update info. + /// + /// The library update info. + private LibraryUpdateInfo LibraryUpdateInfo { get; set; } + + /// + /// Gets or sets the library update timer. + /// + /// The library update timer. + private Timer LibraryUpdateTimer { get; set; } + + /// + /// The library update duration + /// + private const int LibraryUpdateDuration = 60000; + + public LibraryChangedNotifier(ILibraryManager libraryManager, ISessionManager sessionManager, IServerManager serverManager) + { + _libraryManager = libraryManager; + _sessionManager = sessionManager; + _serverManager = serverManager; + } + + public void Run() + { + _libraryManager.ItemAdded += libraryManager_ItemAdded; + _libraryManager.ItemUpdated += libraryManager_ItemUpdated; + _libraryManager.ItemRemoved += libraryManager_ItemRemoved; + + } + + /// + /// Handles the ItemAdded event of the libraryManager control. + /// + /// The source of the event. + /// The instance containing the event data. + void libraryManager_ItemAdded(object sender, ItemChangeEventArgs e) + { + lock (_libraryChangedSyncLock) + { + if (LibraryUpdateInfo == null) + { + LibraryUpdateInfo = new LibraryUpdateInfo(); + } + + if (LibraryUpdateTimer == null) + { + LibraryUpdateTimer = new Timer(LibraryUpdateTimerCallback, null, LibraryUpdateDuration, + Timeout.Infinite); + } + else + { + LibraryUpdateTimer.Change(LibraryUpdateDuration, Timeout.Infinite); + } + + if (e.Item.Parent != null) + { + LibraryUpdateInfo.FoldersAddedTo.Add(e.Item.Parent.Id); + } + + LibraryUpdateInfo.ItemsAdded.Add(e.Item.Id); + } + } + + /// + /// Handles the ItemUpdated event of the libraryManager control. + /// + /// The source of the event. + /// The instance containing the event data. + void libraryManager_ItemUpdated(object sender, ItemChangeEventArgs e) + { + lock (_libraryChangedSyncLock) + { + if (LibraryUpdateInfo == null) + { + LibraryUpdateInfo = new LibraryUpdateInfo(); + } + + if (LibraryUpdateTimer == null) + { + LibraryUpdateTimer = new Timer(LibraryUpdateTimerCallback, null, LibraryUpdateDuration, + Timeout.Infinite); + } + else + { + LibraryUpdateTimer.Change(LibraryUpdateDuration, Timeout.Infinite); + } + + LibraryUpdateInfo.ItemsUpdated.Add(e.Item.Id); + } + } + + /// + /// Handles the ItemRemoved event of the libraryManager control. + /// + /// The source of the event. + /// The instance containing the event data. + void libraryManager_ItemRemoved(object sender, ItemChangeEventArgs e) + { + lock (_libraryChangedSyncLock) + { + if (LibraryUpdateInfo == null) + { + LibraryUpdateInfo = new LibraryUpdateInfo(); + } + + if (LibraryUpdateTimer == null) + { + LibraryUpdateTimer = new Timer(LibraryUpdateTimerCallback, null, LibraryUpdateDuration, + Timeout.Infinite); + } + else + { + LibraryUpdateTimer.Change(LibraryUpdateDuration, Timeout.Infinite); + } + + if (e.Item.Parent != null) + { + LibraryUpdateInfo.FoldersRemovedFrom.Add(e.Item.Parent.Id); + } + + LibraryUpdateInfo.ItemsRemoved.Add(e.Item.Id); + } + } + + /// + /// Libraries the update timer callback. + /// + /// The state. + private void LibraryUpdateTimerCallback(object state) + { + lock (_libraryChangedSyncLock) + { + // Remove dupes in case some were saved multiple times + LibraryUpdateInfo.FoldersAddedTo = LibraryUpdateInfo.FoldersAddedTo.Distinct().ToList(); + + LibraryUpdateInfo.FoldersRemovedFrom = LibraryUpdateInfo.FoldersRemovedFrom.Distinct().ToList(); + + LibraryUpdateInfo.ItemsUpdated = LibraryUpdateInfo.ItemsUpdated + .Where(i => !LibraryUpdateInfo.ItemsAdded.Contains(i)) + .Distinct() + .ToList(); + + _serverManager.SendWebSocketMessage("LibraryChanged", LibraryUpdateInfo); + + if (LibraryUpdateTimer != null) + { + LibraryUpdateTimer.Dispose(); + LibraryUpdateTimer = null; + } + + LibraryUpdateInfo = null; + } + } + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + public void Dispose() + { + Dispose(true); + } + + /// + /// Releases unmanaged and - optionally - managed resources. + /// + /// true to release both managed and unmanaged resources; false to release only unmanaged resources. + protected virtual void Dispose(bool dispose) + { + if (dispose) + { + if (LibraryUpdateTimer != null) + { + LibraryUpdateTimer.Dispose(); + LibraryUpdateTimer = null; + } + + _libraryManager.ItemAdded -= libraryManager_ItemAdded; + _libraryManager.ItemUpdated -= libraryManager_ItemUpdated; + _libraryManager.ItemRemoved -= libraryManager_ItemRemoved; + } + } + } +} diff --git a/MediaBrowser.ServerApplication/EntryPoints/WebSocketEvents.cs b/MediaBrowser.ServerApplication/EntryPoints/WebSocketEvents.cs index 5e0a477f3..1a29d5029 100644 --- a/MediaBrowser.ServerApplication/EntryPoints/WebSocketEvents.cs +++ b/MediaBrowser.ServerApplication/EntryPoints/WebSocketEvents.cs @@ -8,13 +8,10 @@ using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Plugins; using MediaBrowser.Controller.Updates; -using MediaBrowser.Model.Entities; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Tasks; using MediaBrowser.Model.Updates; using System; -using System.Linq; -using System.Threading; namespace MediaBrowser.ServerApplication.EntryPoints { @@ -37,11 +34,6 @@ namespace MediaBrowser.ServerApplication.EntryPoints /// private readonly IUserManager _userManager; - /// - /// The _library manager - /// - private readonly ILibraryManager _libraryManager; - /// /// The _installation manager /// @@ -57,40 +49,17 @@ namespace MediaBrowser.ServerApplication.EntryPoints /// private readonly ITaskManager _taskManager; - /// - /// The _library changed sync lock - /// - private readonly object _libraryChangedSyncLock = new object(); - - /// - /// Gets or sets the library update info. - /// - /// The library update info. - private LibraryUpdateInfo LibraryUpdateInfo { get; set; } - - /// - /// Gets or sets the library update timer. - /// - /// The library update timer. - private Timer LibraryUpdateTimer { get; set; } - - /// - /// The library update duration - /// - private const int LibraryUpdateDuration = 60000; - /// /// Initializes a new instance of the class. /// /// The server manager. /// The logger. /// The user manager. - public WebSocketEvents(IServerManager serverManager, IServerApplicationHost appHost, ILogger logger, IUserManager userManager, ILibraryManager libraryManager, IInstallationManager installationManager, ITaskManager taskManager) + public WebSocketEvents(IServerManager serverManager, IServerApplicationHost appHost, ILogger logger, IUserManager userManager, IInstallationManager installationManager, ITaskManager taskManager) { _serverManager = serverManager; _logger = logger; _userManager = userManager; - _libraryManager = libraryManager; _installationManager = installationManager; _appHost = appHost; _taskManager = taskManager; @@ -103,10 +72,6 @@ namespace MediaBrowser.ServerApplication.EntryPoints _appHost.HasPendingRestartChanged += kernel_HasPendingRestartChanged; - _libraryManager.ItemAdded += libraryManager_ItemAdded; - _libraryManager.ItemUpdated += libraryManager_ItemUpdated; - _libraryManager.ItemRemoved += libraryManager_ItemRemoved; - _installationManager.PluginUninstalled += InstallationManager_PluginUninstalled; _installationManager.PackageInstalling += installationManager_PackageInstalling; _installationManager.PackageInstallationCancelled += installationManager_PackageInstallationCancelled; @@ -168,130 +133,6 @@ namespace MediaBrowser.ServerApplication.EntryPoints _serverManager.SendWebSocketMessage("PackageInstalling", e.Argument); } - /// - /// Handles the ItemAdded event of the libraryManager control. - /// - /// The source of the event. - /// The instance containing the event data. - void libraryManager_ItemAdded(object sender, ItemChangeEventArgs e) - { - lock (_libraryChangedSyncLock) - { - if (LibraryUpdateInfo == null) - { - LibraryUpdateInfo = new LibraryUpdateInfo(); - } - - if (LibraryUpdateTimer == null) - { - LibraryUpdateTimer = new Timer(LibraryUpdateTimerCallback, null, LibraryUpdateDuration, - Timeout.Infinite); - } - else - { - LibraryUpdateTimer.Change(LibraryUpdateDuration, Timeout.Infinite); - } - - if (e.Item.Parent != null) - { - LibraryUpdateInfo.FoldersAddedTo.Add(e.Item.Parent.Id); - } - - LibraryUpdateInfo.ItemsAdded.Add(e.Item.Id); - } - } - - /// - /// Handles the ItemUpdated event of the libraryManager control. - /// - /// The source of the event. - /// The instance containing the event data. - void libraryManager_ItemUpdated(object sender, ItemChangeEventArgs e) - { - lock (_libraryChangedSyncLock) - { - if (LibraryUpdateInfo == null) - { - LibraryUpdateInfo = new LibraryUpdateInfo(); - } - - if (LibraryUpdateTimer == null) - { - LibraryUpdateTimer = new Timer(LibraryUpdateTimerCallback, null, LibraryUpdateDuration, - Timeout.Infinite); - } - else - { - LibraryUpdateTimer.Change(LibraryUpdateDuration, Timeout.Infinite); - } - - LibraryUpdateInfo.ItemsUpdated.Add(e.Item.Id); - } - } - - /// - /// Handles the ItemRemoved event of the libraryManager control. - /// - /// The source of the event. - /// The instance containing the event data. - void libraryManager_ItemRemoved(object sender, ItemChangeEventArgs e) - { - lock (_libraryChangedSyncLock) - { - if (LibraryUpdateInfo == null) - { - LibraryUpdateInfo = new LibraryUpdateInfo(); - } - - if (LibraryUpdateTimer == null) - { - LibraryUpdateTimer = new Timer(LibraryUpdateTimerCallback, null, LibraryUpdateDuration, - Timeout.Infinite); - } - else - { - LibraryUpdateTimer.Change(LibraryUpdateDuration, Timeout.Infinite); - } - - if (e.Item.Parent != null) - { - LibraryUpdateInfo.FoldersRemovedFrom.Add(e.Item.Parent.Id); - } - - LibraryUpdateInfo.ItemsRemoved.Add(e.Item.Id); - } - } - - /// - /// Libraries the update timer callback. - /// - /// The state. - private void LibraryUpdateTimerCallback(object state) - { - lock (_libraryChangedSyncLock) - { - // Remove dupes in case some were saved multiple times - LibraryUpdateInfo.FoldersAddedTo = LibraryUpdateInfo.FoldersAddedTo.Distinct().ToList(); - - LibraryUpdateInfo.FoldersRemovedFrom = LibraryUpdateInfo.FoldersRemovedFrom.Distinct().ToList(); - - LibraryUpdateInfo.ItemsUpdated = LibraryUpdateInfo.ItemsUpdated - .Where(i => !LibraryUpdateInfo.ItemsAdded.Contains(i)) - .Distinct() - .ToList(); - - _serverManager.SendWebSocketMessage("LibraryChanged", LibraryUpdateInfo); - - if (LibraryUpdateTimer != null) - { - LibraryUpdateTimer.Dispose(); - LibraryUpdateTimer = null; - } - - LibraryUpdateInfo = null; - } - } - /// /// Installations the manager_ plugin uninstalled. /// @@ -350,16 +191,6 @@ namespace MediaBrowser.ServerApplication.EntryPoints { if (dispose) { - if (LibraryUpdateTimer != null) - { - LibraryUpdateTimer.Dispose(); - LibraryUpdateTimer = null; - } - - _libraryManager.ItemAdded -= libraryManager_ItemAdded; - _libraryManager.ItemUpdated -= libraryManager_ItemUpdated; - _libraryManager.ItemRemoved -= libraryManager_ItemRemoved; - _userManager.UserDeleted -= userManager_UserDeleted; _userManager.UserUpdated -= userManager_UserUpdated; diff --git a/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj b/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj index 40dde03aa..19b2d91ca 100644 --- a/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj +++ b/MediaBrowser.ServerApplication/MediaBrowser.ServerApplication.csproj @@ -192,6 +192,7 @@ +