aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.UI/Controller/UIKernel.cs
blob: e90599934816d6ceadacbbf1154b977c850375de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using MediaBrowser.ApiInteraction;
using MediaBrowser.Common.IO;
using MediaBrowser.Common.Kernel;
using MediaBrowser.Common.Logging;
using MediaBrowser.Model.Connectivity;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Net;
using MediaBrowser.UI.Configuration;
using MediaBrowser.UI.Playback;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Cache;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;

namespace MediaBrowser.UI.Controller
{
    /// <summary>
    /// This controls application logic as well as server interaction within the UI.
    /// </summary>
    public class UIKernel : BaseKernel<UIApplicationConfiguration, UIApplicationPaths>
    {
        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>The instance.</value>
        public static UIKernel Instance { get; private set; }

        /// <summary>
        /// Gets the API client.
        /// </summary>
        /// <value>The API client.</value>
        public ApiClient ApiClient { get; private set; }

        /// <summary>
        /// Gets the playback manager.
        /// </summary>
        /// <value>The playback manager.</value>
        public PlaybackManager PlaybackManager { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="UIKernel" /> class.
        /// </summary>
        public UIKernel(IIsoManager isoManager, ILogger logger)
            : base(isoManager, logger)
        {
            Instance = this;
        }

        /// <summary>
        /// Gets the media players.
        /// </summary>
        /// <value>The media players.</value>
        [ImportMany(typeof(BaseMediaPlayer))]
        public IEnumerable<BaseMediaPlayer> MediaPlayers { get; private set; }

        /// <summary>
        /// Gets the list of currently loaded themes
        /// </summary>
        /// <value>The themes.</value>
        [ImportMany(typeof(BaseTheme))]
        public IEnumerable<BaseTheme> Themes { get; private set; }

        /// <summary>
        /// Gets the kernel context.
        /// </summary>
        /// <value>The kernel context.</value>
        public override KernelContext KernelContext
        {
            get { return KernelContext.Ui; }
        }

        /// <summary>
        /// Gets the UDP server port number.
        /// </summary>
        /// <value>The UDP server port number.</value>
        public override int UdpServerPortNumber
        {
            get { return 7360; }
        }

        /// <summary>
        /// Give the UI a different url prefix so that they can share the same port, in case they are installed on the same machine.
        /// </summary>
        /// <value>The HTTP server URL prefix.</value>
        public override string HttpServerUrlPrefix
        {
            get
            {
                return "http://+:" + Configuration.HttpServerPortNumber + "/mediabrowserui/";
            }
        }

        /// <summary>
        /// Reload api client and update plugins after loading configuration
        /// </summary>
        /// <returns>Task.</returns>
        protected override async Task OnConfigurationLoaded()
        {
            ReloadApiClient();

            try
            {
                await new PluginUpdater(Logger).UpdatePlugins().ConfigureAwait(false);
            }
            catch (HttpException ex)
            {
                Logger.ErrorException("Error updating plugins from the server", ex);
            }
        }

        /// <summary>
        /// Disposes the current ApiClient and creates a new one
        /// </summary>
        private void ReloadApiClient()
        {
            DisposeApiClient();

            var logger = LogManager.GetLogger("ApiClient");

            ApiClient = new ApiClient(logger, new AsyncHttpClient(new WebRequestHandler
            {
                AutomaticDecompression = DecompressionMethods.Deflate,
                CachePolicy = new RequestCachePolicy(RequestCacheLevel.Revalidate)
            }))
            {
                ServerHostName = Configuration.ServerHostName,
                ServerApiPort = Configuration.ServerApiPort,
                ClientType = ClientType.Pc,
                DeviceName = Environment.MachineName,
                SerializationFormat = SerializationFormats.Json
            };
        }

        /// <summary>
        /// Reloads the internal.
        /// </summary>
        /// <returns>Task.</returns>
        protected override Task ReloadInternal()
        {
            PlaybackManager = new PlaybackManager(this, Logger);

            return base.ReloadInternal();
        }

        /// <summary>
        /// Called when [composable parts loaded].
        /// </summary>
        /// <returns>Task.</returns>
        protected override async Task OnComposablePartsLoaded()
        {
            await base.OnComposablePartsLoaded().ConfigureAwait(false);

            // Once plugins have loaded give the api a reference to our protobuf serializer
            DataSerializer.DynamicSerializer = ProtobufSerializer.TypeModel;
        }

        /// <summary>
        /// Disposes the current ApiClient
        /// </summary>
        private void DisposeApiClient()
        {
            if (ApiClient != null)
            {
                ApiClient.Dispose();
            }
        }

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources.
        /// </summary>
        /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected override void Dispose(bool dispose)
        {
            if (dispose)
            {
                DisposeApiClient();
            }

            base.Dispose(dispose);
        }
    }
}