ApplicationHost.cs 9.2 KB

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