aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/EntryPoints/ServerEventNotifier.cs
blob: 549e1d56903860b9691d6c8902435e357a522b70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using System;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Updates;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Updates;

namespace Emby.Server.Implementations.EntryPoints
{
    /// <summary>
    /// Class WebSocketEvents.
    /// </summary>
    public class ServerEventNotifier : IServerEntryPoint
    {
        /// <summary>
        /// The installation manager.
        /// </summary>
        private readonly IInstallationManager _installationManager;

        private readonly ISessionManager _sessionManager;

        /// <summary>
        /// Initializes a new instance of the <see cref="ServerEventNotifier"/> class.
        /// </summary>
        /// <param name="installationManager">The installation manager.</param>
        /// <param name="sessionManager">The session manager.</param>
        public ServerEventNotifier(
            IInstallationManager installationManager,
            ISessionManager sessionManager)
        {
            _installationManager = installationManager;
            _sessionManager = sessionManager;
        }

        /// <inheritdoc />
        public Task RunAsync()
        {
            _installationManager.PackageInstallationCancelled += OnPackageInstallationCancelled;
            _installationManager.PackageInstallationCompleted += OnPackageInstallationCompleted;

            return Task.CompletedTask;
        }

        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 Task SendMessageToAdminSessions<T>(string name, T data)
        {
            try
            {
                await _sessionManager.SendMessageToAdminSessions(name, data, CancellationToken.None).ConfigureAwait(false);
            }
            catch (Exception)
            {
            }
        }

        /// <inheritdoc />
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <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)
            {
                _installationManager.PackageInstallationCancelled -= OnPackageInstallationCancelled;
                _installationManager.PackageInstallationCompleted -= OnPackageInstallationCompleted;
            }
        }
    }
}