using BCG; using bts.collections; using bts.utils; using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Runtime.CompilerServices; namespace bts; public interface IPubSubMessage { void serialize_into_str(Stream str); void construct_deserialize(Stream str); } public static class PubSubMessageUtils { public static bool compare_obj(T t1, T t2) { if (t1 == null) { return t2 == null; } else return t1.Equals(t2); } public static bool compare_arrays(IEnumerable col1, IEnumerable col2) { if (col1.Count() != col2.Count()) return false; int i = 0; foreach (T t in col1) { T t1 = t; T t2 = col2.ElementAt(i++); var eqs = compare_obj(t1, t2); if (!eqs) return false; } return true; } } // Subscribe ////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// public class SubscribeMsg : IPubSubMessage, IEquatable { public string? connection; public string subscribing_app = ClientInfo.thisClient.app; public string[]? tags; public void serialize_into_str(Stream str) { if (tags is null) { throw new InvalidOperationException("Unable to serialize SubscribeMsg because tags is null"); } var bw = new BinaryWriter(str); bw.write_string_cpp(connection); bw.write_string_cpp(subscribing_app); bw.Write(tags.Length); foreach (var t in tags) { bw.write_string_cpp(t); } } public void construct_deserialize(Stream str) { var br = new BinaryReader(str); connection = br.read_string_cpp(); subscribing_app = br.read_string_cpp(); var n_tags = br.ReadInt32(); tags = new string[n_tags]; for (var i = 0; i < n_tags; i++) { tags[i] = br.read_string_cpp(); } } public bool Equals(SubscribeMsg? other) => other is not null && connection == other.connection && subscribing_app == other.subscribing_app && tags.safe_sequence_equals(other.tags); public override bool Equals(object? obj) => Equals(obj as SubscribeMsg); public override int GetHashCode() => ToString().GetHashCode(); public override string ToString() => $"{GetType().Name} connection: {connection} subscribing_app: {subscribing_app} tags: {string.Concat(tags.empty_if_null())}"; } // Unsubscribe //////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// public class UnsubscribeMsg : SubscribeMsg, IEquatable { public bool Equals(UnsubscribeMsg? other) => other is not null && base.Equals(other); public override bool Equals(object? obj) => Equals(obj as UnsubscribeMsg); public override int GetHashCode() => base.GetHashCode(); } // Trasnaction ///////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////// public class TransactionMsg : IPubSubMessage, IEquatable { uint n_msgs; public void serialize_into_str(Stream str) { var bw = new BinaryWriter(str); bw.Write(n_msgs); } public void construct_deserialize(Stream str) { var br = new BinaryReader(str); n_msgs = br.ReadUInt32(); } public override string ToString() => $"TransactionMsg n_msgs: {n_msgs}"; public bool Equals(TransactionMsg? other) => n_msgs == other?.n_msgs; public override bool Equals(object? obj) => Equals(obj as TransactionMsg); public override int GetHashCode() => n_msgs.GetHashCode(); } // Publish //////////////////////////////////////////////////////////////// /// /////////////////////////////////////////////////////////////////////// public class PublishMsg : IPubSubMessage, IEquatable { public string? connection; public string publishing_app = ClientInfo.thisClient.app; public string? tag; public string? tag_type; public bool is_compressed = false; // this is the size that of the msg, regarless of it being serialized or not public long publish_time_epoch_ms = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); public byte[]? data_to_publish; [MemberNotNull(nameof(connection), nameof(tag), nameof(tag_type))] public void sanity_check() { Log.assert(!string.IsNullOrEmpty(tag_type)); Log.assert(!string.IsNullOrEmpty(tag)); Log.assert(!string.IsNullOrEmpty(connection)); // The 'ThrowIfNullOrEmpty' calls are redundant (given the assert statements above), but // necessary for the compiler to recognize the strings won't be null when this method exits. ArgumentException.ThrowIfNullOrEmpty(tag_type); ArgumentException.ThrowIfNullOrEmpty(tag); ArgumentException.ThrowIfNullOrEmpty(connection); } public void serialize_into_str(Stream str) { sanity_check(); if (data_to_publish is null) { throw new InvalidOperationException("Unable to serialize PublishMsg because data_to_publish is null"); } var bw = new BinaryWriter(str); bw.write_string_cpp(connection); bw.write_string_cpp(publishing_app); bw.write_string_cpp(tag); bw.write_string_cpp(tag_type); bw.Write(is_compressed); bw.Write(publish_time_epoch_ms); var sz = data_to_publish.Length; bw.Write(sz); bw.Write(data_to_publish); } public void construct_deserialize(Stream str) { var br = new BinaryReader(str); connection = br.read_string_cpp(); publishing_app = br.read_string_cpp(); tag = br.read_string_cpp(); tag_type = br.read_string_cpp(); is_compressed = br.ReadBoolean(); publish_time_epoch_ms = br.ReadInt64(); var size = br.ReadInt32(); data_to_publish = br.ReadBytes(size); } public bool Equals(PublishMsg? other) => other is not null && connection == other.connection && publishing_app == other.publishing_app && tag == other.tag && tag_type == other.tag_type && is_compressed == other.is_compressed && publish_time_epoch_ms == other.publish_time_epoch_ms && data_to_publish.safe_sequence_equals(other.data_to_publish); public override bool Equals(object? obj) => Equals(obj as PublishMsg); public override int GetHashCode() => HashCode.Combine( connection, publishing_app, tag, is_compressed, publish_time_epoch_ms, data_to_publish?.Length); public override string ToString() { var data_txt = StreamUtilityMethods.first_n_bytes_to_string(data_to_publish ?? Array.Empty(), 4); return $"PublishMsg connection: {connection} publishing_app: {publishing_app} " + $"tag: {tag} tag_type: {tag_type} is_compressed: {is_compressed} " + $"publish_time_epoch_ms: {publish_time_epoch_ms} data len: {data_to_publish?.Length ?? 0} " + $"data (4): {data_txt}"; } } public enum PubSubMessageType { SUBSCRIBE = 0, UNSUBSCRIIBE, TRANSACTION, PUBLISH } public class PubSubClientMessage : IPubSubMessage { public PubSubMessageType msg_type; // only one of these wil be non null public SubscribeMsg? sub_msg; public UnsubscribeMsg? unsub_msg; public TransactionMsg? transaction_msg; public PublishMsg? pub_msg; public PubSubClientMessage() { } public PubSubClientMessage(SubscribeMsg sub_msg_) { sub_msg = sub_msg_; msg_type = PubSubMessageType.SUBSCRIBE; } public PubSubClientMessage(UnsubscribeMsg unsub_msg_) { unsub_msg = unsub_msg_; msg_type = PubSubMessageType.UNSUBSCRIIBE; } public PubSubClientMessage(TransactionMsg transaction_msg_) { transaction_msg = transaction_msg_; msg_type = PubSubMessageType.TRANSACTION; } public PubSubClientMessage(PublishMsg pub_msg_) { pub_msg = pub_msg_; msg_type = PubSubMessageType.PUBLISH; } public void serialize_into_str(Stream str) { var bw = new BinaryWriter(str); bw.Write((int)msg_type); switch (msg_type) { case PubSubMessageType.SUBSCRIBE: ValidateMsg(sub_msg).serialize_into_str(str); break; case PubSubMessageType.UNSUBSCRIIBE: ValidateMsg(unsub_msg).serialize_into_str(str); break; case PubSubMessageType.TRANSACTION: ValidateMsg(transaction_msg).serialize_into_str(str); break; case PubSubMessageType.PUBLISH: ValidateMsg(pub_msg).serialize_into_str(str); break; T ValidateMsg([NotNull]T? msg, [CallerArgumentExpression(nameof(msg))]string name = "") { if (msg is null) { throw new InvalidOperationException($"msg type is {msg_type} but {name} is null"); } return msg; } } } public byte[] serialize_into_bytes() { var ms = new MemoryStream(); var br = new BinaryReader(ms); serialize_into_str(ms); var len = Convert.ToInt32(ms.Position); ms.Position = 0; return br.ReadBytes(len); } public void construct_deserialize(Stream str) { var br = new BinaryReader(str); int type = br.ReadInt32(); msg_type = (PubSubMessageType)type; switch (msg_type) { case PubSubMessageType.SUBSCRIBE: sub_msg = new SubscribeMsg(); sub_msg.construct_deserialize(str); break; case PubSubMessageType.UNSUBSCRIIBE: unsub_msg = new UnsubscribeMsg(); unsub_msg.construct_deserialize(str); break; case PubSubMessageType.TRANSACTION: transaction_msg = new TransactionMsg(); transaction_msg.construct_deserialize(str); break; case PubSubMessageType.PUBLISH: pub_msg = new PublishMsg(); pub_msg.construct_deserialize(str); break; } } public void consruct_deserialize(byte[] msg) => construct_deserialize(new MemoryStream(msg) { Position = 0 }); public bool Equals(PubSubClientMessage? other) => other is not null && msg_type == other.msg_type && sub_msg.safe_equals(other.sub_msg) && pub_msg.safe_equals(other.pub_msg) && transaction_msg.safe_equals(other.transaction_msg) && unsub_msg.safe_equals(other.unsub_msg); public override bool Equals(object? obj) => Equals(obj as PubSubClientMessage); public override int GetHashCode() { var msgHash = msg_type switch { PubSubMessageType.SUBSCRIBE => sub_msg?.GetHashCode(), PubSubMessageType.UNSUBSCRIIBE => unsub_msg?.GetHashCode(), PubSubMessageType.TRANSACTION => transaction_msg?.GetHashCode(), PubSubMessageType.PUBLISH => pub_msg?.GetHashCode(), _ => 0, }; return HashCode.Combine(msg_type, msgHash); } public override string ToString() { var rv = $"type: {msg_type} "; rv += msg_type switch { PubSubMessageType.SUBSCRIBE => $"{sub_msg}", PubSubMessageType.UNSUBSCRIIBE => $"{sub_msg}", PubSubMessageType.TRANSACTION => $"{sub_msg}", PubSubMessageType.PUBLISH => $"{sub_msg}", _ => "Invalid msg type", }; return rv; } } public class PubSubServerMessage : IPubSubMessage, IEquatable { public string? tag; public string? tag_type; public byte[]? data; public void serialize_into_str(Stream str) { if (data is null) { throw new InvalidOperationException("Unable to serialize message because data is null"); } var bw = new BinaryWriter(str); bw.write_string_cpp(tag); bw.write_string_cpp(tag_type); bw.Write(data.Length); bw.Write(data); } public byte[] serialize_into_bytes() { var ms = new MemoryStream(); serialize_into_str(ms); var pos = Convert.ToInt32(ms.Position); ms.Position = 0; var br = new BinaryReader(ms); return br.ReadBytes(pos); } [MemberNotNull(nameof(tag), nameof(tag_type))] public void construct_deserialize(Stream str) { var br = new BinaryReader(str); tag = br.read_string_cpp(); tag_type = br.read_string_cpp(); var data_sz = br.ReadInt32(); data = br.ReadBytes(data_sz); } [MemberNotNull(nameof(tag), nameof(tag_type))] public void construct_deserialize(byte[] msg) => construct_deserialize(new MemoryStream(msg) { Position = 0}); public bool Equals(PubSubServerMessage? other) => other is not null && tag == other.tag && tag_type == other.tag_type && PubSubMessageUtils.compare_arrays(data.empty_if_null(), other.data.empty_if_null()); public override bool Equals(object? obj) => Equals(obj as PubSubServerMessage); public override int GetHashCode() => HashCode.Combine(tag, tag_type, data?.Length); public override string ToString() { var data_txt = StreamUtilityMethods.first_n_bytes_to_string(data ?? Array.Empty(), 4); return $"PubSubServerMessage tag: {tag} tag_type: {tag_type} data_len: {data?.Length ?? 0} " + $" data: {data_txt}"; } }