2
0

ApplicationHost.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.IO;
  13. using MediaBrowser.Common.Kernel;
  14. using MediaBrowser.Common.Net;
  15. using MediaBrowser.Common.ScheduledTasks;
  16. using MediaBrowser.Controller;
  17. using MediaBrowser.Controller.Library;
  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.Server.Implementations.Library;
  27. using MediaBrowser.ServerApplication.Implementations;
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Diagnostics;
  31. using System.IO;
  32. using System.Linq;
  33. using System.Reflection;
  34. using System.Threading;
  35. using System.Threading.Tasks;
  36. namespace MediaBrowser.ServerApplication
  37. {
  38. /// <summary>
  39. /// Class CompositionRoot
  40. /// </summary>
  41. public class ApplicationHost : BaseApplicationHost, IApplicationHost
  42. {
  43. /// <summary>
  44. /// Gets or sets the kernel.
  45. /// </summary>
  46. /// <value>The kernel.</value>
  47. internal Kernel Kernel { get; private set; }
  48. /// <summary>
  49. /// The json serializer
  50. /// </summary>
  51. private readonly IJsonSerializer _jsonSerializer = new JsonSerializer();
  52. /// <summary>
  53. /// The _XML serializer
  54. /// </summary>
  55. private readonly IXmlSerializer _xmlSerializer = new XmlSerializer();
  56. /// <summary>
  57. /// Gets the server application paths.
  58. /// </summary>
  59. /// <value>The server application paths.</value>
  60. protected IServerApplicationPaths ServerApplicationPaths
  61. {
  62. get { return (IServerApplicationPaths) ApplicationPaths; }
  63. }
  64. /// <summary>
  65. /// Initializes a new instance of the <see cref="ApplicationHost" /> class.
  66. /// </summary>
  67. /// <param name="logger">The logger.</param>
  68. public ApplicationHost()
  69. : base()
  70. {
  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. RegisterResources(taskManager, networkManager, serverManager);
  78. FindParts();
  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. protected override void RegisterResources(ITaskManager taskManager, INetworkManager networkManager, IServerManager serverManager)
  100. {
  101. base.RegisterResources(taskManager, networkManager, serverManager);
  102. RegisterSingleInstance<IKernel>(Kernel);
  103. RegisterSingleInstance(Kernel);
  104. RegisterSingleInstance<IApplicationHost>(this);
  105. RegisterSingleInstance<IUserManager>(new UserManager(Kernel, Logger));
  106. RegisterSingleInstance(ServerApplicationPaths);
  107. RegisterSingleInstance<IIsoManager>(new PismoIsoManager(Logger));
  108. RegisterSingleInstance<IBlurayExaminer>(new BdInfoExaminer());
  109. RegisterSingleInstance<IZipClient>(new DotNetZipClient());
  110. RegisterSingleInstance(_jsonSerializer);
  111. RegisterSingleInstance(_xmlSerializer);
  112. RegisterSingleInstance(ServerFactory.CreateServer(this, ProtobufSerializer, Logger, "Media Browser", "index.html"), false);
  113. }
  114. /// <summary>
  115. /// Restarts this instance.
  116. /// </summary>
  117. public void Restart()
  118. {
  119. App.Instance.Restart();
  120. }
  121. /// <summary>
  122. /// Gets or sets a value indicating whether this instance can self update.
  123. /// </summary>
  124. /// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
  125. public bool CanSelfUpdate
  126. {
  127. get { return ClickOnceHelper.IsNetworkDeployed; }
  128. }
  129. /// <summary>
  130. /// Checks for update.
  131. /// </summary>
  132. /// <param name="cancellationToken">The cancellation token.</param>
  133. /// <param name="progress">The progress.</param>
  134. /// <returns>Task{CheckForUpdateResult}.</returns>
  135. public Task<CheckForUpdateResult> CheckForApplicationUpdate(CancellationToken cancellationToken, IProgress<double> progress)
  136. {
  137. // Get package manager using Resolve<IPackageManager>()
  138. return new ApplicationUpdateCheck().CheckForApplicationUpdate(cancellationToken, progress);
  139. }
  140. /// <summary>
  141. /// Updates the application.
  142. /// </summary>
  143. /// <param name="cancellationToken">The cancellation token.</param>
  144. /// <param name="progress">The progress.</param>
  145. /// <returns>Task.</returns>
  146. public Task UpdateApplication(CancellationToken cancellationToken, IProgress<double> progress)
  147. {
  148. return new ApplicationUpdater().UpdateApplication(cancellationToken, progress);
  149. }
  150. /// <summary>
  151. /// Gets the composable part assemblies.
  152. /// </summary>
  153. /// <returns>IEnumerable{Assembly}.</returns>
  154. protected override IEnumerable<Assembly> GetComposablePartAssemblies()
  155. {
  156. // Gets all plugin assemblies by first reading all bytes of the .dll and calling Assembly.Load against that
  157. // This will prevent the .dll file from getting locked, and allow us to replace it when needed
  158. foreach (var pluginAssembly in Directory
  159. .EnumerateFiles(ApplicationPaths.PluginsPath, "*.dll", SearchOption.TopDirectoryOnly)
  160. .Select(LoadAssembly).Where(a => a != null))
  161. {
  162. yield return pluginAssembly;
  163. }
  164. var runningDirectory = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
  165. var corePluginDirectory = Path.Combine(runningDirectory, "CorePlugins");
  166. // This will prevent the .dll file from getting locked, and allow us to replace it when needed
  167. foreach (var pluginAssembly in Directory
  168. .EnumerateFiles(corePluginDirectory, "*.dll", SearchOption.TopDirectoryOnly)
  169. .Select(LoadAssembly).Where(a => a != null))
  170. {
  171. yield return pluginAssembly;
  172. }
  173. // Include composable parts in the Model assembly
  174. yield return typeof(SystemInfo).Assembly;
  175. // Include composable parts in the Common assembly
  176. yield return typeof(IKernel).Assembly;
  177. // Include composable parts in the Controller assembly
  178. yield return typeof(Kernel).Assembly;
  179. // Common implementations
  180. yield return typeof(TaskManager).Assembly;
  181. // Server implementations
  182. yield return typeof(ServerApplicationPaths).Assembly;
  183. // Include composable parts in the running assembly
  184. yield return GetType().Assembly;
  185. }
  186. /// <summary>
  187. /// Shuts down.
  188. /// </summary>
  189. public void Shutdown()
  190. {
  191. App.Instance.Shutdown();
  192. }
  193. }
  194. }