using MediaBrowser.Common.Plugins;
using MediaBrowser.Common.ScheduledTasks;
using MediaBrowser.Common.Updates;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Controller.Session;
using MediaBrowser.Controller.Sync;
using MediaBrowser.Model.Events;
using MediaBrowser.Model.Sync;
using System;
using System.Collections.Generic;
using System.Threading;
using MediaBrowser.Model.Tasks;
namespace MediaBrowser.Server.Implementations.EntryPoints
{
///
/// Class WebSocketEvents
///
public class ServerEventNotifier : IServerEntryPoint
{
///
/// The _server manager
///
private readonly IServerManager _serverManager;
///
/// The _user manager
///
private readonly IUserManager _userManager;
///
/// The _installation manager
///
private readonly IInstallationManager _installationManager;
///
/// The _kernel
///
private readonly IServerApplicationHost _appHost;
///
/// The _task manager
///
private readonly ITaskManager _taskManager;
private readonly ISessionManager _sessionManager;
private readonly ISyncManager _syncManager;
public ServerEventNotifier(IServerManager serverManager, IServerApplicationHost appHost, IUserManager userManager, IInstallationManager installationManager, ITaskManager taskManager, ISessionManager sessionManager, ISyncManager syncManager)
{
_serverManager = serverManager;
_userManager = userManager;
_installationManager = installationManager;
_appHost = appHost;
_taskManager = taskManager;
_sessionManager = sessionManager;
_syncManager = syncManager;
}
public void Run()
{
_userManager.UserDeleted += userManager_UserDeleted;
_userManager.UserUpdated += userManager_UserUpdated;
_userManager.UserConfigurationUpdated += _userManager_UserConfigurationUpdated;
_appHost.HasPendingRestartChanged += kernel_HasPendingRestartChanged;
_installationManager.PluginUninstalled += InstallationManager_PluginUninstalled;
_installationManager.PackageInstalling += _installationManager_PackageInstalling;
_installationManager.PackageInstallationCancelled += _installationManager_PackageInstallationCancelled;
_installationManager.PackageInstallationCompleted += _installationManager_PackageInstallationCompleted;
_installationManager.PackageInstallationFailed += _installationManager_PackageInstallationFailed;
_taskManager.TaskCompleted += _taskManager_TaskCompleted;
_syncManager.SyncJobCreated += _syncManager_SyncJobCreated;
_syncManager.SyncJobCancelled += _syncManager_SyncJobCancelled;
}
void _syncManager_SyncJobCancelled(object sender, GenericEventArgs e)
{
_sessionManager.SendMessageToUserDeviceSessions(e.Argument.TargetId, "SyncJobCancelled", e.Argument, CancellationToken.None);
}
void _syncManager_SyncJobCreated(object sender, GenericEventArgs e)
{
_sessionManager.SendMessageToUserDeviceSessions(e.Argument.Job.TargetId, "SyncJobCreated", e.Argument, CancellationToken.None);
}
void _installationManager_PackageInstalling(object sender, InstallationEventArgs e)
{
_serverManager.SendWebSocketMessage("PackageInstalling", e.InstallationInfo);
}
void _installationManager_PackageInstallationCancelled(object sender, InstallationEventArgs e)
{
_serverManager.SendWebSocketMessage("PackageInstallationCancelled", e.InstallationInfo);
}
void _installationManager_PackageInstallationCompleted(object sender, InstallationEventArgs e)
{
_serverManager.SendWebSocketMessage("PackageInstallationCompleted", e.InstallationInfo);
}
void _installationManager_PackageInstallationFailed(object sender, InstallationFailedEventArgs e)
{
_serverManager.SendWebSocketMessage("PackageInstallationFailed", e.InstallationInfo);
}
void _taskManager_TaskCompleted(object sender, TaskCompletionEventArgs e)
{
_serverManager.SendWebSocketMessage("ScheduledTaskEnded", e.Result);
}
///
/// Installations the manager_ plugin uninstalled.
///
/// The sender.
/// The e.
void InstallationManager_PluginUninstalled(object sender, GenericEventArgs e)
{
_serverManager.SendWebSocketMessage("PluginUninstalled", e.Argument.GetPluginInfo());
}
///
/// Handles the HasPendingRestartChanged event of the kernel control.
///
/// The source of the event.
/// The instance containing the event data.
void kernel_HasPendingRestartChanged(object sender, EventArgs e)
{
_sessionManager.SendRestartRequiredNotification(CancellationToken.None);
}
///
/// Users the manager_ user updated.
///
/// The sender.
/// The e.
void userManager_UserUpdated(object sender, GenericEventArgs e)
{
var dto = _userManager.GetUserDto(e.Argument);
SendMessageToUserSession(e.Argument, "UserUpdated", dto);
}
///
/// Users the manager_ user deleted.
///
/// The sender.
/// The e.
void userManager_UserDeleted(object sender, GenericEventArgs e)
{
SendMessageToUserSession(e.Argument, "UserDeleted", e.Argument.Id.ToString("N"));
}
void _userManager_UserConfigurationUpdated(object sender, GenericEventArgs e)
{
var dto = _userManager.GetUserDto(e.Argument);
SendMessageToUserSession(e.Argument, "UserConfigurationUpdated", dto);
}
private async void SendMessageToUserSession(User user, string name, T data)
{
await _sessionManager.SendMessageToUserSessions(new List { user.Id.ToString("N") }, name, data, CancellationToken.None);
}
///
/// 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)
{
_userManager.UserDeleted -= userManager_UserDeleted;
_userManager.UserUpdated -= userManager_UserUpdated;
_userManager.UserConfigurationUpdated -= _userManager_UserConfigurationUpdated;
_installationManager.PluginUninstalled -= InstallationManager_PluginUninstalled;
_installationManager.PackageInstalling -= _installationManager_PackageInstalling;
_installationManager.PackageInstallationCancelled -= _installationManager_PackageInstallationCancelled;
_installationManager.PackageInstallationCompleted -= _installationManager_PackageInstallationCompleted;
_installationManager.PackageInstallationFailed -= _installationManager_PackageInstallationFailed;
_appHost.HasPendingRestartChanged -= kernel_HasPendingRestartChanged;
_syncManager.SyncJobCreated -= _syncManager_SyncJobCreated;
_syncManager.SyncJobCancelled -= _syncManager_SyncJobCancelled;
}
}
}
}