Changes to support network config

This commit is contained in:
Greenback 2020-10-10 14:05:19 +01:00
parent ebe650afa9
commit 6dc81ec8e8
5 changed files with 51 additions and 28 deletions

View File

@ -134,6 +134,35 @@ namespace Emby.Server.Implementations.AppBase
} }
} }
/// <summary>
/// Manually pre-loads a factory so that it is available pre system initialisation.
/// </summary>
/// <typeparam name="T">Class to register.</typeparam>
public virtual void RegisterConfiguration<T>()
{
if (!typeof(IConfigurationFactory).IsAssignableFrom(typeof(T)))
{
throw new ArgumentException("Parameter does not implement IConfigurationFactory");
}
IConfigurationFactory factory = (IConfigurationFactory)Activator.CreateInstance(typeof(T));
if (_configurationFactories == null)
{
_configurationFactories = new IConfigurationFactory[] { factory };
}
else
{
var list = _configurationFactories.ToList<IConfigurationFactory>();
list.Add(factory);
_configurationFactories = list.ToArray();
}
_configurationStores = _configurationFactories
.SelectMany(i => i.GetConfigurations())
.ToArray();
}
/// <summary> /// <summary>
/// Adds parts. /// Adds parts.
/// </summary> /// </summary>
@ -269,7 +298,7 @@ namespace Emby.Server.Implementations.AppBase
} }
/// <inheritdoc /> /// <inheritdoc />
public object GetConfiguration(string key, Type objectType = null) public object GetConfiguration(string key)
{ {
return _configurations.GetOrAdd(key, k => return _configurations.GetOrAdd(key, k =>
{ {
@ -278,12 +307,12 @@ namespace Emby.Server.Implementations.AppBase
var configurationInfo = _configurationStores var configurationInfo = _configurationStores
.FirstOrDefault(i => string.Equals(i.Key, key, StringComparison.OrdinalIgnoreCase)); .FirstOrDefault(i => string.Equals(i.Key, key, StringComparison.OrdinalIgnoreCase));
if (configurationInfo == null && objectType == null) if (configurationInfo == null)
{ {
throw new ResourceNotFoundException("Configuration with key " + key + " not found."); throw new ResourceNotFoundException("Configuration with key " + key + " not found.");
} }
var configurationType = configurationInfo?.ConfigurationType ?? objectType; var configurationType = configurationInfo.ConfigurationType;
lock (_configurationSyncLock) lock (_configurationSyncLock)
{ {

View File

@ -276,9 +276,11 @@ namespace Emby.Server.Implementations
_fileSystemManager = fileSystem; _fileSystemManager = fileSystem;
ConfigurationManager = new ServerConfigurationManager(ApplicationPaths, LoggerFactory, _xmlSerializer, _fileSystemManager); ConfigurationManager = new ServerConfigurationManager(ApplicationPaths, LoggerFactory, _xmlSerializer, _fileSystemManager);
MigrateNetworkConfiguration();
// Have to pre-register the NetworkConfigurationFactory.
ConfigurationManager.RegisterConfiguration<NetworkConfigurationFactory>();
NetManager = new NetworkManager((IServerConfigurationManager)ConfigurationManager, LoggerFactory.CreateLogger<NetworkManager>()); NetManager = new NetworkManager((IServerConfigurationManager)ConfigurationManager, LoggerFactory.CreateLogger<NetworkManager>());
NetManager.UpdateSettings(GetNetworkConfiguration());
Logger = LoggerFactory.CreateLogger<ApplicationHost>(); Logger = LoggerFactory.CreateLogger<ApplicationHost>();
@ -304,7 +306,7 @@ namespace Emby.Server.Implementations
ApplicationUserAgent = Name.Replace(' ', '-') + "/" + ApplicationVersionString; ApplicationUserAgent = Name.Replace(' ', '-') + "/" + ApplicationVersionString;
} }
private NetworkConfiguration GetNetworkConfiguration() private void MigrateNetworkConfiguration()
{ {
string path = Path.Combine(ConfigurationManager.CommonApplicationPaths.ConfigurationDirectoryPath, "network.xml"); string path = Path.Combine(ConfigurationManager.CommonApplicationPaths.ConfigurationDirectoryPath, "network.xml");
if (!File.Exists(path)) if (!File.Exists(path))
@ -312,11 +314,8 @@ namespace Emby.Server.Implementations
var networkSettings = new NetworkConfiguration(); var networkSettings = new NetworkConfiguration();
ClassMigrationHelper.CopyProperties(ServerConfigurationManager.Configuration, networkSettings); ClassMigrationHelper.CopyProperties(ServerConfigurationManager.Configuration, networkSettings);
_xmlSerializer.SerializeToFile(networkSettings, path); _xmlSerializer.SerializeToFile(networkSettings, path);
Logger.LogDebug("Successfully migrated network settings.");
return networkSettings;
} }
return (NetworkConfiguration)ConfigurationManager.GetConfiguration("network", typeof(NetworkConfiguration));
} }
public string ExpandVirtualPath(string path) public string ExpandVirtualPath(string path)

View File

@ -51,10 +51,6 @@ namespace Jellyfin.Api.Controllers
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
public ActionResult<ServerConfiguration> GetConfiguration() public ActionResult<ServerConfiguration> GetConfiguration()
{ {
// TODO: Temp workaround until the web can be changed.
var net = _configurationManager.GetNetworkConfiguration();
ClassMigrationHelper.CopyProperties(net, _configurationManager.Configuration);
return _configurationManager.Configuration; return _configurationManager.Configuration;
} }
@ -70,12 +66,6 @@ namespace Jellyfin.Api.Controllers
public ActionResult UpdateConfiguration([FromBody, Required] ServerConfiguration configuration) public ActionResult UpdateConfiguration([FromBody, Required] ServerConfiguration configuration)
{ {
_configurationManager.ReplaceConfiguration(configuration); _configurationManager.ReplaceConfiguration(configuration);
// TODO: Temp workaround until the web can be changed.
var network = _configurationManager.GetNetworkConfiguration();
ClassMigrationHelper.CopyProperties(configuration, network);
_configurationManager.SaveConfiguration("Network", network);
return NoContent(); return NoContent();
} }

View File

@ -110,10 +110,12 @@ namespace Jellyfin.Networking.Manager
_publishedServerUrls = new Dictionary<IPNetAddress, string>(); _publishedServerUrls = new Dictionary<IPNetAddress, string>();
_eventFireLock = new object(); _eventFireLock = new object();
UpdateSettings(_configurationManager.GetNetworkConfiguration());
NetworkChange.NetworkAddressChanged += OnNetworkAddressChanged; NetworkChange.NetworkAddressChanged += OnNetworkAddressChanged;
NetworkChange.NetworkAvailabilityChanged += OnNetworkAvailabilityChanged; NetworkChange.NetworkAvailabilityChanged += OnNetworkAvailabilityChanged;
_configurationManager.ConfigurationUpdated += ConfigurationUpdated; _configurationManager.NamedConfigurationUpdated += ConfigurationUpdated;
} }
#pragma warning restore CS8618 // Non-nullable field is uninitialized. #pragma warning restore CS8618 // Non-nullable field is uninitialized.
@ -600,7 +602,7 @@ namespace Jellyfin.Networking.Manager
{ {
if (disposing) if (disposing)
{ {
_configurationManager.ConfigurationUpdated -= ConfigurationUpdated; _configurationManager.NamedConfigurationUpdated -= ConfigurationUpdated;
NetworkChange.NetworkAddressChanged -= OnNetworkAddressChanged; NetworkChange.NetworkAddressChanged -= OnNetworkAddressChanged;
NetworkChange.NetworkAvailabilityChanged -= OnNetworkAvailabilityChanged; NetworkChange.NetworkAvailabilityChanged -= OnNetworkAvailabilityChanged;
} }
@ -609,9 +611,12 @@ namespace Jellyfin.Networking.Manager
} }
} }
private void ConfigurationUpdated(object? sender, EventArgs args) private void ConfigurationUpdated(object? sender, ConfigurationUpdateEventArgs evt)
{ {
UpdateSettings(_configurationManager.GetNetworkConfiguration()); if (evt.Key.Equals("network", StringComparison.Ordinal))
{
UpdateSettings(evt.NewConfiguration);
}
} }
/// <summary> /// <summary>

View File

@ -47,12 +47,12 @@ namespace MediaBrowser.Common.Configuration
void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration); void ReplaceConfiguration(BaseApplicationConfiguration newConfiguration);
/// <summary> /// <summary>
/// Gets the configuration. /// Manually pre-loads a factory so that it is available pre system initialisation.
/// </summary> /// </summary>
/// <param name="key">The key.</param> /// <typeparam name="T">Class to register.</typeparam>
/// <param name="objectType">Optional parameter containing the key object to create, if it hasn't been registered.</param> void RegisterConfiguration<T>();
/// <returns>System.Object.</returns>
object GetConfiguration(string key, Type objectType = null); object GetConfiguration(string key);
/// <summary> /// <summary>
/// Gets the type of the configuration. /// Gets the type of the configuration.