From 4f67fc4aefc11c1a4293227c70de922dbe03c652 Mon Sep 17 00:00:00 2001 From: LukePulverenti Date: Thu, 7 Mar 2013 00:34:00 -0500 Subject: removed base kernel and ikernel --- MediaBrowser.Common/Kernel/BaseKernel.cs | 167 -------------- .../Kernel/BasePeriodicWebSocketListener.cs | 251 --------------------- MediaBrowser.Common/Kernel/IKernel.cs | 74 ------ MediaBrowser.Common/Kernel/IServerManager.cs | 61 ----- MediaBrowser.Common/Kernel/IWebSocketListener.cs | 18 -- MediaBrowser.Common/Kernel/KernelContext.cs | 18 -- 6 files changed, 589 deletions(-) delete mode 100644 MediaBrowser.Common/Kernel/BaseKernel.cs delete mode 100644 MediaBrowser.Common/Kernel/BasePeriodicWebSocketListener.cs delete mode 100644 MediaBrowser.Common/Kernel/IKernel.cs delete mode 100644 MediaBrowser.Common/Kernel/IServerManager.cs delete mode 100644 MediaBrowser.Common/Kernel/IWebSocketListener.cs delete mode 100644 MediaBrowser.Common/Kernel/KernelContext.cs (limited to 'MediaBrowser.Common/Kernel') diff --git a/MediaBrowser.Common/Kernel/BaseKernel.cs b/MediaBrowser.Common/Kernel/BaseKernel.cs deleted file mode 100644 index cf8133e977..0000000000 --- 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 -{ - /// - /// Represents a shared base kernel for both the Ui and server apps - /// - public abstract class BaseKernel : IKernel - { - /// - /// Occurs when [has pending restart changed]. - /// - public event EventHandler HasPendingRestartChanged; - - #region ApplicationUpdated Event - /// - /// Occurs when [application updated]. - /// - public event EventHandler> ApplicationUpdated; - /// - /// Called when [application updated]. - /// - /// The new version. - public void OnApplicationUpdated(Version newVersion) - { - EventHelper.QueueEventIfNotNull(ApplicationUpdated, this, new GenericEventArgs { Argument = newVersion }, Logger); - - NotifyPendingRestart(); - } - #endregion - - /// - /// Gets or sets a value indicating whether this instance has changes that require the entire application to restart. - /// - /// true if this instance has pending application restart; otherwise, false. - public bool HasPendingRestart { get; private set; } - - /// - /// 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. - /// - /// The UDP server port number. - public abstract int UdpServerPortNumber { get; } - - /// - /// 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}/... - /// - /// The name of the web application. - public string WebApplicationName - { - get { return "mediabrowser"; } - } - - /// - /// Gets the HTTP server URL prefix. - /// - /// The HTTP server URL prefix. - public virtual string HttpServerUrlPrefix - { - get - { - return "http://+:" + _configurationManager.CommonConfiguration.HttpServerPortNumber + "/" + WebApplicationName + "/"; - } - } - - /// - /// Gets the kernel context. Subclasses will have to override. - /// - /// The kernel context. - public abstract KernelContext KernelContext { get; } - - /// - /// Gets the logger. - /// - /// The logger. - protected ILogger Logger { get; private set; } - - /// - /// Gets or sets the application host. - /// - /// The application host. - protected IApplicationHost ApplicationHost { get; private set; } - - private readonly IConfigurationManager _configurationManager; - - /// - /// Initializes a new instance of the class. - /// - /// The app host. - /// The log manager. - protected BaseKernel(IApplicationHost appHost, ILogManager logManager, IConfigurationManager configurationManager) - { - ApplicationHost = appHost; - _configurationManager = configurationManager; - Logger = logManager.GetLogger("Kernel"); - } - - /// - /// Initializes the Kernel - /// - /// Task. - public void Init() - { - ReloadInternal(); - - Logger.Info("Kernel.Init Complete"); - } - - /// - /// Performs initializations that can be reloaded at anytime - /// - /// Task. - protected virtual void ReloadInternal() - { - } - - /// - /// Notifies that the kernel that a change has been made that requires a restart - /// - public void NotifyPendingRestart() - { - HasPendingRestart = true; - - EventHelper.QueueEventIfNotNull(HasPendingRestartChanged, this, EventArgs.Empty, Logger); - } - - /// - /// Performs the pending restart. - /// - /// Task. - public void PerformPendingRestart() - { - if (HasPendingRestart) - { - Logger.Info("Restarting the application"); - - ApplicationHost.Restart(); - } - else - { - Logger.Info("PerformPendingRestart - not needed"); - } - } - - /// - /// Gets the system status. - /// - /// SystemInfo. - public virtual SystemInfo GetSystemInfo() - { - return new SystemInfo - { - HasPendingRestart = HasPendingRestart, - Version = ApplicationHost.ApplicationVersion.ToString(), - IsNetworkDeployed = ApplicationHost.CanSelfUpdate, - WebSocketPortNumber = ApplicationHost.Resolve().WebSocketPortNumber, - SupportsNativeWebSocket = ApplicationHost.Resolve().SupportsNativeWebSocket, - FailedPluginAssemblies = ApplicationHost.FailedAssemblies.ToArray() - }; - } - } -} diff --git a/MediaBrowser.Common/Kernel/BasePeriodicWebSocketListener.cs b/MediaBrowser.Common/Kernel/BasePeriodicWebSocketListener.cs deleted file mode 100644 index 6a44cf3728..0000000000 --- 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 -{ - /// - /// 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 return data type. - /// The type of the T state type. - public abstract class BasePeriodicWebSocketListener : IWebSocketListener, IDisposable - where TStateType : class, new() - { - /// - /// The _active connections - /// - protected readonly List> ActiveConnections = - new List>(); - - /// - /// Gets the name. - /// - /// The name. - protected abstract string Name { get; } - - /// - /// Gets the data to send. - /// - /// The state. - /// Task{`1}. - protected abstract Task GetDataToSend(TStateType state); - - /// - /// The logger - /// - protected ILogger Logger; - - /// - /// Initializes a new instance of the class. - /// - /// The logger. - /// logger - protected BasePeriodicWebSocketListener(ILogger logger) - { - if (logger == null) - { - throw new ArgumentNullException("logger"); - } - - Logger = logger; - } - - /// - /// The null task result - /// - protected Task NullTaskResult = Task.FromResult(true); - - /// - /// Processes the message. - /// - /// The message. - /// Task. - 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; - } - - /// - /// Starts sending messages over a web socket - /// - /// The message. - 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(message.Connection, cancellationTokenSource, timer, state, semaphore)); - } - - timer.Change(TimeSpan.FromMilliseconds(dueTimeMs), TimeSpan.FromMilliseconds(periodMs)); - } - - /// - /// Timers the callback. - /// - /// The state. - private async void TimerCallback(object state) - { - var connection = (IWebSocketConnection)state; - - Tuple 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 - { - 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(); - } - } - - /// - /// Stops sending messages over a web socket - /// - /// The message. - private void Stop(WebSocketMessageInfo message) - { - lock (ActiveConnections) - { - var connection = ActiveConnections.FirstOrDefault(c => c.Item1 == message.Connection); - - if (connection != null) - { - DisposeConnection(connection); - } - } - } - - /// - /// Disposes the connection. - /// - /// The connection. - private void DisposeConnection(Tuple 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); - } - - /// - /// 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) - { - if (dispose) - { - lock (ActiveConnections) - { - foreach (var connection in ActiveConnections.ToList()) - { - DisposeConnection(connection); - } - } - } - } - - /// - /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. - /// - public void Dispose() - { - Dispose(true); - } - } -} diff --git a/MediaBrowser.Common/Kernel/IKernel.cs b/MediaBrowser.Common/Kernel/IKernel.cs deleted file mode 100644 index 51677677ac..0000000000 --- a/MediaBrowser.Common/Kernel/IKernel.cs +++ /dev/null @@ -1,74 +0,0 @@ -using MediaBrowser.Model.System; -using System; - -namespace MediaBrowser.Common.Kernel -{ - /// - /// Interface IKernel - /// - public interface IKernel - { - /// - /// Occurs when [has pending restart changed]. - /// - event EventHandler HasPendingRestartChanged; - - /// - /// Gets the kernel context. - /// - /// The kernel context. - KernelContext KernelContext { get; } - - /// - /// Inits this instance. - /// - /// Task. - void Init(); - - /// - /// Gets or sets a value indicating whether this instance has pending kernel reload. - /// - /// true if this instance has pending kernel reload; otherwise, false. - bool HasPendingRestart { get; } - - /// - /// Gets the system status. - /// - /// SystemInfo. - SystemInfo GetSystemInfo(); - - /// - /// Called when [application updated]. - /// - /// The new version. - void OnApplicationUpdated(Version newVersion); - - /// - /// Gets the name of the web application. - /// - /// The name of the web application. - string WebApplicationName { get; } - - /// - /// Performs the pending restart. - /// - void PerformPendingRestart(); - - /// - /// Gets the UDP server port number. - /// - /// The UDP server port number. - int UdpServerPortNumber { get; } - - /// - /// Gets the HTTP server URL prefix. - /// - /// The HTTP server URL prefix. - string HttpServerUrlPrefix { get; } - - /// - /// Notifies the pending restart. - /// - void NotifyPendingRestart(); - } -} diff --git a/MediaBrowser.Common/Kernel/IServerManager.cs b/MediaBrowser.Common/Kernel/IServerManager.cs deleted file mode 100644 index a8c6a253a9..0000000000 --- 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 - { - /// - /// Gets a value indicating whether [supports web socket]. - /// - /// true if [supports web socket]; otherwise, false. - bool SupportsNativeWebSocket { get; } - - /// - /// Gets the web socket port number. - /// - /// The web socket port number. - int WebSocketPortNumber { get; } - - /// - /// Starts this instance. - /// - void Start(); - - /// - /// Sends a message to all clients currently connected via a web socket - /// - /// - /// Type of the message. - /// The data. - /// Task. - void SendWebSocketMessage(string messageType, T data); - - /// - /// Sends a message to all clients currently connected via a web socket - /// - /// - /// Type of the message. - /// The function that generates the data to send, if there are any connected clients - void SendWebSocketMessage(string messageType, Func dataFunction); - - /// - /// Sends a message to all clients currently connected via a web socket - /// - /// - /// Type of the message. - /// The function that generates the data to send, if there are any connected clients - /// The cancellation token. - /// Task. - /// messageType - Task SendWebSocketMessageAsync(string messageType, Func dataFunction, CancellationToken cancellationToken); - - /// - /// Adds the web socket listeners. - /// - /// The listeners. - void AddWebSocketListeners(IEnumerable 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 b04bb68851..0000000000 --- a/MediaBrowser.Common/Kernel/IWebSocketListener.cs +++ /dev/null @@ -1,18 +0,0 @@ -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/Kernel/KernelContext.cs b/MediaBrowser.Common/Kernel/KernelContext.cs deleted file mode 100644 index 1f84c02426..0000000000 --- a/MediaBrowser.Common/Kernel/KernelContext.cs +++ /dev/null @@ -1,18 +0,0 @@ - -namespace MediaBrowser.Common.Kernel -{ - /// - /// Enum KernelContext - /// - public enum KernelContext - { - /// - /// The server - /// - Server, - /// - /// The UI - /// - Ui - } -} -- cgit v1.2.3