created IWebSocketListener

This commit is contained in:
LukePulverenti 2013-02-22 10:16:48 -05:00
parent fab627c845
commit 8b3e8ad478
10 changed files with 83 additions and 142 deletions

View File

@ -1,5 +1,6 @@
using MediaBrowser.Common.Kernel; using MediaBrowser.Common.Kernel;
using MediaBrowser.Common.ScheduledTasks; using MediaBrowser.Common.ScheduledTasks;
using MediaBrowser.Controller;
using MediaBrowser.Model.Logging; using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Tasks; using MediaBrowser.Model.Tasks;
using System.Collections.Generic; using System.Collections.Generic;
@ -13,7 +14,7 @@ namespace MediaBrowser.Api.ScheduledTasks
/// Class ScheduledTasksWebSocketListener /// Class ScheduledTasksWebSocketListener
/// </summary> /// </summary>
[Export(typeof(IWebSocketListener))] [Export(typeof(IWebSocketListener))]
public class ScheduledTasksWebSocketListener : BasePeriodicWebSocketListener<IKernel, IEnumerable<TaskInfo>, object> public class ScheduledTasksWebSocketListener : BasePeriodicWebSocketListener<IEnumerable<TaskInfo>, object>
{ {
/// <summary> /// <summary>
/// Gets the name. /// Gets the name.
@ -24,15 +25,21 @@ namespace MediaBrowser.Api.ScheduledTasks
get { return "ScheduledTasksInfo"; } get { return "ScheduledTasksInfo"; }
} }
/// <summary>
/// The _kernel
/// </summary>
private readonly IKernel _kernel;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ScheduledTasksWebSocketListener" /> class. /// Initializes a new instance of the <see cref="ScheduledTasksWebSocketListener" /> class.
/// </summary> /// </summary>
/// <param name="kernel">The kernel.</param>
/// <param name="logger">The logger.</param> /// <param name="logger">The logger.</param>
[ImportingConstructor] [ImportingConstructor]
public ScheduledTasksWebSocketListener([Import("logger")] ILogger logger) public ScheduledTasksWebSocketListener([Import("kernel")] Kernel kernel, [Import("logger")] ILogger logger)
: base(logger) : base(logger)
{ {
_kernel = kernel;
} }
/// <summary> /// <summary>
@ -42,7 +49,7 @@ namespace MediaBrowser.Api.ScheduledTasks
/// <returns>Task{IEnumerable{TaskInfo}}.</returns> /// <returns>Task{IEnumerable{TaskInfo}}.</returns>
protected override Task<IEnumerable<TaskInfo>> GetDataToSend(object state) protected override Task<IEnumerable<TaskInfo>> GetDataToSend(object state)
{ {
return Task.FromResult(Kernel.ScheduledTasks.OrderBy(i => i.Name) return Task.FromResult(_kernel.ScheduledTasks.OrderBy(i => i.Name)
.Select(ScheduledTaskHelpers.GetTaskInfo)); .Select(ScheduledTaskHelpers.GetTaskInfo));
} }
} }

View File

