From 2b8b98b59090ab6d077ac76cf9185da7d6ac126a Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 15 Apr 2013 14:45:58 -0400 Subject: reduce scanning overhead a bit --- .../EntryPoints/WebSocketEvents.cs | 80 +++++++++++++++++++++- 1 file changed, 77 insertions(+), 3 deletions(-) (limited to 'MediaBrowser.ServerApplication/EntryPoints/WebSocketEvents.cs') diff --git a/MediaBrowser.ServerApplication/EntryPoints/WebSocketEvents.cs b/MediaBrowser.ServerApplication/EntryPoints/WebSocketEvents.cs index 19c42e8d8..a8eb8f4c0 100644 --- a/MediaBrowser.ServerApplication/EntryPoints/WebSocketEvents.cs +++ b/MediaBrowser.ServerApplication/EntryPoints/WebSocketEvents.cs @@ -1,4 +1,6 @@ -using MediaBrowser.Common.Events; +using System.Linq; +using System.Threading; +using MediaBrowser.Common.Events; using MediaBrowser.Common.Net; using MediaBrowser.Common.Plugins; using MediaBrowser.Common.ScheduledTasks; @@ -8,6 +10,7 @@ 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; @@ -49,8 +52,33 @@ namespace MediaBrowser.ServerApplication.EntryPoints /// private readonly IServerApplicationHost _appHost; + /// + /// The _task manager + /// 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. /// @@ -145,7 +173,47 @@ namespace MediaBrowser.ServerApplication.EntryPoints /// The instance containing the event data. void libraryManager_LibraryChanged(object sender, ChildrenChangedEventArgs e) { - _serverManager.SendWebSocketMessage("LibraryChanged", () => DtoBuilder.GetLibraryUpdateInfo(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.Folders.Add(e.Folder.Id); + + LibraryUpdateInfo.ItemsAdded.AddRange(e.ItemsAdded.Select(i => i.Id)); + LibraryUpdateInfo.ItemsUpdated.AddRange(e.ItemsUpdated.Select(i => i.Id)); + LibraryUpdateInfo.ItemsRemoved.AddRange(e.ItemsRemoved.Select(i => i.Id)); + } + } + + /// + /// Libraries the update timer callback. + /// + /// The state. + private void LibraryUpdateTimerCallback(object state) + { + lock (_libraryChangedSyncLock) + { + _serverManager.SendWebSocketMessage("LibraryChanged", LibraryUpdateInfo); + + if (LibraryUpdateTimer != null) + { + LibraryUpdateTimer.Dispose(); + LibraryUpdateTimer = null; + } + } } /// @@ -206,6 +274,12 @@ namespace MediaBrowser.ServerApplication.EntryPoints { if (dispose) { + if (LibraryUpdateTimer != null) + { + LibraryUpdateTimer.Dispose(); + LibraryUpdateTimer = null; + } + _userManager.UserDeleted -= userManager_UserDeleted; _userManager.UserUpdated -= userManager_UserUpdated; -- cgit v1.2.3