using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Plugins;
using MediaBrowser.Common.Updates;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Events;
using MediaBrowser.Model.Tasks;
using MediaBrowser.Model.Updates;
namespace Emby.Server.Implementations.EntryPoints
{
///
/// Class WebSocketEvents.
///
public class ServerEventNotifier : IServerEntryPoint
{
///
/// 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;
///
/// Initializes a new instance of the class.
///
/// The application host.
/// The user manager.
/// The installation manager.
/// The task manager.
/// The session manager.
public ServerEventNotifier(
IServerApplicationHost appHost,
IUserManager userManager,
IInstallationManager installationManager,
ITaskManager taskManager,
ISessionManager sessionManager)
{
_userManager = userManager;
_installationManager = installationManager;
_appHost = appHost;
_taskManager = taskManager;
_sessionManager = sessionManager;
}
///
public Task RunAsync()
{
_userManager.UserDeleted += OnUserDeleted;
_userManager.UserUpdated += OnUserUpdated;
_userManager.UserPolicyUpdated += OnUserPolicyUpdated;
_userManager.UserConfigurationUpdated += OnUserConfigurationUpdated;
_appHost.HasPendingRestartChanged += OnHasPendingRestartChanged;
_installationManager.PluginUninstalled += OnPluginUninstalled;
_installationManager.PackageInstalling += OnPackageInstalling;
_installationManager.PackageInstallationCancelled += OnPackageInstallationCancelled;
_installationManager.PackageInstallationCompleted += OnPackageInstallationCompleted;
_installationManager.PackageInstallationFailed += OnPackageInstallationFailed;
_taskManager.TaskCompleted += OnTaskCompleted;
return Task.CompletedTask;
}
private void OnPackageInstalling(object sender, InstallationInfo e)
{
SendMessageToAdminSessions("PackageInstalling", e);
}
private void OnPackageInstallationCancelled(object sender, InstallationInfo e)
{
SendMessageToAdminSessions("PackageInstallationCancelled", e);
}
private void OnPackageInstallationCompleted(object sender, InstallationInfo e)
{
SendMessageToAdminSessions("PackageInstallationCompleted", e);
}
private void OnPackageInstallationFailed(object sender, InstallationFailedEventArgs e)
{
SendMessageToAdminSessions("PackageInstallationFailed", e.InstallationInfo);
}
private void OnTaskCompleted(object sender, TaskCompletionEventArgs e)
{
SendMessageToAdminSessions("ScheduledTaskEnded", e.Result);
}
///
/// Installations the manager_ plugin uninstalled.
///
/// The sender.
/// The e.
private void OnPluginUninstalled(object sender, IPlugin e)
{
SendMessageToAdminSessions("PluginUninstalled", e);
}
///
/// Handles the HasPendingRestartChanged event of the kernel control.
///
/// The source of the event.
/// The instance containing the event data.
private void OnHasPendingRestartChanged(object sender, EventArgs e)
{
_sessionManager.SendRestartRequiredNotification(CancellationToken.None);
}
///
/// Users the manager_ user updated.
///
/// The sender.
/// The e.
private void OnUserUpdated(object sender, GenericEventArgs e)
{
var dto = _userManager.GetUserDto(e.Argument);
SendMessageToUserSession(e.Argument, "UserUpdated", dto);
}
///
/// Users the manager_ user deleted.
///
/// The sender.
/// The e.
private void OnUserDeleted(object sender, GenericEventArgs e)
{
SendMessageToUserSession(e.Argument, "UserDeleted", e.Argument.Id.ToString("N", CultureInfo.InvariantCulture));
}
private void OnUserPolicyUpdated(object sender, GenericEventArgs e)
{
var dto = _userManager.GetUserDto(e.Argument);
SendMessageToUserSession(e.Argument, "UserPolicyUpdated", dto);
}
private void OnUserConfigurationUpdated(object sender, GenericEventArgs e)
{
var dto = _userManager.GetUserDto(e.Argument);
SendMessageToUserSession(e.Argument, "UserConfigurationUpdated", dto);
}
private async void SendMessageToAdminSessions(string name, T data)
{
try
{
await _sessionManager.SendMessageToAdminSessions(name, data, CancellationToken.None).ConfigureAwait(false);
}
catch (Exception)
{
}
}
private async void SendMessageToUserSession(User user, string name, T data)
{
try
{
await _sessionManager.SendMessageToUserSessions(
new List { user.Id },
name,
data,
CancellationToken.None).ConfigureAwait(false);
}
catch (Exception)
{
}
}
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// 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 -= OnUserDeleted;
_userManager.UserUpdated -= OnUserUpdated;
_userManager.UserPolicyUpdated -= OnUserPolicyUpdated;
_userManager.UserConfigurationUpdated -= OnUserConfigurationUpdated;
_installationManager.PluginUninstalled -= OnPluginUninstalled;
_installationManager.PackageInstalling -= OnPackageInstalling;
_installationManager.PackageInstallationCancelled -= OnPackageInstallationCancelled;
_installationManager.PackageInstallationCompleted -= OnPackageInstallationCompleted;
_installationManager.PackageInstallationFailed -= OnPackageInstallationFailed;
_appHost.HasPendingRestartChanged -= OnHasPendingRestartChanged;
_taskManager.TaskCompleted -= OnTaskCompleted;
}
}
}
}