2013-03-04 06:43:06 +01:00
using MediaBrowser.Common.Configuration ;
using MediaBrowser.Common.Implementations.Logging ;
using MediaBrowser.Common.Implementations.NetworkManagement ;
using MediaBrowser.Common.Implementations.ScheduledTasks ;
using MediaBrowser.Common.Implementations.Security ;
using MediaBrowser.Common.Implementations.Serialization ;
2013-03-03 08:27:40 +01:00
using MediaBrowser.Common.Implementations.Udp ;
2013-02-27 18:07:12 +01:00
using MediaBrowser.Common.Implementations.Updates ;
2013-02-27 17:46:48 +01:00
using MediaBrowser.Common.Implementations.WebSocket ;
using MediaBrowser.Common.Kernel ;
using MediaBrowser.Common.Net ;
2013-03-03 03:47:04 +01:00
using MediaBrowser.Common.Plugins ;
2013-02-27 17:46:48 +01:00
using MediaBrowser.Common.ScheduledTasks ;
2013-03-04 06:43:06 +01:00
using MediaBrowser.Common.Security ;
2013-02-27 15:39:11 +01:00
using MediaBrowser.Common.Updates ;
2013-02-26 04:43:04 +01:00
using MediaBrowser.Model.Logging ;
using MediaBrowser.Model.Serialization ;
2013-03-04 06:43:06 +01:00
using MediaBrowser.Model.Updates ;
2013-02-26 04:43:04 +01:00
using SimpleInjector ;
using System ;
using System.Collections.Generic ;
2013-02-27 05:19:05 +01:00
using System.IO ;
using System.Linq ;
using System.Reflection ;
2013-02-26 04:43:04 +01:00
using System.Threading ;
2013-03-04 06:43:06 +01:00
using System.Threading.Tasks ;
2013-02-26 04:43:04 +01:00
namespace MediaBrowser.Common.Implementations
{
2013-03-04 06:43:06 +01:00
public abstract class BaseApplicationHost < TApplicationPathsType > : IApplicationHost
where TApplicationPathsType : class , IApplicationPaths , new ( )
2013-02-26 04:43:04 +01:00
{
/// <summary>
/// Gets or sets the logger.
/// </summary>
/// <value>The logger.</value>
2013-03-04 06:43:06 +01:00
protected ILogger Logger { get ; private set ; }
2013-02-26 04:43:04 +01:00
2013-03-03 03:47:04 +01:00
/// <summary>
/// Gets or sets the plugins.
/// </summary>
/// <value>The plugins.</value>
public IEnumerable < IPlugin > Plugins { get ; protected set ; }
2013-02-26 18:21:18 +01:00
/// <summary>
/// Gets or sets the log manager.
/// </summary>
/// <value>The log manager.</value>
public ILogManager LogManager { get ; protected set ; }
/// <summary>
/// Gets the application paths.
/// </summary>
/// <value>The application paths.</value>
2013-03-04 06:43:06 +01:00
protected TApplicationPathsType ApplicationPaths = new TApplicationPathsType ( ) ;
2013-02-26 18:21:18 +01:00
2013-02-26 04:43:04 +01:00
/// <summary>
/// The container
/// </summary>
protected readonly Container Container = new Container ( ) ;
2013-03-03 08:27:40 +01:00
2013-03-04 06:43:06 +01:00
/// <summary>
/// The json serializer
/// </summary>
protected readonly IJsonSerializer JsonSerializer = new JsonSerializer ( ) ;
/// <summary>
/// The _XML serializer
/// </summary>
protected readonly IXmlSerializer XmlSerializer = new XmlSerializer ( ) ;
2013-02-26 04:43:04 +01:00
/// <summary>
/// Gets assemblies that failed to load
/// </summary>
public List < string > FailedAssemblies { get ; protected set ; }
/// <summary>
/// Gets all types within all running assemblies
/// </summary>
/// <value>All types.</value>
public Type [ ] AllTypes { get ; protected set ; }
/// <summary>
/// Gets all concrete types.
/// </summary>
/// <value>All concrete types.</value>
public Type [ ] AllConcreteTypes { get ; protected set ; }
/// <summary>
/// The disposable parts
/// </summary>
protected readonly List < IDisposable > DisposableParts = new List < IDisposable > ( ) ;
2013-02-26 18:21:18 +01:00
/// <summary>
/// Gets a value indicating whether this instance is first run.
/// </summary>
/// <value><c>true</c> if this instance is first run; otherwise, <c>false</c>.</value>
public bool IsFirstRun { get ; private set ; }
2013-02-26 04:43:04 +01:00
/// <summary>
/// The _protobuf serializer initialized
/// </summary>
private bool _protobufSerializerInitialized ;
/// <summary>
/// The _protobuf serializer sync lock
/// </summary>
private object _protobufSerializerSyncLock = new object ( ) ;
/// <summary>
/// Gets a dynamically compiled generated serializer that can serialize protocontracts without reflection
/// </summary>
private IProtobufSerializer _protobufSerializer ;
/// <summary>
/// Gets the protobuf serializer.
/// </summary>
/// <value>The protobuf serializer.</value>
protected IProtobufSerializer ProtobufSerializer
{
get
{
// Lazy load
LazyInitializer . EnsureInitialized ( ref _protobufSerializer , ref _protobufSerializerInitialized , ref _protobufSerializerSyncLock , ( ) = > Serialization . ProtobufSerializer . Create ( AllTypes ) ) ;
return _protobufSerializer ;
}
private set
{
_protobufSerializer = value ;
_protobufSerializerInitialized = value ! = null ;
}
}
2013-03-04 06:43:06 +01:00
/// <summary>
/// Gets the kernel.
/// </summary>
/// <value>The kernel.</value>
protected IKernel Kernel { get ; private set ; }
protected ITaskManager TaskManager { get ; private set ; }
protected ISecurityManager SecurityManager { get ; private set ; }
protected IConfigurationManager ConfigurationManager { get ; private set ; }
2013-02-26 04:43:04 +01:00
/// <summary>
/// Initializes a new instance of the <see cref="BaseApplicationHost" /> class.
/// </summary>
protected BaseApplicationHost ( )
{
FailedAssemblies = new List < string > ( ) ;
2013-03-04 06:43:06 +01:00
LogManager = new NlogManager ( ApplicationPaths . LogDirectoryPath , LogFilePrefixName ) ;
ConfigurationManager = GetConfigurationManager ( ) ;
2013-03-03 08:27:40 +01:00
}
2013-02-26 18:21:18 +01:00
2013-03-03 08:27:40 +01:00
/// <summary>
/// Inits this instance.
/// </summary>
/// <returns>Task.</returns>
2013-03-04 19:13:47 +01:00
public virtual async Task Init ( )
2013-03-03 08:27:40 +01:00
{
2013-03-04 19:13:47 +01:00
IsFirstRun = ! ConfigurationManager . CommonConfiguration . IsStartupWizardCompleted ;
2013-02-26 18:21:18 +01:00
2013-03-04 19:13:47 +01:00
Logger = LogManager . GetLogger ( "App" ) ;
2013-03-04 06:43:06 +01:00
2013-03-04 19:13:47 +01:00
DiscoverTypes ( ) ;
2013-03-04 06:43:06 +01:00
2013-03-04 19:13:47 +01:00
LogManager . ReloadLogger ( ConfigurationManager . CommonConfiguration . EnableDebugLevelLogging ? LogSeverity . Debug : LogSeverity . Info ) ;
2013-03-04 06:43:06 +01:00
2013-03-04 19:13:47 +01:00
Logger . Info ( "Version {0} initializing" , ApplicationVersion ) ;
2013-03-04 06:43:06 +01:00
2013-03-04 19:13:47 +01:00
Kernel = GetKernel ( ) ;
2013-03-04 06:43:06 +01:00
2013-03-04 19:13:47 +01:00
await RegisterResources ( ) . ConfigureAwait ( false ) ;
2013-03-04 06:43:06 +01:00
2013-03-04 19:13:47 +01:00
FindParts ( ) ;
2013-03-04 06:43:06 +01:00
2013-03-04 19:13:47 +01:00
Task . Run ( ( ) = > ConfigureAutoRunAtStartup ( ) ) ;
Kernel . Init ( ) ;
2013-02-26 04:43:04 +01:00
}
/// <summary>
/// Gets the composable part assemblies.
/// </summary>
/// <returns>IEnumerable{Assembly}.</returns>
protected abstract IEnumerable < Assembly > GetComposablePartAssemblies ( ) ;
2013-02-26 18:21:18 +01:00
/// <summary>
2013-03-04 06:43:06 +01:00
/// Gets the name of the log file prefix.
2013-02-26 18:21:18 +01:00
/// </summary>
2013-03-04 06:43:06 +01:00
/// <value>The name of the log file prefix.</value>
protected abstract string LogFilePrefixName { get ; }
2013-02-26 18:21:18 +01:00
2013-03-04 06:43:06 +01:00
protected abstract IKernel GetKernel ( ) ;
protected abstract IConfigurationManager GetConfigurationManager ( ) ;
2013-02-27 17:46:48 +01:00
/// <summary>
/// Finds the parts.
/// </summary>
protected virtual void FindParts ( )
{
Resolve < IHttpServer > ( ) . Init ( GetExports < IRestfulService > ( false ) ) ;
2013-02-27 18:28:15 +01:00
Resolve < IServerManager > ( ) . AddWebSocketListeners ( GetExports < IWebSocketListener > ( false ) ) ;
2013-02-27 17:46:48 +01:00
2013-02-27 18:28:15 +01:00
Resolve < IServerManager > ( ) . Start ( ) ;
2013-03-03 03:47:04 +01:00
Resolve < ITaskManager > ( ) . AddTasks ( GetExports < IScheduledTask > ( false ) ) ;
Plugins = GetExports < IPlugin > ( ) ;
2013-02-27 17:46:48 +01:00
}
2013-03-03 08:27:40 +01:00
2013-02-26 04:43:04 +01:00
/// <summary>
/// Discovers the types.
/// </summary>
protected void DiscoverTypes ( )
{
FailedAssemblies . Clear ( ) ;
AllTypes = GetComposablePartAssemblies ( ) . SelectMany ( GetTypes ) . ToArray ( ) ;
AllConcreteTypes = AllTypes . Where ( t = > t . IsClass & & ! t . IsAbstract & & ! t . IsInterface & & ! t . IsGenericType ) . ToArray ( ) ;
}
2013-02-27 17:46:48 +01:00
/// <summary>
/// Registers resources that classes will depend on
/// </summary>
2013-03-04 19:13:47 +01:00
protected virtual Task RegisterResources ( )
2013-02-27 17:46:48 +01:00
{
2013-03-04 19:13:47 +01:00
return Task . Run ( ( ) = >
{
RegisterSingleInstance ( ConfigurationManager ) ;
RegisterSingleInstance < IApplicationHost > ( this ) ;
2013-03-04 06:43:06 +01:00
2013-03-04 19:13:47 +01:00
RegisterSingleInstance < IApplicationPaths > ( ApplicationPaths ) ;
2013-03-04 06:43:06 +01:00
2013-03-04 19:13:47 +01:00
var networkManager = new NetworkManager ( ) ;
2013-03-04 06:43:06 +01:00
2013-03-04 19:13:47 +01:00
var serverManager = new ServerManager . ServerManager ( this , Kernel , networkManager , JsonSerializer , Logger , ConfigurationManager ) ;
2013-03-04 06:43:06 +01:00
2013-03-04 19:13:47 +01:00
TaskManager = new TaskManager ( ApplicationPaths , JsonSerializer , Logger , serverManager ) ;
2013-03-04 06:43:06 +01:00
2013-03-04 19:13:47 +01:00
RegisterSingleInstance ( JsonSerializer ) ;
RegisterSingleInstance ( XmlSerializer ) ;
2013-03-04 06:43:06 +01:00
2013-03-04 19:13:47 +01:00
RegisterSingleInstance ( LogManager ) ;
RegisterSingleInstance ( Logger ) ;
2013-02-27 17:46:48 +01:00
2013-03-04 19:13:47 +01:00
RegisterSingleInstance ( Kernel ) ;
2013-03-04 06:43:06 +01:00
2013-03-04 19:13:47 +01:00
RegisterSingleInstance ( TaskManager ) ;
RegisterSingleInstance < IWebSocketServer > ( ( ) = > new AlchemyServer ( Logger ) ) ;
RegisterSingleInstance ( ProtobufSerializer ) ;
RegisterSingleInstance < IUdpServer > ( new UdpServer ( Logger ) , false ) ;
2013-02-27 17:46:48 +01:00
2013-03-04 19:13:47 +01:00
var httpClient = new HttpClientManager . HttpClientManager ( ApplicationPaths , Logger ) ;
2013-03-04 06:43:06 +01:00
2013-03-04 19:13:47 +01:00
RegisterSingleInstance < IHttpClient > ( httpClient ) ;
2013-03-04 06:43:06 +01:00
2013-03-04 19:13:47 +01:00
RegisterSingleInstance < INetworkManager > ( networkManager ) ;
RegisterSingleInstance < IServerManager > ( serverManager ) ;
2013-03-04 06:43:06 +01:00
2013-03-04 19:13:47 +01:00
SecurityManager = new PluginSecurityManager ( Kernel , httpClient , JsonSerializer , ApplicationPaths ) ;
2013-03-04 06:43:06 +01:00
2013-03-04 19:13:47 +01:00
RegisterSingleInstance ( SecurityManager ) ;
2013-03-04 06:43:06 +01:00
2013-03-04 19:13:47 +01:00
RegisterSingleInstance < IPackageManager > ( new PackageManager ( SecurityManager , networkManager , httpClient , ApplicationPaths , JsonSerializer , Logger ) ) ;
} ) ;
2013-02-27 17:46:48 +01:00
}
2013-03-03 08:27:40 +01:00
2013-02-26 04:43:04 +01:00
/// <summary>
/// Gets a list of types within an assembly
/// This will handle situations that would normally throw an exception - such as a type within the assembly that depends on some other non-existant reference
/// </summary>
/// <param name="assembly">The assembly.</param>
/// <returns>IEnumerable{Type}.</returns>
/// <exception cref="System.ArgumentNullException">assembly</exception>
protected IEnumerable < Type > GetTypes ( Assembly assembly )
{
if ( assembly = = null )
{
throw new ArgumentNullException ( "assembly" ) ;
}
try
{
return assembly . GetTypes ( ) ;
}
catch ( ReflectionTypeLoadException ex )
{
// If it fails we can still get a list of the Types it was able to resolve
return ex . Types . Where ( t = > t ! = null ) ;
}
}
/// <summary>
/// Creates an instance of type and resolves all constructor dependancies
/// </summary>
/// <param name="type">The type.</param>
/// <returns>System.Object.</returns>
public object CreateInstance ( Type type )
{
try
{
return Container . GetInstance ( type ) ;
}
catch
{
Logger . Error ( "Error creating {0}" , type . Name ) ;
throw ;
}
}
/// <summary>
/// Registers the specified obj.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj">The obj.</param>
2013-02-26 17:10:55 +01:00
/// <param name="manageLifetime">if set to <c>true</c> [manage lifetime].</param>
protected void RegisterSingleInstance < T > ( T obj , bool manageLifetime = true )
2013-02-26 04:43:04 +01:00
where T : class
{
Container . RegisterSingle ( obj ) ;
2013-02-26 17:10:55 +01:00
if ( manageLifetime )
{
var disposable = obj as IDisposable ;
if ( disposable ! = null )
{
Logger . Info ( "Registering " + disposable . GetType ( ) . Name ) ;
DisposableParts . Add ( disposable ) ;
}
}
2013-02-26 04:43:04 +01:00
}
/// <summary>
/// Registers the single instance.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="func">The func.</param>
protected void RegisterSingleInstance < T > ( Func < T > func )
where T : class
{
Container . RegisterSingle ( func ) ;
}
/// <summary>
/// Resolves this instance.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns>``0.</returns>
public T Resolve < T > ( )
{
return ( T ) Container . GetRegistration ( typeof ( T ) , true ) . GetInstance ( ) ;
}
/// <summary>
/// Resolves this instance.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns>``0.</returns>
public T TryResolve < T > ( )
{
var result = Container . GetRegistration ( typeof ( T ) , false ) ;
if ( result = = null )
{
return default ( T ) ;
}
return ( T ) result . GetInstance ( ) ;
}
/// <summary>
/// Loads the assembly.
/// </summary>
/// <param name="file">The file.</param>
/// <returns>Assembly.</returns>
protected Assembly LoadAssembly ( string file )
{
try
{
return Assembly . Load ( File . ReadAllBytes ( ( file ) ) ) ;
}
catch ( Exception ex )
{
FailedAssemblies . Add ( file ) ;
Logger . ErrorException ( "Error loading assembly {0}" , ex , file ) ;
return null ;
}
}
/// <summary>
/// Gets the exports.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="manageLiftime">if set to <c>true</c> [manage liftime].</param>
/// <returns>IEnumerable{``0}.</returns>
public IEnumerable < T > GetExports < T > ( bool manageLiftime = true )
{
var currentType = typeof ( T ) ;
Logger . Info ( "Composing instances of " + currentType . Name ) ;
2013-03-04 06:43:06 +01:00
var parts = AllConcreteTypes . AsParallel ( ) . Where ( currentType . IsAssignableFrom ) . Select ( CreateInstance ) . Cast < T > ( ) . ToArray ( ) ;
2013-02-26 04:43:04 +01:00
if ( manageLiftime )
{
DisposableParts . AddRange ( parts . OfType < IDisposable > ( ) ) ;
}
return parts ;
}
/// <summary>
/// Gets the current application version
/// </summary>
/// <value>The application version.</value>
public Version ApplicationVersion
{
get
{
return GetType ( ) . Assembly . GetName ( ) . Version ;
}
}
2013-02-27 22:07:51 +01:00
/// <summary>
/// Configures the auto run at startup.
/// </summary>
2013-03-04 06:43:06 +01:00
private void ConfigureAutoRunAtStartup ( )
2013-02-27 22:07:51 +01:00
{
}
2013-03-03 03:47:04 +01:00
/// <summary>
/// Removes the plugin.
/// </summary>
/// <param name="plugin">The plugin.</param>
public void RemovePlugin ( IPlugin plugin )
{
var list = Plugins . ToList ( ) ;
list . Remove ( plugin ) ;
Plugins = list ;
}
2013-02-26 04:43:04 +01:00
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose ( )
{
Dispose ( true ) ;
}
/// <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 )
{
2013-02-27 05:19:05 +01:00
if ( dispose )
{
var type = GetType ( ) ;
2013-02-26 17:10:55 +01:00
2013-02-27 05:19:05 +01:00
Logger . Info ( "Disposing " + type . Name ) ;
2013-02-26 17:10:55 +01:00
2013-02-27 05:19:05 +01:00
var parts = DisposableParts . Distinct ( ) . Where ( i = > i . GetType ( ) ! = type ) . ToList ( ) ;
DisposableParts . Clear ( ) ;
2013-02-26 17:10:55 +01:00
2013-02-27 05:19:05 +01:00
foreach ( var part in parts )
{
Logger . Info ( "Disposing " + part . GetType ( ) . Name ) ;
2013-02-26 04:43:04 +01:00
2013-02-27 05:19:05 +01:00
part . Dispose ( ) ;
}
}
2013-02-26 04:43:04 +01:00
}
2013-03-04 06:43:06 +01:00
public abstract void Restart ( ) ;
public abstract bool CanSelfUpdate { get ; }
public abstract Task < CheckForUpdateResult > CheckForApplicationUpdate ( CancellationToken cancellationToken , IProgress < double > progress ) ;
public abstract Task UpdateApplication ( PackageVersionInfo package , CancellationToken cancellationToken , IProgress < double > progress ) ;
public abstract void Shutdown ( ) ;
2013-02-26 04:43:04 +01:00
}
}