ApplicationHost.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. RegisterResources(taskManager, networkManager, serverManager);
  79. <<<<<<< HEAD
  80. FindParts();
  81. =======
  82. RegisterResources(taskManager, httpServer, networkManager, serverManager, PackageManager);
  83. FindParts(taskManager, httpServer);
  84. >>>>>>> c9f48fe0d0d5cf4aec62df1d1e97f629967aff6f
  85. }
  86. /// <summary>
  87. /// Gets the application paths.
  88. /// </summary>
  89. /// <returns>IApplicationPaths.</returns>
  90. protected override IApplicationPaths GetApplicationPaths()
  91. {
  92. return new ServerApplicationPaths();
  93. }
  94. /// <summary>
  95. /// Gets the log manager.
  96. /// </summary>
  97. /// <returns>ILogManager.</returns>
  98. protected override ILogManager GetLogManager()
  99. {
  100. return new NlogManager(ApplicationPaths.LogDirectoryPath, "Server");
  101. }
  102. /// <summary>
  103. /// Registers resources that classes will depend on
  104. /// </summary>
  105. <<<<<<< HEAD
  106. protected override void RegisterResources(ITaskManager taskManager, INetworkManager networkManager, IServerManager serverManager)
  107. =======
  108. private void RegisterResources(ITaskManager taskManager, IHttpServer httpServer, INetworkManager networkManager, IServerManager serverManager, IPackageManager packageManager)
  109. >>>>>>> c9f48fe0d0d5cf4aec62df1d1e97f629967aff6f
  110. {
  111. base.RegisterResources(taskManager, networkManager, serverManager);
  112. RegisterSingleInstance<IKernel>(Kernel);
  113. RegisterSingleInstance(Kernel);
  114. RegisterSingleInstance<IApplicationHost>(this);
  115. RegisterSingleInstance(ServerApplicationPaths);
  116. RegisterSingleInstance<IIsoManager>(new PismoIsoManager(Logger));
  117. RegisterSingleInstance<IBlurayExaminer>(new BdInfoExaminer());
  118. RegisterSingleInstance<IHttpClient>(new HttpClientManager(ApplicationPaths, Logger));
  119. RegisterSingleInstance<IZipClient>(new DotNetZipClient());
  120. RegisterSingleInstance(_jsonSerializer);
  121. RegisterSingleInstance(_xmlSerializer);
  122. <<<<<<< HEAD
  123. RegisterSingleInstance(ServerFactory.CreateServer(this, ProtobufSerializer, Logger, "Media Browser", "index.html"), false);
  124. =======
  125. RegisterSingleInstance(ProtobufSerializer);
  126. RegisterSingleInstance<IUdpServer>(new UdpServer(Logger), false);
  127. RegisterSingleInstance(httpServer, false);
  128. RegisterSingleInstance(networkManager);
  129. RegisterSingleInstance(serverManager);
  130. RegisterSingleInstance(packageManager);
  131. }
  132. /// <summary>
  133. /// Finds the parts.
  134. /// </summary>
  135. private void FindParts(ITaskManager taskManager, IHttpServer httpServer)
  136. {
  137. taskManager.AddTasks(GetExports<IScheduledTask>(false));
  138. httpServer.Init(GetExports<IRestfulService>(false));
  139. >>>>>>> c9f48fe0d0d5cf4aec62df1d1e97f629967aff6f
  140. }
  141. /// <summary>
  142. /// Restarts this instance.
  143. /// </summary>
  144. public void Restart()
  145. {
  146. App.Instance.Restart();
  147. }
  148. /// <summary>
  149. /// Gets or sets a value indicating whether this instance can self update.
  150. /// </summary>
  151. /// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
  152. public bool CanSelfUpdate
  153. {
  154. get { return ClickOnceHelper.IsNetworkDeployed; }
  155. }
  156. /// <summary>
  157. /// Checks for update.
  158. /// </summary>
  159. /// <param name="cancellationToken">The cancellation token.</param>
  160. /// <param name="progress">The progress.</param>
  161. /// <returns>Task{CheckForUpdateResult}.</returns>
  162. public Task<CheckForUpdateResult> CheckForApplicationUpdate(CancellationToken cancellationToken, IProgress<double> progress)
  163. {
  164. return new ApplicationUpdateCheck().CheckForApplicationUpdate(cancellationToken, progress);
  165. }
  166. /// <summary>
  167. /// Updates the application.
  168. /// </summary>
  169. /// <param name="cancellationToken">The cancellation token.</param>
  170. /// <param name="progress">The progress.</param>
  171. /// <returns>Task.</returns>
  172. public Task UpdateApplication(CancellationToken cancellationToken, IProgress<double> progress)
  173. {
  174. return new ApplicationUpdater().UpdateApplication(cancellationToken, progress);
  175. }
  176. /// <summary>
  177. /// Gets the composable part assemblies.
  178. /// </summary>
  179. /// <returns>IEnumerable{Assembly}.</returns>
  180. protected override IEnumerable<Assembly> GetComposablePartAssemblies()
  181. {
  182. // Gets all plugin assemblies by first reading all bytes of the .dll and calling Assembly.Load against that
  183. // This will prevent the .dll file from getting locked, and allow us to replace it when needed
  184. foreach (var pluginAssembly in Directory
  185. .EnumerateFiles(ApplicationPaths.PluginsPath, "*.dll", SearchOption.TopDirectoryOnly)
  186. .Select(LoadAssembly).Where(a => a != null))
  187. {
  188. yield return pluginAssembly;
  189. }
  190. var runningDirectory = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
  191. var corePluginDirectory = Path.Combine(runningDirectory, "CorePlugins");
  192. // This will prevent the .dll file from getting locked, and allow us to replace it when needed
  193. foreach (var pluginAssembly in Directory
  194. .EnumerateFiles(corePluginDirectory, "*.dll", SearchOption.TopDirectoryOnly)
  195. .Select(LoadAssembly).Where(a => a != null))
  196. {
  197. yield return pluginAssembly;
  198. }
  199. // Include composable parts in the Model assembly
  200. yield return typeof(SystemInfo).Assembly;
  201. // Include composable parts in the Common assembly
  202. yield return typeof(IKernel).Assembly;
  203. // Include composable parts in the Controller assembly
  204. yield return typeof(Kernel).Assembly;
  205. // Common implementations
  206. yield return typeof(TaskManager).Assembly;
  207. // Server implementations
  208. yield return typeof(ServerApplicationPaths).Assembly;
  209. // Include composable parts in the running assembly
  210. yield return GetType().Assembly;
  211. }
  212. /// <summary>
  213. /// Shuts down.
  214. /// </summary>
  215. public void Shutdown()
  216. {
  217. App.Instance.Shutdown();
  218. }
  219. }
  220. }