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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
using MediaBrowser.Common.Events;
using MediaBrowser.Common.Security;
using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Model.System;
using System;
using System.IO;
using System.Linq;
using System.Threading;
namespace MediaBrowser.Common.Kernel
{
/// <summary>
/// Represents a shared base kernel for both the Ui and server apps
/// </summary>
/// <typeparam name="TConfigurationType">The type of the T configuration type.</typeparam>
/// <typeparam name="TApplicationPathsType">The type of the T application paths type.</typeparam>
public abstract class BaseKernel<TConfigurationType, TApplicationPathsType> : IDisposable, IKernel
where TConfigurationType : BaseApplicationConfiguration, new()
where TApplicationPathsType : IApplicationPaths
{
/// <summary>
/// Occurs when [has pending restart changed].
/// </summary>
public event EventHandler HasPendingRestartChanged;
#region ConfigurationUpdated Event
/// <summary>
/// Occurs when [configuration updated].
/// </summary>
public event EventHandler<EventArgs> ConfigurationUpdated;
/// <summary>
/// Called when [configuration updated].
/// </summary>
internal void OnConfigurationUpdated()
{
EventHelper.QueueEventIfNotNull(ConfigurationUpdated, this, EventArgs.Empty, Logger);
}
#endregion
#region ApplicationUpdated Event
/// <summary>
/// Occurs when [application updated].
/// </summary>
public event EventHandler<GenericEventArgs<Version>> ApplicationUpdated;
/// <summary>
/// Called when [application updated].
/// </summary>
/// <param name="newVersion">The new version.</param>
public void OnApplicationUpdated(Version newVersion)
{
EventHelper.QueueEventIfNotNull(ApplicationUpdated, this, new GenericEventArgs<Version> { Argument = newVersion }, Logger);
NotifyPendingRestart();
}
#endregion
/// <summary>
/// The _configuration loaded
/// </summary>
private bool _configurationLoaded;
/// <summary>
/// The _configuration sync lock
/// </summary>
private object _configurationSyncLock = new object();
/// <summary>
/// The _configuration
/// </summary>
private TConfigurationType _configuration;
/// <summary>
/// Gets the system configuration
/// </summary>
/// <value>The configuration.</value>
public TConfigurationType Configuration
{
get
{
// Lazy load
LazyInitializer.EnsureInitialized(ref _configuration, ref _configurationLoaded, ref _configurationSyncLock, () => GetXmlConfiguration<TConfigurationType>(ApplicationPaths.SystemConfigurationFilePath));
return _configuration;
}
protected set
{
_configuration = value;
if (value == null)
{
_configurationLoaded = false;
}
}
}
/// <summary>
/// Gets or sets a value indicating whether this instance has changes that require the entire application to restart.
/// </summary>
/// <value><c>true</c> if this instance has pending application restart; otherwise, <c>false</c>.</value>
public bool HasPendingRestart { get; private set; }
/// <summary>
/// Gets the application paths.
/// </summary>
/// <value>The application paths.</value>
public TApplicationPathsType ApplicationPaths { get; private set; }
/// <summary>
/// Gets or sets the TCP manager.
/// </summary>
/// <value>The TCP manager.</value>
private IServerManager ServerManager { get; set; }
/// <summary>
/// Gets the plug-in security manager.
/// </summary>
/// <value>The plug-in security manager.</value>
public ISecurityManager SecurityManager { get; set; }
/// <summary>
/// Gets the UDP server port number.
/// This can't be configurable because then the user would have to configure their client to discover the server.
/// </summary>
/// <value>The UDP server port number.</value>
public abstract int UdpServerPortNumber { get; }
/// <summary>
/// Gets the name of the web application that can be used for url building.
/// All api urls will be of the form {protocol}://{host}:{port}/{appname}/...
/// </summary>
/// <value>The name of the web application.</value>
public string WebApplicationName
{
get { return "mediabrowser"; }
}
/// <summary>
/// Gets the HTTP server URL prefix.
/// </summary>
/// <value>The HTTP server URL prefix.</value>
public virtual string HttpServerUrlPrefix
{
get
{
return "http://+:" + Configuration.HttpServerPortNumber + "/" + WebApplicationName + "/";
}
}
/// <summary>
/// Gets the kernel context. Subclasses will have to override.
/// </summary>
/// <value>The kernel context.</value>
public abstract KernelContext KernelContext { get; }
/// <summary>
/// Gets the logger.
/// </summary>
/// <value>The logger.</value>
protected ILogger Logger { get; private set; }
/// <summary>
/// Gets or sets the application host.
/// </summary>
/// <value>The application host.</value>
protected IApplicationHost ApplicationHost { get; private set; }
/// <summary>
/// The _XML serializer
/// </summary>
private readonly IXmlSerializer _xmlSerializer;
/// <summary>
/// Initializes a new instance of the <see cref="BaseKernel{TApplicationPathsType}" /> class.
/// </summary>
/// <param name="appHost">The app host.</param>
/// <param name="appPaths">The app paths.</param>
/// <param name="xmlSerializer">The XML serializer.</param>
/// <param name="logger">The logger.</param>
/// <exception cref="System.ArgumentNullException">isoManager</exception>
protected BaseKernel(IApplicationHost appHost, TApplicationPathsType appPaths, IXmlSerializer xmlSerializer, ILogger logger)
{
ApplicationPaths = appPaths;
ApplicationHost = appHost;
_xmlSerializer = xmlSerializer;
Logger = logger;
}
/// <summary>
/// Initializes the Kernel
/// </summary>
/// <returns>Task.</returns>
public void Init()
{
ReloadInternal();
Logger.Info("Kernel.Init Complete");
}
/// <summary>
/// Performs initializations that can be reloaded at anytime
/// </summary>
/// <returns>Task.</returns>
protected virtual void ReloadInternal()
{
ServerManager = ApplicationHost.Resolve<IServerManager>();
}
/// <summary>
/// Notifies that the kernel that a change has been made that requires a restart
/// </summary>
public void NotifyPendingRestart()
{
HasPendingRestart = true;
EventHelper.QueueEventIfNotNull(HasPendingRestartChanged, this, EventArgs.Empty, Logger);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <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 virtual void Dispose(bool dispose)
{
}
/// <summary>
/// Performs the pending restart.
/// </summary>
/// <returns>Task.</returns>
public void PerformPendingRestart()
{
if (HasPendingRestart)
{
Logger.Info("Restarting the application");
ApplicationHost.Restart();
}
else
{
Logger.Info("PerformPendingRestart - not needed");
}
}
/// <summary>
/// Gets the system status.
/// </summary>
/// <returns>SystemInfo.</returns>
public virtual SystemInfo GetSystemInfo()
{
return new SystemInfo
{
HasPendingRestart = HasPendingRestart,
Version = ApplicationHost.ApplicationVersion.ToString(),
IsNetworkDeployed = ApplicationHost.CanSelfUpdate,
WebSocketPortNumber = ServerManager.WebSocketPortNumber,
SupportsNativeWebSocket = ServerManager.SupportsNativeWebSocket,
FailedPluginAssemblies = ApplicationHost.FailedAssemblies.ToArray()
};
}
/// <summary>
/// The _save lock
/// </summary>
private readonly object _configurationSaveLock = new object();
/// <summary>
/// Saves the current configuration
/// </summary>
public void SaveConfiguration()
{
lock (_configurationSaveLock)
{
_xmlSerializer.SerializeToFile(Configuration, ApplicationPaths.SystemConfigurationFilePath);
}
OnConfigurationUpdated();
}
/// <summary>
/// Gets the application paths.
/// </summary>
/// <value>The application paths.</value>
IApplicationPaths IKernel.ApplicationPaths
{
get { return ApplicationPaths; }
}
/// <summary>
/// Gets the configuration.
/// </summary>
/// <value>The configuration.</value>
BaseApplicationConfiguration IKernel.Configuration
{
get { return Configuration; }
}
/// <summary>
/// Reads an xml configuration file from the file system
/// It will immediately re-serialize and save if new serialization data is available due to property changes
/// </summary>
/// <param name="type">The type.</param>
/// <param name="path">The path.</param>
/// <returns>System.Object.</returns>
public object GetXmlConfiguration(Type type, string path)
{
Logger.Info("Loading {0} at {1}", type.Name, path);
object configuration;
byte[] buffer = null;
// Use try/catch to avoid the extra file system lookup using File.Exists
try
{
buffer = File.ReadAllBytes(path);
configuration = _xmlSerializer.DeserializeFromBytes(type, buffer);
}
catch (FileNotFoundException)
{
configuration = Activator.CreateInstance(type);
}
// Take the object we just got and serialize it back to bytes
var newBytes = _xmlSerializer.SerializeToBytes(configuration);
// If the file didn't exist before, or if something has changed, re-save
if (buffer == null || !buffer.SequenceEqual(newBytes))
{
Logger.Info("Saving {0} to {1}", type.Name, path);
// Save it after load in case we got new items
File.WriteAllBytes(path, newBytes);
}
return configuration;
}
/// <summary>
/// Reads an xml configuration file from the file system
/// It will immediately save the configuration after loading it, just
/// in case there are new serializable properties
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="path">The path.</param>
/// <returns>``0.</returns>
private T GetXmlConfiguration<T>(string path)
where T : class
{
return GetXmlConfiguration(typeof(T), path) as T;
}
/// <summary>
/// Limits simultaneous access to various resources
/// </summary>
/// <value>The resource pools.</value>
public ResourcePool ResourcePools { get; set; }
}
}
|