UIKernel.cs 5.7 KB

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