cleanup: Modernize PubSubRoute code, json parsing

This commit is contained in:
John Colvin committed 2025-11-21 14:20:49 -05:00
1 parent c3da5a5d0c
commit a442e27d3d
5 files changed
+66 -100

No files matched your search

@@ -1,104 +1,67 @@
using bts;
using mbts.utils;
using System;
using BCG;
//using BCG.Utilities;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Serialization;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
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;
namespace BCG;
public PubSubRoute() { }
public class PubSubRoute : IEquatable<PubSubRoute>, IComparable<PubSubRoute>, ICloneable<PubSubRoute> {
[JsonPropertyName("server_address")]
public string? ServerAddress { get; set; }
public PubSubRoute(string the_server_address, string the_tag_reg_exp) {
server_address = the_server_address;
tag_reg_exp = the_tag_reg_exp;
}
[JsonPropertyName("tag")]
public string? TagRegExp { get; set; }
//route names are used in intermediary code (i.e. while substations are being deployed)
//so that MC Hammer can advertise them.
[JsonPropertyName("name")]
public string? Name { get; set; }
public string[] getAllPubSubs() {
return server_address.Split(';');
}
static readonly IComparer<PubSubRoute> comparer = new ComparerBuilder<PubSubRoute>() {
p => p.ServerAddress,
p => p.TagRegExp,
};
public Regex get_regex() {
return _regex ?? new Regex(tag_reg_exp);
}
Regex? myRegex;
#region IComparable<PubSubRoute> Members
public PubSubRoute() { }
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 PubSubRoute(string theServerAddress, string theTagRegExp) {
ServerAddress = theServerAddress;
TagRegExp = theTagRegExp;
}
public static class PubSubRouteExtensions {
public static bool IsMatch(this PubSubRoute route, string tag) =>
route.get_regex().IsMatch(tag);
[MemberNotNull(nameof(TagRegExp), nameof(myRegex))]
public bool IsMatch(string tag) {
if (TagRegExp is null) {
throw new InvalidOperationException();
}
myRegex ??= new Regex(TagRegExp);
return myRegex.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;
public PubSubRoute Clone() =>
new() {
ServerAddress = ServerAddress,
TagRegExp = TagRegExp,
Name = Name,
};
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 int CompareTo(PubSubRoute? other) =>
other is null ? -1 : comparer.Compare(this, other);
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);
}
}
public bool Equals(PubSubRoute? other) =>
other is not null
&& ServerAddress == other.ServerAddress
&& TagRegExp == other.TagRegExp;
public override bool Equals(object? obj) =>
Equals(obj as PubSubRoute);
public override int GetHashCode() =>
HashCode.Combine(ServerAddress, TagRegExp);
}
@@ -48,7 +48,8 @@ namespace BCG {
throw new ArgumentException($"Failed to find route for tag '{tag}'", nameof(tag));
}
Log.Gen.info($"PubSubRouter.get_sub_url_for_tag tag: {tag} route: {route}");
return RemoteConnectionInfo.parse_url(route.server_address);
ArgumentNullException.ThrowIfNull(route.ServerAddress);
return RemoteConnectionInfo.parse_url(route.ServerAddress);
}
public RemoteConnectionInfo get_sub_route_for_tag(string tag) {
@@ -56,17 +57,18 @@ namespace BCG {
throw new ArgumentException($"Failed to find route for tag '{tag}'", nameof(tag));
}
Log.Gen.info($"PubSubRouter.get_sub_url_for_tag tag: {tag} route: {route}");
return RemoteConnectionInfo.parse_url(route.server_address);
ArgumentNullException.ThrowIfNull(route.ServerAddress);
return RemoteConnectionInfo.parse_url(route.ServerAddress);
}
public List<string> get_all_pubsub_sub_urls() =>
_sub_routes.Select(r => r.server_address).ToList();
_sub_routes.Select(r => r.ServerAddress ?? throw new InvalidOperationException("Route.ServerAddress is null")).ToList();
public List<string> get_all_pubsub_pub_urlss() =>
_pub_routes.Select(r => r.server_address).ToList();
_pub_routes.Select(r => r.ServerAddress ?? throw new InvalidOperationException("Route.ServerAddress is null")).ToList();
public event PubSubTagURLChangeDelegate onSubURLTagRouteChange;
public event PubSubTagURLChangeDelegate onPubURLTagRouteChange;
public event PubSubTagURLChangeDelegate? onSubURLTagRouteChange;
public event PubSubTagURLChangeDelegate? onPubURLTagRouteChange;
#endregion
@@ -77,7 +79,5 @@ namespace BCG {
aVar = aVar + 2;
}
}
}
}
@@ -19,10 +19,10 @@ namespace BCG {
BlueTable rows = new BlueTable(schema);
var routes = _router.sub_routes();
foreach (var route in routes) {
IBlueRow nr = new BlueRow(schema, rows.Count, route.tag_reg_exp, route.server_address);
IBlueRow nr = new BlueRow(schema, rows.Count, route.TagRegExp, route.ServerAddress);
rows.addRow(nr);
}
myFireEvent(new DataPacket(rows, rows, new BlueTable(schema)));
}
}
}
}
+1 -1
View File
@@ -4,7 +4,7 @@ namespace Spark.Core.Protobuf {
using System;
public interface IProtobuf<T> : ICloneable<T>, IEquatable<T> {
public interface IProtobuf<T> : ICloneable<T>, IEquatable<T> where T : notnull {
object ICloneable.Clone() =>
Clone() ?? throw new InvalidOperationException();
}
@@ -2,7 +2,10 @@
namespace bts {
using System;
public interface ICloneable<T> : ICloneable {
public interface ICloneable<T> : ICloneable where T : notnull {
new T Clone();
object ICloneable.Clone() =>
Clone();
}
}