UIKernel.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using MediaBrowser.ApiInteraction;
  2. using MediaBrowser.Common.Kernel;
  3. using MediaBrowser.Model.Configuration;
  4. using MediaBrowser.Model.DTO;
  5. using MediaBrowser.Model.Progress;
  6. using MediaBrowser.UI.Configuration;
  7. using System;
  8. using System.Threading.Tasks;
  9. namespace MediaBrowser.UI.Controller
  10. {
  11. /// <summary>
  12. /// This controls application logic as well as server interaction within the UI.
  13. /// </summary>
  14. public class UIKernel : BaseKernel<UIApplicationConfiguration, UIApplicationPaths>
  15. {
  16. public static UIKernel Instance { get; private set; }
  17. public ApiClient ApiClient { get; private set; }
  18. public DtoUser CurrentUser { get; set; }
  19. public ServerConfiguration ServerConfiguration { get; set; }
  20. public UIKernel()
  21. : base()
  22. {
  23. Instance = this;
  24. }
  25. public override KernelContext KernelContext
  26. {
  27. get { return KernelContext.Ui; }
  28. }
  29. /// <summary>
  30. /// Give the UI a different url prefix so that they can share the same port, in case they are installed on the same machine.
  31. /// </summary>
  32. protected override string HttpServerUrlPrefix
  33. {
  34. get
  35. {
  36. return "http://+:" + Configuration.HttpServerPortNumber + "/mediabrowser/ui/";
  37. }
  38. }
  39. /// <summary>
  40. /// Performs initializations that can be reloaded at anytime
  41. /// </summary>
  42. protected override async Task ReloadInternal(IProgress<TaskProgress> progress)
  43. {
  44. ReloadApiClient();
  45. await new PluginUpdater().UpdatePlugins().ConfigureAwait(false);
  46. await base.ReloadInternal(progress).ConfigureAwait(false);
  47. }
  48. /// <summary>
  49. /// Updates and installs new plugin assemblies and configurations from the server
  50. /// </summary>
  51. protected async Task<PluginUpdateResult> UpdatePlugins()
  52. {
  53. return await new PluginUpdater().UpdatePlugins().ConfigureAwait(false);
  54. }
  55. /// <summary>
  56. /// Disposes the current ApiClient and creates a new one
  57. /// </summary>
  58. private void ReloadApiClient()
  59. {
  60. DisposeApiClient();
  61. ApiClient = new ApiClient
  62. {
  63. ServerHostName = Configuration.ServerHostName,
  64. ServerApiPort = Configuration.ServerApiPort
  65. };
  66. }
  67. /// <summary>
  68. /// Disposes the current ApiClient
  69. /// </summary>
  70. private void DisposeApiClient()
  71. {
  72. if (ApiClient != null)
  73. {
  74. ApiClient.Dispose();
  75. }
  76. }
  77. public override void Dispose()
  78. {
  79. base.Dispose();
  80. DisposeApiClient();
  81. }
  82. }
  83. }