105 lines
3.4 KiB
C#
105 lines
3.4 KiB
C#
using System;
|
|
using BCG;
|
|
//using BCG.Utilities;
|
|
using System.Text.RegularExpressions;
|
|
using System.Text.Json.Serialization;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Nodes;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace BCG {
|
|
[JsonConverter(typeof(PubSubRouteJsonConverter))]
|
|
public class PubSubRoute : IComparable<PubSubRoute> {
|
|
public string server_address;
|
|
public string tag_reg_exp;
|
|
//route names are used in intermediary code (i.e. while substations are being deployed)
|
|
//so that MC Hammer can advertise them.
|
|
public string name;
|
|
private Regex _regex = null;
|
|
|
|
public PubSubRoute() { }
|
|
|
|
public PubSubRoute(string the_server_address, string the_tag_reg_exp) {
|
|
server_address = the_server_address;
|
|
tag_reg_exp = the_tag_reg_exp;
|
|
}
|
|
|
|
public string[] getAllPubSubs() {
|
|
return server_address.Split(';');
|
|
}
|
|
|
|
public Regex get_regex() {
|
|
return _regex ?? new Regex(tag_reg_exp);
|
|
}
|
|
|
|
#region IComparable<PubSubRoute> Members
|
|
|
|
public int CompareTo(PubSubRoute other) {
|
|
return server_address == other.server_address ? tag_reg_exp.CompareTo(other.tag_reg_exp)
|
|
: server_address.CompareTo(other.server_address);
|
|
}
|
|
|
|
#endregion
|
|
|
|
public override bool Equals(object obj) {
|
|
if (!(obj is PubSubRoute) || obj == null)
|
|
return false;
|
|
else {
|
|
PubSubRoute other = obj as PubSubRoute;
|
|
return server_address.Equals(other.server_address) &&
|
|
tag_reg_exp.Equals(other.tag_reg_exp);
|
|
}
|
|
}
|
|
|
|
public override int GetHashCode() {
|
|
return base.GetHashCode();
|
|
}
|
|
|
|
public static PubSubRoute get_default_route() {
|
|
return new PubSubRoute("bluto://127.0.0.1:12345", ".*");
|
|
}
|
|
|
|
public override string ToString() {
|
|
return $"tag_reg_exp: {tag_reg_exp} server_address: {server_address}";
|
|
}
|
|
}
|
|
|
|
public static class PubSubRouteExtensions {
|
|
public static bool IsMatch(this PubSubRoute route, string tag) =>
|
|
route.get_regex().IsMatch(tag);
|
|
}
|
|
|
|
public class PubSubRouteJsonConverter : JsonConverter<PubSubRoute> {
|
|
public override PubSubRoute? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
|
|
var json_obj = JsonSerializer.Deserialize<JsonObject>(ref reader);
|
|
if (json_obj is null) {
|
|
return null;
|
|
}
|
|
var rv = new PubSubRoute();
|
|
ReadString("name", ref rv.name);
|
|
ReadString("server_address", ref rv.server_address);
|
|
ReadString("tag", ref rv.tag_reg_exp);
|
|
return rv;
|
|
|
|
void ReadString(string key, ref string valueOut) {
|
|
if (json_obj.TryGetPropertyValue(key, out var valueJson)) {
|
|
var valueStr = valueJson?.GetValue<string>();
|
|
// Only (over)write valueOut if non-null
|
|
if (valueStr is not null) {
|
|
valueOut = valueStr;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void Write(Utf8JsonWriter writer, PubSubRoute value, JsonSerializerOptions options) {
|
|
var jsonObj = new JsonObject {
|
|
["name"] = value.name,
|
|
["server_address"] = value.server_address,
|
|
["tag"] = value.tag_reg_exp,
|
|
};
|
|
JsonSerializer.Serialize(writer, jsonObj);
|
|
}
|
|
}
|
|
}
|