created IWebSocketListener
This commit is contained in:
parent
fab627c845
commit
8b3e8ad478
|
@ -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
|
||||
/// </summary>
|
||||
[Export(typeof(IWebSocketListener))]
|
||||
public class ScheduledTasksWebSocketListener : BasePeriodicWebSocketListener<IKernel, IEnumerable<TaskInfo>, object>
|
||||
public class ScheduledTasksWebSocketListener : BasePeriodicWebSocketListener<IEnumerable<TaskInfo>, object>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the name.
|
||||
|
@ -24,15 +25,21 @@ namespace MediaBrowser.Api.ScheduledTasks
|
|||
get { return "ScheduledTasksInfo"; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The _kernel
|
||||
/// </summary>
|
||||
private readonly IKernel _kernel;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ScheduledTasksWebSocketListener" /> class.
|
||||
/// </summary>
|
||||
/// <param name="kernel">The kernel.</param>
|
||||
/// <param name="logger">The logger.</param>
|
||||
[ImportingConstructor]
|
||||
public ScheduledTasksWebSocketListener([Import("logger")] ILogger logger)
|
||||
public ScheduledTasksWebSocketListener([Import("kernel")] Kernel kernel, [Import("logger")] ILogger logger)
|
||||
: base(logger)
|
||||
{
|
||||
|
||||
_kernel = kernel;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -42,7 +49,7 @@ namespace MediaBrowser.Api.ScheduledTasks
|
|||
/// <returns>Task{IEnumerable{TaskInfo}}.</returns>
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
/// </summary>
|
||||
[Export(typeof(IWebSocketListener))]
|
||||
public class LogFileWebSocketListener : BasePeriodicWebSocketListener<IKernel, IEnumerable<string>, LogFileWebSocketState>
|
||||
public class LogFileWebSocketListener : BasePeriodicWebSocketListener<IEnumerable<string>, LogFileWebSocketState>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the name.
|
||||
|
@ -25,26 +26,22 @@ namespace MediaBrowser.Api.WebSocket
|
|||
get { return "LogFile"; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The _kernel
|
||||
/// </summary>
|
||||
private readonly IKernel _kernel;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LogFileWebSocketListener" /> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">The logger.</param>
|
||||
/// <param name="kernel">The kernel.</param>
|
||||
[ImportingConstructor]
|
||||
public LogFileWebSocketListener([Import("logger")] ILogger logger)
|
||||
public LogFileWebSocketListener([Import("logger")] ILogger logger, [Import("kernel")] Kernel kernel)
|
||||
: base(logger)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <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;
|
||||
_kernel = kernel;
|
||||
_kernel.LoggerLoaded += kernel_LoggerLoaded;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -54,9 +51,9 @@ namespace MediaBrowser.Api.WebSocket
|
|||
/// <returns>IEnumerable{System.String}.</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -75,7 +72,7 @@ namespace MediaBrowser.Api.WebSocket
|
|||
{
|
||||
if (dispose)
|
||||
{
|
||||
Kernel.LoggerLoaded -= kernel_LoggerLoaded;
|
||||
_kernel.LoggerLoaded -= kernel_LoggerLoaded;
|
||||
}
|
||||
base.Dispose(dispose);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
/// </summary>
|
||||
[Export(typeof(IWebSocketListener))]
|
||||
public class SystemInfoWebSocketListener : BasePeriodicWebSocketListener<IKernel, SystemInfo, object>
|
||||
public class SystemInfoWebSocketListener : BasePeriodicWebSocketListener<SystemInfo, object>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the name.
|
||||
|
@ -21,15 +22,21 @@ namespace MediaBrowser.Api.WebSocket
|
|||
get { return "SystemInfo"; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The _kernel
|
||||
/// </summary>
|
||||
private readonly IKernel _kernel;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SystemInfoWebSocketListener" /> class.
|
||||
/// </summary>
|
||||
/// <param name="kernel">The kernel.</param>
|
||||
/// <param name="logger">The logger.</param>
|
||||
[ImportingConstructor]
|
||||
public SystemInfoWebSocketListener([Import("logger")] ILogger logger)
|
||||
public SystemInfoWebSocketListener([Import("kernel")] Kernel kernel, [Import("logger")] ILogger logger)
|
||||
: base(logger)
|
||||
{
|
||||
|
||||
_kernel = kernel;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -39,7 +46,7 @@ namespace MediaBrowser.Api.WebSocket
|
|||
/// <returns>Task{SystemInfo}.</returns>
|
||||
protected override Task<SystemInfo> GetDataToSend(object state)
|
||||
{
|
||||
return Task.FromResult(Kernel.GetSystemInfo());
|
||||
return Task.FromResult(_kernel.GetSystemInfo());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -12,11 +12,9 @@ namespace MediaBrowser.Common.Kernel
|
|||
/// <summary>
|
||||
/// Starts sending data over a web socket periodically when a message is received, and then stops when a corresponding stop message is received
|
||||
/// </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="TStateType">The type of the T state type.</typeparam>
|
||||
public abstract class BasePeriodicWebSocketListener<TKernelType, TReturnDataType, TStateType> : BaseWebSocketListener<TKernelType>
|
||||
where TKernelType : IKernel
|
||||
public abstract class BasePeriodicWebSocketListener<TReturnDataType, TStateType> : IWebSocketListener, IDisposable
|
||||
where TStateType : class, new()
|
||||
{
|
||||
/// <summary>
|
||||
|
@ -47,6 +45,7 @@ namespace MediaBrowser.Common.Kernel
|
|||
/// Initializes a new instance of the <see cref="BasePeriodicWebSocketListener{TStateType}" /> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">The logger.</param>
|
||||
/// <exception cref="System.ArgumentNullException">logger</exception>
|
||||
protected BasePeriodicWebSocketListener(ILogger logger)
|
||||
{
|
||||
if (logger == null)
|
||||
|
@ -58,11 +57,16 @@ namespace MediaBrowser.Common.Kernel
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// Processes the message internal.
|
||||
/// The null task result
|
||||
/// </summary>
|
||||
protected Task NullTaskResult = Task.FromResult(true);
|
||||
|
||||
/// <summary>
|
||||
/// Processes the message.
|
||||
/// </summary>
|
||||
/// <param name="message">The message.</param>
|
||||
/// <returns>Task.</returns>
|
||||
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.
|
||||
/// </summary>
|
||||
/// <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)
|
||||
{
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
18
MediaBrowser.Common/Kernel/IWebSocketListener.cs
Normal file
18
MediaBrowser.Common/Kernel/IWebSocketListener.cs
Normal 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);
|
||||
}
|
||||
}
|
|
@ -137,7 +137,7 @@
|
|||
<Compile Include="Kernel\BaseApplicationPaths.cs" />
|
||||
<Compile Include="Kernel\BaseManager.cs" />
|
||||
<Compile Include="Kernel\BasePeriodicWebSocketListener.cs" />
|
||||
<Compile Include="Kernel\BaseWebSocketListener.cs" />
|
||||
<Compile Include="Kernel\IWebSocketListener.cs" />
|
||||
<Compile Include="Kernel\IApplicationHost.cs" />
|
||||
<Compile Include="Kernel\IKernel.cs" />
|
||||
<Compile Include="Kernel\TcpManager.cs" />
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
using MediaBrowser.Common.ScheduledTasks;
|
||||
using MediaBrowser.Model.Tasks;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.Composition;
|
||||
|
|
|
@ -10,7 +10,7 @@ namespace MediaBrowser.WebDashboard.Api
|
|||
/// Class DashboardInfoWebSocketListener
|
||||
/// </summary>
|
||||
[Export(typeof(IWebSocketListener))]
|
||||
class DashboardInfoWebSocketListener : BasePeriodicWebSocketListener<IKernel, DashboardInfo, object>
|
||||
class DashboardInfoWebSocketListener : BasePeriodicWebSocketListener<DashboardInfo, object>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the name.
|
||||
|
@ -21,15 +21,21 @@ namespace MediaBrowser.WebDashboard.Api
|
|||
get { return "DashboardInfo"; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The _kernel
|
||||
/// </summary>
|
||||
private readonly Kernel _kernel;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="DashboardInfoWebSocketListener" /> class.
|
||||
/// </summary>
|
||||
/// <param name="kernel">The kernel.</param>
|
||||
/// <param name="logger">The logger.</param>
|
||||
[ImportingConstructor]
|
||||
public DashboardInfoWebSocketListener([Import("logger")] ILogger logger)
|
||||
public DashboardInfoWebSocketListener([Import("kernel")] Kernel kernel, [Import("logger")] ILogger logger)
|
||||
: base(logger)
|
||||
{
|
||||
|
||||
_kernel = kernel;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -39,7 +45,7 @@ namespace MediaBrowser.WebDashboard.Api
|
|||
/// <returns>Task{IEnumerable{TaskInfo}}.</returns>
|
||||
protected override Task<DashboardInfo> GetDataToSend(object state)
|
||||
{
|
||||
return Task.FromResult(DashboardService.GetDashboardInfo((Kernel)Kernel, Logger));
|
||||
return Task.FromResult(DashboardService.GetDashboardInfo(_kernel, Logger));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user