@ -1,12 +1,13 @@
using MediaBrowser.Common.IO; using MediaBrowser.Common.IO;
using MediaBrowser.Common.Kernel; using MediaBrowser.Common.Kernel;
using MediaBrowser.Controller;
using MediaBrowser.Model.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Model.Logging;
namespace MediaBrowser.Api.WebSocket namespace MediaBrowser.Api.WebSocket
{ {
@ -14,7 +15,7 @@ namespace MediaBrowser.Api.WebSocket
/// Class ScheduledTasksWebSocketListener /// Class ScheduledTasksWebSocketListener
/// </summary> /// </summary>
[Export(typeof(IWebSocketListener))] [Export(typeof(IWebSocketListener))]
public class LogFileWebSocketListener : BasePeriodicWebSocketListener<IKernel, IEnumerable<string>, LogFileWebSocketState> public class LogFileWebSocketListener : BasePeriodicWebSocketListener<IEnumerable<string>, LogFileWebSocketState>
{ {
/// <summary> /// <summary>
/// Gets the name. /// Gets the name.
@ -25,26 +26,22 @@ namespace MediaBrowser.Api.WebSocket
get { return "LogFile"; } get { return "LogFile"; }
} }
/// <summary>
/// The _kernel
/// </summary>
private readonly IKernel _kernel;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="LogFileWebSocketListener" /> class. /// Initializes a new instance of the <see cref="LogFileWebSocketListener" /> class.
/// </summary> /// </summary>
/// <param name="logger">The logger.</param> /// <param name="logger">The logger.</param>
/// <param name="kernel">The kernel.</param>
[ImportingConstructor] [ImportingConstructor]
public LogFileWebSocketListener([Import("logger")] ILogger logger) public LogFileWebSocketListener([Import("logger")] ILogger logger, [Import("kernel")] Kernel kernel)
: base(logger) : base(logger)
{ {
_kernel = kernel;
} _kernel.LoggerLoaded += kernel_LoggerLoaded;
/// <summary>
/// Initializes the specified kernel.
/// </summary>
/// <param name="kernel">The kernel.</param>
public override void Initialize(IKernel kernel)
{
base.Initialize(kernel);
kernel.LoggerLoaded += kernel_LoggerLoaded;
} }
/// <summary> /// <summary>
@ -54,9 +51,9 @@ namespace MediaBrowser.Api.WebSocket
/// <returns>IEnumerable{System.String}.</returns> /// <returns>IEnumerable{System.String}.</returns>
protected override async Task<IEnumerable<string>> GetDataToSend(LogFileWebSocketState state) protected override async Task<IEnumerable<string>> 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; state.StartLine = 0;
} }
@ -75,7 +72,7 @@ namespace MediaBrowser.Api.WebSocket
{ {
if (dispose) if (dispose)
{ {
Kernel.LoggerLoaded -= kernel_LoggerLoaded; _kernel.LoggerLoaded -= kernel_LoggerLoaded;
} }
base.Dispose(dispose); base.Dispose(dispose);
} }

View File

@ -1,4 +1,5 @@
using MediaBrowser.Common.Kernel; using MediaBrowser.Common.Kernel;
using MediaBrowser.Controller;
using MediaBrowser.Model.Logging; using MediaBrowser.Model.Logging;
using MediaBrowser.Model.System; using MediaBrowser.Model.System;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
@ -10,7 +11,7 @@ namespace MediaBrowser.Api.WebSocket
/// Class SystemInfoWebSocketListener /// Class SystemInfoWebSocketListener
/// </summary> /// </summary>
[Export(typeof(IWebSocketListener))] [Export(typeof(IWebSocketListener))]
public class SystemInfoWebSocketListener : BasePeriodicWebSocketListener<IKernel, SystemInfo, object> public class SystemInfoWebSocketListener : BasePeriodicWebSocketListener<SystemInfo, object>
{ {
/// <summary> /// <summary>
/// Gets the name. /// Gets the name.
@ -21,15 +22,21 @@ namespace MediaBrowser.Api.WebSocket
get { return "SystemInfo"; } get { return "SystemInfo"; }
} }
/// <summary>
/// The _kernel
/// </summary>
private readonly IKernel _kernel;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="SystemInfoWebSocketListener" /> class. /// Initializes a new instance of the <see cref="SystemInfoWebSocketListener" /> class.
/// </summary> /// </summary>
/// <param name="kernel">The kernel.</param>
/// <param name="logger">The logger.</param> /// <param name="logger">The logger.</param>
[ImportingConstructor] [ImportingConstructor]
public SystemInfoWebSocketListener([Import("logger")] ILogger logger) public SystemInfoWebSocketListener([Import("kernel")] Kernel kernel, [Import("logger")] ILogger logger)
: base(logger) : base(logger)
{ {
_kernel = kernel;
} }
/// <summary> /// <summary>
@ -39,7 +46,7 @@ namespace MediaBrowser.Api.WebSocket
/// <returns>Task{SystemInfo}.</returns> /// <returns>Task{SystemInfo}.</returns>
protected override Task<SystemInfo> GetDataToSend(object state) protected override Task<SystemInfo> GetDataToSend(object state)
{ {
return Task.FromResult(Kernel.GetSystemInfo()); return Task.FromResult(_kernel.GetSystemInfo());
} }
} }
} }

View File

