2013-02-21 01:33:05 +00:00
|
|
|
|
using MediaBrowser.Common.Events;
|
2013-02-26 22:19:45 +00:00
|
|
|
|
using MediaBrowser.Common.Security;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using MediaBrowser.Model.Configuration;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-02-24 21:53:54 +00:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using MediaBrowser.Model.System;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Common.Kernel
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a shared base kernel for both the Ui and server apps
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TConfigurationType">The type of the T configuration type.</typeparam>
|
|
|
|
|
/// <typeparam name="TApplicationPathsType">The type of the T application paths type.</typeparam>
|
|
|
|
|
public abstract class BaseKernel<TConfigurationType, TApplicationPathsType> : IDisposable, IKernel
|
|
|
|
|
where TConfigurationType : BaseApplicationConfiguration, new()
|
2013-02-24 21:53:54 +00:00
|
|
|
|
where TApplicationPathsType : IApplicationPaths
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-03-01 21:22:34 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Occurs when [has pending restart changed].
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler HasPendingRestartChanged;
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
#region ConfigurationUpdated Event
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Occurs when [configuration updated].
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<EventArgs> ConfigurationUpdated;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when [configuration updated].
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal void OnConfigurationUpdated()
|
|
|
|
|
{
|
2013-02-21 21:06:23 +00:00
|
|
|
|
EventHelper.QueueEventIfNotNull(ConfigurationUpdated, this, EventArgs.Empty, Logger);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ReloadCompleted Event
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fires whenever the kernel completes reloading
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<EventArgs> ReloadCompleted;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when [reload completed].
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void OnReloadCompleted()
|
|
|
|
|
{
|
2013-02-21 21:06:23 +00:00
|
|
|
|
EventHelper.QueueEventIfNotNull(ReloadCompleted, this, EventArgs.Empty, Logger);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#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)
|
|
|
|
|
{
|
2013-02-21 21:06:23 +00:00
|
|
|
|
EventHelper.QueueEventIfNotNull(ApplicationUpdated, this, new GenericEventArgs<Version> { Argument = newVersion }, Logger);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
NotifyPendingRestart();
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _configuration loaded
|
|
|
|
|
/// </summary>
|
|
|
|
|
private bool _configurationLoaded;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _configuration sync lock
|
|
|
|
|
/// </summary>
|
|
|
|
|
private object _configurationSyncLock = new object();
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _configuration
|
|
|
|
|
/// </summary>
|
|
|
|
|
private TConfigurationType _configuration;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the system configuration
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The configuration.</value>
|
|
|
|
|
public TConfigurationType Configuration
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
// Lazy load
|
2013-02-24 21:53:54 +00:00
|
|
|
|
LazyInitializer.EnsureInitialized(ref _configuration, ref _configurationLoaded, ref _configurationSyncLock, () => GetXmlConfiguration<TConfigurationType>(ApplicationPaths.SystemConfigurationFilePath));
|
2013-02-21 01:33:05 +00:00
|
|
|
|
return _configuration;
|
|
|
|
|
}
|
|
|
|
|
protected set
|
|
|
|
|
{
|
|
|
|
|
_configuration = value;
|
|
|
|
|
|
|
|
|
|
if (value == null)
|
|
|
|
|
{
|
|
|
|
|
_configurationLoaded = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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 application paths.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The application paths.</value>
|
|
|
|
|
public TApplicationPathsType ApplicationPaths { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the TCP manager.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The TCP manager.</value>
|
2013-03-01 21:22:34 +00:00
|
|
|
|
private IServerManager ServerManager { get; set; }
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-02-26 22:19:45 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the plug-in security manager.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The plug-in security manager.</value>
|
|
|
|
|
public ISecurityManager SecurityManager { get; set; }
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <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://+:" + Configuration.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; }
|
|
|
|
|
|
2013-02-22 01:26:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the application host.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The application host.</value>
|
|
|
|
|
protected IApplicationHost ApplicationHost { get; private set; }
|
|
|
|
|
|
2013-02-23 07:57:11 +00:00
|
|
|
|
/// <summary>
|
2013-02-24 21:53:54 +00:00
|
|
|
|
/// The _XML serializer
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// </summary>
|
2013-02-24 21:53:54 +00:00
|
|
|
|
private readonly IXmlSerializer _xmlSerializer;
|
2013-02-23 00:24:50 +00:00
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
2013-02-21 04:37:50 +00:00
|
|
|
|
/// Initializes a new instance of the <see cref="BaseKernel{TApplicationPathsType}" /> class.
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// </summary>
|
2013-02-22 01:26:35 +00:00
|
|
|
|
/// <param name="appHost">The app host.</param>
|
2013-02-24 21:53:54 +00:00
|
|
|
|
/// <param name="appPaths">The app paths.</param>
|
|
|
|
|
/// <param name="xmlSerializer">The XML serializer.</param>
|
2013-02-21 21:06:23 +00:00
|
|
|
|
/// <param name="logger">The logger.</param>
|
2013-02-22 01:26:35 +00:00
|
|
|
|
/// <exception cref="System.ArgumentNullException">isoManager</exception>
|
2013-02-24 21:53:54 +00:00
|
|
|
|
protected BaseKernel(IApplicationHost appHost, TApplicationPathsType appPaths, IXmlSerializer xmlSerializer, ILogger logger)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-02-24 21:53:54 +00:00
|
|
|
|
ApplicationPaths = appPaths;
|
2013-02-22 01:26:35 +00:00
|
|
|
|
ApplicationHost = appHost;
|
2013-02-24 21:53:54 +00:00
|
|
|
|
_xmlSerializer = xmlSerializer;
|
2013-02-21 21:06:23 +00:00
|
|
|
|
Logger = logger;
|
2013-02-21 04:37:50 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-02-21 04:37:50 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes the Kernel
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Task.</returns>
|
2013-03-03 02:47:04 +00:00
|
|
|
|
public void Init()
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-03-03 02:47:04 +00:00
|
|
|
|
ReloadInternal();
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
OnReloadCompleted();
|
|
|
|
|
|
|
|
|
|
Logger.Info("Kernel.Reload Complete");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs initializations that can be reloaded at anytime
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Task.</returns>
|
2013-03-03 02:47:04 +00:00
|
|
|
|
protected virtual void ReloadInternal()
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-02-26 16:10:55 +00:00
|
|
|
|
ServerManager = ApplicationHost.Resolve<IServerManager>();
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Notifies that the kernel that a change has been made that requires a restart
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void NotifyPendingRestart()
|
|
|
|
|
{
|
|
|
|
|
HasPendingRestart = true;
|
|
|
|
|
|
2013-03-01 21:22:34 +00:00
|
|
|
|
EventHelper.QueueEventIfNotNull(HasPendingRestartChanged, this, EventArgs.Empty, Logger);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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>
|
|
|
|
|
/// Performs the pending restart.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
public void PerformPendingRestart()
|
|
|
|
|
{
|
|
|
|
|
if (HasPendingRestart)
|
|
|
|
|
{
|
2013-02-26 03:43:04 +00:00
|
|
|
|
Logger.Info("Restarting the application");
|
|
|
|
|
|
|
|
|
|
ApplicationHost.Restart();
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("PerformPendingRestart - not needed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the system status.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>SystemInfo.</returns>
|
|
|
|
|
public virtual SystemInfo GetSystemInfo()
|
|
|
|
|
{
|
|
|
|
|
return new SystemInfo
|
|
|
|
|
{
|
|
|
|
|
HasPendingRestart = HasPendingRestart,
|
2013-02-26 03:43:04 +00:00
|
|
|
|
Version = ApplicationHost.ApplicationVersion.ToString(),
|
2013-02-22 04:23:06 +00:00
|
|
|
|
IsNetworkDeployed = ApplicationHost.CanSelfUpdate,
|
2013-02-26 16:10:55 +00:00
|
|
|
|
WebSocketPortNumber = ServerManager.WebSocketPortNumber,
|
|
|
|
|
SupportsNativeWebSocket = ServerManager.SupportsNativeWebSocket,
|
2013-02-24 21:53:54 +00:00
|
|
|
|
FailedPluginAssemblies = ApplicationHost.FailedAssemblies.ToArray()
|
2013-02-21 01:33:05 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _save lock
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly object _configurationSaveLock = new object();
|
2013-02-21 04:37:50 +00:00
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Saves the current configuration
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void SaveConfiguration()
|
|
|
|
|
{
|
|
|
|
|
lock (_configurationSaveLock)
|
|
|
|
|
{
|
2013-02-24 21:53:54 +00:00
|
|
|
|
_xmlSerializer.SerializeToFile(Configuration, ApplicationPaths.SystemConfigurationFilePath);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OnConfigurationUpdated();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the application paths.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The application paths.</value>
|
2013-02-24 21:53:54 +00:00
|
|
|
|
IApplicationPaths IKernel.ApplicationPaths
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
get { return ApplicationPaths; }
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the configuration.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The configuration.</value>
|
|
|
|
|
BaseApplicationConfiguration IKernel.Configuration
|
|
|
|
|
{
|
|
|
|
|
get { return Configuration; }
|
2013-02-24 21:53:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads an xml configuration file from the file system
|
|
|
|
|
/// It will immediately re-serialize and save if new serialization data is available due to property changes
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="type">The type.</param>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
|
public object GetXmlConfiguration(Type type, string path)
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Loading {0} at {1}", type.Name, path);
|
|
|
|
|
|
|
|
|
|
object configuration;
|
|
|
|
|
|
|
|
|
|
byte[] buffer = null;
|
|
|
|
|
|
|
|
|
|
// Use try/catch to avoid the extra file system lookup using File.Exists
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
buffer = File.ReadAllBytes(path);
|
|
|
|
|
|
|
|
|
|
configuration = _xmlSerializer.DeserializeFromBytes(type, buffer);
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
2013-03-02 07:16:52 +00:00
|
|
|
|
configuration = Activator.CreateInstance(type);
|
2013-02-24 21:53:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Take the object we just got and serialize it back to bytes
|
|
|
|
|
var newBytes = _xmlSerializer.SerializeToBytes(configuration);
|
|
|
|
|
|
|
|
|
|
// If the file didn't exist before, or if something has changed, re-save
|
|
|
|
|
if (buffer == null || !buffer.SequenceEqual(newBytes))
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Saving {0} to {1}", type.Name, path);
|
|
|
|
|
|
|
|
|
|
// Save it after load in case we got new items
|
|
|
|
|
File.WriteAllBytes(path, newBytes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return configuration;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reads an xml configuration file from the file system
|
|
|
|
|
/// It will immediately save the configuration after loading it, just
|
|
|
|
|
/// in case there are new serializable properties
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
|
/// <returns>``0.</returns>
|
|
|
|
|
private T GetXmlConfiguration<T>(string path)
|
|
|
|
|
where T : class
|
|
|
|
|
{
|
|
|
|
|
return GetXmlConfiguration(typeof(T), path) as T;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
2013-02-26 21:48:43 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Limits simultaneous access to various resources
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The resource pools.</value>
|
|
|
|
|
public ResourcePool ResourcePools { get; set; }
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|