Add json facilities for DataStream websocket api
This commit is contained in:
1 parent
1531c6e794
commit
a86d4490e4
6 files changed
+436
-88
No files matched your search
@@ -1,104 +1,61 @@
|
||||
using mbts.utils;
|
||||
|
||||
using Spark.Core.Json;
|
||||
|
||||
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> {
|
||||
[JsonConverter(typeof(JsonValueOrArrayConverter<string>))]
|
||||
[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 int CompareTo(PubSubRoute? other) =>
|
||||
other is null ? -1 : comparer.Compare(this, other);
|
||||
|
||||
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 bool Equals(PubSubRoute? other) =>
|
||||
other is not null
|
||||
&& ServerAddress == other.ServerAddress
|
||||
&& TagRegExp == other.TagRegExp;
|
||||
|
||||
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 override bool Equals(object? obj) =>
|
||||
Equals(obj as PubSubRoute);
|
||||
|
||||
public override int GetHashCode() =>
|
||||
HashCode.Combine(ServerAddress, TagRegExp);
|
||||
}
|
||||
Reference in new issue
Block a user