2013-04-15 18:45:58 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using MediaBrowser.Common.Events;
|
2013-03-07 05:34:00 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2013-03-01 21:22:34 +00:00
|
|
|
|
using MediaBrowser.Common.Plugins;
|
2013-03-07 05:34:00 +00:00
|
|
|
|
using MediaBrowser.Common.ScheduledTasks;
|
|
|
|
|
using MediaBrowser.Controller;
|
2013-04-13 18:02:30 +00:00
|
|
|
|
using MediaBrowser.Controller.Dto;
|
2013-03-01 21:22:34 +00:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
2013-03-05 04:25:27 +00:00
|
|
|
|
using MediaBrowser.Controller.Plugins;
|
2013-03-01 21:22:34 +00:00
|
|
|
|
using MediaBrowser.Controller.Updates;
|
2013-04-15 18:45:58 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2013-03-01 21:22:34 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-03-07 05:34:00 +00:00
|
|
|
|
using MediaBrowser.Model.Tasks;
|
2013-03-01 21:22:34 +00:00
|
|
|
|
using MediaBrowser.Model.Updates;
|
|
|
|
|
using System;
|
|
|
|
|
|
2013-03-08 05:08:27 +00:00
|
|
|
|
namespace MediaBrowser.ServerApplication.EntryPoints
|
2013-03-01 21:22:34 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Class WebSocketEvents
|
|
|
|
|
/// </summary>
|
2013-03-05 04:34:00 +00:00
|
|
|
|
public class WebSocketEvents : IServerEntryPoint
|
2013-03-01 21:22:34 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _server manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IServerManager _serverManager;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _logger
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _user manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _library manager
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _installation manager
|
|
|
|
|
/// </summary>
|
2013-03-05 04:25:27 +00:00
|
|
|
|
private readonly IInstallationManager _installationManager;
|
2013-03-01 21:22:34 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _kernel
|
|
|
|
|
/// </summary>
|
2013-03-07 05:34:00 +00:00
|
|
|
|
private readonly IServerApplicationHost _appHost;
|
|
|
|
|
|
2013-04-15 18:45:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _task manager
|
|
|
|
|
/// </summary>
|
2013-03-07 05:34:00 +00:00
|
|
|
|
private readonly ITaskManager _taskManager;
|
2013-04-15 18:45:58 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _library changed sync lock
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly object _libraryChangedSyncLock = new object();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the library update info.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The library update info.</value>
|
|
|
|
|
private LibraryUpdateInfo LibraryUpdateInfo { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the library update timer.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The library update timer.</value>
|
|
|
|
|
private Timer LibraryUpdateTimer { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The library update duration
|
|
|
|
|
/// </summary>
|
|
|
|
|
private const int LibraryUpdateDuration = 60000;
|
|
|
|
|
|
2013-03-01 21:22:34 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="WebSocketEvents" /> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="serverManager">The server manager.</param>
|
|
|
|
|
/// <param name="logger">The logger.</param>
|
|
|
|
|
/// <param name="userManager">The user manager.</param>
|
2013-03-07 05:34:00 +00:00
|
|
|
|
public WebSocketEvents(IServerManager serverManager, IServerApplicationHost appHost, ILogger logger, IUserManager userManager, ILibraryManager libraryManager, IInstallationManager installationManager, ITaskManager taskManager)
|
2013-03-01 21:22:34 +00:00
|
|
|
|
{
|
|
|
|
|
_serverManager = serverManager;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_userManager = userManager;
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_installationManager = installationManager;
|
2013-03-07 05:34:00 +00:00
|
|
|
|
_appHost = appHost;
|
|
|
|
|
_taskManager = taskManager;
|
2013-03-05 04:25:27 +00:00
|
|
|
|
}
|
2013-03-01 21:22:34 +00:00
|
|
|
|
|
2013-03-05 04:25:27 +00:00
|
|
|
|
public void Run()
|
|
|
|
|
{
|
2013-03-01 21:22:34 +00:00
|
|
|
|
_userManager.UserDeleted += userManager_UserDeleted;
|
|
|
|
|
_userManager.UserUpdated += userManager_UserUpdated;
|
|
|
|
|
|
|
|
|
|
_libraryManager.LibraryChanged += libraryManager_LibraryChanged;
|
|
|
|
|
|
2013-03-07 05:34:00 +00:00
|
|
|
|
_appHost.HasPendingRestartChanged += kernel_HasPendingRestartChanged;
|
2013-03-01 21:22:34 +00:00
|
|
|
|
|
2013-03-05 04:25:27 +00:00
|
|
|
|
_installationManager.PluginUninstalled += InstallationManager_PluginUninstalled;
|
|
|
|
|
_installationManager.PackageInstalling += installationManager_PackageInstalling;
|
|
|
|
|
_installationManager.PackageInstallationCancelled += installationManager_PackageInstallationCancelled;
|
|
|
|
|
_installationManager.PackageInstallationCompleted += installationManager_PackageInstallationCompleted;
|
|
|
|
|
_installationManager.PackageInstallationFailed += installationManager_PackageInstallationFailed;
|
2013-03-07 05:34:00 +00:00
|
|
|
|
|
|
|
|
|
_taskManager.TaskExecuting += _taskManager_TaskExecuting;
|
|
|
|
|
_taskManager.TaskCompleted += _taskManager_TaskCompleted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _taskManager_TaskCompleted(object sender, GenericEventArgs<TaskResult> e)
|
|
|
|
|
{
|
2013-03-28 05:19:58 +00:00
|
|
|
|
_serverManager.SendWebSocketMessage("ScheduledTaskEnded", e.Argument);
|
2013-03-07 05:34:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _taskManager_TaskExecuting(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var task = (IScheduledTask) sender;
|
2013-03-28 05:19:58 +00:00
|
|
|
|
_serverManager.SendWebSocketMessage("ScheduledTaskStarted", task.Name);
|
2013-03-01 21:22:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Installations the manager_ package installation failed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The sender.</param>
|
|
|
|
|
/// <param name="e">The e.</param>
|
|
|
|
|
void installationManager_PackageInstallationFailed(object sender, GenericEventArgs<InstallationInfo> e)
|
|
|
|
|
{
|
|
|
|
|
_serverManager.SendWebSocketMessage("PackageInstallationFailed", e.Argument);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Installations the manager_ package installation completed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The sender.</param>
|
|
|
|
|
/// <param name="e">The e.</param>
|
|
|
|
|
void installationManager_PackageInstallationCompleted(object sender, GenericEventArgs<InstallationInfo> e)
|
|
|
|
|
{
|
|
|
|
|
_serverManager.SendWebSocketMessage("PackageInstallationCompleted", e.Argument);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Installations the manager_ package installation cancelled.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The sender.</param>
|
|
|
|
|
/// <param name="e">The e.</param>
|
|
|
|
|
void installationManager_PackageInstallationCancelled(object sender, GenericEventArgs<InstallationInfo> e)
|
|
|
|
|
{
|
|
|
|
|
_serverManager.SendWebSocketMessage("PackageInstallationCancelled", e.Argument);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Installations the manager_ package installing.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The sender.</param>
|
|
|
|
|
/// <param name="e">The e.</param>
|
|
|
|
|
void installationManager_PackageInstalling(object sender, GenericEventArgs<InstallationInfo> e)
|
|
|
|
|
{
|
|
|
|
|
_serverManager.SendWebSocketMessage("PackageInstalling", e.Argument);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles the LibraryChanged event of the libraryManager control.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The source of the event.</param>
|
|
|
|
|
/// <param name="e">The <see cref="ChildrenChangedEventArgs" /> instance containing the event data.</param>
|
|
|
|
|
void libraryManager_LibraryChanged(object sender, ChildrenChangedEventArgs e)
|
|
|
|
|
{
|
2013-04-15 18:45:58 +00:00
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Libraries the update timer callback.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
|
private void LibraryUpdateTimerCallback(object state)
|
|
|
|
|
{
|
|
|
|
|
lock (_libraryChangedSyncLock)
|
|
|
|
|
{
|
|
|
|
|
_serverManager.SendWebSocketMessage("LibraryChanged", LibraryUpdateInfo);
|
|
|
|
|
|
|
|
|
|
if (LibraryUpdateTimer != null)
|
|
|
|
|
{
|
|
|
|
|
LibraryUpdateTimer.Dispose();
|
|
|
|
|
LibraryUpdateTimer = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-03-01 21:22:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Installations the manager_ plugin uninstalled.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The sender.</param>
|
|
|
|
|
/// <param name="e">The e.</param>
|
|
|
|
|
void InstallationManager_PluginUninstalled(object sender, GenericEventArgs<IPlugin> e)
|
|
|
|
|
{
|
|
|
|
|
_serverManager.SendWebSocketMessage("PluginUninstalled", e.Argument.GetPluginInfo());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles the HasPendingRestartChanged event of the kernel control.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The source of the event.</param>
|
|
|
|
|
/// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
|
|
|
|
|
void kernel_HasPendingRestartChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
2013-03-28 05:19:58 +00:00
|
|
|
|
_serverManager.SendWebSocketMessage("RestartRequired", _appHost.GetSystemInfo());
|
2013-03-01 21:22:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Users the manager_ user updated.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The sender.</param>
|
|
|
|
|
/// <param name="e">The e.</param>
|
2013-04-17 22:30:20 +00:00
|
|
|
|
async void userManager_UserUpdated(object sender, GenericEventArgs<User> e)
|
2013-03-01 21:22:34 +00:00
|
|
|
|
{
|
2013-04-17 22:30:20 +00:00
|
|
|
|
var dto = await new UserDtoBuilder(_logger).GetUserDto(e.Argument).ConfigureAwait(false);
|
2013-03-08 16:40:01 +00:00
|
|
|
|
|
|
|
|
|
_serverManager.SendWebSocketMessage("UserUpdated", dto);
|
2013-03-01 21:22:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Users the manager_ user deleted.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender">The sender.</param>
|
|
|
|
|
/// <param name="e">The e.</param>
|
|
|
|
|
void userManager_UserDeleted(object sender, GenericEventArgs<User> e)
|
|
|
|
|
{
|
|
|
|
|
_serverManager.SendWebSocketMessage("UserDeleted", e.Argument.Id.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Releases unmanaged and - optionally - managed resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
|
|
|
|
protected virtual void Dispose(bool dispose)
|
|
|
|
|
{
|
|
|
|
|
if (dispose)
|
|
|
|
|
{
|
2013-04-15 18:45:58 +00:00
|
|
|
|
if (LibraryUpdateTimer != null)
|
|
|
|
|
{
|
|
|
|
|
LibraryUpdateTimer.Dispose();
|
|
|
|
|
LibraryUpdateTimer = null;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-01 21:22:34 +00:00
|
|
|
|
_userManager.UserDeleted -= userManager_UserDeleted;
|
|
|
|
|
_userManager.UserUpdated -= userManager_UserUpdated;
|
|
|
|
|
|
|
|
|
|
_libraryManager.LibraryChanged -= libraryManager_LibraryChanged;
|
|
|
|
|
|
|
|
|
|
_installationManager.PluginUninstalled -= InstallationManager_PluginUninstalled;
|
|
|
|
|
_installationManager.PackageInstalling -= installationManager_PackageInstalling;
|
|
|
|
|
_installationManager.PackageInstallationCancelled -= installationManager_PackageInstallationCancelled;
|
|
|
|
|
_installationManager.PackageInstallationCompleted -= installationManager_PackageInstallationCompleted;
|
|
|
|
|
_installationManager.PackageInstallationFailed -= installationManager_PackageInstallationFailed;
|
|
|
|
|
|
2013-03-07 05:34:00 +00:00
|
|
|
|
_appHost.HasPendingRestartChanged -= kernel_HasPendingRestartChanged;
|
2013-03-01 21:22:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|