using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Events;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.System;
using System;
namespace MediaBrowser.Common.Kernel
{
///
/// Represents a shared base kernel for both the Ui and server apps
///
public abstract class BaseKernel : IKernel
{
///
/// Occurs when [has pending restart changed].
///
public event EventHandler HasPendingRestartChanged;
#region ApplicationUpdated Event
///
/// Occurs when [application updated].
///
public event EventHandler> ApplicationUpdated;
///
/// Called when [application updated].
///
/// The new version.
public void OnApplicationUpdated(Version newVersion)
{
EventHelper.QueueEventIfNotNull(ApplicationUpdated, this, new GenericEventArgs { Argument = newVersion }, Logger);
NotifyPendingRestart();
}
#endregion
///
/// Gets or sets a value indicating whether this instance has changes that require the entire application to restart.
///
/// true if this instance has pending application restart; otherwise, false.
public bool HasPendingRestart { get; private set; }
///
/// 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.
///
/// The UDP server port number.
public abstract int UdpServerPortNumber { get; }
///
/// 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}/...
///
/// The name of the web application.
public string WebApplicationName
{
get { return "mediabrowser"; }
}
///
/// Gets the HTTP server URL prefix.
///
/// The HTTP server URL prefix.
public virtual string HttpServerUrlPrefix
{
get
{
return "http://+:" + _configurationManager.CommonConfiguration.HttpServerPortNumber + "/" + WebApplicationName + "/";
}
}
///
/// Gets the kernel context. Subclasses will have to override.
///
/// The kernel context.
public abstract KernelContext KernelContext { get; }
///
/// Gets the logger.
///
/// The logger.
protected ILogger Logger { get; private set; }
///
/// Gets or sets the application host.
///
/// The application host.
protected IApplicationHost ApplicationHost { get; private set; }
private readonly IConfigurationManager _configurationManager;
///
/// Initializes a new instance of the class.
///
/// The app host.
/// The log manager.
protected BaseKernel(IApplicationHost appHost, ILogManager logManager, IConfigurationManager configurationManager)
{
ApplicationHost = appHost;
_configurationManager = configurationManager;
Logger = logManager.GetLogger("Kernel");
}
///
/// Initializes the Kernel
///
/// Task.
public void Init()
{
ReloadInternal();
Logger.Info("Kernel.Init Complete");
}
///
/// Performs initializations that can be reloaded at anytime
///
/// Task.
protected virtual void ReloadInternal()
{
}
///
/// Notifies that the kernel that a change has been made that requires a restart
///
public void NotifyPendingRestart()
{
HasPendingRestart = true;
EventHelper.QueueEventIfNotNull(HasPendingRestartChanged, this, EventArgs.Empty, Logger);
}
///
/// Performs the pending restart.
///
/// Task.
public void PerformPendingRestart()
{
if (HasPendingRestart)
{
Logger.Info("Restarting the application");
ApplicationHost.Restart();
}
else
{
Logger.Info("PerformPendingRestart - not needed");
}
}
///
/// Gets the system status.
///
/// SystemInfo.
public virtual SystemInfo GetSystemInfo()
{
return new SystemInfo
{
HasPendingRestart = HasPendingRestart,
Version = ApplicationHost.ApplicationVersion.ToString(),
IsNetworkDeployed = ApplicationHost.CanSelfUpdate,
WebSocketPortNumber = ApplicationHost.Resolve().WebSocketPortNumber,
SupportsNativeWebSocket = ApplicationHost.Resolve().SupportsNativeWebSocket,
FailedPluginAssemblies = ApplicationHost.FailedAssemblies.ToArray()
};
}
}
}