UIKernel.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System.Net;
  2. using System.Net.Cache;
  3. using System.Net.Http;
  4. using MediaBrowser.ApiInteraction;
  5. using MediaBrowser.Common.Kernel;
  6. using MediaBrowser.Common.Logging;
  7. using MediaBrowser.Model.Connectivity;
  8. using MediaBrowser.Model.Net;
  9. using MediaBrowser.UI.Configuration;
  10. using MediaBrowser.UI.Playback;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.ComponentModel.Composition;
  14. using System.Diagnostics;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Reflection;
  18. using System.Threading.Tasks;
  19. namespace MediaBrowser.UI.Controller
  20. {
  21. /// <summary>
  22. /// This controls application logic as well as server interaction within the UI.
  23. /// </summary>
  24. public class UIKernel : BaseKernel<UIApplicationConfiguration, UIApplicationPaths>
  25. {
  26. /// <summary>
  27. /// Gets the instance.
  28. /// </summary>
  29. /// <value>The instance.</value>
  30. public static UIKernel Instance { get; private set; }
  31. /// <summary>
  32. /// Gets the API client.
  33. /// </summary>
  34. /// <value>The API client.</value>
  35. public ApiClient ApiClient { get; private set; }
  36. /// <summary>
  37. /// Gets the playback manager.
  38. /// </summary>
  39. /// <value>The playback manager.</value>
  40. public PlaybackManager PlaybackManager { get; private set; }
  41. /// <summary>
  42. /// Initializes a new instance of the <see cref="UIKernel" /> class.
  43. /// </summary>
  44. public UIKernel()
  45. : base()
  46. {
  47. Instance = this;
  48. }
  49. /// <summary>
  50. /// Gets the media players.
  51. /// </summary>
  52. /// <value>The media players.</value>
  53. [ImportMany(typeof(BaseMediaPlayer))]
  54. public IEnumerable<BaseMediaPlayer> MediaPlayers { get; private set; }
  55. /// <summary>
  56. /// Gets the list of currently loaded themes
  57. /// </summary>
  58. /// <value>The themes.</value>
  59. [ImportMany(typeof(BaseTheme))]
  60. public IEnumerable<BaseTheme> Themes { get; private set; }
  61. /// <summary>
  62. /// Gets the kernel context.
  63. /// </summary>
  64. /// <value>The kernel context.</value>
  65. public override KernelContext KernelContext
  66. {
  67. get { return KernelContext.Ui; }
  68. }
  69. /// <summary>
  70. /// Gets the UDP server port number.
  71. /// </summary>
  72. /// <value>The UDP server port number.</value>
  73. public override int UdpServerPortNumber
  74. {
  75. get { return 7360; }
  76. }
  77. /// <summary>
  78. /// Give the UI a different url prefix so that they can share the same port, in case they are installed on the same machine.
  79. /// </summary>
  80. /// <value>The HTTP server URL prefix.</value>
  81. public override string HttpServerUrlPrefix
  82. {
  83. get
  84. {
  85. return "http://+:" + Configuration.HttpServerPortNumber + "/mediabrowserui/";
  86. }
  87. }
  88. /// <summary>
  89. /// Reload api client and update plugins after loading configuration
  90. /// </summary>
  91. /// <returns>Task.</returns>
  92. protected override async Task OnConfigurationLoaded()
  93. {
  94. ReloadApiClient();
  95. try
  96. {
  97. await new PluginUpdater().UpdatePlugins().ConfigureAwait(false);
  98. }
  99. catch (HttpException ex)
  100. {
  101. Logger.ErrorException("Error updating plugins from the server", ex);
  102. }
  103. }
  104. /// <summary>
  105. /// Disposes the current ApiClient and creates a new one
  106. /// </summary>
  107. private void ReloadApiClient()
  108. {
  109. DisposeApiClient();
  110. var logger = LogManager.GetLogger("ApiClient");
  111. ApiClient = new ApiClient(logger, new AsyncHttpClient(new WebRequestHandler
  112. {
  113. AutomaticDecompression = DecompressionMethods.Deflate,
  114. CachePolicy = new RequestCachePolicy(RequestCacheLevel.Revalidate)
  115. }))
  116. {
  117. ServerHostName = Configuration.ServerHostName,
  118. ServerApiPort = Configuration.ServerApiPort,
  119. ClientType = ClientType.Pc,
  120. DeviceName = Environment.MachineName,
  121. SerializationFormat = SerializationFormats.Json
  122. };
  123. }
  124. /// <summary>
  125. /// Reloads the internal.
  126. /// </summary>
  127. /// <returns>Task.</returns>
  128. protected override Task ReloadInternal()
  129. {
  130. PlaybackManager = new PlaybackManager(this);
  131. return base.ReloadInternal();
  132. }
  133. /// <summary>
  134. /// Gets the composable part assemblies.
  135. /// </summary>
  136. /// <returns>IEnumerable{Assembly}.</returns>
  137. protected override IEnumerable<Assembly> GetComposablePartAssemblies()
  138. {
  139. var runningDirectory = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
  140. return base.GetComposablePartAssemblies().Concat(new[] {
  141. Assembly.Load(File.ReadAllBytes(Path.Combine(runningDirectory, "MediaBrowser.Plugins.DefaultTheme.dll")))
  142. });
  143. }
  144. /// <summary>
  145. /// Called when [composable parts loaded].
  146. /// </summary>
  147. /// <returns>Task.</returns>
  148. protected override async Task OnComposablePartsLoaded()
  149. {
  150. await base.OnComposablePartsLoaded().ConfigureAwait(false);
  151. // Once plugins have loaded give the api a reference to our protobuf serializer
  152. DataSerializer.DynamicSerializer = ProtobufSerializer.TypeModel;
  153. }
  154. /// <summary>
  155. /// Disposes the current ApiClient
  156. /// </summary>
  157. private void DisposeApiClient()
  158. {
  159. if (ApiClient != null)
  160. {
  161. ApiClient.Dispose();
  162. }
  163. }
  164. /// <summary>
  165. /// Releases unmanaged and - optionally - managed resources.
  166. /// </summary>
  167. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  168. protected override void Dispose(bool dispose)
  169. {
  170. if (dispose)
  171. {
  172. DisposeApiClient();
  173. }
  174. base.Dispose(dispose);
  175. }
  176. }
  177. }