ApplicationHost.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using MediaBrowser.Api;
  2. using MediaBrowser.Common;
  3. using MediaBrowser.Common.Configuration;
  4. using MediaBrowser.Common.Constants;
  5. using MediaBrowser.Common.Events;
  6. using MediaBrowser.Common.Implementations;
  7. using MediaBrowser.Common.Implementations.ScheduledTasks;
  8. using MediaBrowser.Common.IO;
  9. using MediaBrowser.Common.Net;
  10. using MediaBrowser.Common.Updates;
  11. using MediaBrowser.Controller;
  12. using MediaBrowser.Controller.Configuration;
  13. using MediaBrowser.Controller.Entities;
  14. using MediaBrowser.Controller.Library;
  15. using MediaBrowser.Controller.Resolvers;
  16. using MediaBrowser.Controller.Updates;
  17. using MediaBrowser.IsoMounter;
  18. using MediaBrowser.Model.IO;
  19. using MediaBrowser.Model.MediaInfo;
  20. using MediaBrowser.Model.System;
  21. using MediaBrowser.Model.Updates;
  22. using MediaBrowser.Server.Implementations;
  23. using MediaBrowser.Server.Implementations.BdInfo;
  24. using MediaBrowser.Server.Implementations.Configuration;
  25. using MediaBrowser.Server.Implementations.HttpServer;
  26. using MediaBrowser.Server.Implementations.Library;
  27. using MediaBrowser.Server.Implementations.ServerManager;
  28. using MediaBrowser.Server.Implementations.Udp;
  29. using MediaBrowser.Server.Implementations.Updates;
  30. using MediaBrowser.Server.Implementations.WebSocket;
  31. using MediaBrowser.ServerApplication.Implementations;
  32. using MediaBrowser.WebDashboard.Api;
  33. using System;
  34. using System.Collections.Generic;
  35. using System.IO;
  36. using System.Linq;
  37. using System.Reflection;
  38. using System.Threading;
  39. using System.Threading.Tasks;
  40. namespace MediaBrowser.ServerApplication
  41. {
  42. /// <summary>
  43. /// Class CompositionRoot
  44. /// </summary>
  45. public class ApplicationHost : BaseApplicationHost<ServerApplicationPaths>, IServerApplicationHost
  46. {
  47. /// <summary>
  48. /// Gets the server kernel.
  49. /// </summary>
  50. /// <value>The server kernel.</value>
  51. protected Kernel ServerKernel { get; set; }
  52. /// <summary>
  53. /// Gets the server configuration manager.
  54. /// </summary>
  55. /// <value>The server configuration manager.</value>
  56. public IServerConfigurationManager ServerConfigurationManager
  57. {
  58. get { return (IServerConfigurationManager)ConfigurationManager; }
  59. }
  60. /// <summary>
  61. /// Gets the name of the log file prefix.
  62. /// </summary>
  63. /// <value>The name of the log file prefix.</value>
  64. protected override string LogFilePrefixName
  65. {
  66. get { return "Server"; }
  67. }
  68. /// <summary>
  69. /// Gets the configuration manager.
  70. /// </summary>
  71. /// <returns>IConfigurationManager.</returns>
  72. protected override IConfigurationManager GetConfigurationManager()
  73. {
  74. return new ServerConfigurationManager(ApplicationPaths, LogManager, XmlSerializer);
  75. }
  76. private IInstallationManager InstallationManager { get; set; }
  77. private IServerManager ServerManager { get; set; }
  78. public override async Task Init()
  79. {
  80. await base.Init().ConfigureAwait(false);
  81. await ServerKernel.Init().ConfigureAwait(false);
  82. }
  83. /// <summary>
  84. /// Registers resources that classes will depend on
  85. /// </summary>
  86. protected override async Task RegisterResources()
  87. {
  88. ServerKernel = new Kernel(this, XmlSerializer, LogManager, ServerConfigurationManager);
  89. await base.RegisterResources().ConfigureAwait(false);
  90. RegisterSingleInstance<IServerApplicationHost>(this);
  91. RegisterSingleInstance<IServerApplicationPaths>(ApplicationPaths);
  92. RegisterSingleInstance(ServerKernel);
  93. RegisterSingleInstance(ServerConfigurationManager);
  94. RegisterSingleInstance<IWebSocketServer>(() => new AlchemyServer(Logger));
  95. RegisterSingleInstance<IUdpServer>(new UdpServer(Logger), false);
  96. RegisterSingleInstance<IIsoManager>(new PismoIsoManager(Logger));
  97. RegisterSingleInstance<IBlurayExaminer>(new BdInfoExaminer());
  98. RegisterSingleInstance<IZipClient>(new DotNetZipClient());
  99. RegisterSingleInstance(ServerFactory.CreateServer(this, ProtobufSerializer, Logger, "Media Browser", "index.html"), false);
  100. ServerManager = new ServerManager(this, NetworkManager, JsonSerializer, Logger, ServerConfigurationManager, ServerKernel);
  101. RegisterSingleInstance(ServerManager);
  102. var userManager = new UserManager(ServerKernel, Logger, ServerConfigurationManager);
  103. RegisterSingleInstance<IUserManager>(userManager);
  104. RegisterSingleInstance<ILibraryManager>(new LibraryManager(ServerKernel, Logger, TaskManager, userManager, ServerConfigurationManager));
  105. InstallationManager = new InstallationManager(HttpClient, PackageManager, JsonSerializer, Logger, this);
  106. RegisterSingleInstance(InstallationManager);
  107. }
  108. /// <summary>
  109. /// Finds the parts.
  110. /// </summary>
  111. protected override void FindParts()
  112. {
  113. base.FindParts();
  114. Resolve<IHttpServer>().Init(GetExports<IRestfulService>(false));
  115. Resolve<IServerManager>().AddWebSocketListeners(GetExports<IWebSocketListener>(false));
  116. Resolve<IServerManager>().Start();
  117. Resolve<ILibraryManager>().AddParts(GetExports<IResolverIgnoreRule>(), GetExports<IVirtualFolderCreator>(), GetExports<IItemResolver>(), GetExports<IIntroProvider>());
  118. }
  119. /// <summary>
  120. /// Restarts this instance.
  121. /// </summary>
  122. public override void Restart()
  123. {
  124. App.Instance.Restart();
  125. }
  126. /// <summary>
  127. /// Gets or sets a value indicating whether this instance can self update.
  128. /// </summary>
  129. /// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
  130. public override bool CanSelfUpdate
  131. {
  132. get { return ConfigurationManager.CommonConfiguration.EnableAutoUpdate; }
  133. }
  134. /// <summary>
  135. /// Checks for update.
  136. /// </summary>
  137. /// <param name="cancellationToken">The cancellation token.</param>
  138. /// <param name="progress">The progress.</param>
  139. /// <returns>Task{CheckForUpdateResult}.</returns>
  140. public async override Task<CheckForUpdateResult> CheckForApplicationUpdate(CancellationToken cancellationToken, IProgress<double> progress)
  141. {
  142. var pkgManager = Resolve<IPackageManager>();
  143. var availablePackages = await pkgManager.GetAvailablePackages(CancellationToken.None).ConfigureAwait(false);
  144. var version = Resolve<IInstallationManager>().GetLatestCompatibleVersion(availablePackages, Constants.MBServerPkgName, ConfigurationManager.CommonConfiguration.SystemUpdateLevel);
  145. return version != null ? new CheckForUpdateResult { AvailableVersion = version.version, IsUpdateAvailable = version.version > ApplicationVersion, Package = version } :
  146. new CheckForUpdateResult { AvailableVersion = ApplicationVersion, IsUpdateAvailable = false };
  147. }
  148. /// <summary>
  149. /// Gets the composable part assemblies.
  150. /// </summary>
  151. /// <returns>IEnumerable{Assembly}.</returns>
  152. protected override IEnumerable<Assembly> GetComposablePartAssemblies()
  153. {
  154. // Gets all plugin assemblies by first reading all bytes of the .dll and calling Assembly.Load against that
  155. // This will prevent the .dll file from getting locked, and allow us to replace it when needed
  156. foreach (var pluginAssembly in Directory
  157. .EnumerateFiles(ApplicationPaths.PluginsPath, "*.dll", SearchOption.TopDirectoryOnly)
  158. .Select(LoadAssembly).Where(a => a != null))
  159. {
  160. yield return pluginAssembly;
  161. }
  162. // Include composable parts in the Api assembly
  163. yield return typeof(ApiService).Assembly;
  164. // Include composable parts in the Dashboard assembly
  165. yield return typeof(DashboardInfo).Assembly;
  166. // Include composable parts in the Model assembly
  167. yield return typeof(SystemInfo).Assembly;
  168. // Include composable parts in the Common assembly
  169. yield return typeof(IApplicationHost).Assembly;
  170. // Include composable parts in the Controller assembly
  171. yield return typeof(Kernel).Assembly;
  172. // Common implementations
  173. yield return typeof(TaskManager).Assembly;
  174. // Server implementations
  175. yield return typeof(ServerApplicationPaths).Assembly;
  176. // Include composable parts in the running assembly
  177. yield return GetType().Assembly;
  178. }
  179. /// <summary>
  180. /// Gets the system status.
  181. /// </summary>
  182. /// <returns>SystemInfo.</returns>
  183. public virtual SystemInfo GetSystemInfo()
  184. {
  185. return new SystemInfo
  186. {
  187. HasPendingRestart = HasPendingRestart,
  188. Version = ApplicationVersion.ToString(),
  189. IsNetworkDeployed = CanSelfUpdate,
  190. WebSocketPortNumber = ServerManager.WebSocketPortNumber,
  191. SupportsNativeWebSocket = ServerManager.SupportsNativeWebSocket,
  192. FailedPluginAssemblies = FailedAssemblies.ToArray(),
  193. InProgressInstallations = InstallationManager.CurrentInstallations.Select(i => i.Item1).ToArray(),
  194. CompletedInstallations = InstallationManager.CompletedInstallations.ToArray()
  195. };
  196. }
  197. /// <summary>
  198. /// Shuts down.
  199. /// </summary>
  200. public override void Shutdown()
  201. {
  202. App.Instance.Dispatcher.Invoke(App.Instance.Shutdown);
  203. }
  204. }
  205. }