ApplicationHost.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using BDInfo;
  2. using MediaBrowser.ClickOnce;
  3. using MediaBrowser.Common.Implementations;
  4. using MediaBrowser.Common.Implementations.HttpClientManager;
  5. using MediaBrowser.Common.Implementations.HttpServer;
  6. using MediaBrowser.Common.Implementations.Logging;
  7. using MediaBrowser.Common.Implementations.NetworkManagement;
  8. using MediaBrowser.Common.Implementations.ScheduledTasks;
  9. using MediaBrowser.Common.Implementations.Serialization;
  10. using MediaBrowser.Common.Implementations.ServerManager;
  11. using MediaBrowser.Common.Implementations.Udp;
  12. using MediaBrowser.Common.IO;
  13. using MediaBrowser.Common.Kernel;
  14. using MediaBrowser.Common.Net;
  15. using MediaBrowser.Common.ScheduledTasks;
  16. using MediaBrowser.Controller;
  17. using MediaBrowser.IsoMounter;
  18. using MediaBrowser.Model.IO;
  19. using MediaBrowser.Model.Logging;
  20. using MediaBrowser.Model.MediaInfo;
  21. using MediaBrowser.Model.Serialization;
  22. using MediaBrowser.Model.System;
  23. using MediaBrowser.Model.Updates;
  24. using MediaBrowser.Server.Implementations;
  25. using MediaBrowser.ServerApplication.Implementations;
  26. using System;
  27. using System.Collections.Generic;
  28. using System.Diagnostics;
  29. using System.IO;
  30. using System.Linq;
  31. using System.Reflection;
  32. using System.Threading;
  33. using System.Threading.Tasks;
  34. namespace MediaBrowser.ServerApplication
  35. {
  36. /// <summary>
  37. /// Class CompositionRoot
  38. /// </summary>
  39. public class ApplicationHost : BaseApplicationHost, IApplicationHost
  40. {
  41. /// <summary>
  42. /// Gets or sets the kernel.
  43. /// </summary>
  44. /// <value>The kernel.</value>
  45. internal Kernel Kernel { get; private set; }
  46. /// <summary>
  47. /// The json serializer
  48. /// </summary>
  49. private readonly IJsonSerializer _jsonSerializer = new JsonSerializer();
  50. /// <summary>
  51. /// The _XML serializer
  52. /// </summary>
  53. private readonly IXmlSerializer _xmlSerializer = new XmlSerializer();
  54. /// <summary>
  55. /// Gets the server application paths.
  56. /// </summary>
  57. /// <value>The server application paths.</value>
  58. protected IServerApplicationPaths ServerApplicationPaths
  59. {
  60. get { return (IServerApplicationPaths) ApplicationPaths; }
  61. }
  62. /// <summary>
  63. /// Initializes a new instance of the <see cref="ApplicationHost" /> class.
  64. /// </summary>
  65. /// <param name="logger">The logger.</param>
  66. public ApplicationHost()
  67. : base()
  68. {
  69. Kernel = new Kernel(this, ServerApplicationPaths, _xmlSerializer, Logger);
  70. var networkManager = new NetworkManager();
  71. var serverManager = new ServerManager(this, Kernel, networkManager, _jsonSerializer, Logger);
  72. var taskManager = new TaskManager(ApplicationPaths, _jsonSerializer, Logger, serverManager);
  73. LogManager.ReloadLogger(Kernel.Configuration.EnableDebugLevelLogging ? LogSeverity.Debug : LogSeverity.Info);
  74. Logger.Info("Version {0} initializing", ApplicationVersion);
  75. RegisterResources(taskManager, networkManager, serverManager);
  76. FindParts();
  77. }
  78. /// <summary>
  79. /// Gets the application paths.
  80. /// </summary>
  81. /// <returns>IApplicationPaths.</returns>
  82. protected override IApplicationPaths GetApplicationPaths()
  83. {
  84. return new ServerApplicationPaths();
  85. }
  86. /// <summary>
  87. /// Gets the log manager.
  88. /// </summary>
  89. /// <returns>ILogManager.</returns>
  90. protected override ILogManager GetLogManager()
  91. {
  92. return new NlogManager(ApplicationPaths.LogDirectoryPath, "Server");
  93. }
  94. /// <summary>
  95. /// Registers resources that classes will depend on
  96. /// </summary>
  97. protected override void RegisterResources(ITaskManager taskManager, INetworkManager networkManager, IServerManager serverManager)
  98. {
  99. base.RegisterResources(taskManager, networkManager, serverManager);
  100. RegisterSingleInstance<IKernel>(Kernel);
  101. RegisterSingleInstance(Kernel);
  102. RegisterSingleInstance<IApplicationHost>(this);
  103. RegisterSingleInstance(ServerApplicationPaths);
  104. RegisterSingleInstance<IIsoManager>(new PismoIsoManager(Logger));
  105. RegisterSingleInstance<IBlurayExaminer>(new BdInfoExaminer());
  106. RegisterSingleInstance<IHttpClient>(new HttpClientManager(ApplicationPaths, Logger));
  107. RegisterSingleInstance<IZipClient>(new DotNetZipClient());
  108. RegisterSingleInstance(_jsonSerializer);
  109. RegisterSingleInstance(_xmlSerializer);
  110. RegisterSingleInstance(ServerFactory.CreateServer(this, ProtobufSerializer, Logger, "Media Browser", "index.html"), false);
  111. RegisterSingleInstance(ProtobufSerializer);
  112. RegisterSingleInstance<IUdpServer>(new UdpServer(Logger), false);
  113. RegisterSingleInstance(networkManager);
  114. RegisterSingleInstance(serverManager);
  115. }
  116. /// <summary>
  117. /// Finds the parts.
  118. /// </summary>
  119. protected override void FindParts()
  120. {
  121. Resolve<ITaskManager>().AddTasks(GetExports<IScheduledTask>(false));
  122. Resolve<IHttpServer>().Init(GetExports<IRestfulService>(false));
  123. Resolve<IServerManager>().AddWebSocketListeners(GetExports<IWebSocketListener>(false));
  124. }
  125. /// <summary>
  126. /// Restarts this instance.
  127. /// </summary>
  128. public void Restart()
  129. {
  130. App.Instance.Restart();
  131. }
  132. /// <summary>
  133. /// Gets or sets a value indicating whether this instance can self update.
  134. /// </summary>
  135. /// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
  136. public bool CanSelfUpdate
  137. {
  138. get { return ClickOnceHelper.IsNetworkDeployed; }
  139. }
  140. /// <summary>
  141. /// Checks for update.
  142. /// </summary>
  143. /// <param name="cancellationToken">The cancellation token.</param>
  144. /// <param name="progress">The progress.</param>
  145. /// <returns>Task{CheckForUpdateResult}.</returns>
  146. public Task<CheckForUpdateResult> CheckForApplicationUpdate(CancellationToken cancellationToken, IProgress<double> progress)
  147. {
  148. // Get package manager using Resolve<IPackageManager>()
  149. return new ApplicationUpdateCheck().CheckForApplicationUpdate(cancellationToken, progress);
  150. }
  151. /// <summary>
  152. /// Updates the application.
  153. /// </summary>
  154. /// <param name="cancellationToken">The cancellation token.</param>
  155. /// <param name="progress">The progress.</param>
  156. /// <returns>Task.</returns>
  157. public Task UpdateApplication(CancellationToken cancellationToken, IProgress<double> progress)
  158. {
  159. return new ApplicationUpdater().UpdateApplication(cancellationToken, progress);
  160. }
  161. /// <summary>
  162. /// Gets the composable part assemblies.
  163. /// </summary>
  164. /// <returns>IEnumerable{Assembly}.</returns>
  165. protected override IEnumerable<Assembly> GetComposablePartAssemblies()
  166. {
  167. // Gets all plugin assemblies by first reading all bytes of the .dll and calling Assembly.Load against that
  168. // This will prevent the .dll file from getting locked, and allow us to replace it when needed
  169. foreach (var pluginAssembly in Directory
  170. .EnumerateFiles(ApplicationPaths.PluginsPath, "*.dll", SearchOption.TopDirectoryOnly)
  171. .Select(LoadAssembly).Where(a => a != null))
  172. {
  173. yield return pluginAssembly;
  174. }
  175. var runningDirectory = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
  176. var corePluginDirectory = Path.Combine(runningDirectory, "CorePlugins");
  177. // This will prevent the .dll file from getting locked, and allow us to replace it when needed
  178. foreach (var pluginAssembly in Directory
  179. .EnumerateFiles(corePluginDirectory, "*.dll", SearchOption.TopDirectoryOnly)
  180. .Select(LoadAssembly).Where(a => a != null))
  181. {
  182. yield return pluginAssembly;
  183. }
  184. // Include composable parts in the Model assembly
  185. yield return typeof(SystemInfo).Assembly;
  186. // Include composable parts in the Common assembly
  187. yield return typeof(IKernel).Assembly;
  188. // Include composable parts in the Controller assembly
  189. yield return typeof(Kernel).Assembly;
  190. // Common implementations
  191. yield return typeof(TaskManager).Assembly;
  192. // Server implementations
  193. yield return typeof(ServerApplicationPaths).Assembly;
  194. // Include composable parts in the running assembly
  195. yield return GetType().Assembly;
  196. }
  197. /// <summary>
  198. /// Shuts down.
  199. /// </summary>
  200. public void Shutdown()
  201. {
  202. App.Instance.Shutdown();
  203. }
  204. }
  205. }