ApplicationHost.cs 9.3 KB

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