ServerEventNotifier.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using MediaBrowser.Common.Plugins;
  2. using MediaBrowser.Common.ScheduledTasks;
  3. using MediaBrowser.Common.Updates;
  4. using MediaBrowser.Controller;
  5. using MediaBrowser.Controller.Entities;
  6. using MediaBrowser.Controller.Library;
  7. using MediaBrowser.Controller.Net;
  8. using MediaBrowser.Controller.Plugins;
  9. using MediaBrowser.Controller.Session;
  10. using MediaBrowser.Controller.Sync;
  11. using MediaBrowser.Model.Events;
  12. using MediaBrowser.Model.Sync;
  13. using System;
  14. using System.Threading;
  15. namespace MediaBrowser.Server.Implementations.EntryPoints
  16. {
  17. /// <summary>
  18. /// Class WebSocketEvents
  19. /// </summary>
  20. public class ServerEventNotifier : IServerEntryPoint
  21. {
  22. /// <summary>
  23. /// The _server manager
  24. /// </summary>
  25. private readonly IServerManager _serverManager;
  26. /// <summary>
  27. /// The _user manager
  28. /// </summary>
  29. private readonly IUserManager _userManager;
  30. /// <summary>
  31. /// The _installation manager
  32. /// </summary>
  33. private readonly IInstallationManager _installationManager;
  34. /// <summary>
  35. /// The _kernel
  36. /// </summary>
  37. private readonly IServerApplicationHost _appHost;
  38. /// <summary>
  39. /// The _task manager
  40. /// </summary>
  41. private readonly ITaskManager _taskManager;
  42. private readonly ISessionManager _sessionManager;
  43. private readonly ISyncManager _syncManager;
  44. public ServerEventNotifier(IServerManager serverManager, IServerApplicationHost appHost, IUserManager userManager, IInstallationManager installationManager, ITaskManager taskManager, ISessionManager sessionManager, ISyncManager syncManager)
  45. {
  46. _serverManager = serverManager;
  47. _userManager = userManager;
  48. _installationManager = installationManager;
  49. _appHost = appHost;
  50. _taskManager = taskManager;
  51. _sessionManager = sessionManager;
  52. _syncManager = syncManager;
  53. }
  54. public void Run()
  55. {
  56. _userManager.UserDeleted += userManager_UserDeleted;
  57. _userManager.UserUpdated += userManager_UserUpdated;
  58. _userManager.UserConfigurationUpdated += _userManager_UserConfigurationUpdated;
  59. _appHost.HasPendingRestartChanged += kernel_HasPendingRestartChanged;
  60. _installationManager.PluginUninstalled += InstallationManager_PluginUninstalled;
  61. _installationManager.PackageInstalling += _installationManager_PackageInstalling;
  62. _installationManager.PackageInstallationCancelled += _installationManager_PackageInstallationCancelled;
  63. _installationManager.PackageInstallationCompleted += _installationManager_PackageInstallationCompleted;
  64. _installationManager.PackageInstallationFailed += _installationManager_PackageInstallationFailed;
  65. _taskManager.TaskCompleted += _taskManager_TaskCompleted;
  66. _syncManager.SyncJobCreated += _syncManager_SyncJobCreated;
  67. _syncManager.SyncJobCancelled += _syncManager_SyncJobCancelled;
  68. }
  69. void _syncManager_SyncJobCancelled(object sender, GenericEventArgs<SyncJob> e)
  70. {
  71. _sessionManager.SendMessageToUserDeviceSessions(e.Argument.TargetId, "SyncJobCancelled", e.Argument, CancellationToken.None);
  72. }
  73. void _syncManager_SyncJobCreated(object sender, GenericEventArgs<SyncJobCreationResult> e)
  74. {
  75. _sessionManager.SendMessageToUserDeviceSessions(e.Argument.Job.TargetId, "SyncJobCreated", e.Argument, CancellationToken.None);
  76. }
  77. void _installationManager_PackageInstalling(object sender, InstallationEventArgs e)
  78. {
  79. _serverManager.SendWebSocketMessage("PackageInstalling", e.InstallationInfo);
  80. }
  81. void _installationManager_PackageInstallationCancelled(object sender, InstallationEventArgs e)
  82. {
  83. _serverManager.SendWebSocketMessage("PackageInstallationCancelled", e.InstallationInfo);
  84. }
  85. void _installationManager_PackageInstallationCompleted(object sender, InstallationEventArgs e)
  86. {
  87. _serverManager.SendWebSocketMessage("PackageInstallationCompleted", e.InstallationInfo);
  88. }
  89. void _installationManager_PackageInstallationFailed(object sender, InstallationFailedEventArgs e)
  90. {
  91. _serverManager.SendWebSocketMessage("PackageInstallationFailed", e.InstallationInfo);
  92. }
  93. void _taskManager_TaskCompleted(object sender, TaskCompletionEventArgs e)
  94. {
  95. _serverManager.SendWebSocketMessage("ScheduledTaskEnded", e.Result);
  96. }
  97. /// <summary>
  98. /// Installations the manager_ plugin uninstalled.
  99. /// </summary>
  100. /// <param name="sender">The sender.</param>
  101. /// <param name="e">The e.</param>
  102. void InstallationManager_PluginUninstalled(object sender, GenericEventArgs<IPlugin> e)
  103. {
  104. _serverManager.SendWebSocketMessage("PluginUninstalled", e.Argument.GetPluginInfo());
  105. }
  106. /// <summary>
  107. /// Handles the HasPendingRestartChanged event of the kernel control.
  108. /// </summary>
  109. /// <param name="sender">The source of the event.</param>
  110. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  111. void kernel_HasPendingRestartChanged(object sender, EventArgs e)
  112. {
  113. _sessionManager.SendRestartRequiredNotification(CancellationToken.None);
  114. }
  115. /// <summary>
  116. /// Users the manager_ user updated.
  117. /// </summary>
  118. /// <param name="sender">The sender.</param>
  119. /// <param name="e">The e.</param>
  120. void userManager_UserUpdated(object sender, GenericEventArgs<User> e)
  121. {
  122. var dto = _userManager.GetUserDto(e.Argument);
  123. SendMessageToUserSession(e.Argument, "UserUpdated", dto);
  124. }
  125. /// <summary>
  126. /// Users the manager_ user deleted.
  127. /// </summary>
  128. /// <param name="sender">The sender.</param>
  129. /// <param name="e">The e.</param>
  130. void userManager_UserDeleted(object sender, GenericEventArgs<User> e)
  131. {
  132. SendMessageToUserSession(e.Argument, "UserDeleted", e.Argument.Id.ToString("N"));
  133. }
  134. void _userManager_UserConfigurationUpdated(object sender, GenericEventArgs<User> e)
  135. {
  136. var dto = _userManager.GetUserDto(e.Argument);
  137. SendMessageToUserSession(e.Argument, "UserConfigurationUpdated", dto);
  138. }
  139. private async void SendMessageToUserSession<T>(User user, string name, T data)
  140. {
  141. await _sessionManager.SendMessageToUserSessions(user.Id.ToString("N"), name, data, CancellationToken.None);
  142. }
  143. /// <summary>
  144. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  145. /// </summary>
  146. public void Dispose()
  147. {
  148. Dispose(true);
  149. }
  150. /// <summary>
  151. /// Releases unmanaged and - optionally - managed resources.
  152. /// </summary>
  153. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  154. protected virtual void Dispose(bool dispose)
  155. {
  156. if (dispose)
  157. {
  158. _userManager.UserDeleted -= userManager_UserDeleted;
  159. _userManager.UserUpdated -= userManager_UserUpdated;
  160. _userManager.UserConfigurationUpdated -= _userManager_UserConfigurationUpdated;
  161. _installationManager.PluginUninstalled -= InstallationManager_PluginUninstalled;
  162. _installationManager.PackageInstalling -= _installationManager_PackageInstalling;
  163. _installationManager.PackageInstallationCancelled -= _installationManager_PackageInstallationCancelled;
  164. _installationManager.PackageInstallationCompleted -= _installationManager_PackageInstallationCompleted;
  165. _installationManager.PackageInstallationFailed -= _installationManager_PackageInstallationFailed;
  166. _appHost.HasPendingRestartChanged -= kernel_HasPendingRestartChanged;
  167. _syncManager.SyncJobCreated -= _syncManager_SyncJobCreated;
  168. _syncManager.SyncJobCancelled -= _syncManager_SyncJobCancelled;
  169. }
  170. }
  171. }
  172. }