ApplicationHost.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using BDInfo;
  2. using MediaBrowser.ClickOnce;
  3. using MediaBrowser.Common.Implementations;
  4. using MediaBrowser.Common.Implementations.ScheduledTasks;
  5. using MediaBrowser.Common.Implementations.Serialization;
  6. using MediaBrowser.Common.IO;
  7. using MediaBrowser.Common.Implementations.Server;
  8. using MediaBrowser.Common.Kernel;
  9. using MediaBrowser.Common.Net;
  10. using MediaBrowser.Common.ScheduledTasks;
  11. using MediaBrowser.Controller;
  12. using MediaBrowser.IsoMounter;
  13. using MediaBrowser.Logging.Nlog;
  14. using MediaBrowser.Model.IO;
  15. using MediaBrowser.Model.MediaInfo;
  16. using MediaBrowser.Model.Serialization;
  17. using MediaBrowser.Model.System;
  18. using MediaBrowser.Model.Updates;
  19. using MediaBrowser.Networking.HttpManager;
  20. using MediaBrowser.Networking.HttpServer;
  21. using MediaBrowser.Networking.Management;
  22. using MediaBrowser.Networking.Udp;
  23. using MediaBrowser.Networking.WebSocket;
  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 log file path.
  43. /// </summary>
  44. /// <value>The log file path.</value>
  45. public string LogFilePath { get; private set; }
  46. /// <summary>
  47. /// Gets or sets the kernel.
  48. /// </summary>
  49. /// <value>The kernel.</value>
  50. internal Kernel Kernel { get; private set; }
  51. /// <summary>
  52. /// The json serializer
  53. /// </summary>
  54. private readonly IJsonSerializer _jsonSerializer = new JsonSerializer();
  55. /// <summary>
  56. /// The _XML serializer
  57. /// </summary>
  58. private readonly IXmlSerializer _xmlSerializer = new XmlSerializer();
  59. /// <summary>
  60. /// The _application paths
  61. /// </summary>
  62. private readonly IServerApplicationPaths _applicationPaths = new ServerApplicationPaths();
  63. /// <summary>
  64. /// Gets a value indicating whether this instance is first run.
  65. /// </summary>
  66. /// <value><c>true</c> if this instance is first run; otherwise, <c>false</c>.</value>
  67. public bool IsFirstRun { get; private set; }
  68. /// <summary>
  69. /// Initializes a new instance of the <see cref="ApplicationHost" /> class.
  70. /// </summary>
  71. /// <param name="logger">The logger.</param>
  72. public ApplicationHost()
  73. : base()
  74. {
  75. IsFirstRun = !File.Exists(_applicationPaths.SystemConfigurationFilePath);
  76. Logger = new NLogger("App");
  77. DiscoverTypes();
  78. Kernel = new Kernel(this, _applicationPaths, _xmlSerializer, Logger);
  79. var networkManager = new NetworkManager();
  80. var serverManager = new ServerManager(this, Kernel, networkManager, _jsonSerializer, Logger);
  81. var taskManager = new TaskManager(_applicationPaths, _jsonSerializer, Logger, serverManager);
  82. ReloadLogger();
  83. Logger.Info("Version {0} initializing", ApplicationVersion);
  84. var httpServer = ServerFactory.CreateServer(this, ProtobufSerializer, Logger, "Media Browser", "index.html");
  85. RegisterResources(taskManager, httpServer, networkManager, serverManager);
  86. FindParts(taskManager, httpServer);
  87. }
  88. /// <summary>
  89. /// Registers resources that classes will depend on
  90. /// </summary>
  91. private void RegisterResources(ITaskManager taskManager, IHttpServer httpServer, INetworkManager networkManager, IServerManager serverManager)
  92. {
  93. RegisterSingleInstance<IKernel>(Kernel);
  94. RegisterSingleInstance(Kernel);
  95. RegisterSingleInstance<IApplicationHost>(this);
  96. RegisterSingleInstance(Logger);
  97. RegisterSingleInstance(_applicationPaths);
  98. RegisterSingleInstance<IApplicationPaths>(_applicationPaths);
  99. RegisterSingleInstance(taskManager);
  100. RegisterSingleInstance<IIsoManager>(new PismoIsoManager(Logger));
  101. RegisterSingleInstance<IBlurayExaminer>(new BdInfoExaminer());
  102. RegisterSingleInstance<IHttpClient>(new HttpManager(_applicationPaths, Logger));
  103. RegisterSingleInstance<IZipClient>(new DotNetZipClient());
  104. RegisterSingleInstance<IWebSocketServer>(() => new AlchemyServer(Logger));
  105. RegisterSingleInstance(_jsonSerializer);
  106. RegisterSingleInstance(_xmlSerializer);
  107. RegisterSingleInstance(ProtobufSerializer);
  108. RegisterSingleInstance<IUdpServer>(new UdpServer(Logger), false);
  109. RegisterSingleInstance(httpServer, false);
  110. RegisterSingleInstance(networkManager);
  111. RegisterSingleInstance(serverManager);
  112. }
  113. /// <summary>
  114. /// Finds the parts.
  115. /// </summary>
  116. private void FindParts(ITaskManager taskManager, IHttpServer httpServer)
  117. {
  118. taskManager.AddTasks(GetExports<IScheduledTask>(false));
  119. httpServer.Init(GetExports<IRestfulService>(false));
  120. }
  121. /// <summary>
  122. /// Restarts this instance.
  123. /// </summary>
  124. /// <exception cref="System.NotImplementedException"></exception>
  125. public void Restart()
  126. {
  127. App.Instance.Restart();
  128. }
  129. /// <summary>
  130. /// Reloads the logger.
  131. /// </summary>
  132. /// <exception cref="System.NotImplementedException"></exception>
  133. public void ReloadLogger()
  134. {
  135. LogFilePath = Path.Combine(_applicationPaths.LogDirectoryPath, "Server-" + DateTime.Now.Ticks + ".log");
  136. NlogManager.AddFileTarget(LogFilePath, Kernel.Configuration.EnableDebugLevelLogging);
  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. }