diff --git a/src/DataStreamSuite/DataStream/Primitives/Misc/PubSubTestStream.cs b/src/DataStreamSuite/DataStream/Primitives/Misc/PubSubTestStream.cs new file mode 100644 index 000000000..c0dee5d3c --- /dev/null +++ b/src/DataStreamSuite/DataStream/Primitives/Misc/PubSubTestStream.cs @@ -0,0 +1,193 @@ +using BCG; +using BCG.Utilities.General; +using System.Collections.Generic; + +//using MongoDB.Driver; + +using System; +using System.IO; +using System.Linq; + +namespace bts { + public class PubSubTestingStream : ViewStream, IWorker { + public class PayloadMessage { + public int msg_id = 0; + public string guid; + public Int64 travel_epoch_ms = 0; + public byte[] payload = null; + public PayloadMessage(int msg_id_args, Int64 send_epoch_args, int payload_size, byte payload_byte) { + msg_id = msg_id_args; + travel_epoch_ms = send_epoch_args; + payload = new byte[payload_size]; + for (int i = 0; i < payload_size; i++) { + payload[i] = payload_byte; + } + } + private PayloadMessage() { } + public PayloadMessage(int msg_id, int payload_size, byte payload_byte) : + this(msg_id, PubSubTestingStream.epoch_now(), payload_size, payload_byte) { } + + public byte[] serialize() { + var str = new MemoryStream(); + BinaryWriter bw = new BinaryWriter(str); + bw.Write(msg_id); + bw.Write(guid); + bw.Write(travel_epoch_ms); + bw.Write(payload.Length); + bw.Write(payload); + str.Position = 0; + var bytes = str.ToArray(); + return bytes; + } + + public static PayloadMessage deserialize(byte[] ser_bytes) { + var str = new MemoryStream(ser_bytes); + BinaryReader br = new BinaryReader(str); + int msg_id = br.ReadInt32(); + string guid = br.ReadString(); + Int64 epoch = br.ReadInt64(); + int len = br.ReadInt32(); + var payload = br.ReadBytes(len); + return new PayloadMessage() { + msg_id = msg_id, + guid = guid, + travel_epoch_ms = epoch, + payload = payload + }; + } + + } + //public void + private readonly int _msg_size; + private readonly TimeSpan _publish_period; + private DateTime _last_publish_time = DateTime.MinValue; + private PublisherCore _pub_core = new PublisherCore(); + private BlueTable _table; + private int _n_pubs = 0; + private readonly int _max_pubs; + private int _n_updates = 0; + private bool _continue_sub_thread = true; + private ViewStreamDatabase _vsd; + private bool _fire_dirty = false; + private PayloadMessage _last_sent_msg, _last_recvd_msg; + private readonly int _payload_sz; + private const byte _payload_byte = (byte)'A'; + public PubSubTestingStream(int publish_period_seconds, int max_pubs, int payload_size, ViewStreamDatabase vsd) { + _vsd = vsd; + _payload_sz = payload_size; + var schema = BlueDataUtility.form_schema("PubSubTest", + Tuple.Create("id", TypeCode.Int32), + Tuple.Create("n_pubs", TypeCode.Int32), + Tuple.Create("n_recvs", TypeCode.Int32), + Tuple.Create("last_sent_id", TypeCode.Int32), + Tuple.Create("last_recv_id", TypeCode.Int32), + Tuple.Create("last_sent_guid", TypeCode.String), + Tuple.Create("last_recv_guid", TypeCode.String), + Tuple.Create("match", TypeCode.Boolean), + Tuple.Create("echo_time", TypeCode.Int32)); + _table = new BlueTable(schema); + _publish_period = TimeSpan.FromSeconds(publish_period_seconds); + _msg_size = payload_size; + _max_pubs = max_pubs; + } + + public PubSubTestingStream(int publish_period_seconds, int max_pubs, int msg_size) + : this(publish_period_seconds, max_pubs, msg_size, ViewStreamDatabase.instance) { } + + protected override void myOnFirstSubscriber() { + _vsd.addWorker(this); + System.Threading.Thread th = new System.Threading.Thread(_subscribe_thread_func); + th.IsBackground = true; + th.Start(); + } + + protected override void myOnLastSubscriber() { + _vsd.removeWorker(this); + _continue_sub_thread = false; + } + + void _log_updates(IEnumerable updates) { + var idx = 0; + foreach (var msg in updates) { + bts.Log.Gen.info($"PubSubTesStream._subscribe_thread_func idx: {idx} msg: {msg}"); + idx++; + } + } + + private void _subscribe_thread_func() { + // subscription is done in another thread + SubscriberCore sub_core = new SubscriberCore(); + sub_core.subscribe("test_tag"); + while (_continue_sub_thread) { + // TODO recv_id + var updates = sub_core.get_updates(TimeSpan.FromMilliseconds(250)); + if (updates == null || updates.Count() == 0) + continue; + _log_updates(updates); + _n_updates += updates.Count(); + if (updates.Count() > 0) { + var server_msg = updates.Last(); + if (server_msg.data != null && server_msg.data.Length > 0) { + _last_recvd_msg = PayloadMessage.deserialize(server_msg.data); + _last_recvd_msg.travel_epoch_ms = epoch_now(); + } else { + // why are we getting empty or null data ? (server is sending data regardless?) + } + _fire_dirty = true; + } + } + } + + private void _publish_next_packet() { + if (_last_sent_msg == null) { + _last_sent_msg = new PayloadMessage(0, _payload_sz, _payload_byte) { + guid = Guid.NewGuid().ToString() + }; + } else { + _last_sent_msg.msg_id++; + _last_sent_msg.guid = Guid.NewGuid().ToString(); + _last_sent_msg.travel_epoch_ms = epoch_now(); + } + var ser_bytes = _last_sent_msg.serialize(); + _n_pubs++; + _pub_core.publish(String.Empty, "test_tag", ser_bytes); + } + + private void _fire_dp() { + object last_sent_id = _last_sent_msg == null ? (object)DBNull.Value : (object)_last_sent_msg.msg_id; + object last_recv_id = _last_recvd_msg == null ? (object)DBNull.Value : (object)_last_recvd_msg.msg_id; + object echo_time = DBNull.Value; + object last_sent_guid = _last_sent_msg == null ? (object)DBNull.Value : (object)_last_sent_msg.guid; + object last_recv_guid = _last_recvd_msg == null ? (object)DBNull.Value : (object)_last_recvd_msg.guid; + if (_last_sent_msg != null && _last_recvd_msg != null) { + echo_time = _last_recvd_msg.travel_epoch_ms - _last_sent_msg.travel_epoch_ms; + } + var match = last_sent_id.Equals(last_recv_id) && last_sent_guid.Equals(last_recv_guid); + IBlueRow nr = new BlueRow(_table.schema, 0, _n_pubs, _n_updates, + last_sent_id, last_recv_id, last_sent_guid, last_recv_guid, match, echo_time); + BlueTable table = new BlueTable(_table.schema); + table.addRow(nr); + var dp = BlueDataUtility.build_dp_inc(table, myLastValue as DataPacket, true); + myFireEvent(dp); + } + + public void doWork() { + if (DateTime.Now - _last_publish_time > _publish_period) + if (_n_pubs <= _max_pubs) { + _publish_next_packet(); + _last_publish_time = DateTime.Now; + } + // publish + // TODO: send id + if (_fire_dirty) { + _fire_dirty = false; + _fire_dp(); + } + } + + public static long epoch_now() { + return DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); + } + + } +} \ No newline at end of file diff --git a/src/DataStreamSuite/DataStream/Primitives/Misc/TestStream.cs b/src/DataStreamSuite/DataStream/Primitives/Misc/TestStream.cs index 0af779a35..2f9fa9ca6 100644 --- a/src/DataStreamSuite/DataStream/Primitives/Misc/TestStream.cs +++ b/src/DataStreamSuite/DataStream/Primitives/Misc/TestStream.cs @@ -10,7 +10,7 @@ namespace BCG { /// /// This stream (not for prod) provies a way to run tests - that could otherwise be run in NUnits. - /// (truth is stream rpovide a RDE for quickly coding and running, so it was eaiser to run etsts here) + /// (truth is stream rpovide a RDE for quickly coding and running, so it was eaiser to run tests here) /// [DataStreamUserRespresentation("tests")] public class TesStream : ViewStream { diff --git a/src/DataStreamSuite/DataStreamCore/Messaging/Publish/PubTablePublisher.cs b/src/DataStreamSuite/DataStreamCore/Messaging/Publish/PubTablePublisher.cs index 8debb5c3d..c9a87286f 100644 --- a/src/DataStreamSuite/DataStreamCore/Messaging/Publish/PubTablePublisher.cs +++ b/src/DataStreamSuite/DataStreamCore/Messaging/Publish/PubTablePublisher.cs @@ -92,7 +92,7 @@ namespace BCG { if (data.Length > 0) myPubCore.publish("DataStreamTag", PubStreamPrefixer.prefix(tableName), - new MemoryStream(data, 0, data.Length, false, true)); + data); } private IBlueRowCollection[] myGetRowCollectionArray(IBlueRow r) { diff --git a/src/DataStreamSuite/DataStreamCore/ViewStreamDatabase.cs b/src/DataStreamSuite/DataStreamCore/ViewStreamDatabase.cs index 635d43c73..e1b2eeac8 100644 --- a/src/DataStreamSuite/DataStreamCore/ViewStreamDatabase.cs +++ b/src/DataStreamSuite/DataStreamCore/ViewStreamDatabase.cs @@ -7,6 +7,7 @@ using BCG.Utilities.General; using System.Collections.Generic; using System.Threading; using mbts; +using System.Linq; namespace BCG { using HighPerformanceTimer = mbts.spark.HighPerformanceTimer; @@ -22,15 +23,15 @@ namespace BCG { //and how big they are. //public delegate void OnTagUpdateDelegate(PubMetaData metaData); //public event OnTagUpdateDelegate onTagUpdate; - private IDictionary myITableAccumulatorMap = new Hashtable(); + private Dictionary _table_accumulator_map = new Dictionary(); private static ViewStreamDatabase mySelf; private ISubscriberCore mySubscriberCore; - private ArrayList myWorkers = new ArrayList(); + private List _workers = new List(); private bool myIsDisposed = false; - private bool myIsInDoWork = false; + private bool _is_in_do_work = false; private static object myTimeInterval; - private HighPerformanceTimer myTimer = new HighPerformanceTimer(); - private double myLogTimeThreshold = 10000; + private HighPerformanceTimer _timer = new HighPerformanceTimer(); + private readonly double myLogTimeThreshold = 10000; private readonly double _dowork_threshold = 500; private Thread myThreadOwner = null; // ViewStreamDatabase takes a thread affinity when created. If called from a different thread it will assert. @@ -85,8 +86,8 @@ namespace BCG { List result = new List(); - foreach (string s in myITableAccumulatorMap.Keys) - if (((ITableAccumulator)myITableAccumulatorMap[s]).isActive()) + foreach (string s in _table_accumulator_map.Keys) + if (((ITableAccumulator)_table_accumulator_map[s]).isActive()) result.Add(s); return result; @@ -99,94 +100,85 @@ namespace BCG { // If theres a ITableAccumulator for the tableName its returned (to share subscriptions), otherwise // a new one is obtained from the Subscriber factory - myITableAccumulatorMap[tableName] = myITableAccumulatorMap[tableName] == null ? - AccumulatorFactory.getAccumulator(tableName, mySubscriberCore) : myITableAccumulatorMap[tableName]; + _table_accumulator_map[tableName] = _table_accumulator_map[tableName] == null ? + AccumulatorFactory.getAccumulator(tableName, mySubscriberCore) : _table_accumulator_map[tableName]; - return myITableAccumulatorMap[tableName] as ITableAccumulator; + return _table_accumulator_map[tableName] as ITableAccumulator; } public void doWork() { - verifyAccess(); - bts.Log.assert(false); // TODO + verifyAccess(); - // if (myIsSuspended) - // return; - // if (myIsInDoWork) - // return; - // else - // { - // myIsInDoWork = true; // to allow reentrancy - // try - // { - // if (onDoWorkStart != null) - // onDoWorkStart(); + if (_is_suspended) + return; + if (_is_worker_call_in_progress) + return; + else { + _is_worker_call_in_progress = true; // to allow reentrancy + try { + if (onDoWorkStart != null) + onDoWorkStart(); - // //ArrayList updateList = mySubscriberCore.getUpdates(myGetTimeInterval()); - // IDictionary updateMap = myGetUpdateMap(updateList); + var updates = mySubscriberCore.get_updates(TimeSpan.FromMilliseconds(10)); + if (updates != null) { + var consolidated_update_map = _consolidate_updates(updates); - // foreach (DictionaryEntry de in updateMap) - // { - // Stream[] allUpdates = (de.Value as ArrayList).ToArray(typeof(Stream)) as Stream[]; - // string tag = de.Key.ToString(); - // if (myITableAccumulatorMap[tag] == null){} - // // BlueEventLog.logEvent("Received a SubscriberCore's callback for a tag that was never subscribed", BlueEventLevel.Warning); - // else - // { - // myTimer.start(); + foreach (var kv in consolidated_update_map) { + string tag = kv.Key; + List tag_updates = kv.Value; + ITableAccumulator table_accumulator; + if (!_table_accumulator_map.TryGetValue(tag, out table_accumulator)) { + bts.Log.Gen.warn("Received callback for tag: {tag}"); + continue; + } + _timer.start(); + if (onTagFireStart != null) + onTagFireStart(tag); + table_accumulator.update(tag_updates.Select(t => new MemoryStream(t)).ToArray()); + if (onTagFireEnd != null) + onTagFireEnd(tag); + _timer.stop(); + if (_timer.lastIntervalTime > myLogTimeThreshold) { + bts.Log.Gen.error($"ViewStreamDatabase.doWork tag: {tag} took {_timer.lastIntervalTime} msecs"); + } + } + } - // if (onTagFireStart != null) - // onTagFireStart(tag); + // to allow for registering of workers on the worker callback - // (myITableAccumulatorMap[tag] as ITableAccumulator).update(allUpdates); - // if (onTagFireEnd != null) - // onTagFireEnd(tag); - // myTimer.stop(); - // if (myTimer.lastIntervalTime > myLogTimeThreshold) - // { - // // BlueEventLog.logEvent("callback on tag " + tag.ToUpper() + " took " + - // // myTimer.lastIntervalTime + " msecs", BlueEventLevel.Warning); - // } - // } - // } - - // // to allow for registering of workers on the worker callback - // myIsCallingWorkers = true; - // myPendingOps = new ArrayList(); - // foreach (IWorker w in myWorkers) - // { - // myTimer.start(); - // w.doWork(); - // myTimer.stop(); - // if (myTimer.lastIntervalTime > _dowork_threshold){} - // // mbts.Logger.log (bts.log.Level.Warn, bts.log.Subsystem.gen, String.Format ("VSD.dowork on worker {0} took {1}", w.GetType().ToString (), myTimer.lastIntervalTime)); - // } - // myIsCallingWorkers = false; - - // foreach (PendingAddRemoveWorker op in myPendingOps) - // if (op.isAdd) - // addWorker(op.w); - // else - // removeWorker(op.w); - // } - // finally - // { - // myPendingOps = null; - // myIsInDoWork = false; - // myIsCallingWorkers = false; - // if (onDoWorkEnd != null) - // onDoWorkEnd (); - // } - // }//end if we're not in do work + _is_worker_call_in_progress = true; + _pending_worker_ops.Clear(); + foreach (IWorker w in _workers) { + _timer.start(); + w.doWork(); + _timer.stop(); + if (_timer.lastIntervalTime > _dowork_threshold) + bts.Log.Gen.warn($"VSD.dowork on worker {w} took {_timer.lastIntervalTime}"); + } + _is_worker_call_in_progress = false; + foreach (PendingAddRemoveWorker op in _pending_worker_ops) + if (op.isAdd) + addWorker(op.w); + else + removeWorker(op.w); + } finally { + _pending_worker_ops.Clear(); + _is_in_do_work = false; + _is_worker_call_in_progress = false; + if (onDoWorkEnd != null) + onDoWorkEnd(); + } + } } - private bool myIsSuspended = false; + private bool _is_suspended = false; - public void suspend() { myIsSuspended = true; } + public void suspend() { _is_suspended = true; } - public void resume() { myIsSuspended = false; } + public void resume() { _is_suspended = false; } public bool checkAccess() { if (this.myThreadOwner == null) this.myThreadOwner = Thread.CurrentThread; @@ -206,38 +198,36 @@ namespace BCG { } } - private IDictionary myGetUpdateMap(ArrayList updates) { - IDictionary retVal = new Hashtable(); - foreach (PubSubUpdate update in updates) { - if (retVal[update.tag] == null) - retVal[update.tag] = new ArrayList(); - (retVal[update.tag] as ArrayList).Add(update.body); + private IDictionary> _consolidate_updates(IEnumerable updates) { + Dictionary> rv = new Dictionary>(); + foreach (var msg in updates) { + List tag_updates = null; + if (!rv.TryGetValue(msg.tag, out tag_updates)) { + tag_updates = new List(); + rv.Add(msg.tag, tag_updates); + } + tag_updates.Add(msg.data); } - if (retVal.Count > 0) { - string tagString = ""; - foreach (string tag in retVal.Keys) - tagString += tag + " , "; - } - return retVal; + return rv; } public bool isConsistent { get { return true; } } public void addWorker(IWorker w) { verifyAccess(); - if (myIsCallingWorkers) - myPendingOps.Add(new PendingAddRemoveWorker(true, w)); + if (_is_worker_call_in_progress) + _pending_worker_ops.Add(new PendingAddRemoveWorker(true, w)); else - myWorkers.Add(w); + _workers.Add(w); } public void removeWorker(IWorker w) { verifyAccess(); - if (myIsCallingWorkers) - myPendingOps.Add(new PendingAddRemoveWorker(false, w)); + if (_is_worker_call_in_progress) + _pending_worker_ops.Add(new PendingAddRemoveWorker(false, w)); else - myWorkers.Remove(w); + _workers.Remove(w); } #region IDisposable Members @@ -249,8 +239,8 @@ namespace BCG { } } - private ArrayList myPendingOps; - private bool myIsCallingWorkers = false; + private List _pending_worker_ops = new List(); + private bool _is_worker_call_in_progress = false; class PendingAddRemoveWorker { public PendingAddRemoveWorker(bool isAdd, IWorker w) { this.isAdd = isAdd; diff --git a/src/DataStreamSuite/DataStreamQueryAnalyzer/MainForm.cs b/src/DataStreamSuite/DataStreamQueryAnalyzer/MainForm.cs index 2630394f1..4fee092a0 100644 --- a/src/DataStreamSuite/DataStreamQueryAnalyzer/MainForm.cs +++ b/src/DataStreamSuite/DataStreamQueryAnalyzer/MainForm.cs @@ -74,7 +74,9 @@ namespace BCG { } private ViewStream myGetAllTagsStream() { // TODO: Port PublishedTagsStream from edge (then we can uncomment impl below) - return new FilterStream(new PubSubServerStateStream(), "tag~='DS_'"); + //ViewStream vs = new PubSubServerStateStream(); + ViewStream vs = new BlueRowCollectionStaticStream(); + return new FilterStream(vs, "tag~='DS_'"); //return new LiteralStream("tag/String", "DS_Comedians;DS_Baldies"); } @@ -350,18 +352,20 @@ namespace BCG { private DataStreamExpression myExp; private void myTimer_Tick(object sender, System.EventArgs e) { - try { - ViewStreamDatabase.instance.doWork(); - MasterWorker.getInstance(false).doWork(); - } catch (Exception exc) { - Log.Ui.warn(exc.ToString()); + // try + // { + ViewStreamDatabase.instance.doWork(); + MasterWorker.getInstance(false).doWork(); + //} + // catch (Exception exc) { + // Log.Ui.warn(exc.ToString()); - myDataGrid.DataSource = null; - //myTabControl.SelectedIndex = 1; - myTabControl.SelectedTab = myErrorTabPage; - myErrorTextBox.Text = exc.Message; - myErrorTextBox.Text += "\n" + exc.StackTrace; - } + // myDataGrid.DataSource = null; + // //myTabControl.SelectedIndex = 1; + // myTabControl.SelectedTab = myErrorTabPage; + // myErrorTextBox.Text = exc.Message; + // myErrorTextBox.Text += "\n" + exc.StackTrace; + // } } class StringWrapper { diff --git a/src/DataStreamSuite/PubSub/Client/Communication/ManagedTCP/PubSubServerManagedTCPConnection.cs b/src/DataStreamSuite/PubSub/Client/Communication/ManagedTCP/PubSubServerManagedTCPConnection.cs index ce3bcae94..93b6077cd 100644 --- a/src/DataStreamSuite/PubSub/Client/Communication/ManagedTCP/PubSubServerManagedTCPConnection.cs +++ b/src/DataStreamSuite/PubSub/Client/Communication/ManagedTCP/PubSubServerManagedTCPConnection.cs @@ -71,8 +71,8 @@ namespace BCG { } } - public void send(byte[] data) { - bts.Log.Gen.info($"PubSubServerManagedTCPConnection.send server: {_server_address} data: {data.Length} myConnection : {_connection != null}"); + private void _send(byte[] data) { + bts.Log.Gen.info($"PubSubServerManagedTCPConnection._send server: {_server_address} data: {data.Length} _connection : {_connection != null}"); if (_connection != null) { bts.Log.Gen.info("PubSubServerManagedTCPConnection.send server: " + _server_address + "data: " + data.Length); _connection.send(data); @@ -80,20 +80,22 @@ namespace BCG { } public void send(bts.PubSubClientMessage ps_msg) { - MemoryStream ms = new MemoryStream(); - ps_msg.serialize_into_str(ms); - var msg_bytes = StreamUtilityMethods.getBytesFromStream(ms); - send(msg_bytes); + bts.Log.Gen.info($"PubSubServerManagedTCPConnection.send server: {_server_address} ps_msg: {ps_msg}"); + var msg_bytes = ps_msg.serialize_into_bytes(); + PubSubClientMessage msg_retrv = new PubSubClientMessage(); + msg_retrv.consruct_deserialize(msg_bytes); + bts.Log.Gen.info($"PubSubServerManagedTCPConnection.send server: {_server_address} msg_retrv: {msg_retrv}"); + _send(msg_bytes); } - public void send(Stream str) { - BinaryReader br = new BinaryReader(str); - send(br.ReadBytes(Convert.ToInt32(str.Length - str.Position))); - } + // private void send(Stream str) { + // BinaryReader br = new BinaryReader(str); + // _send(br.ReadBytes(Convert.ToInt32(str.Length - str.Position))); + // } - public void send(PubSubMessage msg) { - send(msg.to_stream()); - } + // private void send(PubSubMessage msg) { + // send(msg.to_stream()); + // } public void send_with_confirmation(PubSubMessage msg, ref string conf_id) { var bytes = msg.to_bytes(); @@ -113,11 +115,13 @@ namespace BCG { public IEnumerable get_updates(TimeSpan timeout, int maxElements) { List rv_raw = _in_data_queue.dequeueMultipleElements(Convert.ToInt32(timeout.TotalMilliseconds), maxElements); List rv = new List(); + var i = 0; foreach (var b in rv_raw) { - MemoryStream str = new MemoryStream(b); PubSubServerMessage ps_msg = new PubSubServerMessage(); - ps_msg.consruct_deserialize(str); + ps_msg.consruct_deserialize(b); + bts.Log.Gen.info($"PubSubServerManagedTCPConnection.get_updates downloaded: {ps_msg} msg_no: {i} / {rv_raw.Count}"); rv.Add(ps_msg); + i++; } return rv; } @@ -276,7 +280,11 @@ namespace BCG { /// public void doWork() { //_dispose_and_reconnect_if_missing_hb(); + //try { _check_for_new_connection(); + // } catch (Exception ex) { + // bts.Log.Gen.warn($"PubsSubServerManagedTCPConnection.doWork ex: {ex}"); + // } } #endregion diff --git a/src/DataStreamSuite/PubSub/Client/MetaData/PubSubClientMessage.cs b/src/DataStreamSuite/PubSub/Client/MetaData/PubSubClientMessage.cs index 0281603f4..100c2f063 100644 --- a/src/DataStreamSuite/PubSub/Client/MetaData/PubSubClientMessage.cs +++ b/src/DataStreamSuite/PubSub/Client/MetaData/PubSubClientMessage.cs @@ -141,9 +141,11 @@ namespace bts { data_to_publish = br.ReadBytes(size); } public override string ToString() { - 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_to_publish: {data_to_publish.Length}"; + 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) { @@ -195,10 +197,19 @@ namespace bts { } } + 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(); - var msg_type = (PubSubMessageType)type; + msg_type = (PubSubMessageType)type; switch (msg_type) { case PubSubMessageType.SUBSCRIIBE: sub_msg = new SubscribeMsg(); @@ -219,6 +230,12 @@ namespace bts { } } + 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) { @@ -265,6 +282,14 @@ namespace bts { 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.ReadString(); @@ -273,8 +298,16 @@ namespace bts { 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() { - return $"PubSubServerMessage tag: {tag} tag_type: {tag_type} data_len: {data.Length}"; + 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) { diff --git a/src/DataStreamSuite/PubSub/Client/PublishingUtilities/Publisher.cs b/src/DataStreamSuite/PubSub/Client/PublishingUtilities/Publisher.cs index a3adec3eb..b0e11ddb1 100644 --- a/src/DataStreamSuite/PubSub/Client/PublishingUtilities/Publisher.cs +++ b/src/DataStreamSuite/PubSub/Client/PublishingUtilities/Publisher.cs @@ -1,66 +1,66 @@ -using System; -using System.IO; -using System.Runtime.Serialization; -using System.Runtime.Serialization.Formatters.Binary; +// using System; +// using System.IO; +// using System.Runtime.Serialization; +// using System.Runtime.Serialization.Formatters.Binary; -namespace BCG { - public class Publisher : IPublisher { - private PublisherCore myPubCore = new PublisherCore(); - private BinaryFormatter myBinFormatter = new BinaryFormatter(); - private const string myTagType = "DefaultTag"; +// namespace BCG { +// public class Publisher : IPublisher { +// private PublisherCore myPubCore = new PublisherCore(); +// private BinaryFormatter myBinFormatter = new BinaryFormatter(); +// private const string myTagType = "DefaultTag"; - public Publisher() { } +// public Publisher() { } - #region IPublisher Members +// #region IPublisher Members - public void publish(IPubItem pi) { publish(pi, myBinFormatter); } +// public void publish(IPubItem pi) { publish(pi, myBinFormatter); } - public void publish(IPubItem[] pis) { publish(pis, myBinFormatter); } +// public void publish(IPubItem[] pis) { publish(pis, myBinFormatter); } - public void publish(IPubItem pi, IFormatter f) { - MemoryStream ms = new MemoryStream(); -#pragma warning disable - f.Serialize(ms, pi); -#pragma warning restore SYSLIB0011 - ms.Position = 0; - myPubCore.publish(myTagType, pi.getTag(), ms); - } +// public void publish(IPubItem pi, IFormatter f) { +// MemoryStream ms = new MemoryStream(); +// #pragma warning disable +// f.Serialize(ms, pi); +// #pragma warning restore SYSLIB0011 +// ms.Position = 0; +// myPubCore.publish(myTagType, pi.getTag(), ms); +// } - public void publish(IPubItem[] pis, IFormatter f) { - myPubCore.begin_transaction(); - foreach (IPubItem pi in pis) - publish(pi, f); - myPubCore.commit_transaction(); - } +// public void publish(IPubItem[] pis, IFormatter f) { +// myPubCore.begin_transaction(); +// foreach (IPubItem pi in pis) +// publish(pi, f); +// myPubCore.commit_transaction(); +// } - #endregion - } +// #endregion +// } - public class Publisher : IPublisher where T : IFormatter, new() { - private PublisherCore myPubCore = new PublisherCore(); - private IFormatter myFormatter = new T(); - private const string myTagType = "DefaultTag"; +// public class Publisher : IPublisher where T : IFormatter, new() { +// private PublisherCore myPubCore = new PublisherCore(); +// private IFormatter myFormatter = new T(); +// private const string myTagType = "DefaultTag"; - public Publisher() { } +// public Publisher() { } - #region IPublisher Members +// #region IPublisher Members - public void publish(IPubItem pi) { - MemoryStream ms = new MemoryStream(); -#pragma warning disable - myFormatter.Serialize(ms, pi); -#pragma warning restore SYSLIB0011 - ms.Position = 0; - myPubCore.publish(myTagType, pi.getTag(), ms); - } +// public void publish(IPubItem pi) { +// MemoryStream ms = new MemoryStream(); +// #pragma warning disable +// myFormatter.Serialize(ms, pi); +// #pragma warning restore SYSLIB0011 +// ms.Position = 0; +// myPubCore.publish(myTagType, pi.getTag(), ms); +// } - public void publish(IPubItem[] pis) { - myPubCore.begin_transaction(); - foreach (IPubItem pi in pis) - publish(pi); - myPubCore.commit_transaction(); - } +// public void publish(IPubItem[] pis) { +// myPubCore.begin_transaction(); +// foreach (IPubItem pi in pis) +// publish(pi); +// myPubCore.commit_transaction(); +// } - #endregion - } -} +// #endregion +// } +// } diff --git a/src/DataStreamSuite/PubSub/Client/Routing/PubSubRoute.cs b/src/DataStreamSuite/PubSub/Client/Routing/PubSubRoute.cs index eb8da4645..d8a8b6e9d 100644 --- a/src/DataStreamSuite/PubSub/Client/Routing/PubSubRoute.cs +++ b/src/DataStreamSuite/PubSub/Client/Routing/PubSubRoute.cs @@ -57,7 +57,7 @@ namespace BCG { } public static PubSubRoute get_default_route() { - return new PubSubRoute("bluto://127.0.0.0.1:1976", ".*"); + return new PubSubRoute("bluto://127.0.0.0.1:12345", ".*"); } } } diff --git a/src/DataStreamSuite/PubSub/Client/Routing/PubSubRouter.cs b/src/DataStreamSuite/PubSub/Client/Routing/PubSubRouter.cs index 1536fd4ca..28bb67d6e 100644 --- a/src/DataStreamSuite/PubSub/Client/Routing/PubSubRouter.cs +++ b/src/DataStreamSuite/PubSub/Client/Routing/PubSubRouter.cs @@ -13,7 +13,7 @@ namespace BCG { private Regex[] mySubRouteRegExps; private PubSubRoute[] myPubRoutes; private PubSubRoute[] mySubRoutes; - private readonly string _temp_only_pubsub = "bluto://127.0.0.1:1976"; + private readonly string _temp_only_pubsub = "bluto://127.0.0.1:12345"; public PubSubRouter() : this( diff --git a/src/DataStreamSuite/PubSub/Config/PubSub3Config.cs b/src/DataStreamSuite/PubSub/Config/PubSub3Config.cs index 7e78f5ef4..0cb4ab2d0 100644 --- a/src/DataStreamSuite/PubSub/Config/PubSub3Config.cs +++ b/src/DataStreamSuite/PubSub/Config/PubSub3Config.cs @@ -32,7 +32,7 @@ namespace BCG { private int myPS3_StatisticsUpdatePeriod = 5000; private int myPS3_DisposedConnectionCheckPeriod = 1000; private int myPS3_MessageProcessingWarningThreshold = 1000; - private int myPS3_ServerPort = 1976; + private int myPS3_ServerPort = 12345; private int myPS3_MaxPacketSize = 500000000; private bool myBlueComm_mustDie = true; diff --git a/src/DataStreamSuite/PubSub/IPublisherCore.cs b/src/DataStreamSuite/PubSub/IPublisherCore.cs index 7ed700bb1..be34b92b4 100644 --- a/src/DataStreamSuite/PubSub/IPublisherCore.cs +++ b/src/DataStreamSuite/PubSub/IPublisherCore.cs @@ -2,12 +2,12 @@ using System; using System.Collections.Generic; using System.Text; using System.IO; +using bts; namespace BCG { public interface IPublisherCore : IDisposable { - void publish(string tag, Stream content); - void publish(string tag_type, string tag, Stream content); - //prevents this tag from being republished if the connection is reset. + void publish(PubSubClientMessage msg); + void publish(string tag_type, string tag, byte[] content); void clear_state(string tag); void begin_transaction(); void commit_transaction(); diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnector_APM.cs b/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnector_APM.cs index 3f92756d3..9992cce2f 100644 --- a/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnector_APM.cs +++ b/src/DataStreamSuite/PubSub/ManagedTCP/AsyncTCPConnector_APM.cs @@ -41,7 +41,7 @@ namespace bts { } private static void _on_connect(IAsyncResult res) { - bts.Log.Gen.info("AsyncTCPConnector_APM._on_connect " + res.ToString() + " " + res.AsyncState.ToString()); + bts.Log.Gen.info("AsyncTCPConnector_APM._on_connect res: {res.AsyncState.ToString()}"); SocketConnectionArgs sc_args = res.AsyncState as SocketConnectionArgs; try { sc_args.sock.EndConnect(res); diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/ITCPConnection.cs b/src/DataStreamSuite/PubSub/ManagedTCP/ITCPConnection.cs index 2b61b354d..bb6fe5f99 100644 --- a/src/DataStreamSuite/PubSub/ManagedTCP/ITCPConnection.cs +++ b/src/DataStreamSuite/PubSub/ManagedTCP/ITCPConnection.cs @@ -34,6 +34,7 @@ namespace BCG { /// void register(ITCPMessageHandler messageHandler); + // TODO: represent address diff /// /// Gets the ip and port of the peer to which this /// connection is connected. diff --git a/src/DataStreamSuite/PubSub/ManagedTCP/ServerToClientManagedTCPConnectionAcceptor.cs b/src/DataStreamSuite/PubSub/ManagedTCP/ServerToClientManagedTCPConnectionAcceptor.cs index 7d01bf2c4..70c80a301 100644 --- a/src/DataStreamSuite/PubSub/ManagedTCP/ServerToClientManagedTCPConnectionAcceptor.cs +++ b/src/DataStreamSuite/PubSub/ManagedTCP/ServerToClientManagedTCPConnectionAcceptor.cs @@ -1,58 +1,62 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; -//using BCG.Utilities.General; -//using BCG.Utilities; +using System.Net.Sockets; +using System.Net; -namespace BCG { +using BCG; + +namespace bts { public class ServerToClientManagedTCPConnectionAcceptor { - private static Dictionary myPortToSingleServerAcceptorMap = new Dictionary(); - private static object mySyncObject = new object(); - - public static int activateWithRandomPort(ITCPConnectionHandler handler, TimeSpan clientHBTimeSpan) { - SingleServerToClientManagedTCPConnectionAcceptor theAcceptor = new SingleServerToClientManagedTCPConnectionAcceptor(); - int portNo = theAcceptor.activateWithRandomPort(handler, clientHBTimeSpan); - - lock (mySyncObject) { - myPortToSingleServerAcceptorMap[portNo] = theAcceptor; - - } - return portNo; + private readonly int _port; + private readonly ITCPConnectionHandler _handler; + private readonly Socket _accept_socket; + public ServerToClientManagedTCPConnectionAcceptor(int port, ITCPConnectionHandler handler) { + bts.Log.Gen.info($"ServerToClientManagedTCPConnectionAcceptor.cons port: {port}"); + _port = port; + _handler = handler; + _accept_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); + Func bind = () => { + try { + _accept_socket.Bind(new IPEndPoint(IPAddress.Any, port)); + _accept_socket.Listen(250); + return true; + } catch (Exception ex) { + bts.Log.Gen.error("ServerToClientManagedTCPConnectionAcceptor.cons bind on port: {port} exception: {ex}"); + return false; + } + }; + bts.Log.assert(bind()); + bts.Log.Gen.info("ServerToClientManagedTCPConnectionAcceptor.cons bind to port: {port} successful"); } - // public static int activate(ITCPConnectionHandler handler, int port, TimeSpan clientHBTimeSpan) { - // lock (mySyncObject) { - // if (myPortToSingleServerAcceptorMap.ContainsKey(port)) { - // string msg = string.Format("Error: Application is already accepting clients on port {0}", port); - // // BlueEventLog.logEvent(msg, BlueEventLog2Level.Error); - // return port; - // } else { - // SingleServerToClientManagedTCPConnectionAcceptor theAcceptor = new SingleServerToClientManagedTCPConnectionAcceptor(); - // int assignedport = theAcceptor.activate(handler, port, clientHBTimeSpan); - // myPortToSingleServerAcceptorMap[assignedport] = theAcceptor; - // return assignedport; - // } - // } //end lock - // } //end activate code - - public static int accept_connections(ITCPConnectionHandler handler, int port, TimeSpan clientHBTimeSpan) { - lock (mySyncObject) { - if (myPortToSingleServerAcceptorMap.ContainsKey(port)) { - string msg = string.Format("Error: Application is already accepting clients on port {0}", port); - // BlueEventLog.logEvent(msg, BlueEventLog2Level.Error); - return port; - } else { - SingleServerToClientManagedTCPConnectionAcceptor ps_acceptor = new SingleServerToClientManagedTCPConnectionAcceptor(); - int assignedport = ps_acceptor.accept_on_port(handler, "127.0.0.1", port, clientHBTimeSpan); - myPortToSingleServerAcceptorMap[assignedport] = ps_acceptor; - return assignedport; - } - } //end lock - } //end activate code - + public void activate() { + bts.Log.Gen.info($"ServerToClientManagedTCPConnectionAcceptor.activate called port: {_port}"); + _accept_socket.BeginAccept(new AsyncCallback(_on_accept), new object[] { }); + } + private void _on_accept(IAsyncResult res) { + bts.Log.Gen.info("ServerToClientManagedTCPConnectionAcceptor._on_accept called port: {port}"); + Socket new_sock; + try { + new_sock = _accept_socket.EndAccept(res); + } catch (Exception ex) { + System.Threading.Thread.Sleep(TimeSpan.FromSeconds(5)); + _accept_socket.BeginAccept(new AsyncCallback(_on_accept), new object[] { }); + if (ex is OutOfMemoryException) + throw ex; + return; + } + new_sock.LingerState = new LingerOption(false, 0); + new_sock.Blocking = false; + ITCPConnection new_conn = new bts.AsyncTCPConnection_APM(new_sock, TimeSpan.FromSeconds(1), ITCPConnection.ConnectionSide.Server); + _handler.onConnection(new_conn); + _accept_socket.BeginAccept(new AsyncCallback(_on_accept), new object[] { }); + string ip; int port; + new_conn.getPeerAddress(out ip, out port); + bts.Log.Gen.info("ServerToClientManagedTCPConnectionAcceptor._on_accept connection accepted successfully from ip: {ip} port: {port}"); + } } diff --git a/src/DataStreamSuite/PubSub/Misc/StreamUtilityMethods.cs b/src/DataStreamSuite/PubSub/Misc/StreamUtilityMethods.cs index 112ee52f3..910aa5a2b 100644 --- a/src/DataStreamSuite/PubSub/Misc/StreamUtilityMethods.cs +++ b/src/DataStreamSuite/PubSub/Misc/StreamUtilityMethods.cs @@ -2,70 +2,68 @@ using System; using System.IO; using System.Collections; -namespace BCG -{ +namespace BCG { /// /// Collection of utility methods used throughout /// - public class StreamUtilityMethods - { - public StreamUtilityMethods() {} + public static class StreamUtilityMethods { - public static Stream getAggregatedStream (Stream[] streams) - { + public static Stream getAggregatedStream(Stream[] streams) { if (streams.Length == 1) return streams[0]; - else - { - BinaryWriter bw = new BinaryWriter (new MemoryStream ()); - foreach (Stream s in streams) - { - bw.Write (new BinaryReader(s).ReadBytes (Convert.ToInt32 (s.Length-s.Position))); + else { + BinaryWriter bw = new BinaryWriter(new MemoryStream()); + foreach (Stream s in streams) { + bw.Write(new BinaryReader(s).ReadBytes(Convert.ToInt32(s.Length - s.Position))); } bw.BaseStream.Position = 0; return bw.BaseStream; } } - public static void copyTo(Stream[] sourceStreams, Stream target) - { + public static string first_n_bytes_to_string(byte[] b, int n) { + string rv = String.Empty; + var sz = Math.Min(n, b.Length); + for (int i = 0; i < sz; i++) { + rv += $"{b[i]} "; + } + rv.TrimEnd(); + return rv; + } + + public static void copyTo(Stream[] sourceStreams, Stream target) { long initPos = target.Position; BinaryWriter bw = new BinaryWriter(target); - foreach (Stream s in sourceStreams) - { + foreach (Stream s in sourceStreams) { bw.Write(new BinaryReader(s).ReadBytes(Convert.ToInt32(s.Length - s.Position))); } bw.BaseStream.Position = initPos; } - public static byte[] getBytesFromStream (params Stream[] streams) - { + public static byte[] getBytesFromStream(params Stream[] streams) { long totalSize = 0; - BinaryWriter bw = new BinaryWriter (new MemoryStream ()); + BinaryWriter bw = new BinaryWriter(new MemoryStream()); foreach (Stream s in streams) totalSize += s.Length - s.Position; - bw.Write (Convert.ToInt32 (totalSize)); - foreach (Stream s in streams) - { - BinaryReader br = new BinaryReader (s); + bw.Write(Convert.ToInt32(totalSize)); + foreach (Stream s in streams) { + BinaryReader br = new BinaryReader(s); int lengthToReadForThisStream = Convert.ToInt32(s.Length - s.Position); - bw.Write (br.ReadBytes (lengthToReadForThisStream)); + bw.Write(br.ReadBytes(lengthToReadForThisStream)); } - return (bw.BaseStream as MemoryStream).ToArray (); + return (bw.BaseStream as MemoryStream).ToArray(); } - /*takes the input stream and splits it into two streams at the given position. */ - public static Stream[] splitStream(Stream stream, int splitLocation) - { + /*takes the input stream and splits it into two streams at the given position. */ + public static Stream[] splitStream(Stream stream, int splitLocation) { stream.Position = 0; byte[] streamBytes = getBytesFromStream(new Stream[] { stream }); byte[] firstStreamBytes = new byte[splitLocation]; byte[] secondStreamBytes = new byte[stream.Length - splitLocation]; - for (int i = 0; i < stream.Length; ++i) - { + for (int i = 0; i < stream.Length; ++i) { //for some resaon, the 'getBytesFromStream' function prepends the length of the byte array to the bytes. This means //we have to ignore those bytes here. if (i < splitLocation) @@ -75,7 +73,7 @@ namespace BCG } - return new Stream[] {new MemoryStream(firstStreamBytes), new MemoryStream(secondStreamBytes)}; + return new Stream[] { new MemoryStream(firstStreamBytes), new MemoryStream(secondStreamBytes) }; } diff --git a/src/DataStreamSuite/PubSub/PublisherCore.cs b/src/DataStreamSuite/PubSub/PublisherCore.cs index 77aa5b83a..687df550b 100644 --- a/src/DataStreamSuite/PubSub/PublisherCore.cs +++ b/src/DataStreamSuite/PubSub/PublisherCore.cs @@ -10,45 +10,12 @@ namespace BCG { // this class uses an internal queue + thread to serialize and process calls // made through its public interface (IPublisherCore) public class PublisherCore : IPublisherCore { - public enum PublisherCoreMessageType { - transaction_begin, - transaction_commit, - transaction_abort, - publish, - clear - } - - public class PublisherCoreMessage { - public PublisherCoreMessageType msg_type; - public string tag_type, tag; - public Stream data; - - public PublisherCoreMessage(string tag_to_clear) { - tag = tag_to_clear; - msg_type = PublisherCoreMessageType.clear; - - } - - public PublisherCoreMessage(PublisherCoreMessageType the_msg_type) { - msg_type = the_msg_type; - } - - public PublisherCoreMessage(string theTagType, string theTag, Stream theData) { - msg_type = PublisherCoreMessageType.publish; - tag_type = theTagType; - tag = theTag; - data = theData; - } - public override string ToString() { - return $"PublisherCoreMessage tag: {tag} data.Position: {data.Position} data.Length: {data.Length}"; - } - } // used to allow for different transports // to be used when communicating to a pubsubserver protected PubSubServerConnectionFactory _connection_factory = new PubSubServerConnectionFactory(false); // internal queue used to serialize pubsub method calls - protected SyncQueue _main_queue = new SyncQueue(); + protected SyncQueue _main_queue = new SyncQueue(); // all the data published through this object in snapshot form // i.e. in a form that can be used repopulate a pubsub server @@ -97,13 +64,6 @@ namespace BCG { #region Main Thread - all library functions are called on the main thread and queued to be picked by the pump - public void publish(string tag, Stream content) { publish(String.Empty, tag, content); } - - public void publish(string tag_type, string tag, Stream content) { - PublisherCoreMessage msg = new PublisherCoreMessage(tag_type, tag, content); - bts.Log.Gen.info($"PublisherCore.publish tag: {tag} msg: {msg} len: {content.Length - content.Position}"); - _main_queue.enqueue(msg); - } public void begin_transaction() { bts.Log.Gen.warn("PublisherCore.begin_transaction transactions not supported anymore"); @@ -123,31 +83,57 @@ namespace BCG { public bool is_in_transaction { get { return false; } } public void clear_state(string tag) { - _main_queue.enqueue(new PublisherCoreMessage(tag)); + // TODO + bts.Log.assert(false); + //_main_queue.enqueue(new PublisherCoreMessage(tag)); + } + + public void publish(PubSubClientMessage msg) { + bts.Log.Gen.info($"PublisherCore.publish msg: {msg}"); + _main_queue.enqueue(msg); + } + + public void publish(string tag_type, string tag, byte[] content) { + // makes a PubSubClientMsg and calls the other publish + bts.PublishMsg publish_msg = new bts.PublishMsg() { + connection = String.Empty, // this will be filled when actually send it out in the _publish function + publishing_app = ClientInfo.thisClient.processString, + tag = tag, + tag_type = tag_type, + is_compressed = false, + publish_time_epoch_ms = DateTimeOffset.UtcNow.ToUnixTimeSeconds(), + data_to_publish = content + }; + PubSubClientMessage msg = new PubSubClientMessage { + pub_msg = publish_msg, + msg_type = PubSubMessageType.PUBLISH + }; + publish(msg); + } #endregion #region Message Pump Thread - this is where messages are dequeued and the action performed - + // all the state manahement happens here, the public interface methods merely enqueue the operation private void _message_pump() { while (true) { try { while (true) { - var msg = _main_queue.dequeue(100); + PubSubClientMessage? msg = _main_queue.dequeue(100); if (msg != null) { bts.Log.Gen.info($"PublisherCore._message_pump msg: {msg}"); switch (msg.msg_type) { - case PublisherCoreMessageType.transaction_abort: - case PublisherCoreMessageType.transaction_begin: - case PublisherCoreMessageType.transaction_commit: + case PubSubMessageType.TRANSACTION: bts.Log.assert_d(false, "transactiosn not supported for now"); break; - case PublisherCoreMessageType.clear: - _clear_state(msg.tag); + case PubSubMessageType.SUBSCRIIBE: + case PubSubMessageType.UNSUBSCRIIBE: + bts.Log.assert_d(false, $"Incorrect msg: {msg}"); break; - case PublisherCoreMessageType.publish: - _publish(msg.tag_type, msg.tag, msg.data); + case PubSubMessageType.PUBLISH: + var pub_msg = msg.pub_msg; + _publish(pub_msg); break; } } @@ -266,34 +252,18 @@ namespace BCG { } // updates the state and calls _send_data to send the data out - protected virtual void _publish(string tag_type_arg, string tag_arg, Stream content) { - bts.Log.Gen.info($"PublisherCore._publish tag: {tag_arg} content: {content.Position} / {content.Length} myInTransaction: {_is_in_transaction}"); - var content_bytes = StreamUtilityMethods.getBytesFromStream(content); + protected virtual void _publish(PublishMsg msg) { + bts.Log.Gen.info($"PublisherCore._publish msg: {msg}"); // update the state - _state.publish(tag_arg, tag_type_arg, content_bytes); + _state.publish(msg.tag, msg.tag_type, msg.data_to_publish); // send the data out - _send_data(tag_type_arg, tag_arg, content_bytes); - } - - // This actually send sthe data out - private void _send_data(string tag_type_arg, string tag_arg, byte[] content_bytes) { - bts.Log.Gen.info($"PublisherCore._send_data tag: {tag_arg} tag_type: {tag_type_arg} " + - $"sz: {content_bytes.Length}"); - var sender = _get_or_build_sender(tag_arg); - bts.PublishMsg publish_msg = new bts.PublishMsg() { - connection = sender.server_url, - publishing_app = ClientInfo.thisClient.processString, - tag = tag_arg, - tag_type = tag_type_arg, - is_compressed = false, - publish_time_epoch_ms = DateTimeOffset.UtcNow.ToUnixTimeSeconds(), - data_to_publish = content_bytes - }; - PubSubClientMessage msg = new PubSubClientMessage() { - msg_type = PubSubMessageType.PUBLISH, - pub_msg = publish_msg - }; - sender.send(msg); + var sender = _get_or_build_sender(msg.tag); + // actually send the data out + msg.connection = sender.client_url; + sender.send(new PubSubClientMessage() { + pub_msg = msg, + msg_type = PubSubMessageType.PUBLISH + }); } private void _clear_state(string tag) { @@ -329,13 +299,23 @@ namespace BCG { var conn = kv.Value; if (conn.server_url == server_url) { string tag_type; - var tag_data = _state.getContent(tag, out tag_type); - _send_data(tag, tag_type, tag_data); + var content_bytes = _state.getContent(tag, out tag_type); + //_send_data(tag, tag_type, tag_data); + bts.PublishMsg publish_msg = new bts.PublishMsg() { + connection = kv.Key, + publishing_app = ClientInfo.thisClient.processString, + tag = tag, + tag_type = tag_type, + is_compressed = false, + publish_time_epoch_ms = DateTimeOffset.UtcNow.ToUnixTimeSeconds(), + data_to_publish = content_bytes + }; + _publish(publish_msg); } } } } - - #endregion } + + #endregion } \ No newline at end of file diff --git a/src/DataStreamSuite/PubSub/Server/Communication/IPubsubMessageHub.cs b/src/DataStreamSuite/PubSub/Server/Communication/IPubsubMessageHub.cs index 205052d58..ec152f9b9 100644 --- a/src/DataStreamSuite/PubSub/Server/Communication/IPubsubMessageHub.cs +++ b/src/DataStreamSuite/PubSub/Server/Communication/IPubsubMessageHub.cs @@ -54,7 +54,7 @@ namespace BCG { /// /// /// - bool send(string receiver, byte[] data); + //bool send(string receiver, byte[] data); bool send(string receiver, bts.PubSubServerMessage ps_msg); /// /// sends the data stored in the current position in the Stream. @@ -62,6 +62,6 @@ namespace BCG { /// /// /// - bool send(string receiver, Stream str); + //bool send(string receiver, Stream str); } } diff --git a/src/DataStreamSuite/PubSub/Server/Communication/ManagedTCP/ManagedTCPMessageHub.cs b/src/DataStreamSuite/PubSub/Server/Communication/ManagedTCP/ManagedTCPMessageHub.cs index e7144245c..5835ab4f7 100644 --- a/src/DataStreamSuite/PubSub/Server/Communication/ManagedTCP/ManagedTCPMessageHub.cs +++ b/src/DataStreamSuite/PubSub/Server/Communication/ManagedTCP/ManagedTCPMessageHub.cs @@ -11,6 +11,7 @@ using mbts; using System.Linq; using System.Collections; using System.ComponentModel.DataAnnotations; +using bts; namespace BCG { @@ -76,10 +77,12 @@ namespace BCG { private TimeSpan _collect_disposed_connections_period = new TimeSpan(0, 0, 0, 0, 1000); private DateTime _last_HB_collection_time = DateTime.Now; private DateTime _last_disposed_dead_collection_time = DateTime.Now; + private ServerToClientManagedTCPConnectionAcceptor _connection_acceptor; // TODO, get public ManagedTCPMessageHub(int port, TimeSpan hb_send_interval, int missing_HBs_for_gc, TimeSpan collect_disposed_connections_period) { _port = port; + _connection_acceptor = new ServerToClientManagedTCPConnectionAcceptor(port, this); _hb_send_interval = hb_send_interval; _subscriber_drop_threshold = new TimeSpan(0, 0, 0, 0, Convert.ToInt32(hb_send_interval.TotalMilliseconds * missing_HBs_for_gc)); _collect_disposed_connections_period = collect_disposed_connections_period; @@ -101,32 +104,33 @@ namespace BCG { } public void activate() { - _port = ServerToClientManagedTCPConnectionAcceptor.accept_connections(this, _port, _hb_send_interval); - bts.Log.Gen.info($"TCP Component is ready, listening on port {_port} for incoming connections"); + _connection_acceptor.activate(); + bts.Log.Gen.info($"ManagedTCPMessageHub.activate TCP Component is ready, listening on port {_port} for incoming connections"); _activated = true; - Thread doItAllThread = new Thread(new ThreadStart(_main_message_pump)); - doItAllThread.IsBackground = true; - doItAllThread.Start(); + Thread do_it_all_thread = new Thread(new ThreadStart(_main_message_pump)); + do_it_all_thread.IsBackground = true; + do_it_all_thread.Start(); } public List> get_updates(TimeSpan timeout, int maxElements) { return _received_data_queue.dequeueMultipleElements(timeout, maxElements); } - public bool send(string receiver, byte[] data) { + private bool _enqueue_send(string receiver, byte[] data) { + bts.Log.Gen.info($"ManagedTCPMessageHub._enqueue_send receiver: {receiver} data len: {data.Length}"); _send_data_queue.enqueue(new Pair(receiver, data)); return true; } - public bool send(string receiver, System.IO.Stream str) { - BinaryReader br = new BinaryReader(str); - return send(receiver, br.ReadBytes(Convert.ToInt32(str.Length - str.Position))); - } + // public bool send(string receiver, System.IO.Stream str) { + // BinaryReader br = new BinaryReader(str); + // return send(receiver, br.ReadBytes(Convert.ToInt32(str.Length - str.Position))); + // } public bool send(string receiver, bts.PubSubServerMessage ps_msg) { - MemoryStream str = new MemoryStream(); - ps_msg.serialize_into_str(str); - return send(receiver, str); + bts.Log.Gen.info($"ManagedTCPMessageHub.send receiver: {receiver} ps_msg: {ps_msg}"); + var ps_msg_bytes = ps_msg.serialize_into_bytes(); + return _enqueue_send(receiver, ps_msg_bytes); } #endregion @@ -158,7 +162,11 @@ namespace BCG { /// /// public void onConnection(ITCPConnection connection) { - // this is where we get new connections, dead connections are collected in _collect_disposed_connections + // this is where we get new connections, dead connections are collected in _collect_disposed_connections + string ip; + int port; + connection.getPeerAddress(out ip, out port); + bts.Log.Gen.info($"ManagedTCPMessageHub.onConnection ip: {ip} port: {port}"); _new_connections.enqueue(connection); } @@ -214,19 +222,8 @@ namespace BCG { } else { string warningMsg = String.Format("sending data to {0} failed, this client might have " + "disconnected, in which case it will be garbage collected within 60 secs", p.first); - //BlueEventLog.logEvent(warningMsg, PS3LogLevels.disconnection); } } - //times["send"].stop(); - - // if (PubSub3Config.instance.PS3_DoItAllSecondsBetweenLogs > 0 && - // (DateTime.Now - lastTimmingLog).TotalSeconds > PubSub3Config.instance.PS3_DoItAllSecondsBetweenLogs) { - // lastTimmingLog = DateTime.Now; - - // StringBuilder b = new StringBuilder(); - // b.AppendFormat("myDoItAll stats:\n"); - // b.AppendFormat("queue size: {0}\n", _send_data_queue.getCount()); - // } } } catch (Exception ex) { //BlueEventLog.logEvent(ex); diff --git a/src/DataStreamSuite/PubSub/Server/PubSubMessageReactor.cs b/src/DataStreamSuite/PubSub/Server/PubSubMessageReactor.cs index 9f184c64e..57fd4730b 100644 --- a/src/DataStreamSuite/PubSub/Server/PubSubMessageReactor.cs +++ b/src/DataStreamSuite/PubSub/Server/PubSubMessageReactor.cs @@ -85,6 +85,10 @@ namespace BCG { while (true) { try { var received_data = _msg_hub.get_updates(poll_timeout, 1); + var n_msgs = received_data.Count; + if (n_msgs > 0) { + bts.Log.Gen.info($"PubSubMessageReactor._polling_thread_func n_msgs: {n_msgs}"); + } foreach (var kv in received_data) { _handle_client_msg(kv.first, kv.second); } @@ -100,8 +104,8 @@ namespace BCG { private void _handle_client_msg(string sender, byte[] msg) { // TODO: reduce object creation, C# has an ObjectPool PubSubClientMessage ps_client_msg = new PubSubClientMessage(); - var str = new MemoryStream(msg); - ps_client_msg.consruct_deserialize(str); + ps_client_msg.consruct_deserialize(msg); + bts.Log.Gen.info($"PubSubMessageReator._handle_client_msg ps_client_msg: {ps_client_msg}"); switch (ps_client_msg.msg_type) { case PubSubMessageType.SUBSCRIIBE: _handle_sub(sender, ps_client_msg.sub_msg); @@ -129,13 +133,14 @@ namespace BCG { byte[] data = _state.getContent(tag); //ie data has been published once if (data != null) { + bts.Log.Gen.info($"ManagedTCPMessgeHub._handle_sub tag: {tag} data len: {data.Length}"); string tagType = _state.getTagType(tag); var msg_to_send = new PubSubServerMessage() { tag = tag, tag_type = tagType, data = data }; - _msg_hub.send(sub_msg.connection, msg_to_send); + _msg_hub.send(sub_msg.connection, ps_msg: msg_to_send); return; } } diff --git a/src/DataStreamSuite/PubSub/Server/PubSubState.cs b/src/DataStreamSuite/PubSub/Server/PubSubState.cs index 61672a6bc..3c1719265 100644 --- a/src/DataStreamSuite/PubSub/Server/PubSubState.cs +++ b/src/DataStreamSuite/PubSub/Server/PubSubState.cs @@ -6,6 +6,7 @@ using System.Reflection; using System.IO; using mbts; +using System.Linq.Expressions; namespace BCG { /// @@ -228,8 +229,10 @@ namespace BCG { public byte[] getContent(string tag) { if (!_tag_to_data_map.ContainsKey(tag)) return null; - else - return _tag_to_data_map[tag].getCurrentContent(); + var rv = _tag_to_data_map[tag].getCurrentContent(); + var rv_txt = StreamUtilityMethods.first_n_bytes_to_string(rv, 4); + bts.Log.Gen.info("PubSubState.getContent tag: {tag} content len: {rv.Length} rv: {rv_txt}"); + return rv; } public string getTagType(string tag) { diff --git a/src/DataStreamSuite/PubSub/Server/SingleTagData.cs b/src/DataStreamSuite/PubSub/Server/SingleTagData.cs index db02a014c..1afcdbf8a 100644 --- a/src/DataStreamSuite/PubSub/Server/SingleTagData.cs +++ b/src/DataStreamSuite/PubSub/Server/SingleTagData.cs @@ -62,7 +62,7 @@ namespace BCG { internal void handle_publish(PublishMsg pub_msg) { string handler_str = _tag_handler != null ? _tag_handler.GetType().ToString() : "NULL"; - bts.Log.Gen.info("SingleTagData.handle_publish pub_msg: {pub_msg} handler: {handler_str}"); + bts.Log.Gen.info($"SingleTagData.handle_publish pub_msg: {pub_msg} handler: {handler_str} pub_msg: {pub_msg}"); if (_tag_handler != null) { try { _tag_handler.update(_tag, new Stream[] { new MemoryStream(pub_msg.data_to_publish) }); @@ -75,6 +75,7 @@ namespace BCG { throw new ApplicationException(err_msg, e); } } else { + bts.Log.Gen.info($"SingleTagData.handle_publish, no handler, so just updating data, len: {pub_msg.data_to_publish.Length}"); _last_published_data = pub_msg.data_to_publish; } } diff --git a/src/DataStreamSuite/PubSub/SingleServerSubscriberCore.cs b/src/DataStreamSuite/PubSub/SingleServerSubscriberCore.cs index 89f053b7e..f832b7ba9 100644 --- a/src/DataStreamSuite/PubSub/SingleServerSubscriberCore.cs +++ b/src/DataStreamSuite/PubSub/SingleServerSubscriberCore.cs @@ -7,6 +7,7 @@ using BCG.Utilities.General; using mbts; using mbts.spark; using System.Linq; +using bts; namespace BCG { /// @@ -18,7 +19,7 @@ namespace BCG { /// internal interface ISingleServerSubscriberCore : IDisposable { - IEnumerable get_updates(int timeout); + IEnumerable get_updates(TimeSpan timeout); event PubSubConnectionChange onPubSubConnectionChange; void subscribe(params string[] tags); void unsubscribe(params string[] tags); @@ -26,22 +27,41 @@ namespace BCG { internal class SingleServerSubscriberCore : IDisposable, ISingleServerSubscriberCore { private readonly IPubSubServerConnection _connection; + private SyncQueue _in_data_queue = new SyncQueue(); private HashSet _tags = new HashSet(); public event PubSubConnectionChange onPubSubConnectionChange; public SingleServerSubscriberCore(string server_url, PubSubServerConnectionFactory connection_factory) { - bts.Log.Gen.info($"SingleServerSubscriberCore consructed for server_url: {server_url}"); + bts.Log.Gen.info($"SingleServerSubscriberCore constructed for server_url: {server_url}"); _connection = connection_factory.getConnection(server_url); _connection.register_connection_callback(new PubSubServerConnectionStatusChange(_handle_server_reboot)); + _connection.register_data_callback(_on_data); } // gets as many data items that it can get in // timeout milliseconds - public IEnumerable get_updates(int timeout) { + public IEnumerable get_updates(TimeSpan timeout) { _connection.doWork(); //List rv = new List(); - return _connection.get_updates(TimeSpan.FromMilliseconds(timeout), Int32.MaxValue); + List conn_data = _in_data_queue.dequeueMultipleElements(TimeSpan.Zero, 1024); // TODO, max_elements + 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); + rv.Add(msg); + } + return rv; + } + + private void _on_data(string sender, byte[] data) { + var data_txt = StreamUtilityMethods.first_n_bytes_to_string(data, 4); + bts.Log.Gen.info($"SingleServerSubscriberCore._on_data sender: {sender} data len: {data.Length} data: {data_txt}"); + _in_data_queue.enqueue(data); + // TODO: enqueue, and dequeue in get_updates } public void subscribe(params string[] tags) { diff --git a/src/DataStreamSuite/PubSub/SubscriberCore.cs b/src/DataStreamSuite/PubSub/SubscriberCore.cs index 71bfa4f15..9fbd99d28 100644 --- a/src/DataStreamSuite/PubSub/SubscriberCore.cs +++ b/src/DataStreamSuite/PubSub/SubscriberCore.cs @@ -56,8 +56,12 @@ namespace BCG { #endregion public IEnumerable get_updates(TimeSpan timeout) { - // TODO - return null; + List rv = new List(); + foreach (var kv in _server_url_to_subcore_map) { + var sub_core = kv.Value; + rv.AddRange(sub_core.get_updates(timeout)); + } + return rv; } private IDictionary _n_update_map = new Dictionary(); @@ -86,7 +90,7 @@ namespace BCG { private ISingleServerSubscriberCore _find_subscribercore(string server_url) { ISingleServerSubscriberCore rv = null; - if (_server_url_to_subcore_map.TryGetValue(server_url, out rv)) { + if (!_server_url_to_subcore_map.TryGetValue(server_url, out rv)) { rv = new SingleServerSubscriberCore(server_url, _connection_factory); _server_url_to_subcore_map.Add(server_url, rv); rv.onPubSubConnectionChange += _handle_pubsubserver_connection_change; diff --git a/src/DataStreamSuite/PubSub/Testing/TestPublisher.cs b/src/DataStreamSuite/PubSub/Testing/TestPublisher.cs index 8de8a9924..b80778f50 100644 --- a/src/DataStreamSuite/PubSub/Testing/TestPublisher.cs +++ b/src/DataStreamSuite/PubSub/Testing/TestPublisher.cs @@ -42,7 +42,8 @@ namespace BCG { pi.to_stream(ms); ms.Position = 0; pcore.begin_transaction(); - pcore.publish(TestPubItem.test_tag, ms); + bts.Log.assert(false); + //pcore.publish(TestPubItem.test_tag, ms); pcore.commit_transaction(); if (!_hit_key_to_send) System.Threading.Thread.Sleep(wait_per); diff --git a/src/DataStreamSuite/PubSub/documentation/readme.txt b/src/DataStreamSuite/PubSub/documentation/readme.txt index 99291e27e..c23845ff1 100644 --- a/src/DataStreamSuite/PubSub/documentation/readme.txt +++ b/src/DataStreamSuite/PubSub/documentation/readme.txt @@ -16,7 +16,7 @@ The only implementation of the above is PubSubServerManagedTCPConnection PubSubMessages: PubSubClientMessage is the only messages thats sent from client to pubsubserver. Its serialized on the -client and sent over to the pubsubserver, where uits received ad deserialized +client and sent over to the pubsubserver, where uints received ad deserialized SubscriberCore:ISubscriberCore (retired now) | @@ -33,8 +33,8 @@ IPubSubServerConnection PubSubServer: PubSubMessageReactor (top level, called from main) -ServerMessageHub : IPubsubMessageHub (just a wrapper so multiple ) -ManagedTCPMessageHub : IPubsubMessageHub (this is what handles each ITCPConnection for eahc connection) +ManagedTCPMessageHub : IPubsubMessageHub (this is what handles each ITCPConnection for each connection) + Streams: