2
0

UIKernel.cs 5.7 KB

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