From 8b3e8ad478e96543aa437f5a416e7db48ce90a63 Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Fri, 22 Feb 2013 10:16:48 -0500 Subject: [PATCH] created IWebSocketListener --- .../ScheduledTasksWebSocketListener.cs | 15 ++- .../WebSocket/LogFileWebSocketListener.cs | 33 +++---- .../WebSocket/SystemInfoWebSocketListener.cs | 15 ++- MediaBrowser.Common/Kernel/BaseKernel.cs | 5 - .../Kernel/BasePeriodicWebSocketListener.cs | 24 +++-- .../Kernel/BaseWebSocketListener.cs | 98 ------------------- .../Kernel/IWebSocketListener.cs | 18 ++++ .../MediaBrowser.Common.csproj | 2 +- .../ScheduledTasks/PeopleValidationTask.cs | 1 - .../Api/DashboardInfoWebSocketListener.cs | 14 ++- 10 files changed, 83 insertions(+), 142 deletions(-) delete mode 100644 MediaBrowser.Common/Kernel/BaseWebSocketListener.cs create mode 100644 MediaBrowser.Common/Kernel/IWebSocketListener.cs diff --git a/MediaBrowser.Api/ScheduledTasks/ScheduledTasksWebSocketListener.cs b/MediaBrowser.Api/ScheduledTasks/ScheduledTasksWebSocketListener.cs index 7fa47735e..cb7863d84 100644 --- a/MediaBrowser.Api/ScheduledTasks/ScheduledTasksWebSocketListener.cs +++ b/MediaBrowser.Api/ScheduledTasks/ScheduledTasksWebSocketListener.cs @@ -1,5 +1,6 @@ using MediaBrowser.Common.Kernel; using MediaBrowser.Common.ScheduledTasks; +using MediaBrowser.Controller; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Tasks; using System.Collections.Generic; @@ -13,7 +14,7 @@ namespace MediaBrowser.Api.ScheduledTasks /// Class ScheduledTasksWebSocketListener /// [Export(typeof(IWebSocketListener))] - public class ScheduledTasksWebSocketListener : BasePeriodicWebSocketListener, object> + public class ScheduledTasksWebSocketListener : BasePeriodicWebSocketListener, object> { /// /// Gets the name. @@ -24,15 +25,21 @@ namespace MediaBrowser.Api.ScheduledTasks get { return "ScheduledTasksInfo"; } } + /// + /// The _kernel + /// + private readonly IKernel _kernel; + /// /// Initializes a new instance of the class. /// + /// The kernel. /// The logger. [ImportingConstructor] - public ScheduledTasksWebSocketListener([Import("logger")] ILogger logger) + public ScheduledTasksWebSocketListener([Import("kernel")] Kernel kernel, [Import("logger")] ILogger logger) : base(logger) { - + _kernel = kernel; } /// @@ -42,7 +49,7 @@ namespace MediaBrowser.Api.ScheduledTasks /// Task{IEnumerable{TaskInfo}}. protected override Task> GetDataToSend(object state) { - return Task.FromResult(Kernel.ScheduledTasks.OrderBy(i => i.Name) + return Task.FromResult(_kernel.ScheduledTasks.OrderBy(i => i.Name) .Select(ScheduledTaskHelpers.GetTaskInfo)); } } diff --git a/MediaBrowser.Api/WebSocket/LogFileWebSocketListener.cs b/MediaBrowser.Api/WebSocket/LogFileWebSocketListener.cs index f725b0222..144997c73 100644 --- a/MediaBrowser.Api/WebSocket/LogFileWebSocketListener.cs +++ b/MediaBrowser.Api/WebSocket/LogFileWebSocketListener.cs @@ -1,12 +1,13 @@ using MediaBrowser.Common.IO; using MediaBrowser.Common.Kernel; +using MediaBrowser.Controller; +using MediaBrowser.Model.Logging; using System; using System.Collections.Generic; using System.ComponentModel.Composition; using System.IO; using System.Linq; using System.Threading.Tasks; -using MediaBrowser.Model.Logging; namespace MediaBrowser.Api.WebSocket { @@ -14,7 +15,7 @@ namespace MediaBrowser.Api.WebSocket /// Class ScheduledTasksWebSocketListener /// [Export(typeof(IWebSocketListener))] - public class LogFileWebSocketListener : BasePeriodicWebSocketListener, LogFileWebSocketState> + public class LogFileWebSocketListener : BasePeriodicWebSocketListener, LogFileWebSocketState> { /// /// Gets the name. @@ -25,26 +26,22 @@ namespace MediaBrowser.Api.WebSocket get { return "LogFile"; } } + /// + /// The _kernel + /// + private readonly IKernel _kernel; + /// /// Initializes a new instance of the class. /// /// The logger. + /// The kernel. [ImportingConstructor] - public LogFileWebSocketListener([Import("logger")] ILogger logger) + public LogFileWebSocketListener([Import("logger")] ILogger logger, [Import("kernel")] Kernel kernel) : base(logger) { - - } - - /// - /// Initializes the specified kernel. - /// - /// The kernel. - public override void Initialize(IKernel kernel) - { - base.Initialize(kernel); - - kernel.LoggerLoaded += kernel_LoggerLoaded; + _kernel = kernel; + _kernel.LoggerLoaded += kernel_LoggerLoaded; } /// @@ -54,9 +51,9 @@ namespace MediaBrowser.Api.WebSocket /// IEnumerable{System.String}. protected override async Task> GetDataToSend(LogFileWebSocketState state) { - if (!string.Equals(Kernel.LogFilePath, state.LastLogFilePath)) + if (!string.Equals(_kernel.LogFilePath, state.LastLogFilePath)) { - state.LastLogFilePath = Kernel.LogFilePath; + state.LastLogFilePath = _kernel.LogFilePath; state.StartLine = 0; } @@ -75,7 +72,7 @@ namespace MediaBrowser.Api.WebSocket { if (dispose) { - Kernel.LoggerLoaded -= kernel_LoggerLoaded; + _kernel.LoggerLoaded -= kernel_LoggerLoaded; } base.Dispose(dispose); } diff --git a/MediaBrowser.Api/WebSocket/SystemInfoWebSocketListener.cs b/MediaBrowser.Api/WebSocket/SystemInfoWebSocketListener.cs index 51d9f2fcd..c41714581 100644 --- a/MediaBrowser.Api/WebSocket/SystemInfoWebSocketListener.cs +++ b/MediaBrowser.Api/WebSocket/SystemInfoWebSocketListener.cs @@ -1,4 +1,5 @@ using MediaBrowser.Common.Kernel; +using MediaBrowser.Controller; using MediaBrowser.Model.Logging; using MediaBrowser.Model.System; using System.ComponentModel.Composition; @@ -10,7 +11,7 @@ namespace MediaBrowser.Api.WebSocket /// Class SystemInfoWebSocketListener /// [Export(typeof(IWebSocketListener))] - public class SystemInfoWebSocketListener : BasePeriodicWebSocketListener + public class SystemInfoWebSocketListener : BasePeriodicWebSocketListener { /// /// Gets the name. @@ -21,15 +22,21 @@ namespace MediaBrowser.Api.WebSocket get { return "SystemInfo"; } } + /// + /// The _kernel + /// + private readonly IKernel _kernel; + /// /// Initializes a new instance of the class. /// + /// The kernel. /// The logger. [ImportingConstructor] - public SystemInfoWebSocketListener([Import("logger")] ILogger logger) + public SystemInfoWebSocketListener([Import("kernel")] Kernel kernel, [Import("logger")] ILogger logger) : base(logger) { - + _kernel = kernel; } /// @@ -39,7 +46,7 @@ namespace MediaBrowser.Api.WebSocket /// Task{SystemInfo}. protected override Task GetDataToSend(object state) { - return Task.FromResult(Kernel.GetSystemInfo()); + return Task.FromResult(_kernel.GetSystemInfo()); } } } diff --git a/MediaBrowser.Common/Kernel/BaseKernel.cs b/MediaBrowser.Common/Kernel/BaseKernel.cs index d172d0a0d..2b3986408 100644 --- a/MediaBrowser.Common/Kernel/BaseKernel.cs +++ b/MediaBrowser.Common/Kernel/BaseKernel.cs @@ -553,11 +553,6 @@ namespace MediaBrowser.Common.Kernel { return Task.Run(() => { - foreach (var listener in WebSocketListeners) - { - listener.Initialize(this); - } - foreach (var task in ScheduledTasks) { task.Initialize(this, Logger); diff --git a/MediaBrowser.Common/Kernel/BasePeriodicWebSocketListener.cs b/MediaBrowser.Common/Kernel/BasePeriodicWebSocketListener.cs index 11ffa8d6c..0f20758d4 100644 --- a/MediaBrowser.Common/Kernel/BasePeriodicWebSocketListener.cs +++ b/MediaBrowser.Common/Kernel/BasePeriodicWebSocketListener.cs @@ -12,11 +12,9 @@ namespace MediaBrowser.Common.Kernel /// /// Starts sending data over a web socket periodically when a message is received, and then stops when a corresponding stop message is received /// - /// The type of the T kernel type. /// The type of the T return data type. /// The type of the T state type. - public abstract class BasePeriodicWebSocketListener : BaseWebSocketListener - where TKernelType : IKernel + public abstract class BasePeriodicWebSocketListener : IWebSocketListener, IDisposable where TStateType : class, new() { /// @@ -47,6 +45,7 @@ namespace MediaBrowser.Common.Kernel /// Initializes a new instance of the class. /// /// The logger. + /// logger protected BasePeriodicWebSocketListener(ILogger logger) { if (logger == null) @@ -58,11 +57,16 @@ namespace MediaBrowser.Common.Kernel } /// - /// Processes the message internal. + /// The null task result + /// + protected Task NullTaskResult = Task.FromResult(true); + + /// + /// Processes the message. /// /// The message. /// Task. - protected override Task ProcessMessageInternal(WebSocketMessageInfo message) + public Task ProcessMessage(WebSocketMessageInfo message) { if (message.MessageType.Equals(Name + "Start", StringComparison.OrdinalIgnoreCase)) { @@ -223,7 +227,7 @@ namespace MediaBrowser.Common.Kernel /// Releases unmanaged and - optionally - managed resources. /// /// true to release both managed and unmanaged resources; false to release only unmanaged resources. - protected override void Dispose(bool dispose) + protected virtual void Dispose(bool dispose) { if (dispose) { @@ -235,8 +239,14 @@ namespace MediaBrowser.Common.Kernel } } } + } - base.Dispose(dispose); + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + public void Dispose() + { + Dispose(true); } } } diff --git a/MediaBrowser.Common/Kernel/BaseWebSocketListener.cs b/MediaBrowser.Common/Kernel/BaseWebSocketListener.cs deleted file mode 100644 index 2870403b2..000000000 --- a/MediaBrowser.Common/Kernel/BaseWebSocketListener.cs +++ /dev/null @@ -1,98 +0,0 @@ -using MediaBrowser.Common.Net; -using System; -using System.Threading.Tasks; - -namespace MediaBrowser.Common.Kernel -{ - /// - /// Represents a class that is notified everytime the server receives a message over a WebSocket - /// - /// The type of the T kernel type. - public abstract class BaseWebSocketListener : IWebSocketListener - where TKernelType : IKernel - { - /// - /// The null task result - /// - protected Task NullTaskResult = Task.FromResult(true); - - /// - /// Gets the kernel. - /// - /// The kernel. - protected TKernelType Kernel { get; private set; } - - /// - /// Initializes the specified kernel. - /// - /// The kernel. - public virtual void Initialize(IKernel kernel) - { - if (kernel == null) - { - throw new ArgumentNullException("kernel"); - } - - Kernel = (TKernelType)kernel; - } - - /// - /// Processes the message. - /// - /// The message. - /// Task. - /// message - public Task ProcessMessage(WebSocketMessageInfo message) - { - if (message == null) - { - throw new ArgumentNullException("message"); - } - - return ProcessMessageInternal(message); - } - - /// - /// Processes the message internal. - /// - /// The message. - /// Task. - protected abstract Task ProcessMessageInternal(WebSocketMessageInfo message); - - /// - /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. - /// - 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) - { - } - } - - /// - /// Interface IWebSocketListener - /// - public interface IWebSocketListener : IDisposable - { - /// - /// Processes the message. - /// - /// The message. - /// Task. - Task ProcessMessage(WebSocketMessageInfo message); - - /// - /// Initializes the specified kernel. - /// - /// The kernel. - void Initialize(IKernel kernel); - } -} diff --git a/MediaBrowser.Common/Kernel/IWebSocketListener.cs b/MediaBrowser.Common/Kernel/IWebSocketListener.cs new file mode 100644 index 000000000..b04bb6885 --- /dev/null +++ b/MediaBrowser.Common/Kernel/IWebSocketListener.cs @@ -0,0 +1,18 @@ +using MediaBrowser.Common.Net; +using System.Threading.Tasks; + +namespace MediaBrowser.Common.Kernel +{ + /// + ///This is an interface for listening to messages coming through a web socket connection + /// + public interface IWebSocketListener + { + /// + /// Processes the message. + /// + /// The message. + /// Task. + Task ProcessMessage(WebSocketMessageInfo message); + } +} diff --git a/MediaBrowser.Common/MediaBrowser.Common.csproj b/MediaBrowser.Common/MediaBrowser.Common.csproj index d30dd703b..0f0c58f17 100644 --- a/MediaBrowser.Common/MediaBrowser.Common.csproj +++ b/MediaBrowser.Common/MediaBrowser.Common.csproj @@ -137,7 +137,7 @@ - + diff --git a/MediaBrowser.Controller/ScheduledTasks/PeopleValidationTask.cs b/MediaBrowser.Controller/ScheduledTasks/PeopleValidationTask.cs index 0fdb91898..2a91f0716 100644 --- a/MediaBrowser.Controller/ScheduledTasks/PeopleValidationTask.cs +++ b/MediaBrowser.Controller/ScheduledTasks/PeopleValidationTask.cs @@ -1,5 +1,4 @@ using MediaBrowser.Common.ScheduledTasks; -using MediaBrowser.Model.Tasks; using System; using System.Collections.Generic; using System.ComponentModel.Composition; diff --git a/MediaBrowser.WebDashboard/Api/DashboardInfoWebSocketListener.cs b/MediaBrowser.WebDashboard/Api/DashboardInfoWebSocketListener.cs index 2dcb5e57e..4b9c0bd10 100644 --- a/MediaBrowser.WebDashboard/Api/DashboardInfoWebSocketListener.cs +++ b/MediaBrowser.WebDashboard/Api/DashboardInfoWebSocketListener.cs @@ -10,7 +10,7 @@ namespace MediaBrowser.WebDashboard.Api /// Class DashboardInfoWebSocketListener /// [Export(typeof(IWebSocketListener))] - class DashboardInfoWebSocketListener : BasePeriodicWebSocketListener + class DashboardInfoWebSocketListener : BasePeriodicWebSocketListener { /// /// Gets the name. @@ -21,15 +21,21 @@ namespace MediaBrowser.WebDashboard.Api get { return "DashboardInfo"; } } + /// + /// The _kernel + /// + private readonly Kernel _kernel; + /// /// Initializes a new instance of the class. /// + /// The kernel. /// The logger. [ImportingConstructor] - public DashboardInfoWebSocketListener([Import("logger")] ILogger logger) + public DashboardInfoWebSocketListener([Import("kernel")] Kernel kernel, [Import("logger")] ILogger logger) : base(logger) { - + _kernel = kernel; } /// @@ -39,7 +45,7 @@ namespace MediaBrowser.WebDashboard.Api /// Task{IEnumerable{TaskInfo}}. protected override Task GetDataToSend(object state) { - return Task.FromResult(DashboardService.GetDashboardInfo((Kernel)Kernel, Logger)); + return Task.FromResult(DashboardService.GetDashboardInfo(_kernel, Logger)); } } }