using System; using System.Threading; using System.Threading.Tasks; using MediaBrowser.Common.Plugins; using MediaBrowser.Common.Updates; using MediaBrowser.Controller; using MediaBrowser.Controller.Plugins; using MediaBrowser.Controller.Session; using MediaBrowser.Model.Updates; namespace Emby.Server.Implementations.EntryPoints { /// /// Class WebSocketEvents. /// public class ServerEventNotifier : IServerEntryPoint { /// /// 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 installation manager. /// The task manager. /// The session manager. public ServerEventNotifier( IServerApplicationHost appHost, IInstallationManager installationManager, ITaskManager taskManager, ISessionManager sessionManager) { _installationManager = installationManager; _appHost = appHost; _taskManager = taskManager; _sessionManager = sessionManager; } /// public Task RunAsync() { _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 async void OnPackageInstalling(object sender, InstallationInfo e) { await SendMessageToAdminSessions("PackageInstalling", e).ConfigureAwait(false); } private async void OnPackageInstallationCancelled(object sender, InstallationInfo e) { await SendMessageToAdminSessions("PackageInstallationCancelled", e).ConfigureAwait(false); } private async void OnPackageInstallationCompleted(object sender, InstallationInfo e) { await SendMessageToAdminSessions("PackageInstallationCompleted", e).ConfigureAwait(false); } private async void OnPackageInstallationFailed(object sender, InstallationFailedEventArgs e) { await SendMessageToAdminSessions("PackageInstallationFailed", e.InstallationInfo).ConfigureAwait(false); } private async void OnTaskCompleted(object sender, TaskCompletionEventArgs e) { await SendMessageToAdminSessions("ScheduledTaskEnded", e.Result).ConfigureAwait(false); } /// /// Installations the manager_ plugin uninstalled. /// /// The sender. /// The e. private async void OnPluginUninstalled(object sender, IPlugin e) { await SendMessageToAdminSessions("PluginUninstalled", e).ConfigureAwait(false); } /// /// Handles the HasPendingRestartChanged event of the kernel control. /// /// The source of the event. /// The instance containing the event data. private async void OnHasPendingRestartChanged(object sender, EventArgs e) { await _sessionManager.SendRestartRequiredNotification(CancellationToken.None).ConfigureAwait(false); } private async Task SendMessageToAdminSessions(string name, T data) { try { await _sessionManager.SendMessageToAdminSessions(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) { _installationManager.PluginUninstalled -= OnPluginUninstalled; _installationManager.PackageInstalling -= OnPackageInstalling; _installationManager.PackageInstallationCancelled -= OnPackageInstallationCancelled; _installationManager.PackageInstallationCompleted -= OnPackageInstallationCompleted; _installationManager.PackageInstallationFailed -= OnPackageInstallationFailed; _appHost.HasPendingRestartChanged -= OnHasPendingRestartChanged; _taskManager.TaskCompleted -= OnTaskCompleted; } } } }