From 5a7b2965be4f8b602287b2f55f9335ca355b544f Mon Sep 17 00:00:00 2001 From: Taha Afzal Date: Sat, 3 Feb 2024 10:41:07 -0500 Subject: [PATCH] PubUBState cleaned up and modernized --- .../SystemSubscriptionMapPublisher.cs | 6 +- .../PubSub/Server/PubSubMessageReactor.cs | 11 +- .../PubSub/Server/PubSubState.cs | 143 ++++++++++++------ ...ubSubMap.cs => SingleTagConnectionData.cs} | 104 ++++++++----- .../PubSub/Server/SingleTagData.cs | 100 +++++++----- src/DataStreamSuite/PubSubServer/Program.cs | 4 +- 6 files changed, 240 insertions(+), 128 deletions(-) rename src/DataStreamSuite/PubSub/Server/{SingleTagPubSubMap.cs => SingleTagConnectionData.cs} (50%) diff --git a/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemSubscriptionMapPublisher.cs b/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemSubscriptionMapPublisher.cs index b184177e5..d3512e40d 100644 --- a/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemSubscriptionMapPublisher.cs +++ b/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemSubscriptionMapPublisher.cs @@ -64,7 +64,7 @@ namespace BCG { foreach (string tag in myTagsReadyToPublish.Keys) { Dictionary subscribers, activePubslishers; ICollection> deadPublishers; - myPubsubState.getAllConnections(tag, out subscribers, out activePubslishers, out deadPublishers); + myPubsubState.get_all_connections(tag, out subscribers, out activePubslishers, out deadPublishers); TagPubSubInfo ti = new TagPubSubInfo(tag, subscribers, activePubslishers, deadPublishers, myIp); ti.writeTo(str); } @@ -86,14 +86,14 @@ namespace BCG { public void update(string tag, Stream[] messageBodies) { } public byte[] getContent(string t) { - ICollection allTags = myPubsubState.allTags; + ICollection allTags = myPubsubState.all_tags; MemoryStream str = new MemoryStream(); BinaryWriter bw = new BinaryWriter(str); bw.Write(allTags.Count); foreach (string tag in allTags) { Dictionary subscribers, activePubslishers; ICollection> deadPublishers; - myPubsubState.getAllConnections(tag, out subscribers, out activePubslishers, out deadPublishers); + myPubsubState.get_all_connections(tag, out subscribers, out activePubslishers, out deadPublishers); TagPubSubInfo ti = new TagPubSubInfo(tag, subscribers, activePubslishers, deadPublishers, myIp); ti.writeTo(str); } diff --git a/src/DataStreamSuite/PubSub/Server/PubSubMessageReactor.cs b/src/DataStreamSuite/PubSub/Server/PubSubMessageReactor.cs index d52ee86a3..9f184c64e 100644 --- a/src/DataStreamSuite/PubSub/Server/PubSubMessageReactor.cs +++ b/src/DataStreamSuite/PubSub/Server/PubSubMessageReactor.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using mbts; using mbts.spark; using bts; +using System.Data; namespace BCG { // This is the main PubSub Server class where it all starts (called by Program.cs). Its constructed and start is called and thats it. @@ -61,7 +62,7 @@ namespace BCG { ///significance until they subscribe, in which case an entry ///will be made if (conn_status == PubSubCallbacks.ConnectionStatus.DISCONNECTED) - _state.removeConnection(client); + _state.remove_connection(client); } private void stop() { @@ -145,7 +146,7 @@ namespace BCG { bts.Log.Gen.info($"PubSubMessageReactor._handle_pub sender: {sender} pub_msg: {pub_msg}"); // this is where pubsub's data state is updated _state.update_pub_data(pub_msg); - var sub_connections_for_tag = _state.getSubscriberConnectionsForTag(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() { tag = pub_msg.tag, @@ -174,5 +175,11 @@ namespace BCG { // TODO //myReceivingQueue.enqueue(new Pair(internalPublisherConnection, data)); } + + public static void internal_publish(PublishMsg pub_msg) { + //_handle_pub("InternalPublisher", pub_msg); + } + + } } diff --git a/src/DataStreamSuite/PubSub/Server/PubSubState.cs b/src/DataStreamSuite/PubSub/Server/PubSubState.cs index 9ce5c31e0..61672a6bc 100644 --- a/src/DataStreamSuite/PubSub/Server/PubSubState.cs +++ b/src/DataStreamSuite/PubSub/Server/PubSubState.cs @@ -13,10 +13,11 @@ namespace BCG { /// This information is keyed off the tags (rather than /// keyed off subscribers or publishers) /// State information consists of 2 distinct sets : - /// myTagToData consists of all the data published to - /// this pubsub. - /// myTagToTagInfo consists of all the subscription and - /// publishing information + /// _tag_to_data_map consists of all the data published to + /// this pubsub, for every tag + /// _tag_to_connection_map consists of all the subscription and + /// publishing information (ie the connection and aplications that + /// are subsribing or publishing the tag) /// /// public class PubSubState { @@ -28,8 +29,8 @@ namespace BCG { public delegate void PubSubStatsDelegate(string tag, int size); public event PubSubStatsDelegate onPublishStats; - private Dictionary myTagToData = new Dictionary(); - private Dictionary myTagToPubsubMap = new Dictionary(); + private Dictionary _tag_to_data_map = new Dictionary(); + private Dictionary _tag_to_connection_map = new Dictionary(); public PubSubState() { SystemSubscriptionMapPublisher.init(this); @@ -49,10 +50,10 @@ namespace BCG { public void sub(SubMetaData sub) { ///tag based key ds update foreach (string tag in sub.tags) { - if (!myTagToPubsubMap.ContainsKey(tag)) { - myTagToPubsubMap.Add(tag, new SingleTagPubSubMap(tag)); + if (!_tag_to_connection_map.ContainsKey(tag)) { + _tag_to_connection_map.Add(tag, new SingleTagConnectionData(tag)); } - myTagToPubsubMap[tag].onSub(sub); + _tag_to_connection_map[tag].onSub(sub); if (onSingleTagChange != null) onSingleTagChange(new PubSubSingleTagStateEvent(tag, PubSubSingleTagStateEvent.EventType.subscription, sub.connection, sub.subscribing_app)); @@ -62,15 +63,28 @@ namespace BCG { } public void handle_sub(bts.SubscribeMsg sub_msg) { + 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)); + } + + if (onTagMapChanged != null) + onTagMapChanged(sub_msg.tags); // TODO } public void unsub(UnSubMetaData unsub) { foreach (string tag in unsub.tags) { - if (myTagToPubsubMap.ContainsKey(tag)) { + if (_tag_to_connection_map.ContainsKey(tag)) { ///maybe check here, that if there are no subscribers ///and publishers for the tag then remove it. - myTagToPubsubMap[tag].onUnsub(unsub); + _tag_to_connection_map[tag].onUnsub(unsub); if (onSingleTagChange != null) onSingleTagChange(new PubSubSingleTagStateEvent(tag, PubSubSingleTagStateEvent.EventType.unsubscription, unsub.connection, unsub.unsubscribing_app)); } else { @@ -84,19 +98,32 @@ namespace BCG { } 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)) { + ///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)); + } else { + bts.Log.Gen.error($"PubSubState.handle_unsub ubnsub_msg: {ubnsub_msg} tag: {tag} was never subscribed, so unsub call is voided"); + } + } + if (onTagMapChanged != null) + onTagMapChanged(ubnsub_msg.tags); } - public void removeConnection(string connection) { - List tagsEffected = new List(); - foreach (KeyValuePair p in myTagToPubsubMap) { + public void remove_connection(string connection) { + List tags_effected = new List(); + foreach (KeyValuePair p in _tag_to_connection_map) { ///maybe check here, that if there are no subscribers ///and publishers for the tag then remove it. bool wasPublisher = false; bool wasSubscriber = false; string tag = p.Key; - if (p.Value.onConnectionRemoveIfExists(connection, out wasPublisher, out wasSubscriber)) - tagsEffected.Add(tag); + if (p.Value.remove_connection(connection, out wasPublisher, out wasSubscriber)) + tags_effected.Add(tag); if (onSingleTagChange != null) { //we don't know the app or key qualifier or anything for these connection. that is unfortunate. @@ -108,7 +135,7 @@ namespace BCG { } } if (onTagMapChanged != null) - onTagMapChanged(tagsEffected.ToArray()); + onTagMapChanged(tags_effected.ToArray()); } @@ -126,17 +153,17 @@ namespace BCG { int sz = (int)(msg.Length - msg.Position); bts.Log.Gen.info($"PubSubData.updatePubData pub: msg : {msg.Position} / {msg.Length} sz: {sz}"); ///this is the only place where SingleTagData should be created - if (!myTagToData.ContainsKey(pub.tag)) { + if (!_tag_to_data_map.ContainsKey(pub.tag)) { ///first time data is published for this tag - myTagToData.Add(pub.tag, new SingleTagData(pub.tag, pub.type)); + _tag_to_data_map.Add(pub.tag, new SingleTagData(pub.tag, pub.type)); } - myTagToData[pub.tag].pubUpdate(msg); + _tag_to_data_map[pub.tag].pubUpdate(msg); - if (!myTagToPubsubMap.ContainsKey(pub.tag)) { - myTagToPubsubMap.Add(pub.tag, new SingleTagPubSubMap(pub.tag)); + if (!_tag_to_connection_map.ContainsKey(pub.tag)) { + _tag_to_connection_map.Add(pub.tag, new SingleTagConnectionData(pub.tag)); } bool pubChanged = false; - myTagToPubsubMap[pub.tag].onPub(pub, out pubChanged); + _tag_to_connection_map[pub.tag].onPub(pub, out pubChanged); if (pubChanged) { if (onTagMapChanged != null) onTagMapChanged(new string[] { pub.tag }); @@ -154,36 +181,62 @@ namespace BCG { public void update_pub_data(bts.PublishMsg pub_msg) { // TODO + // int sz = (int)(msg.Length - msg.Position); + bts.Log.Gen.info($"PubSubData.update_pub_data pub_msg: {pub_msg}"); + ///this is the only place where SingleTagData should be created + if (!_tag_to_data_map.ContainsKey(pub_msg.tag)) { + ///first time data is published for this tag + _tag_to_data_map.Add(pub_msg.tag, new SingleTagData(pub_msg.tag, pub_msg.tag_type)); + } + _tag_to_data_map[pub_msg.tag].handle_publish(pub_msg); + + if (!_tag_to_connection_map.ContainsKey(pub_msg.tag)) { + _tag_to_connection_map.Add(pub_msg.tag, new SingleTagConnectionData(pub_msg.tag)); + } + bool pub_changed = false; + _tag_to_connection_map[pub_msg.tag].handle_pub(pub_msg, out pub_changed); + if (pub_changed) { + if (onTagMapChanged != null) + onTagMapChanged(new string[] { pub_msg.tag }); + + if (onSingleTagChange != null) + onSingleTagChange( + new PubSubSingleTagStateEvent(pub_msg.tag, + PubSubSingleTagStateEvent.EventType.publish, + pub_msg.connection, pub_msg.publishing_app)); + } + if (onPublishStats != null) + onPublishStats(pub_msg.tag, pub_msg.data_to_publish.Length); } #endregion - public ICollection getSubscriberConnectionsForTag(string tag) { - if (myTagToPubsubMap.ContainsKey(tag)) - return myTagToPubsubMap[tag].subscriberConnections; + public ICollection get_subscriber_connections_for_tag(string tag) { + if (_tag_to_connection_map.ContainsKey(tag)) + return _tag_to_connection_map[tag].subscriber_connections; else return new string[] { }; } public ICollection getSubscribingAppsForTag(string tag) { - if (myTagToPubsubMap.ContainsKey(tag)) - return myTagToPubsubMap[tag].subscripingApps; + if (_tag_to_connection_map.ContainsKey(tag)) + return _tag_to_connection_map[tag].subscriping_apps; else return new string[] { }; } public byte[] getContent(string tag) { - if (!myTagToData.ContainsKey(tag)) + if (!_tag_to_data_map.ContainsKey(tag)) return null; else - return myTagToData[tag].getCurrentContent(); + return _tag_to_data_map[tag].getCurrentContent(); } public string getTagType(string tag) { - if (!myTagToData.ContainsKey(tag)) + if (!_tag_to_data_map.ContainsKey(tag)) return null; else - return myTagToData[tag].tagType; + return _tag_to_data_map[tag].tag_type; } /// @@ -196,26 +249,26 @@ namespace BCG { /// /// /// - public void getAllConnections(string tag, out Dictionary subscribers, + public void get_all_connections(string tag, out Dictionary subscribers, out Dictionary activePublishers, out ICollection> deadPublishers) { - if (myTagToPubsubMap.ContainsKey(tag)) { - SingleTagPubSubMap map = myTagToPubsubMap[tag]; + if (_tag_to_connection_map.ContainsKey(tag)) { + SingleTagConnectionData map = _tag_to_connection_map[tag]; subscribers = map.subscribers; - activePublishers = map.activePublishers; - deadPublishers = map.deadPublishers; + activePublishers = map.active_publishers; + deadPublishers = map.dead_publishers; } else { subscribers = activePublishers = new Dictionary(); deadPublishers = new List>(); } } - public ICollection allTags { - get { return myTagToPubsubMap.Keys; } + public ICollection all_tags { + get { return _tag_to_connection_map.Keys; } } - public ICollection tagsWithData { - get { return myTagToData.Keys; } + public ICollection tags_with_data { + get { return _tag_to_data_map.Keys; } } /// @@ -224,11 +277,11 @@ namespace BCG { /// /// /// - public Dictionary> getSubscriberConnectionsForTags(IEnumerable tags) { + public Dictionary> get_subscriber_connections_for_tags(IEnumerable tags) { Dictionary> retVal = new Dictionary>(); foreach (string tag in tags) { - if (myTagToPubsubMap.ContainsKey(tag)) { - foreach (string sub in myTagToPubsubMap[tag].subscriberConnections) { + if (_tag_to_connection_map.ContainsKey(tag)) { + foreach (string sub in _tag_to_connection_map[tag].subscriber_connections) { if (!retVal.ContainsKey(sub)) retVal.Add(sub, new List()); retVal[sub].Add(tag); diff --git a/src/DataStreamSuite/PubSub/Server/SingleTagPubSubMap.cs b/src/DataStreamSuite/PubSub/Server/SingleTagConnectionData.cs similarity index 50% rename from src/DataStreamSuite/PubSub/Server/SingleTagPubSubMap.cs rename to src/DataStreamSuite/PubSub/Server/SingleTagConnectionData.cs index 755d9ee6f..e4afc991f 100644 --- a/src/DataStreamSuite/PubSub/Server/SingleTagPubSubMap.cs +++ b/src/DataStreamSuite/PubSub/Server/SingleTagConnectionData.cs @@ -4,74 +4,77 @@ using System.Text; using BCG.Utilities; +using bts; + using mbts; namespace BCG { /// /// SingleTagPubSubMap consists of all the state w.r.t to - /// publishing and subscription of a single tag. this information + /// publishing and subscription of a single tag (not the data). this information /// consists of the publisher, and a map of the subscriber /// connection vs the apps they're coming from /// - internal class SingleTagPubSubMap { - private string myTag; + internal class SingleTagConnectionData { + private readonly string _tag; /// - /// map of clientURL to client app (machine;app;pid;clientURL) + /// map of client_url to client app (machine;app;pid;clientURL) /// - Dictionary mySubConnToApp = new Dictionary(); + Dictionary _sub_conn_to_app_map = new Dictionary(); /// /// map of all the active publishers (generally unique) to /// the publishing app /// - Dictionary myActivePubConnToApp = new Dictionary(); + Dictionary _active_pub_conn_to_app_map = new Dictionary(); /// /// list of dead publishers to the conn /// - List> myDeadPubConnToApp = new List>(); + List> _dead_pub_conn_to_app_map = new List>(); - public SingleTagPubSubMap(string tag) { - myTag = tag; + public SingleTagConnectionData(string tag) { + _tag = tag; } /// /// Returns a list all the active connections that /// have subscribed to this tag /// - public ICollection subscriberConnections { - get { return mySubConnToApp.Keys; } + public ICollection subscriber_connections { + get { return _sub_conn_to_app_map.Keys; } } /// /// Returns a list of all the applications that have /// subscribed to this tag /// - public ICollection subscripingApps { - get { return mySubConnToApp.Values; } + public ICollection subscriping_apps { + get { return _sub_conn_to_app_map.Values; } } /// /// Returns a dictionary for all subsriber /// connections as key and the application /// as value + /// TODO, make it readonlyDictionary /// public Dictionary subscribers { - get { return mySubConnToApp; } + get { return _sub_conn_to_app_map; } } /// /// Returns a dictionary for all active publishing /// connection as key and the application as value /// - public Dictionary activePublishers { - get { return myActivePubConnToApp; } + public Dictionary active_publishers { + get { return _active_pub_conn_to_app_map; } } /// /// Returns a list of pair of dead connections and /// theur corresponding applications /// - public ICollection> deadPublishers { - get { return myDeadPubConnToApp; } + public ICollection> dead_publishers { + get { return _dead_pub_conn_to_app_map; } } /// @@ -82,20 +85,25 @@ namespace BCG { /// /// public bool onSub(SubMetaData sub) { - if (mySubConnToApp.ContainsKey(sub.connection)) { + if (_sub_conn_to_app_map.ContainsKey(sub.connection)) { string warningMsg = String.Format("tag {0} has already been subscribed by app {0}, subscriber {1}" - + "it should unsubscribe before resubscribing", myTag, sub.connection, sub.subscribing_app); + + "it should unsubscribe before resubscribing", _tag, sub.connection, sub.subscribing_app); //BlueEventLog.logEvent(warningMsg, BlueEventLevel.Warning); return false; } else { - mySubConnToApp.Add(sub.connection, sub.subscribing_app); + _sub_conn_to_app_map.Add(sub.connection, sub.subscribing_app); return true; } } - public bool on_sub(bts.SubscribeMsg sub_msg) { - // TODO - return true; + public bool handle_sub(SubscribeMsg sub_msg) { + if (_sub_conn_to_app_map.ContainsKey(sub_msg.connection)) { + bts.Log.Gen.info($"tag: {_tag} has already been subscribed by connection: {sub_msg.connection}"); + return false; + } else { + _sub_conn_to_app_map.Add(sub_msg.connection, sub_msg.subscribing_app); + return true; + } } /// @@ -106,17 +114,26 @@ namespace BCG { /// /// public bool onUnsub(UnSubMetaData unsub) { - if (mySubConnToApp.ContainsKey(unsub.connection)) { - mySubConnToApp.Remove(unsub.connection); + if (_sub_conn_to_app_map.ContainsKey(unsub.connection)) { + _sub_conn_to_app_map.Remove(unsub.connection); return true; } else { string warningMsg = String.Format("tag {0} has already was either unsubscribed or never subscribed by app {0}, subscriber {1}", - myTag, unsub.connection, unsub.unsubscribing_app); + _tag, unsub.connection, unsub.unsubscribing_app); //BlueEventLog.logEvent(warningMsg, BlueEventLevel.Warning); return false; } } + public bool handle_unsub(UnsubscribeMsg unsub_msg) { + if (_sub_conn_to_app_map.ContainsKey(unsub_msg.connection)) { + _sub_conn_to_app_map.Remove(unsub_msg.connection); + return true; + } else { + bts.Log.Gen.info($"tag: {_tag} does not have subscription via connection: {unsub_msg.connection}"); + return false; + } + } /// /// Called by PubsubState when a client is disconnected. The client /// might not have anything to do with this tag, in which a false is @@ -124,21 +141,21 @@ namespace BCG { /// /// /// - public bool onConnectionRemoveIfExists(string connection, out bool wasPublisher, out bool wasSubscriber) { + public bool remove_connection(string connection, out bool was_publisher, out bool was_subscriber) { bool rv = false; - if (mySubConnToApp.ContainsKey(connection)) { - mySubConnToApp.Remove(connection); - wasSubscriber = true; + if (_sub_conn_to_app_map.ContainsKey(connection)) { + _sub_conn_to_app_map.Remove(connection); + was_subscriber = true; rv = true; } else - wasSubscriber = false; - if (myActivePubConnToApp.ContainsKey(connection)) { - myDeadPubConnToApp.Add(new Pair(connection, myActivePubConnToApp[connection])); - myActivePubConnToApp.Remove(connection); + was_subscriber = false; + if (_active_pub_conn_to_app_map.ContainsKey(connection)) { + _dead_pub_conn_to_app_map.Add(new Pair(connection, _active_pub_conn_to_app_map[connection])); + _active_pub_conn_to_app_map.Remove(connection); rv = true; - wasPublisher = true; + was_publisher = true; } else - wasPublisher = false; + was_publisher = false; return rv; } @@ -149,12 +166,21 @@ namespace BCG { /// /// public void onPub(PubMetaData pub, out bool newPublisher) { - if (myActivePubConnToApp.ContainsKey(pub.connection)) { + if (_active_pub_conn_to_app_map.ContainsKey(pub.connection)) { newPublisher = false; } else { - myActivePubConnToApp.Add(pub.connection, pub.publishingApp); + _active_pub_conn_to_app_map.Add(pub.connection, pub.publishingApp); newPublisher = true; } } + + public void handle_pub(PublishMsg pub_msg, out bool new_publisher) { + if (_active_pub_conn_to_app_map.ContainsKey(pub_msg.connection)) { + new_publisher = false; + } else { + _active_pub_conn_to_app_map.Add(pub_msg.connection, pub_msg.publishing_app); + new_publisher = true; + } + } } } \ No newline at end of file diff --git a/src/DataStreamSuite/PubSub/Server/SingleTagData.cs b/src/DataStreamSuite/PubSub/Server/SingleTagData.cs index 8b6328aa0..db02a014c 100644 --- a/src/DataStreamSuite/PubSub/Server/SingleTagData.cs +++ b/src/DataStreamSuite/PubSub/Server/SingleTagData.cs @@ -6,6 +6,7 @@ using System.Text; using System.Collections.Generic; using BCG.Utilities; using mbts; +using bts; namespace BCG { /// @@ -13,39 +14,39 @@ namespace BCG { /// the data published for a tag /// internal class SingleTagData { - private string myTagType = null; - private string myTag; - private byte[] myLastPublishedMsg; - private IPubSubServerTagHandler myHandler; + private readonly string _tag_type = null; + private readonly string _tag; + private byte[] _last_published_data; + private IPubSubServerTagHandler _tag_handler; - public SingleTagData(string tag, string tagType) { - myTag = tag; - myTagType = tagType; - if (myTagType == SystemSubscriptionMapPublisher.tagType) - myHandler = SystemSubscriptionMapPublisher.getInstance(); - else if (myTagType == SystemStatsPublisher.tagType) - myHandler = SystemStatsPublisher.getInstance(); + public SingleTagData(string tag, string tag_type) { + _tag = tag; + _tag_type = tag_type; + if (_tag_type == SystemSubscriptionMapPublisher.tagType) + _tag_handler = SystemSubscriptionMapPublisher.getInstance(); + else if (_tag_type == SystemStatsPublisher.tagType) + _tag_handler = SystemStatsPublisher.getInstance(); else // this is where it gets the stream handler - myHandler = TagType2HandlerMap.getHandler(myTagType, true); - if (myHandler != null) - myHandler.register(new InternalPublishDelegate(myHandleHandlerCallbacks)); + _tag_handler = TagType2HandlerMap.getHandler(_tag_type, true); + if (_tag_handler != null) + _tag_handler.register(new InternalPublishDelegate(_on_handler_internal_publish)); } - public string tag { get { return myTag; } } + public string tag { get { return _tag; } } - public string tagType { get { return myTagType; } } + public string tag_type { get { return _tag_type; } } internal void pubUpdate(Stream stream) { - string handler_str = myHandler != null ? myHandler.GetType().ToString() : "NULL"; - bts.Log.Gen.info($"SingleTagData.pubUpdate tag: {myTag} handler: {handler_str} stream: {stream.Position} / {stream.Length}"); - if (myHandler != null) { + string handler_str = _tag_handler != null ? _tag_handler.GetType().ToString() : "NULL"; + bts.Log.Gen.info($"SingleTagData.pubUpdate tag: {_tag} handler: {handler_str} stream: {stream.Position} / {stream.Length}"); + if (_tag_handler != null) { try { - myHandler.update(myTag, new Stream[] { stream }); + _tag_handler.update(_tag, new Stream[] { stream }); } catch (Exception e) { // BlueEventLog.logEvent(e); - string errMsg = "tag handler threw an exception " + e.Message + " for tag type = " + myTagType + - " tag = " + myTag + " length = " + stream.Length + "\n" + + string errMsg = "tag handler threw an exception " + e.Message + " for tag type = " + _tag_type + + " tag = " + _tag + " length = " + stream.Length + "\n" + "tag stated not updated. Stack Trace : " + e.StackTrace; Console.WriteLine(errMsg); if (e is OutOfMemoryException) @@ -54,29 +55,54 @@ namespace BCG { throw new ApplicationException(errMsg, e); } } else { - myLastPublishedMsg = new byte[stream.Length - stream.Position]; - stream.Read(myLastPublishedMsg, 0, myLastPublishedMsg.Length); + _last_published_data = new byte[stream.Length - stream.Position]; + stream.Read(_last_published_data, 0, _last_published_data.Length); + } + } + + 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}"); + if (_tag_handler != null) { + try { + _tag_handler.update(_tag, new Stream[] { new MemoryStream(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; + else + throw new ApplicationException(err_msg, e); + } + } else { + _last_published_data = pub_msg.data_to_publish; } } public byte[] getCurrentContent() { - if (myHandler != null) { - return myHandler.getContent(myTag); + if (_tag_handler != null) { + return _tag_handler.getContent(_tag); } else { - return myLastPublishedMsg; + return _last_published_data; } } - private void myHandleHandlerCallbacks(ICollection> cbItems) { - foreach (Triplet p in cbItems) { - string tagName = p.first, tagType = p.second; - byte[] data = p.third; - PubMetaData pub = new PubMetaData(tagType, tagName, data.Length, "local", ClientInfo.thisClient.processString); - MemoryStream str = new MemoryStream(); - MetaData md = new MetaData(pub); - md.save(str); - new BinaryWriter(str).Write(data); - PubSubMessageReactor.internalSend(str.ToArray()); + private void _on_handler_internal_publish(ICollection> cbItems) { + foreach (var p in cbItems) { + PublishMsg pub_msg = new PublishMsg() { + tag = p.first, + tag_type = p.second, + data_to_publish = p.third + }; + PubSubMessageReactor.internal_publish(pub_msg); + // string tag_name = p.first, tag_type = p.second; + // byte[] data = p.third; + // PubMetaData pub = new PubMetaData(tag_type, tag_name, data.Length, "local", ClientInfo.thisClient.processString); + // MemoryStream str = new MemoryStream(); + // MetaData md = new MetaData(pub); + // md.save(str); + // new BinaryWriter(str).Write(data); + // PubSubMessageReactor.internalSend(str.ToArray()); } } diff --git a/src/DataStreamSuite/PubSubServer/Program.cs b/src/DataStreamSuite/PubSubServer/Program.cs index 1c3df22fb..e388f2771 100644 --- a/src/DataStreamSuite/PubSubServer/Program.cs +++ b/src/DataStreamSuite/PubSubServer/Program.cs @@ -50,8 +50,8 @@ namespace BCG { Thread.Sleep(gced ? config.PS3_WaitPeriodAfterGC : defaultWaitTime); } }).Start(); - - PubSubMessageReactor pubSubServer = new PubSubMessageReactor(); + var port_no = 12345; + PubSubMessageReactor pubSubServer = new PubSubMessageReactor(port_no); pubSubServer.start(); }