Skip to content

Commit

Permalink
Engine(-Tests), Server: implemented config overrides
Browse files Browse the repository at this point in the history
Added System.Core assembly reference to Server
  • Loading branch information
meebey committed Apr 14, 2024
1 parent 75e581c commit 2e6e88c
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 6 deletions.
59 changes: 59 additions & 0 deletions src/Engine-Tests/ConfigTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Smuxi - Smart MUltipleXed Irc
//
// Copyright (c) 2014 Mirco Bauer <[email protected]>
//
// Full GPL License: <http://www.gnu.org/licenses/gpl.txt>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Collections.Generic;
using NUnit.Framework;

namespace Smuxi.Engine
{
[TestFixture]
public class ConfigTests
{
[Test]
public void Indexer()
{
var config = new Config();
config["Server/Port"] = 123;
Assert.AreEqual(123, (int) config["Server/Port"]);
}

[Test]
public void ConfigOverridesIntValue()
{
var config = new Config();
var overrides = config.Overrides;
overrides.Add("Server/Port", 7689);

config["Server/Port"] = 123;
Assert.AreEqual(7689, (int) config["Server/Port"]);
}

[Test]
public void ConfigOverridesRegexKey()
{
var config = new Config();
var overrides = config.Overrides;
overrides.Add("/Engine/Users/.*/MessageBuffer/PersistencyType/", "PersistentSqlite");

config["Engine/Users/local/MessageBuffer/PersistencyType"] = "Volatile";
Assert.AreEqual("PersistentSqlite", (string) config["Engine/Users/local/MessageBuffer/PersistencyType"]);
}
}
}
4 changes: 1 addition & 3 deletions src/Engine-Tests/Engine-Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<Compile Include="MessageDtoModelV1Tests.cs" />
<Compile Include="SqliteMessageBufferTests.cs" />
<Compile Include="SessionTests.cs" />
<Compile Include="ConfigTests.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down Expand Up @@ -75,8 +76,5 @@
<Name>Newtonsoft.Json</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
31 changes: 30 additions & 1 deletion src/Engine/Config/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,22 @@ public class Config : PermanentRemoteObject
protected Hashtable m_Preferences = Hashtable.Synchronized(new Hashtable());
public Version PreviousVersion { get; private set; }
public Version CurrentVersion { get; private set; }
internal Dictionary<string, object> Overrides { get; private set; }
public event EventHandler<ConfigChangedEventArgs> Changed;

public object this[string key] {
get {
// config overrides
lock (Overrides) {
foreach (var @override in Overrides) {
var pattern = @override.Key;
var value = @override.Value;
if (Pattern.IsMatch(key, pattern)) {
return value;
}
}
}

return m_Preferences[key];
}
set {
Expand All @@ -66,6 +78,20 @@ public object this[string key] {
#endif
return;
}

// config overrides
lock (Overrides) {
foreach (var @override in Overrides) {
var pattern = @override.Key;
if (Pattern.IsMatch(key, pattern)) {
#if LOG4NET
_Logger.Debug("set[]: ignoring setting an overridden config key " + key + ".");
#endif
return;
}
}
}

var oldValue = m_Preferences[key];
m_Preferences[key] = value;

Expand All @@ -86,6 +112,7 @@ public bool IsCleanConfig {

public Config()
{
Overrides = new Dictionary<string, object>();
#if CONFIG_NINI
m_ConfigPath = Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData), "smuxi");
Expand Down Expand Up @@ -680,7 +707,9 @@ public IDictionary<string, object> GetAll()
lock (m_Preferences) {
var dict = new Dictionary<string, object>(m_Preferences.Count);
foreach (DictionaryEntry entry in m_Preferences) {
dict.Add((string) entry.Key, entry.Value);
// honor config overrides
var value = this[(string) entry.Key];
dict.Add((string) entry.Key, value);
}
return dict;
}
Expand Down
28 changes: 28 additions & 0 deletions src/Engine/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System.IO;
using System.Text;
using System.Reflection;
using System.Collections.Generic;
using Smuxi.Common;

namespace Smuxi.Engine
Expand All @@ -38,6 +39,7 @@ public class Engine
private static Config _Config;
private static SessionManager _SessionManager;
private static ProtocolManagerFactory _ProtocolManagerFactory;
public static Dictionary<string, string> ConfigOverrides { get; set; }

public static Version AssemblyVersion {
get {
Expand Down Expand Up @@ -176,6 +178,32 @@ public static void Init()

_Config.Save();

// config overrides
// we only scan all config keys so we can convert the override
// value into the right type
foreach (var kvp in _Config.GetAll()) {
var configKey = kvp.Key;
var configValue = kvp.Value;

foreach (var @override in ConfigOverrides) {
var keyPattern = @override.Key;

if (!Pattern.IsMatch(configKey, keyPattern)) {
continue;
}

// convert string to type
var overridenValue = Convert.ChangeType(@override.Value,
configValue.GetType());
// as the patttern could match many keys we only need one
// override with the right value type
if (_Config.Overrides.ContainsKey(keyPattern)) {
continue;
}
_Config.Overrides.Add(keyPattern, overridenValue);
}
}

string location = Path.GetDirectoryName(asm.Location);
if (String.IsNullOrEmpty(location) &&
Environment.OSVersion.Platform == PlatformID.Unix) {
Expand Down
20 changes: 19 additions & 1 deletion src/Server/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
using NDesk.Options;
using Smuxi.Common;
using Smuxi.Engine;
Expand Down Expand Up @@ -135,6 +137,22 @@ public static void Main(string[] args)
}
);

var configOverrides = new Dictionary<string, string>();
parser.Add(
"override-config=",
_("Override config values"),
delegate (string val) {
if (String.IsNullOrEmpty(val) || !val.Contains("=")) {
return;
}
var overrideKeyPattern = val.Split('=')[0].Trim();
var overrideValue = String.Join(
"=", val.Split('=').Skip(1).ToArray()
).Trim();
configOverrides.Add(overrideKeyPattern, overrideValue);
}
);

parser.Add(
"h|help",
_("Show this help"),
Expand Down Expand Up @@ -194,7 +212,7 @@ public static void Main(string[] args)
}

try {
Server.Init(args);
Server.Init(args, configOverrides);
} catch (Exception e) {
#if LOG4NET
_Logger.Fatal(e);
Expand Down
1 change: 1 addition & 0 deletions src/Server/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ EXTRAS = \
REFERENCES = \
System.Runtime.Remoting \
System \
System.Core \
Mono.Posix

DLL_REFERENCES =
Expand Down
4 changes: 3 additions & 1 deletion src/Server/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using System;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Lifetime;
using System.Runtime.Remoting.Channels;
Expand All @@ -51,8 +52,9 @@ public class Server
private static readonly log4net.ILog _Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endif

public static void Init(string[] args)
public static void Init(string[] args, Dictionary<string, string> configOverrides)
{
Engine.Engine.ConfigOverrides = configOverrides;
Engine.Engine.Init();
Engine.Engine.InitSessionManager();
string channel = (string)Engine.Engine.Config["Server/Channel"];
Expand Down
1 change: 1 addition & 0 deletions src/Server/Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
<Reference Include="System.Runtime.Remoting" />
<Reference Include="System" />
<Reference Include="Mono.Posix" />
<Reference Include="System.Core" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ProjectExtensions>
Expand Down

0 comments on commit 2e6e88c

Please sign in to comment.