ApplicationHost.cs 9.2 KB

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