From da7af24fca3b2462b971dce595cfa5e548311cce Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 17 May 2013 11:29:22 -0400 Subject: add separate entry point for new item notifier --- MediaBrowser.ServerApplication/ApplicationHost.cs | 15 +- .../EntryPoints/LibraryChangedNotifier.cs | 210 +++++++++++++++++++++ .../EntryPoints/WebSocketEvents.cs | 171 +---------------- .../MediaBrowser.ServerApplication.csproj | 1 + 4 files changed, 218 insertions(+), 179 deletions(-) create mode 100644 MediaBrowser.ServerApplication/EntryPoints/LibraryChangedNotifier.cs (limited to 'MediaBrowser.ServerApplication') 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,18 +378,15 @@ namespace MediaBrowser.ServerApplication RegisterServerWithAdministratorAccess(); } - Parallel.Invoke( + base.FindParts(); - () => base.FindParts(), + HttpServer.Init(GetExports(false)); - () => - { - HttpServer.Init(GetExports(false)); + ServerManager.AddWebSocketListeners(GetExports(false)); - ServerManager.AddWebSocketListeners(GetExports(false)); - - StartServer(true); - }, + StartServer(true); + + Parallel.Invoke( () => LibraryManager.AddParts(GetExports(), GetExports(), GetExports(), GetExports(), GetExports()), 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 @@ + -- cgit v1.2.3