Innovenergy_trunk/csharp/lib/Protocols/DBus/Daemon/MatchRule.cs

118 lines
4.1 KiB
C#

using System.Text;
using InnovEnergy.Lib.Protocols.DBus.Protocol.DataTypes;
using InnovEnergy.Lib.Protocols.DBus.Protocol.Header;
using static InnovEnergy.Lib.Protocols.DBus.Protocol.Header.MessageType;
namespace InnovEnergy.Lib.Protocols.DBus.Daemon;
public interface IMatchRule
{
MessageType? Type { get; }
String? Interface { get; }
String? Member { get; }
ObjectPath? Path { get; }
ObjectPath? PathNamespace { get; }
String? Sender { get; }
String? Destination { get; }
Boolean Eavesdrop { get; }
// TODO: argN, argNpath, arg0namespace
}
public record MatchRule : IMatchRule
{
public MessageType? Type { get; init; } = default;
public String? Interface { get; init; } = default;
public String? Member { get; init; } = default;
public ObjectPath? Path { get; init; } = default;
public ObjectPath? PathNamespace { get; init; } = default;
public String? Sender { get; init; } = default;
public String? Destination { get; init; } = default;
public Boolean Eavesdrop { get; init; } = false;
public override String ToString() => this.Rule();
}
public record SignalMatchRule : IMatchRule
{
public MessageType? Type => Signal;
public String? Interface { get; init; }
public String? Member { get; init; }
public ObjectPath? Path { get; init; }
public ObjectPath? PathNamespace { get; init; }
public String? Sender { get; init; }
public String? Destination => null;
public Boolean Eavesdrop => false;
public override String ToString() => this.Rule();
}
// public record MethodCallMatchRule : IMatchRule
// {
// public MessageType? Type => MethodCall;
// public String? Interface { get; init; } = null;
// public String? Member { get; init; } = null;
// public ObjectPath? Path { get; init; } = null;
// public ObjectPath? PathNamespace { get; init; } = null;
// public String? Destination => null;
// public String? Sender => null;
// public Boolean Eavesdrop => false;
// }
//
// public record MethodReturnMatchRule : IMatchRule
// {
// public MessageType? Type => MethodReturn;
// public String? Interface { get; init; } = null;
// public String? Member { get; init; } = null;
// public ObjectPath? Path { get; init; } = null;
// public ObjectPath? PathNamespace => null;
// public String? Destination => null;
// public String? Sender => null;
// public Boolean Eavesdrop => false;
// }
internal static class MatchRuleExtensions
{
public static String Rule(this IMatchRule rule)
{
var sb = new StringBuilder();
Append("type" , MessageType(rule.Type));
Append("interface" , rule.Interface);
Append("member" , rule.Member);
Append("path" , rule.Path?.Path);
Append("sender" , rule.Sender);
Append("destination" , rule.Destination);
Append("path_namespace", rule.PathNamespace?.Path);
Append("eavesdrop" , rule.Eavesdrop ? "true" : null);
return sb.ToString();
void Append(String key, Object? value)
{
if (value is null) return;
if (sb.Length != 0)
sb.Append(',');
sb.Append(key);
sb.Append("='");
sb.Append(value.ToString()?.Replace(@"\", @"\\").Replace(@"'", @"\'"));
sb.Append('\'');
}
String? MessageType(MessageType? messageType) => messageType switch
{
Signal => "signal",
MethodCall => "method_call",
MethodReturn => "method_return",
Error => "error",
null => null,
_ => throw new ArgumentOutOfRangeException(nameof(messageType), messageType, null)
};
}
}