2012-09-17 20:21:46 +00:00
|
|
|
|
using MediaBrowser.Common.Events;
|
|
|
|
|
using MediaBrowser.Common.Logging;
|
2012-09-07 16:17:39 +00:00
|
|
|
|
using MediaBrowser.Common.Net;
|
2012-09-08 14:52:13 +00:00
|
|
|
|
using MediaBrowser.Common.Net.Handlers;
|
2012-09-07 16:17:39 +00:00
|
|
|
|
using MediaBrowser.Common.Plugins;
|
|
|
|
|
using MediaBrowser.Common.Serialization;
|
|
|
|
|
using MediaBrowser.Model.Configuration;
|
|
|
|
|
using MediaBrowser.Model.Progress;
|
|
|
|
|
using System;
|
2012-07-26 13:51:26 +00:00
|
|
|
|
using System.Collections.Generic;
|
2012-07-26 02:33:11 +00:00
|
|
|
|
using System.ComponentModel.Composition;
|
|
|
|
|
using System.ComponentModel.Composition.Hosting;
|
2012-07-21 18:39:47 +00:00
|
|
|
|
using System.IO;
|
2012-07-26 02:33:11 +00:00
|
|
|
|
using System.Linq;
|
2012-07-21 18:39:47 +00:00
|
|
|
|
using System.Reflection;
|
2012-08-19 20:38:31 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2012-07-21 18:39:47 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Common.Kernel
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2012-09-11 19:37:14 +00:00
|
|
|
|
/// Represents a shared base kernel for both the Ui and server apps
|
2012-07-21 18:39:47 +00:00
|
|
|
|
/// </summary>
|
2012-08-19 22:47:02 +00:00
|
|
|
|
public abstract class BaseKernel<TConfigurationType, TApplicationPathsType> : IDisposable, IKernel
|
2012-07-29 15:19:25 +00:00
|
|
|
|
where TConfigurationType : BaseApplicationConfiguration, new()
|
2012-08-18 20:38:02 +00:00
|
|
|
|
where TApplicationPathsType : BaseApplicationPaths, new()
|
2012-07-21 18:39:47 +00:00
|
|
|
|
{
|
2012-09-17 20:21:46 +00:00
|
|
|
|
#region ReloadBeginning Event
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fires whenever the kernel begins reloading
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<GenericEventArgs<IProgress<TaskProgress>>> ReloadBeginning;
|
|
|
|
|
private void OnReloadBeginning(IProgress<TaskProgress> progress)
|
|
|
|
|
{
|
|
|
|
|
if (ReloadBeginning != null)
|
|
|
|
|
{
|
|
|
|
|
ReloadBeginning(this, new GenericEventArgs<IProgress<TaskProgress>> { Argument = progress });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ReloadCompleted Event
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fires whenever the kernel completes reloading
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<GenericEventArgs<IProgress<TaskProgress>>> ReloadCompleted;
|
|
|
|
|
private void OnReloadCompleted(IProgress<TaskProgress> progress)
|
|
|
|
|
{
|
|
|
|
|
if (ReloadCompleted != null)
|
|
|
|
|
{
|
|
|
|
|
ReloadCompleted(this, new GenericEventArgs<IProgress<TaskProgress>> { Argument = progress });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2012-07-21 18:39:47 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the current configuration
|
|
|
|
|
/// </summary>
|
2012-07-26 02:33:11 +00:00
|
|
|
|
public TConfigurationType Configuration { get; private set; }
|
2012-07-21 18:39:47 +00:00
|
|
|
|
|
2012-08-18 20:38:02 +00:00
|
|
|
|
public TApplicationPathsType ApplicationPaths { get; private set; }
|
|
|
|
|
|
2012-07-26 02:33:11 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the list of currently loaded plugins
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ImportMany(typeof(BasePlugin))]
|
|
|
|
|
public IEnumerable<BasePlugin> Plugins { get; private set; }
|
2012-07-26 13:51:26 +00:00
|
|
|
|
|
2012-09-08 14:52:13 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the list of currently registered http handlers
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ImportMany(typeof(BaseHandler))]
|
|
|
|
|
private IEnumerable<BaseHandler> HttpHandlers { get; set; }
|
|
|
|
|
|
2012-09-18 19:33:57 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the list of currently registered Loggers
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ImportMany(typeof(BaseLogger))]
|
|
|
|
|
public IEnumerable<BaseLogger> Loggers { get; set; }
|
|
|
|
|
|
2012-07-21 18:39:47 +00:00
|
|
|
|
/// <summary>
|
2012-09-11 19:37:14 +00:00
|
|
|
|
/// Both the Ui and server will have a built-in HttpServer.
|
|
|
|
|
/// People will inevitably want remote control apps so it's needed in the Ui too.
|
2012-07-21 18:39:47 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public HttpServer HttpServer { get; private set; }
|
|
|
|
|
|
2012-09-08 14:52:13 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// This subscribes to HttpListener requests and finds the appropate BaseHandler to process it
|
|
|
|
|
/// </summary>
|
|
|
|
|
private IDisposable HttpListener { get; set; }
|
|
|
|
|
|
2012-09-18 19:33:57 +00:00
|
|
|
|
private CompositionContainer CompositionContainer { get; set; }
|
|
|
|
|
|
2012-09-02 17:34:12 +00:00
|
|
|
|
protected virtual string HttpServerUrlPrefix
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return "http://+:" + Configuration.HttpServerPortNumber + "/mediabrowser/";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-21 18:39:47 +00:00
|
|
|
|
/// <summary>
|
2012-09-03 16:40:35 +00:00
|
|
|
|
/// Gets the kernel context. Subclasses will have to override.
|
2012-07-21 18:39:47 +00:00
|
|
|
|
/// </summary>
|
2012-09-03 16:40:35 +00:00
|
|
|
|
public abstract KernelContext KernelContext { get; }
|
2012-07-21 18:39:47 +00:00
|
|
|
|
|
2012-09-17 18:09:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes the Kernel
|
|
|
|
|
/// </summary>
|
|
|
|
|
public async Task Init(IProgress<TaskProgress> progress)
|
2012-07-21 18:39:47 +00:00
|
|
|
|
{
|
2012-09-18 19:33:57 +00:00
|
|
|
|
Logger.Kernel = this;
|
|
|
|
|
|
2012-09-17 18:09:35 +00:00
|
|
|
|
// Performs initializations that only occur once
|
|
|
|
|
InitializeInternal(progress);
|
|
|
|
|
|
|
|
|
|
// Performs initializations that can be reloaded at anytime
|
|
|
|
|
await Reload(progress).ConfigureAwait(false);
|
2012-07-21 18:39:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-17 18:09:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs initializations that only occur once
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void InitializeInternal(IProgress<TaskProgress> progress)
|
2012-07-21 18:39:47 +00:00
|
|
|
|
{
|
2012-09-17 18:09:35 +00:00
|
|
|
|
ApplicationPaths = new TApplicationPathsType();
|
2012-09-17 20:21:46 +00:00
|
|
|
|
|
|
|
|
|
ReportProgress(progress, "Loading Configuration");
|
2012-09-03 19:12:02 +00:00
|
|
|
|
ReloadConfiguration();
|
2012-07-21 18:39:47 +00:00
|
|
|
|
|
2012-09-17 20:21:46 +00:00
|
|
|
|
ReportProgress(progress, "Loading Http Server");
|
2012-09-03 19:12:02 +00:00
|
|
|
|
ReloadHttpServer();
|
2012-07-26 02:33:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-17 18:09:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs initializations that can be reloaded at anytime
|
|
|
|
|
/// </summary>
|
2012-09-17 20:21:46 +00:00
|
|
|
|
public async Task Reload(IProgress<TaskProgress> progress)
|
|
|
|
|
{
|
|
|
|
|
OnReloadBeginning(progress);
|
|
|
|
|
|
|
|
|
|
await ReloadInternal(progress).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
OnReloadCompleted(progress);
|
2012-09-18 19:33:57 +00:00
|
|
|
|
|
|
|
|
|
ReportProgress(progress, "Kernel.Reload Complete");
|
2012-09-17 20:21:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs initializations that can be reloaded at anytime
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual async Task ReloadInternal(IProgress<TaskProgress> progress)
|
2012-09-17 18:09:35 +00:00
|
|
|
|
{
|
|
|
|
|
await Task.Run(() =>
|
|
|
|
|
{
|
2012-09-17 20:21:46 +00:00
|
|
|
|
ReportProgress(progress, "Loading Plugins");
|
2012-09-17 18:09:35 +00:00
|
|
|
|
ReloadComposableParts();
|
|
|
|
|
|
|
|
|
|
}).ConfigureAwait(false);
|
|
|
|
|
}
|
2012-09-17 20:21:46 +00:00
|
|
|
|
|
2012-07-26 13:51:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Uses MEF to locate plugins
|
|
|
|
|
/// Subclasses can use this to locate types within plugins
|
|
|
|
|
/// </summary>
|
2012-09-17 18:09:35 +00:00
|
|
|
|
private void ReloadComposableParts()
|
2012-07-26 02:33:11 +00:00
|
|
|
|
{
|
2012-09-17 18:09:35 +00:00
|
|
|
|
DisposeComposableParts();
|
2012-09-02 13:45:02 +00:00
|
|
|
|
|
2012-09-18 19:33:57 +00:00
|
|
|
|
CompositionContainer = GetCompositionContainer(includeCurrentAssembly: true);
|
2012-09-03 19:12:02 +00:00
|
|
|
|
|
2012-09-18 19:33:57 +00:00
|
|
|
|
CompositionContainer.ComposeParts(this);
|
2012-09-03 19:12:02 +00:00
|
|
|
|
|
2012-09-17 18:09:35 +00:00
|
|
|
|
OnComposablePartsLoaded();
|
2012-09-03 19:12:02 +00:00
|
|
|
|
|
2012-09-18 19:33:57 +00:00
|
|
|
|
CompositionContainer.Catalog.Dispose();
|
2012-09-03 19:12:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-17 18:09:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructs an MEF CompositionContainer based on the current running assembly and all plugin assemblies
|
|
|
|
|
/// </summary>
|
2012-09-03 19:12:02 +00:00
|
|
|
|
public CompositionContainer GetCompositionContainer(bool includeCurrentAssembly = false)
|
|
|
|
|
{
|
2012-07-26 17:00:53 +00:00
|
|
|
|
// Gets all plugin assemblies by first reading all bytes of the .dll and calling Assembly.Load against that
|
|
|
|
|
// This will prevent the .dll file from getting locked, and allow us to replace it when needed
|
2012-09-03 16:40:35 +00:00
|
|
|
|
IEnumerable<Assembly> pluginAssemblies = Directory.GetFiles(ApplicationPaths.PluginsPath, "*.dll", SearchOption.TopDirectoryOnly).Select(f => Assembly.Load(File.ReadAllBytes((f))));
|
2012-07-26 02:33:11 +00:00
|
|
|
|
|
2012-07-26 17:00:53 +00:00
|
|
|
|
var catalog = new AggregateCatalog(pluginAssemblies.Select(a => new AssemblyCatalog(a)));
|
2012-09-02 13:45:02 +00:00
|
|
|
|
|
2012-08-20 15:55:05 +00:00
|
|
|
|
// Include composable parts in the Common assembly
|
2012-09-18 19:33:57 +00:00
|
|
|
|
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
|
2012-09-02 13:45:02 +00:00
|
|
|
|
|
2012-09-03 19:12:02 +00:00
|
|
|
|
if (includeCurrentAssembly)
|
|
|
|
|
{
|
|
|
|
|
// Include composable parts in the subclass assembly
|
|
|
|
|
catalog.Catalogs.Add(new AssemblyCatalog(GetType().Assembly));
|
|
|
|
|
}
|
2012-07-26 13:51:26 +00:00
|
|
|
|
|
2012-09-03 19:12:02 +00:00
|
|
|
|
return new CompositionContainer(catalog);
|
2012-07-26 02:33:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-26 13:51:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Fires after MEF finishes finding composable parts within plugin assemblies
|
|
|
|
|
/// </summary>
|
2012-07-26 02:33:11 +00:00
|
|
|
|
protected virtual void OnComposablePartsLoaded()
|
|
|
|
|
{
|
2012-09-18 19:33:57 +00:00
|
|
|
|
foreach (var logger in Loggers)
|
|
|
|
|
{
|
|
|
|
|
logger.Initialize(this);
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-17 18:09:35 +00:00
|
|
|
|
// Start-up each plugin
|
2012-09-18 19:33:57 +00:00
|
|
|
|
foreach (var plugin in Plugins)
|
2012-07-26 02:33:11 +00:00
|
|
|
|
{
|
2012-09-03 16:40:35 +00:00
|
|
|
|
plugin.Initialize(this);
|
2012-07-26 13:51:26 +00:00
|
|
|
|
}
|
2012-07-21 18:39:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-26 13:51:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reloads application configuration from the config file
|
|
|
|
|
/// </summary>
|
2012-09-17 18:09:35 +00:00
|
|
|
|
private void ReloadConfiguration()
|
2012-07-21 18:39:47 +00:00
|
|
|
|
{
|
2012-08-02 03:13:44 +00:00
|
|
|
|
//Configuration information for anything other than server-specific configuration will have to come via the API... -ebr
|
|
|
|
|
|
2012-08-02 12:51:43 +00:00
|
|
|
|
// Deserialize config
|
2012-09-18 19:33:57 +00:00
|
|
|
|
// Use try/catch to avoid the extra file system lookup using File.Exists
|
|
|
|
|
try
|
2012-08-02 12:51:43 +00:00
|
|
|
|
{
|
2012-09-18 19:33:57 +00:00
|
|
|
|
Configuration = XmlSerializer.DeserializeFromFile<TConfigurationType>(ApplicationPaths.SystemConfigurationFilePath);
|
2012-08-02 12:51:43 +00:00
|
|
|
|
}
|
2012-09-18 19:33:57 +00:00
|
|
|
|
catch (FileNotFoundException)
|
2012-08-02 12:51:43 +00:00
|
|
|
|
{
|
2012-09-18 19:33:57 +00:00
|
|
|
|
Configuration = new TConfigurationType();
|
|
|
|
|
XmlSerializer.SerializeToFile(Configuration, ApplicationPaths.SystemConfigurationFilePath);
|
2012-08-02 12:51:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-21 18:39:47 +00:00
|
|
|
|
|
2012-07-26 13:51:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Restarts the Http Server, or starts it if not currently running
|
|
|
|
|
/// </summary>
|
2012-07-21 18:39:47 +00:00
|
|
|
|
private void ReloadHttpServer()
|
2012-07-30 04:06:05 +00:00
|
|
|
|
{
|
|
|
|
|
DisposeHttpServer();
|
|
|
|
|
|
2012-09-02 17:34:12 +00:00
|
|
|
|
HttpServer = new HttpServer(HttpServerUrlPrefix);
|
2012-09-08 14:52:13 +00:00
|
|
|
|
|
2012-09-11 18:20:12 +00:00
|
|
|
|
HttpListener = HttpServer.Subscribe(ctx =>
|
2012-09-08 14:52:13 +00:00
|
|
|
|
{
|
|
|
|
|
BaseHandler handler = HttpHandlers.FirstOrDefault(h => h.HandlesRequest(ctx.Request));
|
|
|
|
|
|
|
|
|
|
// Find the appropiate http handler
|
|
|
|
|
if (handler != null)
|
|
|
|
|
{
|
|
|
|
|
// Need to create a new instance because handlers are currently stateful
|
|
|
|
|
handler = Activator.CreateInstance(handler.GetType()) as BaseHandler;
|
|
|
|
|
|
|
|
|
|
// No need to await this, despite the compiler warning
|
|
|
|
|
handler.ProcessRequest(ctx);
|
|
|
|
|
}
|
|
|
|
|
});
|
2012-07-30 04:06:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-30 19:03:07 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes all resources currently in use.
|
|
|
|
|
/// </summary>
|
2012-08-01 19:09:24 +00:00
|
|
|
|
public virtual void Dispose()
|
2012-07-30 04:06:05 +00:00
|
|
|
|
{
|
2012-09-17 18:09:35 +00:00
|
|
|
|
Logger.LogInfo("Beginning Kernel.Dispose");
|
2012-09-17 20:21:46 +00:00
|
|
|
|
|
2012-07-30 04:06:05 +00:00
|
|
|
|
DisposeHttpServer();
|
2012-09-17 18:09:35 +00:00
|
|
|
|
|
2012-09-18 19:33:57 +00:00
|
|
|
|
DisposeComposableParts();
|
2012-07-30 13:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 20:38:31 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes all objects gathered through MEF composable parts
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual void DisposeComposableParts()
|
|
|
|
|
{
|
2012-09-18 19:33:57 +00:00
|
|
|
|
if (CompositionContainer != null)
|
2012-08-19 20:38:31 +00:00
|
|
|
|
{
|
2012-09-18 19:33:57 +00:00
|
|
|
|
CompositionContainer.Dispose();
|
2012-08-19 20:38:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-30 19:03:07 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes the current HttpServer
|
|
|
|
|
/// </summary>
|
2012-07-30 13:44:28 +00:00
|
|
|
|
private void DisposeHttpServer()
|
|
|
|
|
{
|
|
|
|
|
if (HttpServer != null)
|
|
|
|
|
{
|
2012-09-17 18:09:35 +00:00
|
|
|
|
Logger.LogInfo("Disposing Http Server");
|
2012-09-17 20:21:46 +00:00
|
|
|
|
|
2012-07-30 13:44:28 +00:00
|
|
|
|
HttpServer.Dispose();
|
|
|
|
|
}
|
2012-09-08 14:52:13 +00:00
|
|
|
|
|
|
|
|
|
if (HttpListener != null)
|
|
|
|
|
{
|
|
|
|
|
HttpListener.Dispose();
|
|
|
|
|
}
|
2012-07-30 13:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-19 21:29:15 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the current application version
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Version ApplicationVersion
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return GetType().Assembly.GetName().Version;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-09-03 16:40:35 +00:00
|
|
|
|
|
2012-09-17 20:21:46 +00:00
|
|
|
|
protected void ReportProgress(IProgress<TaskProgress> progress, string message)
|
|
|
|
|
{
|
|
|
|
|
progress.Report(new TaskProgress { Description = message });
|
|
|
|
|
|
2012-09-18 19:33:57 +00:00
|
|
|
|
Logger.LogInfo(message);
|
2012-09-17 20:21:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-09-03 16:40:35 +00:00
|
|
|
|
BaseApplicationPaths IKernel.ApplicationPaths
|
|
|
|
|
{
|
|
|
|
|
get { return ApplicationPaths; }
|
|
|
|
|
}
|
2012-07-21 18:39:47 +00:00
|
|
|
|
}
|
2012-08-19 22:47:02 +00:00
|
|
|
|
|
|
|
|
|
public interface IKernel
|
|
|
|
|
{
|
2012-09-03 16:40:35 +00:00
|
|
|
|
BaseApplicationPaths ApplicationPaths { get; }
|
|
|
|
|
KernelContext KernelContext { get; }
|
|
|
|
|
|
2012-08-19 22:47:02 +00:00
|
|
|
|
Task Init(IProgress<TaskProgress> progress);
|
2012-09-17 18:09:35 +00:00
|
|
|
|
Task Reload(IProgress<TaskProgress> progress);
|
2012-09-18 19:33:57 +00:00
|
|
|
|
IEnumerable<BaseLogger> Loggers { get; }
|
2012-08-19 22:47:02 +00:00
|
|
|
|
void Dispose();
|
|
|
|
|
}
|
2012-07-21 18:39:47 +00:00
|
|
|
|
}
|