diff options
Diffstat (limited to 'MediaBrowser.Common/Kernel')
| -rw-r--r-- | MediaBrowser.Common/Kernel/BaseKernel.cs | 167 | ||||
| -rw-r--r-- | MediaBrowser.Common/Kernel/BasePeriodicWebSocketListener.cs | 251 | ||||
| -rw-r--r-- | MediaBrowser.Common/Kernel/IKernel.cs | 74 | ||||
| -rw-r--r-- | MediaBrowser.Common/Kernel/IServerManager.cs | 61 | ||||
| -rw-r--r-- | MediaBrowser.Common/Kernel/IWebSocketListener.cs | 18 | ||||
| -rw-r--r-- | MediaBrowser.Common/Kernel/KernelContext.cs | 18 |
6 files changed, 0 insertions, 589 deletions
diff --git a/MediaBrowser.Common/Kernel/BaseKernel.cs b/MediaBrowser.Common/Kernel/BaseKernel.cs deleted file mode 100644 index cf8133e97..000000000 --- a/MediaBrowser.Common/Kernel/BaseKernel.cs +++ /dev/null @@ -1,167 +0,0 @@ -using MediaBrowser.Common.Configuration; -using MediaBrowser.Common.Events; -using MediaBrowser.Model.Logging; -using MediaBrowser.Model.System; -using System; - -namespace MediaBrowser.Common.Kernel -{ - /// <summary> - /// Represents a shared base kernel for both the Ui and server apps - /// </summary> - public abstract class BaseKernel : IKernel - { - /// <summary> - /// Occurs when [has pending restart changed]. - /// </summary> - public event EventHandler HasPendingRestartChanged; - - #region ApplicationUpdated Event - /// <summary> - /// Occurs when [application updated]. - /// </summary> - public event EventHandler<GenericEventArgs<Version>> ApplicationUpdated; - /// <summary> - /// Called when [application updated]. - /// </summary> - /// <param name="newVersion">The new version.</param> - public void OnApplicationUpdated(Version newVersion) - { - EventHelper.QueueEventIfNotNull(ApplicationUpdated, this, new GenericEventArgs<Version> { Argument = newVersion }, Logger); - - NotifyPendingRestart(); - } - #endregion - - /// <summary> - /// Gets or sets a value indicating whether this instance has changes that require the entire application to restart. - /// </summary> - /// <value><c>true</c> if this instance has pending application restart; otherwise, <c>false</c>.</value> - public bool HasPendingRestart { get; private set; } - - /// <summary> - /// Gets the UDP server port number. - /// This can't be configurable because then the user would have to configure their client to discover the server. - /// </summary> - /// <value>The UDP server port number.</value> - public abstract int UdpServerPortNumber { get; } - - /// <summary> - /// Gets the name of the web application that can be used for url building. - /// All api urls will be of the form {protocol}://{host}:{port}/{appname}/... - /// </summary> - /// <value>The name of the web application.</value> - public string WebApplicationName - { - get { return "mediabrowser"; } - } - - /// <summary> - /// Gets the HTTP server URL prefix. - /// </summary> - /// <value>The HTTP server URL prefix.</value> - public virtual string HttpServerUrlPrefix - { - get - { - return "http://+:" + _configurationManager.CommonConfiguration.HttpServerPortNumber + "/" + WebApplicationName + "/"; - } - } - - /// <summary> - /// Gets the kernel context. Subclasses will have to override. - /// </summary> - /// <value>The kernel context.</value> - public abstract KernelContext KernelContext { get; } - - /// <summary> - /// Gets the logger. - /// </summary> - /// <value>The logger.</value> - protected ILogger Logger { get; private set; } - - /// <summary> - /// Gets or sets the application host. - /// </summary> - /// <value>The application host.</value> - protected IApplicationHost ApplicationHost { get; private set; } - - private readonly IConfigurationManager _configurationManager; - - /// <summary> - /// Initializes a new instance of the <see cref="BaseKernel" /> class. - /// </summary> - /// <param name="appHost">The app host.</param> - /// <param name="logManager">The log manager.</param> - protected BaseKernel(IApplicationHost appHost, ILogManager logManager, IConfigurationManager configurationManager) - { - ApplicationHost = appHost; - _configurationManager = configurationManager; - Logger = logManager.GetLogger("Kernel"); - } - - /// <summary> - /// Initializes the Kernel - /// </summary> - /// <returns>Task.</returns> - public void Init() - { - ReloadInternal(); - - Logger.Info("Kernel.Init Complete"); - } - - /// <summary> - /// Performs initializations that can be reloaded at anytime - /// </summary> - /// <returns>Task.</returns> - protected virtual void ReloadInternal() - { - } - - /// <summary> - /// Notifies that the kernel that a change has been made that requires a restart - /// </summary> - public void NotifyPendingRestart() - { - HasPendingRestart = true; - - EventHelper.QueueEventIfNotNull(HasPendingRestartChanged, this, EventArgs.Empty, Logger); - } - - /// <summary> - /// Performs the pending restart. - /// </summary> - /// <returns>Task.</returns> - public void PerformPendingRestart() - { - if (HasPendingRestart) - { - Logger.Info("Restarting the application"); - - ApplicationHost.Restart(); - } - else - { - Logger.Info("PerformPendingRestart - not needed"); - } - } - - /// <summary> - /// Gets the system status. - /// </summary> - /// <returns>SystemInfo.</returns> - public virtual SystemInfo GetSystemInfo() - { - return new SystemInfo - { - HasPendingRestart = HasPendingRestart, - Version = ApplicationHost.ApplicationVersion.ToString(), - IsNetworkDeployed = ApplicationHost.CanSelfUpdate, - WebSocketPortNumber = ApplicationHost.Resolve<IServerManager>().WebSocketPortNumber, - SupportsNativeWebSocket = ApplicationHost.Resolve<IServerManager>().SupportsNativeWebSocket, - FailedPluginAssemblies = ApplicationHost.FailedAssemblies.ToArray() - }; - } - } -} diff --git a/MediaBrowser.Common/Kernel/BasePeriodicWebSocketListener.cs b/MediaBrowser.Common/Kernel/BasePeriodicWebSocketListener.cs deleted file mode 100644 index 6a44cf372..000000000 --- a/MediaBrowser.Common/Kernel/BasePeriodicWebSocketListener.cs +++ /dev/null @@ -1,251 +0,0 @@ -using MediaBrowser.Common.Net; -using MediaBrowser.Model.Logging; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; - -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="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<TReturnDataType, TStateType> : IWebSocketListener, IDisposable - where TStateType : class, new() - { - /// <summary> - /// The _active connections - /// </summary> - protected readonly List<Tuple<IWebSocketConnection, CancellationTokenSource, Timer, TStateType, SemaphoreSlim>> ActiveConnections = - new List<Tuple<IWebSocketConnection, CancellationTokenSource, Timer, TStateType, SemaphoreSlim>>(); - - /// <summary> - /// Gets the name. - /// </summary> - /// <value>The name.</value> - protected abstract string Name { get; } - - /// <summary> - /// Gets the data to send. - /// </summary> - /// <param name="state">The state.</param> - /// <returns>Task{`1}.</returns> - protected abstract Task<TReturnDataType> GetDataToSend(TStateType state); - - /// <summary> - /// The logger - /// </summary> - protected ILogger Logger; - - /// <summary> - /// 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) - { - throw new ArgumentNullException("logger"); - } - - Logger = logger; - } - - /// <summary> - /// 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> - public Task ProcessMessage(WebSocketMessageInfo message) - { - if (message.MessageType.Equals(Name + "Start", StringComparison.OrdinalIgnoreCase)) - { - Start(message); - } - - if (message.MessageType.Equals(Name + "Stop", StringComparison.OrdinalIgnoreCase)) - { - Stop(message); - } - - return NullTaskResult; - } - - /// <summary> - /// Starts sending messages over a web socket - /// </summary> - /// <param name="message">The message.</param> - private void Start(WebSocketMessageInfo message) - { - var vals = message.Data.Split(','); - - var dueTimeMs = long.Parse(vals[0]); - var periodMs = long.Parse(vals[1]); - - var cancellationTokenSource = new CancellationTokenSource(); - - Logger.Info("{1} Begin transmitting over websocket to {0}", message.Connection.RemoteEndPoint, GetType().Name); - - var timer = new Timer(TimerCallback, message.Connection, Timeout.Infinite, Timeout.Infinite); - - var state = new TStateType(); - - var semaphore = new SemaphoreSlim(1, 1); - - lock (ActiveConnections) - { - ActiveConnections.Add(new Tuple<IWebSocketConnection, CancellationTokenSource, Timer, TStateType, SemaphoreSlim>(message.Connection, cancellationTokenSource, timer, state, semaphore)); - } - - timer.Change(TimeSpan.FromMilliseconds(dueTimeMs), TimeSpan.FromMilliseconds(periodMs)); - } - - /// <summary> - /// Timers the callback. - /// </summary> - /// <param name="state">The state.</param> - private async void TimerCallback(object state) - { - var connection = (IWebSocketConnection)state; - - Tuple<IWebSocketConnection, CancellationTokenSource, Timer, TStateType, SemaphoreSlim> tuple; - - lock (ActiveConnections) - { - tuple = ActiveConnections.FirstOrDefault(c => c.Item1 == connection); - } - - if (tuple == null) - { - return; - } - - if (connection.State != WebSocketState.Open || tuple.Item2.IsCancellationRequested) - { - DisposeConnection(tuple); - return; - } - - try - { - await tuple.Item5.WaitAsync(tuple.Item2.Token).ConfigureAwait(false); - - var data = await GetDataToSend(tuple.Item4).ConfigureAwait(false); - - await connection.SendAsync(new WebSocketMessage<TReturnDataType> - { - MessageType = Name, - Data = data - - }, tuple.Item2.Token).ConfigureAwait(false); - } - catch (OperationCanceledException) - { - if (tuple.Item2.IsCancellationRequested) - { - DisposeConnection(tuple); - } - } - catch (Exception ex) - { - Logger.ErrorException("Error sending web socket message {0}", ex, Name); - DisposeConnection(tuple); - } - finally - { - tuple.Item5.Release(); - } - } - - /// <summary> - /// Stops sending messages over a web socket - /// </summary> - /// <param name="message">The message.</param> - private void Stop(WebSocketMessageInfo message) - { - lock (ActiveConnections) - { - var connection = ActiveConnections.FirstOrDefault(c => c.Item1 == message.Connection); - - if (connection != null) - { - DisposeConnection(connection); - } - } - } - - /// <summary> - /// Disposes the connection. - /// </summary> - /// <param name="connection">The connection.</param> - private void DisposeConnection(Tuple<IWebSocketConnection, CancellationTokenSource, Timer, TStateType, SemaphoreSlim> connection) - { - Logger.Info("{1} stop transmitting over websocket to {0}", connection.Item1.RemoteEndPoint, GetType().Name); - - try - { - connection.Item3.Dispose(); - } - catch (ObjectDisposedException) - { - - } - - try - { - connection.Item2.Cancel(); - connection.Item2.Dispose(); - } - catch (ObjectDisposedException) - { - - } - - try - { - connection.Item5.Dispose(); - } - catch (ObjectDisposedException) - { - - } - - ActiveConnections.Remove(connection); - } - - /// <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) - { - lock (ActiveConnections) - { - foreach (var connection in ActiveConnections.ToList()) - { - DisposeConnection(connection); - } - } - } - } - - /// <summary> - /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. - /// </summary> - public void Dispose() - { - Dispose(true); - } - } -} diff --git a/MediaBrowser.Common/Kernel/IKernel.cs b/MediaBrowser.Common/Kernel/IKernel.cs deleted file mode 100644 index 51677677a..000000000 --- a/MediaBrowser.Common/Kernel/IKernel.cs +++ /dev/null @@ -1,74 +0,0 @@ -using MediaBrowser.Model.System; -using System; - -namespace MediaBrowser.Common.Kernel -{ - /// <summary> - /// Interface IKernel - /// </summary> - public interface IKernel - { - /// <summary> - /// Occurs when [has pending restart changed]. - /// </summary> - event EventHandler HasPendingRestartChanged; - - /// <summary> - /// Gets the kernel context. - /// </summary> - /// <value>The kernel context.</value> - KernelContext KernelContext { get; } - - /// <summary> - /// Inits this instance. - /// </summary> - /// <returns>Task.</returns> - void Init(); - - /// <summary> - /// Gets or sets a value indicating whether this instance has pending kernel reload. - /// </summary> - /// <value><c>true</c> if this instance has pending kernel reload; otherwise, <c>false</c>.</value> - bool HasPendingRestart { get; } - - /// <summary> - /// Gets the system status. - /// </summary> - /// <returns>SystemInfo.</returns> - SystemInfo GetSystemInfo(); - - /// <summary> - /// Called when [application updated]. - /// </summary> - /// <param name="newVersion">The new version.</param> - void OnApplicationUpdated(Version newVersion); - - /// <summary> - /// Gets the name of the web application. - /// </summary> - /// <value>The name of the web application.</value> - string WebApplicationName { get; } - - /// <summary> - /// Performs the pending restart. - /// </summary> - void PerformPendingRestart(); - - /// <summary> - /// Gets the UDP server port number. - /// </summary> - /// <value>The UDP server port number.</value> - int UdpServerPortNumber { get; } - - /// <summary> - /// Gets the HTTP server URL prefix. - /// </summary> - /// <value>The HTTP server URL prefix.</value> - string HttpServerUrlPrefix { get; } - - /// <summary> - /// Notifies the pending restart. - /// </summary> - void NotifyPendingRestart(); - } -} diff --git a/MediaBrowser.Common/Kernel/IServerManager.cs b/MediaBrowser.Common/Kernel/IServerManager.cs deleted file mode 100644 index a8c6a253a..000000000 --- a/MediaBrowser.Common/Kernel/IServerManager.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; - -namespace MediaBrowser.Common.Kernel -{ - public interface IServerManager : IDisposable - { - /// <summary> - /// Gets a value indicating whether [supports web socket]. - /// </summary> - /// <value><c>true</c> if [supports web socket]; otherwise, <c>false</c>.</value> - bool SupportsNativeWebSocket { get; } - - /// <summary> - /// Gets the web socket port number. - /// </summary> - /// <value>The web socket port number.</value> - int WebSocketPortNumber { get; } - - /// <summary> - /// Starts this instance. - /// </summary> - void Start(); - - /// <summary> - /// Sends a message to all clients currently connected via a web socket - /// </summary> - /// <typeparam name="T"></typeparam> - /// <param name="messageType">Type of the message.</param> - /// <param name="data">The data.</param> - /// <returns>Task.</returns> - void SendWebSocketMessage<T>(string messageType, T data); - - /// <summary> - /// Sends a message to all clients currently connected via a web socket - /// </summary> - /// <typeparam name="T"></typeparam> - /// <param name="messageType">Type of the message.</param> - /// <param name="dataFunction">The function that generates the data to send, if there are any connected clients</param> - void SendWebSocketMessage<T>(string messageType, Func<T> dataFunction); - - /// <summary> - /// Sends a message to all clients currently connected via a web socket - /// </summary> - /// <typeparam name="T"></typeparam> - /// <param name="messageType">Type of the message.</param> - /// <param name="dataFunction">The function that generates the data to send, if there are any connected clients</param> - /// <param name="cancellationToken">The cancellation token.</param> - /// <returns>Task.</returns> - /// <exception cref="System.ArgumentNullException">messageType</exception> - Task SendWebSocketMessageAsync<T>(string messageType, Func<T> dataFunction, CancellationToken cancellationToken); - - /// <summary> - /// Adds the web socket listeners. - /// </summary> - /// <param name="listeners">The listeners.</param> - void AddWebSocketListeners(IEnumerable<IWebSocketListener> listeners); - } -}
\ No newline at end of file diff --git a/MediaBrowser.Common/Kernel/IWebSocketListener.cs b/MediaBrowser.Common/Kernel/IWebSocketListener.cs deleted file mode 100644 index b04bb6885..000000000 --- a/MediaBrowser.Common/Kernel/IWebSocketListener.cs +++ /dev/null @@ -1,18 +0,0 @@ -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); - } -} diff --git a/MediaBrowser.Common/Kernel/KernelContext.cs b/MediaBrowser.Common/Kernel/KernelContext.cs deleted file mode 100644 index 1f84c0242..000000000 --- a/MediaBrowser.Common/Kernel/KernelContext.cs +++ /dev/null @@ -1,18 +0,0 @@ - -namespace MediaBrowser.Common.Kernel -{ - /// <summary> - /// Enum KernelContext - /// </summary> - public enum KernelContext - { - /// <summary> - /// The server - /// </summary> - Server, - /// <summary> - /// The UI - /// </summary> - Ui - } -} |
