diff --git a/src/DataStreamSuite/DataStream/Primitives/Map/MapStream.cs b/src/DataStreamSuite/DataStream/Primitives/Map/MapStream.cs index f637d3f11..83409555d 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Map/MapStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Map/MapStream.cs @@ -1,6 +1,10 @@ +using bts; + using Bts.DataStream; +using Bts.DataStreamCore; using mbts.config; +using mbts.utils; using System; @@ -16,8 +20,8 @@ namespace BCG { [DataStreamUserRespresentation (false)] public class MapStream : MethodDrivenStream { - protected IStreamMapper myMapper; - protected IChangeableStreamMapper myChangeableMapper = null; + protected IStreamMapper? myMapper; + protected IChangeableStreamMapper? myChangeableMapper; private bool myUseNewMap = Configuration.get().UseNewMap; public class FuncWrapper : IStreamMapper { @@ -72,7 +76,7 @@ namespace BCG { protected override void myHandleFirstCall() { - + ArgumentNullException.ThrowIfNull(myMapper); IBlueTableSchema outputSchema = myMapper.getOutputSchema(myLastArrivedPacket.newRows.schema); myLastFiredPacket = new DataPacket(); myLastFiredPacket.newRows = new BlueTable(outputSchema); @@ -83,28 +87,34 @@ namespace BCG { } protected override void myHandleIncrementalAdd() { - //t.start (); - myLastFiredPacket.addedRows = new BlueTable(myLastFiredPacket.newRows.schema); - foreach (IBlueRow row in myLastArrivedPacket.addedRows) { - IBlueRow newRow = myMapper.map(row); - (myLastFiredPacket.newRows as BlueTable).addRow(newRow); - (myLastFiredPacket.addedRows as BlueTable).addRow(newRow); + if (myLastArrivedPacket.addedRows.Count == 0) { + Log.Gen.warn("MapStream.myHandleIncrementalAdd called with 0 addedRows"); + return; } - //t.stop (); - //System.Diagnostics.Trace.WriteLine ("adding " + myLastFiredPacket.addedRows.Count.ToString () + " : " + t.lastIntervalTime); - //System.Diagnostics.Trace.WriteLine (" "); + ArgumentNullException.ThrowIfNull(myMapper); + var addedRows = new BlueTable(myLastFiredPacket.newRows.schema); + var newRows = ExceptionAssert.Cast(myLastFiredPacket.newRows).To(); + foreach (IBlueRow row in myLastArrivedPacket.addedRows) { + var newRow = myMapper.map(row); + newRows.addRow(newRow); + addedRows.addRow(newRow); + } + myLastFiredPacket.addedRows = addedRows; } protected override void myHandleIncrementalDelete() { - //t.start (); - myLastFiredPacket.deletedRows = new BlueTable(myLastFiredPacket.newRows.schema); - foreach (IBlueRow row in myLastArrivedPacket.deletedRows) { - IBlueRow delRow = (myLastFiredPacket.newRows as BlueTable)[(int)row["id"]]; - (myLastFiredPacket.deletedRows as BlueTable).addRow(delRow); - (myLastFiredPacket.newRows as BlueTable).deleteRow(row); + if (myLastArrivedPacket.deletedRows.Count == 0) { + Log.Gen.warn("MapStream.myHandleIncrementalDelete called with 0 deletedRows"); + return; } - //t.stop (); - //System.Diagnostics.Trace.WriteLine ("deleting " + myLastFiredPacket.deletedRows.Count.ToString () + " : " + t.lastIntervalTime); + var deletedRows = new BlueTable(myLastFiredPacket.newRows.schema); + var newRows = ExceptionAssert.Cast(myLastFiredPacket.newRows).To(); + foreach (IBlueRow row in myLastArrivedPacket.deletedRows) { + var delRow = newRows[row.Id()]; + deletedRows.addRow(delRow); + newRows.deleteRow(row); + } + myLastFiredPacket.deletedRows = deletedRows; } diff --git a/src/DataStreamSuite/DataStream/Primitives/Map/SuperMapStream.cs b/src/DataStreamSuite/DataStream/Primitives/Map/SuperMapStream.cs index b4035e905..7d0d42ce8 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Map/SuperMapStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Map/SuperMapStream.cs @@ -1,11 +1,9 @@ +using Bts.DataStreamCore; + +using mbts.utils; + using System; using System.Collections.Generic; -using System.Text; - -using BCG.Utilities; -using BCG.Utilities.General; - -using mbts; namespace BCG { /// @@ -56,15 +54,15 @@ namespace BCG { return rows == null ? new IBlueRow[] { } : new List(rows).ToArray(); } - public event MapperChangeDelegate onMapperChange; - private void _fake() { onMapperChange(); } + public event MapperChangeDelegate? onMapperChange; } //maps the id of the underlying stream to the //rows of the resulting stream private Dictionary> myMap = new Dictionary>(); private int myID = 0; - private BlueTable myRows, myBaseRows; + private BlueTable? myRows; + private BlueTable? myBaseRows; private ViewStream myStream; private ISuperStreamMapper myMapper; public bool generate_ids = true; @@ -92,49 +90,40 @@ namespace BCG { myRows = null; myBaseRows = null; myMap.Clear(); - //myMapper.onMapperChange -= new MapperChangeDelegate(myHandleMapChange); } private void myHandleData(object newValue) { - DataPacket idp = newValue as DataPacket; - //HighPerformanceTimer timer = new HighPerformanceTimer(); - //timer.start(); - DataPacket dp = newValue as DataPacket; - myRows = myRows ?? new BlueTable(myMapper.getOutputSchema(dp.schema)); - myBaseRows = myBaseRows ?? new BlueTable(dp.schema); + var dp = ExceptionAssert.Cast(newValue).To(); + myRows ??= new BlueTable(myMapper.getOutputSchema(dp.schema)); + myBaseRows ??= new BlueTable(dp.schema); BlueDataUtility.absorbChanges(myBaseRows, dp.deletedRows, dp.addedRows); - BlueTable dels = new BlueTable(myRows.schema), - adds = new BlueTable(myRows.schema); + var dels = new BlueTable(myRows.schema); + var adds = new BlueTable(myRows.schema); foreach (IBlueRow r in dp.deletedRows) { - List mappedRows = myMap[(int)r["id"]]; + var mappedRows = myMap[r.Id()]; foreach (IBlueRow mr in mappedRows) { dels.addRow(mr); myRows.deleteRow(mr); } - myMap.Remove((int)r["id"]); + myMap.Remove(r.Id()); } foreach (IBlueRow r in dp.addedRows) { IBlueRow[] mappedRows = myMapper.map(r); foreach (IBlueRow mr in mappedRows) { IBlueRow idChangedMapRow = generate_ids ? BlueDataUtility.getNewReplacedRow(mr, myID++, "id") : mr; - if (!myMap.ContainsKey((int)r["id"])) { - myMap.Add((int)r["id"], new List()); - } - myMap[(int)r["id"]].Add(idChangedMapRow); + myMap.GetOrAdd(r.Id(), _ => new()).Add(idChangedMapRow); myRows.addRow(idChangedMapRow); adds.addRow(idChangedMapRow); } } - //timer.stop(); - // if (timer.lastIntervalTime > 20) - // {}//mbts.Logger.log(bts.log.Level.Info, bts.log.Subsystem.gen, String.Format("SuperMapper:myHandlerData for mapper {0} took {1} msecs", myMapper.GetType().ToString(), timer.lastIntervalTime)); myFireEvent(new DataPacket(myRows, adds, dels)); } private void myHandleMapChange() { - if (myBaseRows != null) + if (myBaseRows is not null) { myHandleData(new DataPacket(myBaseRows, new BlueTable(myBaseRows), new BlueTable(myBaseRows))); + } } } } diff --git a/src/DataStreamSuite/DataStream/Primitives/Misc/TestStream.cs b/src/DataStreamSuite/DataStream/Primitives/Misc/TestStream.cs index ebd1652ae..b6c6d6628 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Misc/TestStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Misc/TestStream.cs @@ -95,7 +95,7 @@ namespace BCG { sub_msg.serialize_into_str(str); str.Position = 0; bts.SubscribeMsg sub_msg_ = new bts.SubscribeMsg(); - sub_msg_.consruct_deserialize(str); + sub_msg_.construct_deserialize(str); bool res = sub_msg.Equals(sub_msg_); return new TestResult { test_name = "SubscribeMsg", @@ -117,7 +117,7 @@ namespace BCG { pub_msg.serialize_into_str(str); str.Position = 0; var pub_msg_ = new bts.PublishMsg(); - pub_msg_.consruct_deserialize(str); + pub_msg_.construct_deserialize(str); bool res = pub_msg.Equals(pub_msg_); return new TestResult { test_name = "PublishMsg", @@ -135,7 +135,7 @@ namespace BCG { msg.serialize_into_str(str); str.Position = 0; var msg_ = new bts.PubSubServerMessage(); - msg_.consruct_deserialize(str); + msg_.construct_deserialize(str); bool res = msg.Equals(msg_); return new TestResult { test_name = "PubSubServerMessage", @@ -156,7 +156,7 @@ namespace BCG { msg.serialize_into_str(str); str.Position = 0; var msg_ = new PubSubClientMessage(); - msg_.consruct_deserialize(str); + msg_.construct_deserialize(str); var res = msg.Equals(msg_); return new TestResult { test_name = "PubSubClientMessage", @@ -618,7 +618,7 @@ namespace BCG { private static TestResult _run_single_tag_data_test_clean() { // create message - var test_name = MethodBase.GetCurrentMethod().Name; + var test_name = MethodBase.GetCurrentMethod()?.Name; var schema = create_schema_with_all_types(); var tag = "DS_" + schema.tableName; BlueTable table = new BlueTable(schema); diff --git a/src/DataStreamSuite/DataStreamTests/PubSubMessages/PuSubMessagesTests.cs b/src/DataStreamSuite/DataStreamTests/PubSubMessages/PuSubMessagesTests.cs index a7ad5f9cd..72f27a9b3 100644 --- a/src/DataStreamSuite/DataStreamTests/PubSubMessages/PuSubMessagesTests.cs +++ b/src/DataStreamSuite/DataStreamTests/PubSubMessages/PuSubMessagesTests.cs @@ -38,7 +38,7 @@ namespace bts { sub_msg.serialize_into_str(str); str.Position = 0; var sub_msg_desered = new SubscribeMsg(); - sub_msg_desered.consruct_deserialize(str); + sub_msg_desered.construct_deserialize(str); Assert.That(sub_msg_desered.connection, Is.EqualTo(sub_msg.connection)); Assert.That(sub_msg_desered.subscribing_app, Is.EqualTo(sub_msg.subscribing_app)); compare_arrays(sub_msg.tags, sub_msg_desered.tags); @@ -57,7 +57,7 @@ namespace bts { unsub_msg.serialize_into_str(str); str.Position = 0; var sub_msg_desered = new SubscribeMsg(); - sub_msg_desered.consruct_deserialize(str); + sub_msg_desered.construct_deserialize(str); Assert.That(sub_msg_desered.connection, Is.EqualTo(unsub_msg.connection)); Assert.That(sub_msg_desered.subscribing_app, Is.EqualTo(unsub_msg.subscribing_app)); compare_arrays(unsub_msg.tags, sub_msg_desered.tags); @@ -80,7 +80,7 @@ namespace bts { pub_msg.serialize_into_str(str); str.Position = 0; PublishMsg pub_msg_desered = new PublishMsg(); - pub_msg_desered.consruct_deserialize(str); + pub_msg_desered.construct_deserialize(str); Assert.That(pub_msg_desered.connection, Is.EqualTo(pub_msg.connection)); Assert.That(pub_msg_desered.publishing_app, Is.EqualTo(pub_msg.publishing_app)); Assert.That(pub_msg_desered.tag, Is.EqualTo(pub_msg.tag)); @@ -102,7 +102,7 @@ namespace bts { ps_server_msg.serialize_into_str(str); PubSubServerMessage ps_server_msg_desered = new PubSubServerMessage(); str.Position = 0; - ps_server_msg_desered.consruct_deserialize(str); + ps_server_msg_desered.construct_deserialize(str); Assert.That(ps_server_msg_desered.tag, Is.EqualTo(ps_server_msg.tag)); Assert.That(ps_server_msg_desered.tag_type, Is.EqualTo(ps_server_msg.tag_type)); compare_arrays(ps_server_msg.data, ps_server_msg_desered.data); @@ -125,7 +125,7 @@ namespace bts { msg.serialize_into_str(str); PubSubClientMessage msg_deser = new PubSubClientMessage(); str.Position = 0; - msg_deser.consruct_deserialize(str); + msg_deser.construct_deserialize(str); Assert.That(msg_deser.msg_type, Is.EqualTo(msg.msg_type)); Assert.That(msg_deser.sub_msg, Is.EqualTo(msg.sub_msg)); } diff --git a/src/DataStreamSuite/PubSub/Client/Communication/ManagedTCP/PubSubServerManagedTCPConnection.cs b/src/DataStreamSuite/PubSub/Client/Communication/ManagedTCP/PubSubServerManagedTCPConnection.cs index 34c7d0d4f..e690c849f 100644 --- a/src/DataStreamSuite/PubSub/Client/Communication/ManagedTCP/PubSubServerManagedTCPConnection.cs +++ b/src/DataStreamSuite/PubSub/Client/Communication/ManagedTCP/PubSubServerManagedTCPConnection.cs @@ -30,15 +30,15 @@ namespace BCG { public class PubSubServerManagedTCPConnection : IPubSubServerConnection,// external facing interface to communicate msgs to Pubsubserver ITCPMessageHandler, // to get messages from ITCPCOnnection ITCPConnectionHandler { // to get connections from AsyncTCPConnector_APM - private ITCPConnection _connection; + private ITCPConnection? _connection; private readonly RemoteConnectionInfo _server_rc_info; private readonly bool _is_sub; // if _data_callback is not subscribed to , the user of this class can poll updates, // in which case this queue will be used private SyncQueue _in_data_queue = new SyncQueue(); private DateTime _last_hb = DateTime.Now; - private PubSubServerConnectionStatusChange _conn_callback; - private PubSubServerDataDelegate _data_callback; + private PubSubServerConnectionStatusChange? _conn_callback; + private PubSubServerDataDelegate? _data_callback; private readonly TimeSpan _connection_drop_threshold = PubSubConfig.Instance.ConnectionDropThreshold(); private ThreadSafeQueue _conn_cb_queue = new ThreadSafeQueue(); @@ -51,10 +51,10 @@ namespace BCG { DISCONNECTED } public readonly Status connection_satus; - public readonly ITCPConnection new_connection = null; - public ConnectionStatusChange(Status connection_satus_, ITCPConnection new_connection_) { - connection_satus = connection_satus_; - new_connection = new_connection_; + public readonly ITCPConnection? new_connection; + public ConnectionStatusChange(Status connection_satus, ITCPConnection? new_connection) { + this.connection_satus = connection_satus; + this.new_connection = new_connection; } public override string ToString() { return connection_satus.ToString(); @@ -101,7 +101,7 @@ namespace BCG { var i = 0; foreach (var b in rv_raw) { PubSubServerMessage ps_msg = new PubSubServerMessage(); - ps_msg.consruct_deserialize(b); + ps_msg.construct_deserialize(b); bts.Log.Gen.info($"PubSubServerManagedTCPConnection.get_updates downloaded: {ps_msg} msg_no: {i} / {rv_raw.Count}"); rv.Add(ps_msg); i++; @@ -162,10 +162,12 @@ namespace BCG { // TODO check for disconenction switch (message.messageType) { case RemoteMessageType.Data: - if (_data_callback != null) + ArgumentNullException.ThrowIfNull(message.message); + if (_data_callback != null) { _data_callback(message.remoteConnectionInfo.ToString(), message.message); - else + } else { _in_data_queue.enqueue(message.message); + } break; case RemoteMessageType.Heartbeat: _last_hb = DateTime.Now; @@ -200,20 +202,6 @@ namespace BCG { #endregion - void _check_missing_hb() { - if (!_connection.isDisposed) { - TimeSpan hb_interval = DateTime.Now - _last_hb; - bool hb_missed = hb_interval > _connection_drop_threshold; - if (hb_missed) { - bts.Log.Gen.warn("PubSubServerManagedTCPConnection._dispose_and_reconnect_if_missing_hb disposing connecion: {myServerAddress}"); - ConnectionStatusChange ci = new ConnectionStatusChange(ConnectionStatusChange.Status.DISCONNECTED, null); - _conn_cb_queue.enqueue(ci); - } - } else { - // already disposed, nothing to do - } - } - private static string _connection_txt(ITCPConnection conn) { if (conn != null) { return $"conn_type: {conn.GetType()} server: {conn.get_peer_address()} self: {conn.get_self_address()}"; @@ -233,22 +221,22 @@ namespace BCG { if (_conn_cb_queue.getCount() > 0) { var css = _conn_cb_queue.dequeueAll(); foreach (var c in css) { + Log.notnull(c); Log.Gen.info($"PubSubServerManagedTCPConnection.doWork c: {c}"); switch (c.connection_satus) { case ConnectionStatusChange.Status.CONNECTED: { - Log.assert(c.new_connection != null); + Log.notnull(c.new_connection); c.new_connection.register(this); _connection = c.new_connection; _last_hb = DateTime.Now; - if (_conn_callback != null) - _conn_callback(_server_rc_info.ToString(), true); + _conn_callback?.Invoke(_server_rc_info.ToString(), true); break; } case ConnectionStatusChange.Status.DISCONNECTED: { + Log.notnull(_connection); _connection.Dispose(); _connect_to_ps_server(); - if (_conn_callback != null) - _conn_callback(_server_rc_info.ToString(), false); + _conn_callback?.Invoke(_server_rc_info.ToString(), false); break; } } @@ -257,6 +245,5 @@ namespace BCG { } #endregion - } } diff --git a/src/DataStreamSuite/PubSub/Client/IPubSubServerTagHandler.cs b/src/DataStreamSuite/PubSub/Client/IPubSubServerTagHandler.cs index 874f974be..ed9f9b8c4 100644 --- a/src/DataStreamSuite/PubSub/Client/IPubSubServerTagHandler.cs +++ b/src/DataStreamSuite/PubSub/Client/IPubSubServerTagHandler.cs @@ -18,7 +18,7 @@ namespace BCG { /// /// /// - void update(string tag, byte[] msg); + void update(string tag, byte[]? msg); /// /// gets the entire content of the tag /// diff --git a/src/DataStreamSuite/PubSub/Client/MetaData/PubSubClientMessage.cs b/src/DataStreamSuite/PubSub/Client/MetaData/PubSubClientMessage.cs index 9f26588c5..df99394b6 100644 --- a/src/DataStreamSuite/PubSub/Client/MetaData/PubSubClientMessage.cs +++ b/src/DataStreamSuite/PubSub/Client/MetaData/PubSubClientMessage.cs @@ -1,364 +1,405 @@ +using BCG; + +using bts.collections; +using bts.utils; + using System; -using System.IO; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.IO; +using System.Linq; +using System.Runtime.CompilerServices; -namespace bts { - using BCG; - - using System.IO; - using System.Linq; - - - public interface IPubSubMessage { - void serialize_into_str(Stream str); - void consruct_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 { - - public string connection; - public string subscribing_app = ClientInfo.thisClient.app; - public string[] tags; - public void serialize_into_str(Stream str) { - BinaryWriter 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 consruct_deserialize(Stream str) { - BinaryReader br = new BinaryReader(str); - this.connection = br.read_string_cpp(); - this.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 override string ToString() { - var tgs_str = String.Empty; - foreach (var t in tags) - tgs_str += t; - return $"{GetType().Name} connection: {connection} subscribing_app: {subscribing_app} tags: {tgs_str}"; - } - public override bool Equals(object? obj) { - if (obj != null && obj is SubscribeMsg) { - var obj_sub_msg = obj as SubscribeMsg; - return PubSubMessageUtils.compare_obj(this.connection, obj_sub_msg.connection) && - PubSubMessageUtils.compare_obj(this.subscribing_app, obj_sub_msg.subscribing_app) && - PubSubMessageUtils.compare_arrays(tags, obj_sub_msg.tags); - // TODO compare tags - } - return false; - } - - public override int GetHashCode() { - var str = ToString(); - return str.GetHashCode(); - } - } - - // Unsubscribe //////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////////////// - public class UnsubscribeMsg : SubscribeMsg { - } - - // Tasnaction ///////////////////////////////////////////////////////////// - /////////////////////////////////////////////////////////////////////////// - public class TransactionMsg : IPubSubMessage { - uint n_msgs; - public void serialize_into_str(Stream str) { - BinaryWriter bw = new BinaryWriter(str); - bw.Write(n_msgs); - } - public void consruct_deserialize(Stream str) { - BinaryReader br = new BinaryReader(str); - n_msgs = br.ReadUInt32(); - } - public override string ToString() { - return $"TransactionMsg n_msgs: {n_msgs}"; - } - - public override bool Equals(object? obj) { - if (obj != null && (obj is TransactionMsg)) { - TransactionMsg other_ = obj as TransactionMsg; - return n_msgs == other_.n_msgs; - } - return false; - } - } - - // Publish //////////////////////////////////////////////////////////////// - /// /////////////////////////////////////////////////////////////////////// - public class PublishMsg : IPubSubMessage { - 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; - - public void sanity_check() { - Log.assert(!String.IsNullOrEmpty(tag_type)); - Log.assert(!String.IsNullOrEmpty(tag)); - Log.assert(!String.IsNullOrEmpty(connection)); - } - - public void serialize_into_str(Stream str) { - sanity_check(); - BinaryWriter 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 consruct_deserialize(Stream str) { - BinaryReader 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 override string ToString() { - var data_txt = StreamUtilityMethods.first_n_bytes_to_string(data_to_publish, 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} " + - $"data (4): {data_txt}"; - } - - public override bool Equals(object? obj) { - if (obj != null && obj is PublishMsg) { - PublishMsg pub_msg_ = obj as PublishMsg; - return connection.Equals(pub_msg_.connection) && - publishing_app.Equals(pub_msg_.publishing_app) && - tag.Equals(pub_msg_.tag) && - tag_type.Equals(pub_msg_.tag_type) && - is_compressed.Equals(pub_msg_.is_compressed) && - publish_time_epoch_ms.Equals(pub_msg_.publish_time_epoch_ms) && - PubSubMessageUtils.compare_arrays(data_to_publish, pub_msg_.data_to_publish); - } - return false; - } - - } - 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 = null; - public UnsubscribeMsg unsub_msg = null; - public TransactionMsg transaction_msg = null; - public PublishMsg pub_msg = null; - - 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) { - BinaryWriter bw = new BinaryWriter(str); - bw.Write((int)msg_type); - switch (msg_type) { - case PubSubMessageType.SUBSCRIBE: - sub_msg.serialize_into_str(str); - break; - case PubSubMessageType.UNSUBSCRIIBE: - unsub_msg.serialize_into_str(str); - break; - case PubSubMessageType.TRANSACTION: - transaction_msg.serialize_into_str(str); - break; - case PubSubMessageType.PUBLISH: - pub_msg.serialize_into_str(str); - break; - } - } - - public byte[] serialize_into_bytes() { - MemoryStream ms = new MemoryStream(); - BinaryReader br = new BinaryReader(ms); - serialize_into_str(ms); - var len = Convert.ToInt32(ms.Position); - ms.Position = 0; - return br.ReadBytes(len); - } - - public void consruct_deserialize(Stream str) { - BinaryReader br = new BinaryReader(str); - int type = br.ReadInt32(); - msg_type = (PubSubMessageType)type; - switch (msg_type) { - case PubSubMessageType.SUBSCRIBE: - sub_msg = new SubscribeMsg(); - sub_msg.consruct_deserialize(str); - break; - case PubSubMessageType.UNSUBSCRIIBE: - unsub_msg = new UnsubscribeMsg(); - unsub_msg.consruct_deserialize(str); - break; - case PubSubMessageType.TRANSACTION: - transaction_msg = new TransactionMsg(); - transaction_msg.consruct_deserialize(str); - break; - case PubSubMessageType.PUBLISH: - pub_msg = new PublishMsg(); - pub_msg.consruct_deserialize(str); - break; - } - } - - public void consruct_deserialize(byte[] msg) { - MemoryStream ms = new MemoryStream(msg); - ms.Position = 0; - consruct_deserialize(ms); - } - - public override string ToString() { - var rv = $"type: {msg_type} "; - switch (msg_type) { - case PubSubMessageType.SUBSCRIBE: - rv += $"{sub_msg}"; - break; - case PubSubMessageType.UNSUBSCRIIBE: - rv += $"{unsub_msg}"; - break; - case PubSubMessageType.TRANSACTION: - rv += $"{transaction_msg}"; - break; - case PubSubMessageType.PUBLISH: - rv += $"{pub_msg}"; - break; - } - return rv; - } - - public override bool Equals(object? obj) { - if (obj != null && obj is PubSubClientMessage) { - var msg_ = obj as PubSubClientMessage; - return PubSubMessageUtils.compare_obj(msg_type, msg_.msg_type) && - PubSubMessageUtils.compare_obj(sub_msg, msg_.sub_msg) && - PubSubMessageUtils.compare_obj(pub_msg, msg_.pub_msg) && - PubSubMessageUtils.compare_obj(transaction_msg, msg_.transaction_msg) && - PubSubMessageUtils.compare_obj(unsub_msg, msg_.unsub_msg); - } - return false; - } - - - } - - // - public class PubSubServerMessage : IPubSubMessage { - public string tag; - public string tag_type; - public byte[] data; - public void serialize_into_str(Stream str) { - BinaryWriter 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() { - MemoryStream ms = new MemoryStream(); - serialize_into_str(ms); - var pos = Convert.ToInt32(ms.Position); - ms.Position = 0; - BinaryReader br = new BinaryReader(ms); - return br.ReadBytes(pos); - } - public void consruct_deserialize(Stream str) { - BinaryReader 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); - } - - public void consruct_deserialize(byte[] msg) { - MemoryStream str = new MemoryStream(msg); - str.Position = 0; - consruct_deserialize(str); - } - - public override string ToString() { - var data_txt = StreamUtilityMethods.first_n_bytes_to_string(data, 4); - return $"PubSubServerMessage tag: {tag} tag_type: {tag_type} data_len: {data.Length} " + - $" data: {data_txt}"; - } - - public override bool Equals(object? obj) { - if (obj != null && obj is PubSubServerMessage) { - PubSubServerMessage msg_ = obj as PubSubServerMessage; - return PubSubMessageUtils.compare_obj(tag, msg_.tag) && - PubSubMessageUtils.compare_obj(tag_type, msg_.tag_type) && - PubSubMessageUtils.compare_arrays(data, msg_.data); - } - return false; - } - - } +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}"; + } +} diff --git a/src/DataStreamSuite/PubSub/Client/SubscriptionUtilities/ManagedSubscriber.cs b/src/DataStreamSuite/PubSub/Client/SubscriptionUtilities/ManagedSubscriber.cs index c52b0d9b9..0c160ecbd 100644 --- a/src/DataStreamSuite/PubSub/Client/SubscriptionUtilities/ManagedSubscriber.cs +++ b/src/DataStreamSuite/PubSub/Client/SubscriptionUtilities/ManagedSubscriber.cs @@ -17,7 +17,7 @@ namespace BCG { /// with PubSub, as some clients are still using ManagedSubcribers) /// public class ManagedSubscriber : IWorker, IDisposable { - private static ManagedSubscriber _self; + private static ManagedSubscriber? _self; private SubscriberCore _subcore; // private IDictionary myTypedDispatchTable = new Hashtable(), // myRawDispatchTable = new Hashtable(); @@ -37,8 +37,7 @@ namespace BCG { } public void subscribe(string tag, OnData on_data) { - List tag_cbs = null; - if (!_dispatch_table.TryGetValue(tag, out tag_cbs)) { + if (!_dispatch_table.TryGetValue(tag, out var tag_cbs)) { tag_cbs = new List(); _dispatch_table.Add(tag, tag_cbs); _subcore.subscribe(tag); ; @@ -74,8 +73,7 @@ namespace BCG { // } public void unsubcribe(string tag, OnData on_data) { - List tag_cbs = null; - if (_dispatch_table.TryGetValue(tag, out tag_cbs)) { + if (_dispatch_table.TryGetValue(tag, out var tag_cbs)) { tag_cbs.Remove(on_data); if (tag_cbs.Count == 0) { _dispatch_table.Remove(tag); @@ -87,10 +85,10 @@ namespace BCG { private void _fire_events(IEnumerable updates) { // TODO foreach (var u in updates) { - List cbs = null; - if (_dispatch_table.TryGetValue(u.tag, out cbs)) { + ArgumentNullException.ThrowIfNull(u.tag); + if (_dispatch_table.TryGetValue(u.tag, out var cbs)) { foreach (var cb in cbs) { - cb.Invoke(u.tag, u.data); + cb.Invoke(u.tag, u.data ?? Array.Empty()); } } } @@ -102,10 +100,8 @@ namespace BCG { } } - public static ManagedSubscriber instance { - get { return _self = _self == null ? new ManagedSubscriber() : _self; } - } - + public static ManagedSubscriber instance => + _self ??= new(); #region IDisposable Members diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnection_APM.cs b/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnection_APM.cs index f7da41f20..eee1e3f8b 100644 --- a/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnection_APM.cs +++ b/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnection_APM.cs @@ -35,6 +35,7 @@ using System.Threading.Tasks; using System.Linq; using Bts.PubSub; using Spark.Core; +using mbts.utils; namespace bts { @@ -98,23 +99,20 @@ namespace bts public class AsyncSendMessage : AsyncMessageBase { public string message_id; - public SendConfirmationDelegate on_send_del; + public SendConfirmationDelegate? on_send_del; //private int _n_transfers = 0; public AsyncSendMessage(byte[] the_buffer, int the_offset) : this(the_buffer, the_offset, String.Empty, null) { } - public AsyncSendMessage(byte[] the_buffer, int the_transfer_ptr, string the_message_id, SendConfirmationDelegate the_on_send_del) - : base(the_buffer, the_transfer_ptr) - { - message_id = the_message_id; - on_send_del = the_on_send_del; - } - public override string ToString() - { - return base.ToString(); + public AsyncSendMessage(byte[] buffer, int transfer_ptr, string message_id, SendConfirmationDelegate? on_send_del) : base(buffer, transfer_ptr) { + this.message_id = message_id; + this.on_send_del = on_send_del; } + public override string ToString() { + return base.ToString(); + } } public class AsyncReceiveMessage : AsyncMessageBase @@ -153,7 +151,7 @@ namespace bts public class ErrorMessage { public readonly SocketError? socket_error = null; - public readonly Exception exception = null; + public readonly Exception? exception; public readonly bool is_exception = false; public readonly string context = String.Empty; public ErrorMessage(SocketError socket_error_, string context_) @@ -187,11 +185,11 @@ namespace bts private readonly RemoteConnectionInfo _peer_conn_info, _self_conn_info; private readonly ITCPConnection.ConnectionSide _connection_side; private readonly Socket _sock; - private ITCPMessageHandler _message_handler = null; + private ITCPMessageHandler? _message_handler; private bool _is_alive = true; - private byte[] _cached_hb_message = null; + private byte[]? _cached_hb_message; private readonly TimeSpan _hb_period = TimeSpan.FromSeconds(1); // TODO get it from config private MasterWorker _master_worker = MasterWorker.getInstance(true); private bool _hb_received_yet = false, _data_received_yet = false, _hb_sent_once = false; @@ -288,19 +286,8 @@ namespace bts #region IWorker Members bool _last_connected = false; - public void doWork() - { - if (!_is_alive) - return; - return; - // disable hbs for now - if (!_hb_sent_once) - { - bts.Log.Gen.info("Sending heartbeat to peer {_peer_conn_info} for the first time"); - _hb_sent_once = true; - } - var msg = _build_hb_message(); - _begin_or_resume_sending(new AsyncSendMessage(msg, 0)); + public void doWork() { + // TODO: check heartbeats } private static int _tid() @@ -350,11 +337,9 @@ namespace bts _on_send_async(res); } - private void _on_send_async(IAsyncResult res) - { - AsyncSendMessage message = res.AsyncState as AsyncSendMessage; - if (_send_key_map.Contains(message.key)) - { + private void _on_send_async(IAsyncResult res) { + var message = ExceptionAssert.Cast(res.AsyncState).To(); + if (_send_key_map.Contains(message.key)) { bts.Log.Gen.warn($"AsyncTCPConnection_APM._on_send key: {message.key} already handled"); return; } @@ -385,8 +370,8 @@ namespace bts if (message.fully_transfered()) { // fully sent, if confirmation needs to be sent out - if (!String.IsNullOrEmpty(message.message_id)) - { + if (!string.IsNullOrEmpty(message.message_id)) { + ArgumentNullException.ThrowIfNull(message.on_send_del); message.on_send_del(message.message_id, true); } } @@ -422,7 +407,7 @@ namespace bts int msg_sz = 4; bw.Write(msg_sz); bw.Write(HB_MSG_SIGNAL); - _cached_hb_message = (bw.BaseStream as MemoryStream).ToArray(); + _cached_hb_message = ExceptionAssert.Cast(bw.BaseStream).To().ToArray(); } return _cached_hb_message; } @@ -518,11 +503,9 @@ namespace bts // this will always be called on the non main-thread. the reason this is on its own thread is that this function ends up // calling _begin_or_resume_reading, which in tuns calls _on_read_async. So to not cause a stack overflow, this is broken // always called on a different thread than _begin_or_resume_reading - private void _on_read_async(IAsyncResult res) - { - AsyncReceiveMessage rcv_message = res.AsyncState as AsyncReceiveMessage; - if (_read_key_map.Contains(rcv_message.key)) - { + private void _on_read_async(IAsyncResult res) { + var rcv_message = ExceptionAssert.Cast(res.AsyncState).To(); + if (_read_key_map.Contains(rcv_message.key)) { bts.Log.Gen.warn($"AsyncTCPConnection_APM._on_read key: {rcv_message.key} already handled"); return; } @@ -633,6 +616,7 @@ namespace bts //bts.Log.Gen.info($"AsyncTCPConnection_APM HB received for the first time from peer {_peer_conn_info}"); } _last_hb_received_time = DateTime.Now; + ArgumentNullException.ThrowIfNull(_message_handler); _message_handler.onData(new RemoteMessage(null, RemoteMessageType.Heartbeat, _peer_conn_info)); } @@ -652,6 +636,7 @@ namespace bts //bts.Log.Gen.info($"AsyncTCPConnection_APM data received for the first time from peer {_peer_conn_info}"); } // could be an index here + ArgumentNullException.ThrowIfNull(_message_handler); _message_handler.onData(new RemoteMessage(data, RemoteMessageType.Data, _peer_conn_info)); } @@ -672,13 +657,13 @@ namespace bts { bts.Log.Gen.info($"AsyncTCPConnection_APM.Dispose this: {this} shutting down socket now"); _sock.Shutdown(SocketShutdown.Both); + } catch (OutOfMemoryException) { + throw; } - catch (Exception ex) - { - String.Format("AsyncTCPConnection_APM.Dispose exception: {ex} thrown when closing socket"); - if (ex is OutOfMemoryException) - throw ex; + catch (Exception ex) { + Log.Gen.warn($"AsyncTCPConnection_APM.Dispose exception thrown when closing socket: {ex}"); } + ArgumentNullException.ThrowIfNull(_message_handler); _message_handler.onData(new RemoteMessage(null, RemoteMessageType.Disconnection, _peer_conn_info)); } } @@ -692,8 +677,9 @@ namespace bts lock (this) { Log.Gen.error($"AsyncTCPConnection_APM._handle_error this: {this} error_msg: {err_msg}"); - if (err_msg.oom) - throw err_msg.exception; + if (err_msg.oom) { + throw Log.notnull(err_msg.exception); + } _dipose_impetus = $"AsyncTCPConnection_APM._handle_error this: {this} error_msg: {err_msg}"; Dispose(); } diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/RemoteMessage.cs b/src/DataStreamSuite/PubSub/ManagedTCP/RemoteMessage.cs index ccfc9a11d..142691e9f 100644 --- a/src/DataStreamSuite/PubSub/ManagedTCP/RemoteMessage.cs +++ b/src/DataStreamSuite/PubSub/ManagedTCP/RemoteMessage.cs @@ -19,18 +19,18 @@ namespace BCG /// public class RemoteMessage { - private byte[] myMsg; + private byte[]? myMsg; private RemoteMessageType myMsgType; private RemoteConnectionInfo myConnInfo; - public RemoteMessage(byte[] message, RemoteMessageType messageType, RemoteConnectionInfo connInfo) + public RemoteMessage(byte[]? message, RemoteMessageType messageType, RemoteConnectionInfo connInfo) { myMsg = message; myMsgType = messageType; myConnInfo = connInfo; } - public byte[] message + public byte[]? message { get { return myMsg; } } diff --git a/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Tar/TarArchive.cs b/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Tar/TarArchive.cs index adc4bd93c..331634129 100644 --- a/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Tar/TarArchive.cs +++ b/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Tar/TarArchive.cs @@ -33,6 +33,7 @@ // exception statement from your version. using System; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Text; @@ -69,20 +70,20 @@ namespace BCG.Utilities.SharpZipLib.Tar { protected bool asciiTranslate; protected int userId; - protected string userName; + protected string? userName; protected int groupId; - protected string groupName; + protected string? groupName; - protected string rootPath; - protected string pathPrefix; + protected string? rootPath; + protected string? pathPrefix; protected int recordSize; - protected byte[] recordBuf; + protected byte[]? recordBuf; - protected TarInputStream tarIn; - protected TarOutputStream tarOut; + protected TarInputStream? tarIn; + protected TarOutputStream? tarOut; - public event ProgressMessageHandler ProgressMessageEvent; + public event ProgressMessageHandler? ProgressMessageEvent; protected virtual void OnProgressMessageEvent(string message) { @@ -283,11 +284,7 @@ namespace BCG.Utilities.SharpZipLib.Tar { /// /// The current user name. /// - public string UserName { - get { - return this.userName; - } - } + public string? UserName => userName; /// /// Get the group id being used for archive entry headers. @@ -307,11 +304,7 @@ namespace BCG.Utilities.SharpZipLib.Tar { /// /// The current group name. /// - public string GroupName { - get { - return this.groupName; - } - } + public string? GroupName => groupName; /// /// Get the archive's record size. Because of its history, tar @@ -359,8 +352,9 @@ namespace BCG.Utilities.SharpZipLib.Tar { /// public void ListContents() { + assertTarIn(); while (true) { - TarEntry entry = this.tarIn.GetNextEntry(); + var entry = tarIn.GetNextEntry(); if (entry == null) { if (this.debug) { @@ -380,8 +374,9 @@ namespace BCG.Utilities.SharpZipLib.Tar { /// public void ExtractContents(string destDir) { + assertTarIn(); while (true) { - TarEntry entry = this.tarIn.GetNextEntry(); + var entry = tarIn.GetNextEntry(); if (entry == null) { if (this.debug) { @@ -394,8 +389,9 @@ namespace BCG.Utilities.SharpZipLib.Tar { } } - void EnsureDirectoryExists(string directoryName) + void EnsureDirectoryExists(string? directoryName) { + ArgumentException.ThrowIfNullOrEmpty(directoryName); if (!Directory.Exists(directoryName)) { try { Directory.CreateDirectory(directoryName); @@ -455,7 +451,7 @@ namespace BCG.Utilities.SharpZipLib.Tar { if (entry.IsDirectory) { EnsureDirectoryExists(destFile); } else { - string parentDirectory = Path.GetDirectoryName(destFile); + var parentDirectory = Path.GetDirectoryName(destFile); EnsureDirectoryExists(parentDirectory); if (this.keepOldFiles && File.Exists(destFile)) { @@ -490,13 +486,14 @@ namespace BCG.Utilities.SharpZipLib.Tar { // } } - StreamWriter outw = null; + StreamWriter? outw = null; if (asciiTrans) { outw = new StreamWriter(outputStream); } byte[] rdbuf = new byte[32 * 1024]; + assertTarIn(); while (true) { int numRead = this.tarIn.Read(rdbuf, 0, rdbuf.Length); @@ -508,7 +505,7 @@ namespace BCG.Utilities.SharpZipLib.Tar { for (int off = 0, b = 0; b < numRead; ++b) { if (rdbuf[b] == 10) { string s = Encoding.ASCII.GetString(rdbuf, off, (b - off)); - outw.WriteLine(s); + outw!.WriteLine(s); off = b + 1; } } @@ -518,7 +515,7 @@ namespace BCG.Utilities.SharpZipLib.Tar { } if (asciiTrans) { - outw.Close(); + outw!.Close(); } else { outputStream.Close(); } @@ -543,8 +540,8 @@ namespace BCG.Utilities.SharpZipLib.Tar { { bool asciiTrans = false; - string tempFileName = null; - string eFile = entry.File; + string? tempFileName = null; + string? eFile = entry.File; // Work on a copy of the entry so we can manipulate it. // Note that we must distinguish how the entry was constructed. @@ -568,6 +565,9 @@ namespace BCG.Utilities.SharpZipLib.Tar { } if (this.asciiTranslate && !entry.IsDirectory) { + if (eFile is null) { + throw new InvalidOperationException("entry file is null"); + } asciiTrans = !IsBinary(eFile); // original java source : // MimeType mime = null; @@ -606,7 +606,7 @@ namespace BCG.Utilities.SharpZipLib.Tar { Stream outStream = new BufferedStream(File.Create(tempFileName)); while (true) { - string line = inStream.ReadLine(); + var line = inStream.ReadLine(); if (line == null) { break; } @@ -625,7 +625,7 @@ namespace BCG.Utilities.SharpZipLib.Tar { } } - string newName = null; + string? newName = null; if (this.rootPath != null) { if (entry.Name.StartsWith(this.rootPath)) { @@ -641,6 +641,7 @@ namespace BCG.Utilities.SharpZipLib.Tar { entry.Name = newName; } + assertTarOut(); this.tarOut.PutNextEntry(entry); if (entry.IsDirectory) { @@ -651,6 +652,9 @@ namespace BCG.Utilities.SharpZipLib.Tar { } } } else { + if (eFile is null) { + throw new InvalidOperationException("entry file is null"); + } Stream inputStream = File.OpenRead(eFile); int numWritten = 0; byte[] eBuf = new byte[32 * 1024]; @@ -675,6 +679,20 @@ namespace BCG.Utilities.SharpZipLib.Tar { this.tarOut.CloseEntry(); } } + + [MemberNotNull(nameof(tarIn))] + void assertTarIn() { + if (tarIn is null) { + throw new InvalidOperationException("tarIn is null"); + } + } + + [MemberNotNull(nameof(tarOut))] + void assertTarOut() { + if (tarOut is null) { + throw new InvalidOperationException("tarOut is null"); + } + } } } /* The original Java file had this header: diff --git a/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Tar/TarEntry.cs b/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Tar/TarEntry.cs index de4de9779..8a83319b0 100644 --- a/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Tar/TarEntry.cs +++ b/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Tar/TarEntry.cs @@ -33,6 +33,7 @@ // exception statement from your version. using System; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Text; @@ -92,12 +93,12 @@ namespace BCG.Utilities.SharpZipLib.Tar { /// /// If this entry represents a File, this references it. /// - protected string file; + protected string? file; /// /// This is the entry's header information. /// - protected TarHeader header; + protected TarHeader? header; /// /// Only Create Entries with the static CreateXYZ methods or a headerBuffer. @@ -151,12 +152,16 @@ namespace BCG.Utilities.SharpZipLib.Tar { /// /// Initialization code common to all constructors. /// + [MemberNotNull(nameof(header))] void Initialize() { this.file = null; this.header = new TarHeader(); } + public bool Equals(TarEntry? other) => + other is not null && header?.name?.ToString() == other.header?.name?.ToString(); + /// /// Determine if the two entries are equal. Equality is determined /// by the header names being equal. @@ -164,22 +169,15 @@ namespace BCG.Utilities.SharpZipLib.Tar { /// /// True if the entries are equal. /// - public override bool Equals(object it) - { - if (!(it is TarEntry)) { - return false; - } - return this.header.name.ToString().Equals(((TarEntry)it).header.name.ToString()); - } + public override bool Equals(object? obj) => + Equals(obj as TarEntry); /// /// Must be overridden when you override Equals. /// - public override int GetHashCode() - { - return this.header.name.ToString().GetHashCode(); - } - + public override int GetHashCode() => + header?.name?.ToString()?.GetHashCode() ?? 0; + /// /// Determine if the given entry is a descendant of this entry. @@ -192,10 +190,12 @@ namespace BCG.Utilities.SharpZipLib.Tar { /// /// True if entry is a descendant of this. /// - public bool IsDescendent(TarEntry desc) - { - return desc.header.name.ToString().StartsWith(this.header.name.ToString()); - } + [MemberNotNull(nameof(header))] + public bool IsDescendent(TarEntry desc) { + assertHeader(); + desc.assertHeader(); + return desc.header.name.ToString().StartsWith(header.name.ToString()); + } /// /// Get this entry's header. @@ -205,6 +205,7 @@ namespace BCG.Utilities.SharpZipLib.Tar { /// public TarHeader TarHeader { get { + assertHeader(); return this.header; } } @@ -214,9 +215,11 @@ namespace BCG.Utilities.SharpZipLib.Tar { /// public string Name { get { + assertHeader(); return this.header.name.ToString(); } set { + assertHeader(); this.header.name = new StringBuilder(value); } } @@ -226,9 +229,11 @@ namespace BCG.Utilities.SharpZipLib.Tar { /// public int UserId { get { + assertHeader(); return this.header.userId; } set { + assertHeader(); this.header.userId = value; } } @@ -238,9 +243,11 @@ namespace BCG.Utilities.SharpZipLib.Tar { /// public int GroupId { get { + assertHeader(); return this.header.groupId; } set { + assertHeader(); this.header.groupId = value; } } @@ -250,9 +257,11 @@ namespace BCG.Utilities.SharpZipLib.Tar { /// public string UserName { get { + assertHeader(); return this.header.userName.ToString(); } set { + assertHeader(); this.header.userName = new StringBuilder(value); } } @@ -262,9 +271,11 @@ namespace BCG.Utilities.SharpZipLib.Tar { /// public string GroupName { get { + assertHeader(); return this.header.groupName.ToString(); } set { + assertHeader(); this.header.groupName = new StringBuilder(value); } } @@ -319,9 +330,11 @@ namespace BCG.Utilities.SharpZipLib.Tar { */ public DateTime ModTime { get { + assertHeader(); return this.header.modTime; } set { + assertHeader(); this.header.modTime = value; } } @@ -332,20 +345,18 @@ namespace BCG.Utilities.SharpZipLib.Tar { /// /// This entry's file. /// - public string File { - get { - return this.file; - } - } + public string? File => file; /// /// Get/set this entry's file size. /// public long Size { get { + assertHeader(); return this.header.size; } set { + assertHeader(); this.header.size = value; } } @@ -401,7 +412,8 @@ namespace BCG.Utilities.SharpZipLib.Tar { { this.file = file; - string name = Path.GetDirectoryName(file); + var name = Path.GetDirectoryName(file) + ?? throw new ArgumentException($"Invalid file (GetDirectoryName returned null): {file}"); if (Path.DirectorySeparatorChar == '\\') { // check if the OS is a windows // Strip off drive letters! @@ -500,8 +512,10 @@ namespace BCG.Utilities.SharpZipLib.Tar { /// /// The tar entry header buffer to fill in. /// + [MemberNotNull(nameof(header))] public void WriteEntryHeader(byte[] outbuf) { + assertHeader(); int offset = 0; offset = TarHeader.GetNameBytes(this.header.name, outbuf, offset, TarHeader.NAMELEN); @@ -653,6 +667,13 @@ namespace BCG.Utilities.SharpZipLib.Tar { hdr.devMajor = 0; hdr.devMinor = 0; } + + [MemberNotNull(nameof(header))] + void assertHeader() { + if (header is null) { + throw new InvalidOperationException("header is null"); + } + } } } /* The original Java file had this header: diff --git a/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Tar/TarHeader.cs b/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Tar/TarHeader.cs index 072a5d899..b37dfb41f 100644 --- a/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Tar/TarHeader.cs +++ b/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Tar/TarHeader.cs @@ -251,7 +251,7 @@ namespace BCG.Utilities.SharpZipLib.Tar { { TarHeader hdr = new TarHeader(); - hdr.name = (this.name == null) ? null : new StringBuilder(this.name.ToString()); + hdr.name = new(name.ToString()); hdr.mode = this.mode; hdr.userId = this.userId; hdr.groupId = this.groupId; @@ -259,10 +259,10 @@ namespace BCG.Utilities.SharpZipLib.Tar { hdr.modTime = this.modTime; hdr.checkSum = this.checkSum; hdr.linkFlag = this.linkFlag; - hdr.linkName = (this.linkName == null) ? null : new StringBuilder(this.linkName.ToString()); - hdr.magic = (this.magic == null) ? null : new StringBuilder(this.magic.ToString()); - hdr.userName = (this.userName == null) ? null : new StringBuilder(this.userName.ToString()); - hdr.groupName = (this.groupName == null) ? null : new StringBuilder(this.groupName.ToString()); + hdr.linkName = new(linkName.ToString()); + hdr.magic = new(magic.ToString()); + hdr.userName = new(userName.ToString()); + hdr.groupName = new(groupName.ToString()); hdr.devMajor = this.devMajor; hdr.devMinor = this.devMinor; diff --git a/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Tar/TarInputStream.cs b/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Tar/TarInputStream.cs index aeb9aa809..14c48e681 100644 --- a/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Tar/TarInputStream.cs +++ b/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Tar/TarInputStream.cs @@ -288,7 +288,7 @@ namespace BCG.Utilities.SharpZipLib.Tar { /// /// The next TarEntry in the archive, or null. /// - public TarEntry GetNextEntry() + public TarEntry? GetNextEntry() { if (this.hasHitEOF) { return null; diff --git a/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Zip/Compression/InflaterHuffmanTree.cs b/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Zip/Compression/InflaterHuffmanTree.cs index 04954323d..ec79f33d3 100644 --- a/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Zip/Compression/InflaterHuffmanTree.cs +++ b/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Zip/Compression/InflaterHuffmanTree.cs @@ -44,7 +44,7 @@ namespace BCG.Utilities.SharpZipLib.Zip.Compression { public class InflaterHuffmanTree { private static int MAX_BITLEN = 15; - private short[] tree; + private short[]? tree; public static InflaterHuffmanTree defLitLenTree, defDistTree; @@ -168,6 +168,7 @@ namespace BCG.Utilities.SharpZipLib.Zip.Compression { /// public int GetSymbol(StreamManipulator input) { + ArgumentNullException.ThrowIfNull(tree); int lookahead, symbol; if ((lookahead = input.PeekBits(9)) >= 0) { if ((symbol = tree[lookahead]) >= 0) { diff --git a/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Zip/ZipEntry.cs b/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Zip/ZipEntry.cs index a067bf5c8..824639482 100644 --- a/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Zip/ZipEntry.cs +++ b/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Zip/ZipEntry.cs @@ -70,8 +70,8 @@ namespace BCG.Utilities.SharpZipLib.Zip { ushort known = 0; CompressionMethod method = CompressionMethod.Deflated; - byte[] extra = null; - string comment = null; + byte[]? extra = null; + string? comment = null; public int zipFileIndex = -1; /* used by ZipFile */ public int flags; /* used by ZipOutputStream */ @@ -286,7 +286,7 @@ namespace BCG.Utilities.SharpZipLib.Zip { /// /// the extra data or null if not set. /// - public byte[] ExtraData { + public byte[]? ExtraData { get { return extra; } diff --git a/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Zip/ZipFile.cs b/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Zip/ZipFile.cs index c9ec2f7e7..c35278f93 100644 --- a/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Zip/ZipFile.cs +++ b/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Zip/ZipFile.cs @@ -83,9 +83,9 @@ namespace BCG.Utilities.SharpZipLib.Zip { /// public class ZipFile : IEnumerable { - string name; + string? name; Stream baseStream; - ZipEntry[] entries; + ZipEntry[]? entries; /// /// Opens a Zip file with the given name for reading. @@ -288,6 +288,9 @@ namespace BCG.Utilities.SharpZipLib.Zip { int GetEntryIndex(string name) { + if (entries == null) { + throw new InvalidOperationException("ZipFile has closed"); + } for (int i = 0; i < entries.Length; i++) { if (name.Equals(entries[i].Name)) { return i; @@ -305,7 +308,7 @@ namespace BCG.Utilities.SharpZipLib.Zip { /// /// the zip entry, or null if no entry with that name exists. /// - public ZipEntry GetEntry(string name) + public ZipEntry? GetEntry(string name) { if (entries == null) { throw new InvalidOperationException("ZipFile has closed"); @@ -407,7 +410,7 @@ namespace BCG.Utilities.SharpZipLib.Zip { /// /// Returns the name of this zip file. /// - public string Name { + public string? Name { get { return name; } @@ -416,16 +419,9 @@ namespace BCG.Utilities.SharpZipLib.Zip { /// /// Returns the number of entries in this zip file. /// - public int Size { - get { - try { - return entries.Length; - } catch (Exception) { - throw new InvalidOperationException("ZipFile has closed"); - } - } - } - + public int Size => + entries?.Length ?? throw new InvalidOperationException("ZipFile has closed"); + class ZipEntryEnumeration : IEnumerator { ZipEntry[] array; diff --git a/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Zip/ZipInputStream.cs b/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Zip/ZipInputStream.cs index 10f231efe..7046fbbfa 100644 --- a/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Zip/ZipInputStream.cs +++ b/src/DataStreamSuite/PubSub/Misc/CompressionUtilities/SharpZipLib/Zip/ZipInputStream.cs @@ -94,8 +94,8 @@ namespace BCG.Utilities.SharpZipLib.Zip { /// public class ZipInputStream : InflaterInputStream { - private Crc32 crc = new Crc32(); - private ZipEntry entry = null; + private Crc32? crc = new(); + private ZipEntry? entry; private long csize; private long size; @@ -185,7 +185,7 @@ namespace BCG.Utilities.SharpZipLib.Zip { /// Open the next entry from the zip archive, and return its description. /// If the previous entry wasn't closed, this method will close it. /// - public ZipEntry GetNextEntry() + public ZipEntry? GetNextEntry() { if (crc == null) { throw new InvalidOperationException("Closed."); @@ -258,6 +258,7 @@ namespace BCG.Utilities.SharpZipLib.Zip { if (ReadLeInt() != ZipConstants.EXTSIG) { throw new ZipException("Data descriptor signature not found"); } + ArgumentNullException.ThrowIfNull(entry); entry.Crc = ReadLeInt() & 0xFFFFFFFFL; csize = ReadLeInt(); size = ReadLeInt(); diff --git a/src/DataStreamSuite/PubSub/Misc/Pair.cs b/src/DataStreamSuite/PubSub/Misc/Pair.cs index 003e38764..e75b18688 100644 --- a/src/DataStreamSuite/PubSub/Misc/Pair.cs +++ b/src/DataStreamSuite/PubSub/Misc/Pair.cs @@ -14,8 +14,8 @@ namespace mbts [Serializable] public class Pair { - public F first; - public S second; + public F? first; + public S? second; public Pair() { } public Pair(F f, S s) @@ -38,7 +38,7 @@ namespace mbts return ToString().GetHashCode(); } - public override bool Equals(object obj) + public override bool Equals(object? obj) { return obj != null && obj is Pair && ToString() == obj.ToString(); } @@ -47,9 +47,9 @@ namespace mbts [Serializable] public class Triplet { - public F first; - public S second; - public T third; + public F? first; + public S? second; + public T? third; public Triplet() { } @@ -76,7 +76,7 @@ namespace mbts third != null ? third.ToString() : "NULL"); } - public override bool Equals(object obj) + public override bool Equals(object? obj) { return obj != null && obj is Triplet && ToString() == obj.ToString(); } diff --git a/src/DataStreamSuite/PubSub/Misc/StreamUtilityMethods.cs b/src/DataStreamSuite/PubSub/Misc/StreamUtilityMethods.cs index 89533b27e..fb7b2954f 100644 --- a/src/DataStreamSuite/PubSub/Misc/StreamUtilityMethods.cs +++ b/src/DataStreamSuite/PubSub/Misc/StreamUtilityMethods.cs @@ -12,8 +12,8 @@ namespace BCG { public static class StreamUtilityMethods { // extension method to write a string C++ style (len prefixed) - public static void write_string_cpp(this BinaryWriter bw, string str) { - str = str ?? String.Empty; + public static void write_string_cpp(this BinaryWriter bw, string? str) { + str ??= string.Empty; int len = str.Length; bw.Write(len); foreach (char c_orig in str) { diff --git a/src/DataStreamSuite/PubSub/Misc/ThreadSafeQueue.cs b/src/DataStreamSuite/PubSub/Misc/ThreadSafeQueue.cs index 196dac78c..46dc52599 100644 --- a/src/DataStreamSuite/PubSub/Misc/ThreadSafeQueue.cs +++ b/src/DataStreamSuite/PubSub/Misc/ThreadSafeQueue.cs @@ -14,7 +14,7 @@ namespace BCG public class ThreadSafeQueue { private readonly int _max_size = Int32.MaxValue; - private Queue _main_queue = new Queue(); + private Queue _main_queue = new(); private DateTime _last_dequeque_time = DateTime.MinValue; private string _queue_name = String.Empty; @@ -41,7 +41,7 @@ namespace BCG _warning_log_period = warning_log_period; } - public virtual bool enqueue(T t) + public virtual bool enqueue(T? t) { lock (this) { @@ -98,9 +98,9 @@ namespace BCG return rv; } - public virtual T dequeue() + public virtual T? dequeue() { - T rv; + T? rv; lock (this) { if (_main_queue.Count > 0) @@ -109,14 +109,14 @@ namespace BCG rv = _main_queue.Dequeue(); } else - rv = default(T); + rv = default; } return rv; } - public virtual List dequeueAll() + public virtual List dequeueAll() { - List rv = new List(); + var rv = new List(); lock (this) { int sz = getCount(); @@ -126,13 +126,13 @@ namespace BCG return rv; } - public virtual bool enqueue_and_crush(T new_elem, Comparison comp) + public virtual bool enqueue_and_crush(T new_elem, Comparison comp) { bool rv; lock (this) { Dictionary marked_for_deletion = new Dictionary (); - List all_elements = dequeueAll(); + var all_elements = dequeueAll(); for (int i = 0; i < all_elements.Count; i++) if (_compare (new_elem, all_elements[i], comp)) marked_for_deletion.Add (i,true); @@ -144,7 +144,7 @@ namespace BCG return rv; } - private static bool _compare(T t1, T t2, Comparison comp) + private static bool _compare(T? t1, T? t2, Comparison comp) { if (comp != null) return comp (t1, t2) == 0; @@ -152,9 +152,9 @@ namespace BCG return t1 != null && t1.Equals(t2); } - public virtual List dequeueLimited(int max) + public virtual List dequeueLimited(int max) { - List rv = new List(); + var rv = new List(); lock (this) { int limit = Math.Min (max, _main_queue.Count); diff --git a/src/DataStreamSuite/PubSub/PublishedCache.cs b/src/DataStreamSuite/PubSub/PublishedCache.cs index 6cb392194..899396a02 100644 --- a/src/DataStreamSuite/PubSub/PublishedCache.cs +++ b/src/DataStreamSuite/PubSub/PublishedCache.cs @@ -10,29 +10,21 @@ namespace bts { public class PublishedCache { private Dictionary _data_map = new Dictionary(); - public PublishedCache() { } - - public void publish(PublishMsg msg) { Log.Gen.info($"PublishedCache.publish msg: {msg}"); - SingleTagData tag_data = null; - if (!_data_map.TryGetValue(msg.tag, out tag_data)) { + ArgumentException.ThrowIfNullOrEmpty(msg.tag); + if (!_data_map.TryGetValue(msg.tag, out var tag_data)) { + ArgumentException.ThrowIfNullOrEmpty(msg.tag_type); tag_data = new SingleTagData(msg.tag, msg.tag_type); _data_map.Add(msg.tag, tag_data); } tag_data.handle_publish(msg); } - public Tuple get_content(string tag) { - SingleTagData tag_data; - byte[] rv = new byte[] { }; - if (_data_map.TryGetValue(tag, out tag_data)) { - string tag_type = tag_data.tag_type; - byte[] data = tag_data.get_current_content(); - return new Tuple(tag_type, data); - } - return new Tuple(String.Empty, new byte[] { }); - } + public Tuple get_content(string tag) => + _data_map.TryGetValue(tag, out var tag_data) + ? new(tag_data.tag_type, tag_data.get_current_content()) + : new(string.Empty, Array.Empty()); public void clear(string tag) { if (_data_map.ContainsKey(tag)) { diff --git a/src/DataStreamSuite/PubSub/PublisherCore.cs b/src/DataStreamSuite/PubSub/PublisherCore.cs index a0fe85066..7e1b57b7a 100644 --- a/src/DataStreamSuite/PubSub/PublisherCore.cs +++ b/src/DataStreamSuite/PubSub/PublisherCore.cs @@ -34,7 +34,7 @@ namespace BCG { new Dictionary(); // router object, used to select which pubsub should be used for each tag - protected IPubSubRouter _router = null; + protected IPubSubRouter? _router; // flag indicating wether we are in the middle of a transaction or not protected bool _is_in_transaction = false; @@ -133,8 +133,8 @@ namespace BCG { bts.Log.assert_d(false, $"Incorrect msg: {msg}"); break; case PubSubMessageType.PUBLISH: - var pub_msg = msg.pub_msg; - _publish(pub_msg); + ArgumentNullException.ThrowIfNull(msg.pub_msg); + _publish(msg.pub_msg); break; } } @@ -146,7 +146,7 @@ namespace BCG { } catch (Exception ex) { bts.Log.Gen.error($"PublisherCore.myMessagePump exception ex: {ex}"); if (ex is OutOfMemoryException) - throw ex; + throw; } } } @@ -169,6 +169,7 @@ namespace BCG { // update the state _state.publish(msg); // send the data out + ArgumentNullException.ThrowIfNull(msg.tag); var sender = _get_or_build_sender(msg.tag); // actually send the data out msg.connection = sender.client_url; @@ -178,17 +179,10 @@ namespace BCG { }); } - private void _clear_state(string tag) { - if (_tag_sender_map.ContainsKey(tag)) { - _tag_sender_map.Remove(tag); - } - _state.clear(tag); - } - // tag -> server_url, server_url -> IPubSubServerConnection protected IPubSubServerConnection _get_or_build_sender(string tag) { - IPubSubServerConnection tag_conn = null; - if (!_tag_sender_map.TryGetValue(tag, out tag_conn)) { + if (!_tag_sender_map.TryGetValue(tag, out var tag_conn)) { + ArgumentNullException.ThrowIfNull(_router); RemoteConnectionInfo server_rci = _router.get_pub_route_for_tag(tag); if (!_connection_map.TryGetValue(server_rci, out tag_conn)) { tag_conn = _connection_factory.get_connection(server_rci); @@ -234,4 +228,4 @@ namespace BCG { } #endregion -} \ No newline at end of file +} diff --git a/src/DataStreamSuite/PubSub/PubsubDeserializationController.cs b/src/DataStreamSuite/PubSub/PubsubDeserializationController.cs index 93aecd09b..621df9703 100644 --- a/src/DataStreamSuite/PubSub/PubsubDeserializationController.cs +++ b/src/DataStreamSuite/PubSub/PubsubDeserializationController.cs @@ -2,6 +2,7 @@ using System.IO; using System.Collections; using BCG.Utilities; +using mbts.utils; namespace BCG { /// @@ -10,7 +11,7 @@ namespace BCG { /// are passed though to the interface /// public class PubsubDeserializationController : IPubsubDeserializer { - private IPubsubDeserializer myDeserializer; + private IPubsubDeserializer? myDeserializer; public PubsubDeserializationController() { } @@ -45,7 +46,11 @@ namespace BCG { // TODOds if (deserName == "BCG.PubsubUniversalDeserializer, DataStreamGateway") // so we can play old replay files which would not include the assembly name deserName = "BCG.PubsubUniversalDeserializer, DataStreamCore"; - myDeserializer = Activator.CreateInstance(Type.GetType(deserName)) as IPubsubDeserializer; + ArgumentNullException.ThrowIfNull(myDeserializer); + var deserType = Type.GetType(deserName); + ArgumentNullException.ThrowIfNull(deserType); + myDeserializer = Activator.CreateInstance(deserType) as IPubsubDeserializer; + ArgumentNullException.ThrowIfNull(myDeserializer); totalStream.Position = initialPos; myDeserializer.deserialize(new Stream[] { totalStream }, out theOps, out theRows); bts.Log.Gen.info($"PubsubDeserializationController.deserialize myDeserializer: {myDeserializer} ops: {theOps.Length} theRows: {theRows.Length}"); diff --git a/src/DataStreamSuite/PubSub/Server/Communication/ManagedTCP/ManagedTCPMessageHub.cs b/src/DataStreamSuite/PubSub/Server/Communication/ManagedTCP/ManagedTCPMessageHub.cs index 887bb4aef..88300cf6d 100644 --- a/src/DataStreamSuite/PubSub/Server/Communication/ManagedTCP/ManagedTCPMessageHub.cs +++ b/src/DataStreamSuite/PubSub/Server/Communication/ManagedTCP/ManagedTCPMessageHub.cs @@ -31,8 +31,8 @@ namespace BCG { } } private bool _activated = false; - private PubSubCallbacks.PubSubServerConnectionStatusChange _connection_cb; - private PubSubServerDataDelegate _data_cb; + private PubSubCallbacks.PubSubServerConnectionStatusChange? _connection_cb; + private PubSubServerDataDelegate? _data_cb; /// /// client to connection map @@ -210,13 +210,14 @@ namespace BCG { // times["send"].start(); if (p != null) { - ITCPConnection conn; - _connection_map.TryGetValue(p.first, out conn); + ArgumentNullException.ThrowIfNull(p.first); + ArgumentNullException.ThrowIfNull(p.second); + _connection_map.TryGetValue(p.first, out var conn); if (conn != null) { conn.send(p.second); Log.Gen.info($"ManagedTCPMessageHub._main_message_pump sending data client: {p.first} size: {p.second.Length}"); } else { - string error_msg = $"sending data of sz: {p.second.Count()} to {p.first} failed, this client might have " + + string error_msg = $"sending data of sz: {p.second.Length} to {p.first} failed, this client might have " + "disconnected, in which case it will be garbage collected within 60 secs"; Log.Gen.error(error_msg); @@ -226,7 +227,7 @@ namespace BCG { } catch (Exception ex) { //BlueEventLog.logEvent(ex); if (ex is OutOfMemoryException) - throw ex; + throw; } } } diff --git a/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemHeartbeatPublisher.cs b/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemHeartbeatPublisher.cs index fb506104b..7867661c7 100644 --- a/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemHeartbeatPublisher.cs +++ b/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemHeartbeatPublisher.cs @@ -36,7 +36,7 @@ namespace BCG { public static string tag_type = "System_Heartbeat"; public static string tag_name = "System_Heartbeat"; - private static SystemHeartbeatPublisher _self; + private static SystemHeartbeatPublisher? _self; private SystemHeartbeatPublisher() { diff --git a/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemStatsPublisher.cs b/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemStatsPublisher.cs index 4cab06afd..efeb633b9 100644 --- a/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemStatsPublisher.cs +++ b/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemStatsPublisher.cs @@ -20,7 +20,7 @@ namespace BCG { private HashSet _dirty_tags = new HashSet(); private Dictionary _tag_stats_map = new Dictionary(); private MasterWorker _master_worker = MasterWorker.getInstance(false); - private static SystemStatsPublisher _self = null; + private static SystemStatsPublisher? _self; private SystemStatsPublisher(PubSubState state) { bts.Log.Gen.info("SystemStatsPublisher constructed"); @@ -33,7 +33,7 @@ namespace BCG { _self = new SystemStatsPublisher(state); } - public static IPubSubServerTagHandler get_instance() { + public static IPubSubServerTagHandler? get_instance() { return _self; } @@ -41,8 +41,7 @@ namespace BCG { bts.Log.Gen.info($"SystemStatsPublisher._handle_publish tag: {tag} size: {size}"); if (tag == tag_name) return; - TagStatsInfo tag_stats = null; - if (!_tag_stats_map.TryGetValue(tag, out tag_stats)) { + if (!_tag_stats_map.TryGetValue(tag, out var tag_stats)) { tag_stats = new TagStatsInfo(tag); _tag_stats_map.Add(tag, tag_stats); } @@ -79,8 +78,7 @@ namespace BCG { #region IPubSubServerTagHandler Members - public void update(string tag, byte[] msg) { - } + public void update(string tag, byte[]? msg) { } public byte[] get_content(string tag) { var ser = TagStatsInfo.serialize(_tag_stats_map.Values); diff --git a/src/DataStreamSuite/PubSub/Server/PubSubMessageReactor.cs b/src/DataStreamSuite/PubSub/Server/PubSubMessageReactor.cs index 85cff8bf5..a043649ae 100644 --- a/src/DataStreamSuite/PubSub/Server/PubSubMessageReactor.cs +++ b/src/DataStreamSuite/PubSub/Server/PubSubMessageReactor.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Threading; using bts; @@ -17,7 +18,7 @@ namespace BCG { private IPubsubMessageHub _msg_hub; // private delegate void MyMsgHandler(string sender, Stream msg, MetaData m); // private Dictionary myOperationToHandler = new Dictionary(); - private Thread _msg_polling_thread = null; + private Thread _msg_polling_thread; // all the data,list of subscribers etc are in the state private PubSubState _state = new PubSubState(); private MasterWorker _mainloop_master_worker = MasterWorker.getInstance(false); @@ -89,14 +90,17 @@ namespace BCG { bts.Log.Gen.info($"PubSubMessageReactor._polling_thread_func post get_updates n_msgs: {n_msgs}"); foreach (var kv in received_data) { // multiple pubs for the same tags should be batched here + ArgumentException.ThrowIfNullOrEmpty(kv.first); + ArgumentNullException.ThrowIfNull(kv.second); _handle_client_msg(kv.first, kv.second); } handle_internal_pubs(); _mainloop_master_worker.doWork(); } catch (Exception ex) { bts.Log.Gen.error($"PubSubMessageReactor._polling_thread_func ex: {ex}"); - if (ex is OutOfMemoryException) - throw ex; + if (ex is OutOfMemoryException) { + throw; + } } } } @@ -125,15 +129,19 @@ namespace BCG { bts.Log.Gen.info($"PubsubMessageReactor._handle_client_msg sender: {sender} ps_client_msg: {ps_client_msg}"); switch (ps_client_msg.msg_type) { case PubSubMessageType.SUBSCRIBE: + ArgumentNullException.ThrowIfNull(ps_client_msg.sub_msg); _handle_sub(sender, ps_client_msg.sub_msg); break; case PubSubMessageType.UNSUBSCRIIBE: + ArgumentNullException.ThrowIfNull(ps_client_msg.unsub_msg); _handle_unsub(sender, ps_client_msg.unsub_msg); break; case PubSubMessageType.TRANSACTION: + ArgumentNullException.ThrowIfNull(ps_client_msg.transaction_msg); _handle_transaction(sender, ps_client_msg.transaction_msg); break; case PubSubMessageType.PUBLISH: + ArgumentNullException.ThrowIfNull(ps_client_msg.pub_msg); _handle_pub(sender, ps_client_msg.pub_msg, false); break; } @@ -153,7 +161,7 @@ namespace BCG { ///update the state _state.handle_sub(sub_msg); // state keeps the list of all subscribers ///to all these new subscriber s, send the existing data - foreach (string tag in sub_msg.tags) { + foreach (string tag in sub_msg.tags.empty_if_null()) { byte[] data = _state.get_content(tag); //ie data has been published once if (data != null) { @@ -178,6 +186,7 @@ namespace BCG { //bts.Log.Gen.info($"PubSubMessageReactor._handle_pub sender: {sender} pub_msg: {pub_msg} is_internal_pub: {is_internal_pub}"); // this is where pubsub's data state is updated _state.update_pub_data(pub_msg); + ArgumentNullException.ThrowIfNull(pub_msg.tag); var sub_connections_for_tag = _state.get_subscriber_connections_for_tag(pub_msg.tag); bts.Log.Gen.info($"PubSubMessageReactor._handle_pub {sub_connections_for_tag.Count} subs for tag: {pub_msg.tag}"); bts.PubSubServerMessage msg_to_send = new PubSubServerMessage() { diff --git a/src/DataStreamSuite/PubSub/Server/PubSubSingleTagStateEvent.cs b/src/DataStreamSuite/PubSub/Server/PubSubSingleTagStateEvent.cs index 2f2d2803a..9b81109ef 100644 --- a/src/DataStreamSuite/PubSub/Server/PubSubSingleTagStateEvent.cs +++ b/src/DataStreamSuite/PubSub/Server/PubSubSingleTagStateEvent.cs @@ -1,99 +1,68 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Text; using System.IO; -/* - * This class represents an event happening to a single tag in a pubsub (or substation.) It could be that a tag is being published, - * or it could be a subscription to a tag or an unsubscription to a tag. - * - */ -namespace BCG -{ - public class PubSubSingleTagStateEvent - { +namespace BCG; - public enum EventType { publish, subscription, unsubscription, publisherDie }; - - public string tag; - public EventType eventType; - public string connection; - public string app; +/// +/// This class represents an event happening to a single tag in a pubsub (or substation). +/// It could be that a tag is being published, or it could be a subscription to a tag or +/// an unsubscription to a tag. +/// +public class PubSubSingleTagStateEvent { + public PubSubSingleTagStateEvent(string tag, EventType eventType, string connection, string app) { + this.tag = tag; + this.eventType = eventType; + this.connection = connection; + this.app = app; + } - public static PubSubSingleTagStateEvent fromStream(Stream theStream) - { - BinaryReader bw = new BinaryReader(theStream); + public enum EventType { publish, subscription, unsubscription, publisherDie }; - PubSubSingleTagStateEvent thisEvent = new PubSubSingleTagStateEvent(); + public string tag { get; init; } + public EventType eventType { get; init; } + public string connection { get; init; } + public string app { get; init; } - thisEvent.tag = bw.ReadString(); - thisEvent.eventType = (EventType) bw.ReadInt32(); - thisEvent.connection = bw.ReadString(); - thisEvent.app = bw.ReadString(); + public static PubSubSingleTagStateEvent fromStream(Stream theStream) { + using var bw = new BinaryReader(theStream); + return new PubSubSingleTagStateEvent(bw.ReadString(), (EventType)bw.ReadInt32(), bw.ReadString(), bw.ReadString()); + } + public static PubSubSingleTagStateEvent fromBytes(byte[] bytes) { + using var mem = new MemoryStream(bytes); + return fromStream(mem); + } - return thisEvent; - - } - public static PubSubSingleTagStateEvent fromBytes(byte[] bytes) - { - MemoryStream mem = new MemoryStream(bytes); - return fromStream(mem); - - } - //for debugging - public override string ToString() - { - StringBuilder theBuilder = new StringBuilder(); - theBuilder.Append(tag+" "); - switch (eventType) - { - case EventType.publish: - theBuilder.Append("published"); - break; - case EventType.publisherDie: - theBuilder.Append("publisher died"); - break; - case EventType.subscription: - theBuilder.Append("was subscribed to"); - break; - case EventType.unsubscription: - theBuilder.Append("was unsubscribed to"); - break; - - } - theBuilder.Append(" app: " + app); - theBuilder.Append(" connection: " + connection); - - return theBuilder.ToString(); + //for debugging + public override string ToString() { + var bd = new StringBuilder(); + bd.Append(tag + " "); + switch (eventType) { + case EventType.publish: + bd.Append("published"); + break; + case EventType.publisherDie: + bd.Append("publisher died"); + break; + case EventType.subscription: + bd.Append("was subscribed to"); + break; + case EventType.unsubscription: + bd.Append("was unsubscribed to"); + break; } - public byte[] toBytes() - { - MemoryStream mem = new MemoryStream(); - BinaryWriter bw = new BinaryWriter(mem); + bd.Append(" app: " + app); + bd.Append(" connection: " + connection); + return bd.ToString(); + } - bw.Write(tag); - bw.Write((int) eventType); - bw.Write(connection); - bw.Write(app); - - return mem.GetBuffer(); - - } - - - public PubSubSingleTagStateEvent(string argTag, EventType type, string theConnection, string theApp) - { - - tag = argTag; - eventType = type; - connection = theConnection; - app = theApp; - - } - - private PubSubSingleTagStateEvent() {} - + public byte[] toBytes() { + using var mem = new MemoryStream(); + using var bw = new BinaryWriter(mem); + bw.Write(tag); + bw.Write((int)eventType); + bw.Write(connection); + bw.Write(app); + return mem.GetBuffer(); } } diff --git a/src/DataStreamSuite/PubSub/Server/PubSubState.cs b/src/DataStreamSuite/PubSub/Server/PubSubState.cs index a901a012b..aa456c595 100644 --- a/src/DataStreamSuite/PubSub/Server/PubSubState.cs +++ b/src/DataStreamSuite/PubSub/Server/PubSubState.cs @@ -8,6 +8,8 @@ using System.IO; using mbts; using System.Linq.Expressions; using bts; +using mbts.utils; +using bts.collections; namespace BCG { /// @@ -25,11 +27,11 @@ namespace BCG { public class PubSubState { public delegate void PubSubStateSingleTagChangeDelegate(PubSubSingleTagStateEvent theEvent); - private event PubSubStateSingleTagChangeDelegate onSingleTagChange; + private event PubSubStateSingleTagChangeDelegate? onSingleTagChange; public delegate void PubSubStateChangeDelegate(string[] tags); - public event PubSubStateChangeDelegate onTagMapChanged; + public event PubSubStateChangeDelegate? onTagMapChanged; public delegate void PubSubStatsDelegate(string tag, int size); - public event PubSubStatsDelegate onPublishStats; + public event PubSubStatsDelegate? onPublishStats; private Dictionary _tag_to_data_map = new Dictionary(); private Dictionary _tag_to_connection_map = new Dictionary(); @@ -44,37 +46,33 @@ namespace BCG { public void handle_sub(bts.SubscribeMsg sub_msg) { Log.Gen.info($"PubSubState.handle_sub sub_msg: {sub_msg}"); + ExceptionAssert.Argument.NotNullOrEmpty(sub_msg.tags); + ArgumentException.ThrowIfNullOrEmpty(sub_msg.connection); foreach (string tag in sub_msg.tags) { - SingleTagConnectionData tag_conn_map = null; - if (!_tag_to_connection_map.TryGetValue(tag, out tag_conn_map)) { - tag_conn_map = new SingleTagConnectionData(tag); - _tag_to_connection_map.Add(tag, tag_conn_map); - } - tag_conn_map.handle_sub(sub_msg); - if (onSingleTagChange != null) - onSingleTagChange(new PubSubSingleTagStateEvent(tag, PubSubSingleTagStateEvent.EventType.subscription, sub_msg.connection, sub_msg.subscribing_app)); + _tag_to_connection_map.GetOrAdd(tag, _ => new SingleTagConnectionData(tag)).handle_sub(sub_msg); + onSingleTagChange?.Invoke(new( + tag, PubSubSingleTagStateEvent.EventType.subscription, sub_msg.connection, sub_msg.subscribing_app)); } - - if (onTagMapChanged != null) - onTagMapChanged(sub_msg.tags); + onTagMapChanged?.Invoke(sub_msg.tags); // TODO } - public void handle_unsub(bts.UnsubscribeMsg ubnsub_msg) { - foreach (string tag in ubnsub_msg.tags) { - SingleTagConnectionData conn_data = null; - if (_tag_to_connection_map.TryGetValue(tag, out conn_data)) { + public void handle_unsub(bts.UnsubscribeMsg unsub_msg) { + ExceptionAssert.Argument.NotNullOrEmpty(unsub_msg.tags); + ArgumentException.ThrowIfNullOrEmpty(unsub_msg.connection); + foreach (string tag in unsub_msg.tags) { + if (_tag_to_connection_map.TryGetValue(tag, out var conn_data)) { ///maybe check here, that if there are no subscribers ///and publishers for the tag then remove it. - conn_data.handle_unsub(ubnsub_msg); - if (onSingleTagChange != null) - onSingleTagChange(new PubSubSingleTagStateEvent(tag, PubSubSingleTagStateEvent.EventType.unsubscription, ubnsub_msg.connection, ubnsub_msg.subscribing_app)); + conn_data.handle_unsub(unsub_msg); + onSingleTagChange?.Invoke(new( + tag, PubSubSingleTagStateEvent.EventType.unsubscription, unsub_msg.connection, unsub_msg.subscribing_app)); } else { - bts.Log.Gen.error($"PubSubState.handle_unsub ubnsub_msg: {ubnsub_msg} tag: {tag} was never subscribed, so unsub call is voided"); + bts.Log.Gen.error($"PubSubState.handle_unsub ubnsub_msg: {unsub_msg} tag: {tag} was never subscribed, so unsub call is voided"); } } if (onTagMapChanged != null) - onTagMapChanged(ubnsub_msg.tags); + onTagMapChanged(unsub_msg.tags); } public void remove_connection(string connection) { @@ -105,6 +103,10 @@ namespace BCG { #region data based state changes public void update_pub_data(bts.PublishMsg pub_msg) { + ArgumentException.ThrowIfNullOrEmpty(pub_msg.tag); + ArgumentException.ThrowIfNullOrEmpty(pub_msg.tag_type); + ArgumentException.ThrowIfNullOrEmpty(pub_msg.connection); + ArgumentNullException.ThrowIfNull(pub_msg.data_to_publish); try { bts.Log.Gen.info($"PubSubData.update_pub_data pub_msg: {pub_msg}"); ///this is the only place where SingleTagData should be created @@ -132,8 +134,7 @@ namespace BCG { } catch (Exception ex) { bts.Log.Gen.error($"PubSubState.update_pub_data ex: {ex} pub_msg: {pub_msg}"); } - if (onPublishStats != null) - onPublishStats(pub_msg.tag, pub_msg.data_to_publish.Length); + onPublishStats?.Invoke(pub_msg.tag, pub_msg.data_to_publish.Length); } #endregion @@ -221,4 +222,4 @@ namespace BCG { } } -} \ No newline at end of file +} diff --git a/src/DataStreamSuite/PubSub/Server/SingleTagData.cs b/src/DataStreamSuite/PubSub/Server/SingleTagData.cs index 87cc5d9fc..d96903cc7 100644 --- a/src/DataStreamSuite/PubSub/Server/SingleTagData.cs +++ b/src/DataStreamSuite/PubSub/Server/SingleTagData.cs @@ -18,21 +18,19 @@ namespace bts { /// It routes all that to IPubSubServerTagHandler, which is the main interface to deal with pubsubdata in a context aware manner /// public class SingleTagData { - private readonly string _tag_type = String.Empty; - private readonly string _tag = String.Empty; - private readonly IPubSubServerTagHandler _tag_handler; + private readonly IPubSubServerTagHandler? _tag_handler; public SingleTagData(string tag, string tag_type) { bts.Log.Gen.info($"SingleTagData constructed for tag: {tag} tag_type: {tag_type}"); - _tag = tag; - _tag_type = tag_type; + this.tag = tag; + this.tag_type = tag_type; _tag_handler = get_handler(tag_type); - Log.assert(_tag_handler != null); + Log.notnull(_tag_handler); Log.Gen.info($"SingleTagData.cons tag: {tag} tag_type: {tag_type} _tag_handler: {_tag_handler}"); _tag_handler.register(new InternalPublishDelegate(_on_handler_internal_publish)); } - public IPubSubServerTagHandler get_handler(string tag_type) { + public IPubSubServerTagHandler? get_handler(string tag_type) { if (tag_type == SystemSubscriptionMapPublisher.tag_type) return SystemSubscriptionMapPublisher.getInstance(); else if (tag_type == SystemStatsPublisher.tag_type) @@ -41,30 +39,32 @@ namespace bts { } - public string tag { get { return _tag; } } + public string tag { get; } - public string tag_type { get { return _tag_type; } } + public string tag_type { get; } // PubsubState calls this public void handle_publish(PublishMsg pub_msg) { string handler_str = _tag_handler != null ? _tag_handler.GetType().ToString() : "NULL"; - bts.Log.Gen.info($"SingleTagData.handle_publish tag: {_tag} pub_msg: {pub_msg} handler: {handler_str} "); - + bts.Log.Gen.info($"SingleTagData.handle_publish tag: {tag} pub_msg: {pub_msg} handler: {handler_str} "); try { - _tag_handler.update(_tag, pub_msg.data_to_publish); + // tag_handler may be null. If it throws (a null reference ex), we'll catch and handle + // it like any other ex. + _tag_handler!.update(tag, pub_msg.data_to_publish); } catch (Exception e) { var err_msg = $"SingleTagData.handle_publish pub_msg: {pub_msg} handler: {handler_str} threw exception: {e}"; bts.Log.Gen.error(err_msg); if (e is OutOfMemoryException) - throw e; + throw; else throw new ApplicationException(err_msg, e); } } public byte[] get_current_content() { - byte[] rv = _tag_handler.get_content(_tag); - bts.Log.Gen.info($"SingleTagData.get_current_content tag: {_tag} rv: {rv}"); + ArgumentNullException.ThrowIfNull(_tag_handler); + var rv = _tag_handler.get_content(tag); + Log.Gen.info($"SingleTagData.get_current_content tag: {tag} rv (n bytes): {rv.Length}"); return rv; } @@ -78,6 +78,5 @@ namespace bts { PubSubMessageReactor.internal_publish(pub_msg); } } - } -} \ No newline at end of file +} diff --git a/src/DataStreamSuite/PubSub/SingleServerSubscriberCore.cs b/src/DataStreamSuite/PubSub/SingleServerSubscriberCore.cs index ab6c69693..1e5c2b515 100644 --- a/src/DataStreamSuite/PubSub/SingleServerSubscriberCore.cs +++ b/src/DataStreamSuite/PubSub/SingleServerSubscriberCore.cs @@ -27,7 +27,7 @@ namespace BCG { private SyncQueue _in_data_queue = new SyncQueue(); private HashSet _tags = new HashSet(); - public event PubSubConnectionChange onPubSubConnectionChange; + public event PubSubConnectionChange? onPubSubConnectionChange; public SingleServerSubscriberCore(RemoteConnectionInfo server_rci, PubSubServerConnectionFactory connection_factory) { bts.Log.Gen.info($"SingleServerSubscriberCore constructed for server_url: {server_rci}"); @@ -42,17 +42,17 @@ namespace BCG { _connection.doWork(); //List rv = new List(); List conn_data = _in_data_queue.dequeueMultipleElements(TimeSpan.Zero, 1024); // TODO, max_elements - if (conn_data.Count() > 0) { + if (conn_data.Count > 0) { bts.Log.Gen.info($"SingleServerSubscriberCore.get_updates n_updates: {conn_data.Count}"); } List rv = new List(); foreach (var m in conn_data) { var msg = new PubSubServerMessage(); - msg.consruct_deserialize(m); - if (_tags.Contains(msg.tag)) + msg.construct_deserialize(m); + if (_tags.Contains(msg.tag)) { rv.Add(msg); - else { - Log.Gen.warn("SingleSeverSubsciberCore.get_updates msg: {msg} does not have a subscription"); + } else { + Log.Gen.warn($"SingleServerSubscriberCore.get_updates msg: {msg} does not have a subscription"); } } return rv; diff --git a/src/DataStreamSuite/PubSub/SubscriberCore.cs b/src/DataStreamSuite/PubSub/SubscriberCore.cs index fb17bb243..eb102e03f 100644 --- a/src/DataStreamSuite/PubSub/SubscriberCore.cs +++ b/src/DataStreamSuite/PubSub/SubscriberCore.cs @@ -17,12 +17,11 @@ namespace BCG { /// /// public class SubscriberCore : ISubscriberCore { - private Dictionary _server_route_to_subcore_map = - new Dictionary(); + private Dictionary _server_route_to_subcore_map = new(); private IPubSubRouter _tag_router; private PubSubServerConnectionFactory _connection_factory = new PubSubServerConnectionFactory(true); - public event PubSubConnectionChange onPubSubConnectionChange; + public event PubSubConnectionChange? onPubSubConnectionChange; #region constructors @@ -57,20 +56,6 @@ namespace BCG { return rv; } - private IDictionary _n_update_map = new Dictionary(); - private void _log_n_updates(IEnumerable updates) { - foreach (PubSubUpdate u in updates) { - if (!_n_update_map.ContainsKey(u.tag)) { - _n_update_map.Add(u.tag, 0); - } - _n_update_map[u.tag] = _n_update_map[u.tag] + 1; - } - // log it now - foreach (var kv in _n_update_map) { - bts.Log.Gen.info($"StaticSubscrberCore tag: {kv.Key} n_updates: {kv.Value}"); - } - } - public void subscribe(params string[] tags) { lock (_server_route_to_subcore_map) { foreach (string tag in tags) { @@ -82,8 +67,7 @@ namespace BCG { } private ISingleServerSubscriberCore _find_subscribercore(RemoteConnectionInfo server_rci) { - ISingleServerSubscriberCore rv = null; - if (!_server_route_to_subcore_map.TryGetValue(server_rci, out rv)) { + if (!_server_route_to_subcore_map.TryGetValue(server_rci, out var rv)) { rv = new SingleServerSubscriberCore(server_rci, _connection_factory); _server_route_to_subcore_map.Add(server_rci, rv); rv.onPubSubConnectionChange += _handle_pubsubserver_connection_change; @@ -91,16 +75,13 @@ namespace BCG { return rv; } - public void _handle_pubsubserver_connection_change(bool is_connected, string server_url) { if (onPubSubConnectionChange != null) onPubSubConnectionChange(is_connected, server_url); - } public void unsubscribe(params string[] tags) { lock (_server_route_to_subcore_map) { - foreach (string tag in tags) { var server_rci = _tag_router.get_sub_route_for_tag(tag); var server_subcore = _find_subscribercore(server_rci); @@ -116,8 +97,9 @@ namespace BCG { #region IDisposable Members public void Dispose() { - foreach (var sssc in _server_route_to_subcore_map.Values) + foreach (var sssc in _server_route_to_subcore_map.Values) { sssc.Dispose(); + } _connection_factory.Dispose(); } diff --git a/src/client/spark_core/utils/Exceptions/ExceptionAssert.cs b/src/client/spark_core/utils/Exceptions/ExceptionAssert.cs index 19c73dc6d..1fdf14867 100644 --- a/src/client/spark_core/utils/Exceptions/ExceptionAssert.cs +++ b/src/client/spark_core/utils/Exceptions/ExceptionAssert.cs @@ -5,14 +5,15 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Numerics; +using System.Runtime.CompilerServices; namespace mbts.utils { - static class ExceptionAssert { + public static class ExceptionAssert { public static T of_type(object obj, string name = null) => obj is T ? (T)obj : throw new ArgumentException($"Expected {typeof(T)}, actually {(obj?.GetType()?.ToString() ?? "null")}.", name); #nullable enable - public static T not_null([NotNull]T? obj, string? name = null) { + public static T not_null([NotNull] T? obj, string? name = null) { if (obj is null) { throw new ArgumentNullException(name ?? nameof(obj)); } @@ -25,12 +26,32 @@ namespace mbts.utils { if (index < 0) throw ExceptionBuilder.IndexOutOfRange.lt_zero(); } + public static ArgumentCastor Cast(T argument) => + new(argument); + + public readonly struct ArgumentCastor { + public readonly T Argument; + + public ArgumentCastor(T argument) => + Argument = argument; + + public TResult To() => + ExceptionAssert.Argument.OfType(Argument); + } + public static class Argument { - public static T Is(object argument, string name) { + public static T Is(object argument, [CallerArgumentExpression(nameof(argument))]string name = "") { if (argument is T t) return t; else throw new ArgumentException($"Must be of type {typeof(T)}.", name); } + public static TResult OfType(T argument, [CallerArgumentExpression(nameof(argument))]string name = "") { + if (argument is TResult r) { + return r; + } + throw new ArgumentException($"Must be of type {typeof(T)}.", name); + } + public static void IsNot(object argument, string name) { if (argument is T) throw new ArgumentException($"Must not be of type {typeof(T)}.", name); @@ -41,6 +62,11 @@ namespace mbts.utils { throw ExceptionBuilder.Argument.must_be(condition_description, name); } +#nullable enable + public static T[] NotNullOrEmpty([NotNull]T[]? argument, [CallerArgumentExpression(nameof(argument))] string name = "") => + argument?.Length > 0 ? argument : throw new ArgumentException("Must not be null or empty.", name); +#nullable disable + public static void NotNull(object argument, string name) { if (argument == null) throw new ArgumentNullException(name);