@ -553,11 +553,6 @@ namespace MediaBrowser.Common.Kernel
{ {
return Task.Run(() => return Task.Run(() =>
{ {
foreach (var listener in WebSocketListeners)
{
listener.Initialize(this);
}
foreach (var task in ScheduledTasks) foreach (var task in ScheduledTasks)
{ {
task.Initialize(this, Logger); task.Initialize(this, Logger);

View File

@ -12,11 +12,9 @@ namespace MediaBrowser.Common.Kernel
/// <summary> /// <summary>
/// Starts sending data over a web socket periodically when a message is received, and then stops when a corresponding stop message is received /// Starts sending data over a web socket periodically when a message is received, and then stops when a corresponding stop message is received
/// </summary> /// </summary>
/// <typeparam name="TKernelType">The type of the T kernel type.</typeparam>
/// <typeparam name="TReturnDataType">The type of the T return data type.</typeparam> /// <typeparam name="TReturnDataType">The type of the T return data type.</typeparam>
/// <typeparam name="TStateType">The type of the T state type.</typeparam> /// <typeparam name="TStateType">The type of the T state type.</typeparam>
public abstract class BasePeriodicWebSocketListener<TKernelType, TReturnDataType, TStateType> : BaseWebSocketListener<TKernelType> public abstract class BasePeriodicWebSocketListener<TReturnDataType, TStateType> : IWebSocketListener, IDisposable
where TKernelType : IKernel
where TStateType : class, new() where TStateType : class, new()
{ {
/// <summary> /// <summary>
@ -47,6 +45,7 @@ namespace MediaBrowser.Common.Kernel
/// Initializes a new instance of the <see cref="BasePeriodicWebSocketListener{TStateType}" /> class. /// Initializes a new instance of the <see cref="BasePeriodicWebSocketListener{TStateType}" /> class.
/// </summary> /// </summary>
/// <param name="logger">The logger.</param> /// <param name="logger">The logger.</param>
/// <exception cref="System.ArgumentNullException">logger</exception>
protected BasePeriodicWebSocketListener(ILogger logger) protected BasePeriodicWebSocketListener(ILogger logger)
{ {
if (logger == null) if (logger == null)
@ -58,11 +57,16 @@ namespace MediaBrowser.Common.Kernel
} }
/// <summary> /// <summary>
/// Processes the message internal. /// The null task result
/// </summary>
protected Task NullTaskResult = Task.FromResult(true);
/// <summary>
/// Processes the message.
/// </summary> /// </summary>
/// <param name="message">The message.</param> /// <param name="message">The message.</param>
/// <returns>Task.</returns> /// <returns>Task.</returns>
protected override Task ProcessMessageInternal(WebSocketMessageInfo message) public Task ProcessMessage(WebSocketMessageInfo message)
{ {
if (message.MessageType.Equals(Name + "Start", StringComparison.OrdinalIgnoreCase)) if (message.MessageType.Equals(Name + "Start", StringComparison.OrdinalIgnoreCase))
{ {
@ -223,7 +227,7 @@ namespace MediaBrowser.Common.Kernel
/// Releases unmanaged and - optionally - managed resources. /// Releases unmanaged and - optionally - managed resources.
/// </summary> /// </summary>
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected override void Dispose(bool dispose) protected virtual void Dispose(bool dispose)
{ {
if (dispose) if (dispose)
{ {
@ -235,8 +239,14 @@ namespace MediaBrowser.Common.Kernel
} }
} }
} }
}
base.Dispose(dispose); /// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
} }
} }
} }

View File

@ -1,98 +0,0 @@
using MediaBrowser.Common.Net;
using System;
using System.Threading.Tasks;
namespace MediaBrowser.Common.Kernel
{
/// <summary>
/// Represents a class that is notified everytime the server receives a message over a WebSocket
/// </summary>
/// <typeparam name="TKernelType">The type of the T kernel type.</typeparam>
public abstract class BaseWebSocketListener<TKernelType> : IWebSocketListener
where TKernelType : IKernel
{
/// <summary>
/// The null task result
/// </summary>
protected Task NullTaskResult = Task.FromResult(true);
/// <summary>
/// Gets the kernel.
/// </summary>
/// <value>The kernel.</value>
protected TKernelType Kernel { get; private set; }
/// <summary>
/// Initializes the specified kernel.
/// </summary>
/// <param name="kernel">The kernel.</param>
public virtual void Initialize(IKernel kernel)
{
if (kernel == null)
{
throw new ArgumentNullException("kernel");
}
Kernel = (TKernelType)kernel;
}
/// <summary>
/// Processes the message.
/// </summary>
/// <param name="message">The message.</param>
/// <returns>Task.</returns>
/// <exception cref="System.ArgumentNullException">message</exception>
public Task ProcessMessage(WebSocketMessageInfo message)
{
if (message == null)
{
throw new ArgumentNullException("message");
}
return ProcessMessageInternal(message);
}
/// <summary>
/// Processes the message internal.
/// </summary>
/// <param name="message">The message.</param>
/// <returns>Task.</returns>
protected abstract Task ProcessMessageInternal(WebSocketMessageInfo message);
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
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)
{
}
}
/// <summary>
/// Interface IWebSocketListener
/// </summary>
public interface IWebSocketListener : IDisposable
{
/// <summary>
/// Processes the message.
/// </summary>
/// <param name="message">The message.</param>
/// <returns>Task.</returns>
Task ProcessMessage(WebSocketMessageInfo message);
/// <summary>
/// Initializes the specified kernel.
/// </summary>
/// <param name="kernel">The kernel.</param>
void Initialize(IKernel kernel);
}
}

View File

@ -0,0 +1,18 @@
using MediaBrowser.Common.Net;
using System.Threading.Tasks;
namespace MediaBrowser.Common.Kernel
{
/// <summary>
///This is an interface for listening to messages coming through a web socket connection
/// </summary>
public interface IWebSocketListener
{
/// <summary>
/// Processes the message.
/// </summary>
/// <param name="message">The message.</param>
/// <returns>Task.</returns>
Task ProcessMessage(WebSocketMessageInfo message);
}
}

View File

@ -137,7 +137,7 @@
<Compile Include="Kernel\BaseApplicationPaths.cs" /> <Compile Include="Kernel\BaseApplicationPaths.cs" />
<Compile Include="Kernel\BaseManager.cs" /> <Compile Include="Kernel\BaseManager.cs" />
<Compile Include="Kernel\BasePeriodicWebSocketListener.cs" /> <Compile Include="Kernel\BasePeriodicWebSocketListener.cs" />
<Compile Include="Kernel\BaseWebSocketListener.cs" /> <Compile Include="Kernel\IWebSocketListener.cs" />
<Compile Include="Kernel\IApplicationHost.cs" /> <Compile Include="Kernel\IApplicationHost.cs" />
<Compile Include="Kernel\IKernel.cs" /> <Compile Include="Kernel\IKernel.cs" />
<Compile Include="Kernel\TcpManager.cs" /> <Compile Include="Kernel\TcpManager.cs" />

View File

@ -1,5 +1,4 @@
using MediaBrowser.Common.ScheduledTasks; using MediaBrowser.Common.ScheduledTasks;
using MediaBrowser.Model.Tasks;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;

View File

@ -10,7 +10,7 @@ namespace MediaBrowser.WebDashboard.Api
/// Class DashboardInfoWebSocketListener /// Class DashboardInfoWebSocketListener
/// </summary> /// </summary>
[Export(typeof(IWebSocketListener))] [Export(typeof(IWebSocketListener))]
class DashboardInfoWebSocketListener : BasePeriodicWebSocketListener<IKernel, DashboardInfo, object> class DashboardInfoWebSocketListener : BasePeriodicWebSocketListener<DashboardInfo, object>
{ {
/// <summary> /// <summary>
/// Gets the name. /// Gets the name.
@ -21,15 +21,21 @@ namespace MediaBrowser.WebDashboard.Api
get { return "DashboardInfo"; } get { return "DashboardInfo"; }
} }
/// <summary>
/// The _kernel
/// </summary>
private readonly Kernel _kernel;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="DashboardInfoWebSocketListener" /> class. /// Initializes a new instance of the <see cref="DashboardInfoWebSocketListener" /> class.
/// </summary> /// </summary>
/// <param name="kernel">The kernel.</param>
/// <param name="logger">The logger.</param> /// <param name="logger">The logger.</param>
[ImportingConstructor] [ImportingConstructor]
public DashboardInfoWebSocketListener([Import("logger")] ILogger logger) public DashboardInfoWebSocketListener([Import("kernel")] Kernel kernel, [Import("logger")] ILogger logger)
: base(logger) : base(logger)
{ {
_kernel = kernel;
} }
/// <summary> /// <summary>
@ -39,7 +45,7 @@ namespace MediaBrowser.WebDashboard.Api
/// <returns>Task{IEnumerable{TaskInfo}}.</returns> /// <returns>Task{IEnumerable{TaskInfo}}.</returns>
protected override Task<DashboardInfo> GetDataToSend(object state) protected override Task<DashboardInfo> GetDataToSend(object state)
{ {
return Task.FromResult(DashboardService.GetDashboardInfo((Kernel)Kernel, Logger)); return Task.FromResult(DashboardService.GetDashboardInfo(_kernel, Logger));
} }
} }
} }