PubUBState cleaned up and modernized
This commit is contained in:
1 parent
0aab1628bc
commit
5a7b2965be
6 files changed
+240
-128
No files matched your search
+3
-3
@@ -64,7 +64,7 @@ namespace BCG {
|
||||
foreach (string tag in myTagsReadyToPublish.Keys) {
|
||||
Dictionary<string, string> subscribers, activePubslishers;
|
||||
ICollection<Pair<string, string>> 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<string> allTags = myPubsubState.allTags;
|
||||
ICollection<string> allTags = myPubsubState.all_tags;
|
||||
MemoryStream str = new MemoryStream();
|
||||
BinaryWriter bw = new BinaryWriter(str);
|
||||
bw.Write(allTags.Count);
|
||||
foreach (string tag in allTags) {
|
||||
Dictionary<string, string> subscribers, activePubslishers;
|
||||
ICollection<Pair<string, string>> 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);
|
||||
}
|
||||
|
||||
@@ -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<string, byte[]>(internalPublisherConnection, data));
|
||||
}
|
||||
|
||||
public static void internal_publish(PublishMsg pub_msg) {
|
||||
//_handle_pub("InternalPublisher", pub_msg);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
/// </summary>
|
||||
///
|
||||
public class PubSubState {
|
||||
@@ -28,8 +29,8 @@ namespace BCG {
|
||||
public delegate void PubSubStatsDelegate(string tag, int size);
|
||||
public event PubSubStatsDelegate onPublishStats;
|
||||
|
||||
private Dictionary<string, SingleTagData> myTagToData = new Dictionary<string, SingleTagData>();
|
||||
private Dictionary<string, SingleTagPubSubMap> myTagToPubsubMap = new Dictionary<string, SingleTagPubSubMap>();
|
||||
private Dictionary<string, SingleTagData> _tag_to_data_map = new Dictionary<string, SingleTagData>();
|
||||
private Dictionary<string, SingleTagConnectionData> _tag_to_connection_map = new Dictionary<string, SingleTagConnectionData>();
|
||||
|
||||
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<string> tagsEffected = new List<string>();
|
||||
foreach (KeyValuePair<string, SingleTagPubSubMap> p in myTagToPubsubMap) {
|
||||
public void remove_connection(string connection) {
|
||||
List<string> tags_effected = new List<string>();
|
||||
foreach (KeyValuePair<string, SingleTagConnectionData> 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<string> getSubscriberConnectionsForTag(string tag) {
|
||||
if (myTagToPubsubMap.ContainsKey(tag))
|
||||
return myTagToPubsubMap[tag].subscriberConnections;
|
||||
public ICollection<string> 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<string> 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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -196,26 +249,26 @@ namespace BCG {
|
||||
/// <param name="subscribers"></param>
|
||||
/// <param name="activePublishers"></param>
|
||||
/// <param name="deadPublishers"></param>
|
||||
public void getAllConnections(string tag, out Dictionary<string, string> subscribers,
|
||||
public void get_all_connections(string tag, out Dictionary<string, string> subscribers,
|
||||
out Dictionary<string, string> activePublishers,
|
||||
out ICollection<Pair<string, string>> 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<string, string>();
|
||||
deadPublishers = new List<Pair<string, string>>();
|
||||
}
|
||||
}
|
||||
|
||||
public ICollection<string> allTags {
|
||||
get { return myTagToPubsubMap.Keys; }
|
||||
public ICollection<string> all_tags {
|
||||
get { return _tag_to_connection_map.Keys; }
|
||||
}
|
||||
|
||||
public ICollection<string> tagsWithData {
|
||||
get { return myTagToData.Keys; }
|
||||
public ICollection<string> tags_with_data {
|
||||
get { return _tag_to_data_map.Keys; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -224,11 +277,11 @@ namespace BCG {
|
||||
/// </summary>
|
||||
/// <param name="tags"></param>
|
||||
/// <returns></returns>
|
||||
public Dictionary<string, List<string>> getSubscriberConnectionsForTags(IEnumerable<string> tags) {
|
||||
public Dictionary<string, List<string>> get_subscriber_connections_for_tags(IEnumerable<string> tags) {
|
||||
Dictionary<string, List<string>> retVal = new Dictionary<string, List<string>>();
|
||||
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<string>());
|
||||
retVal[sub].Add(tag);
|
||||
|
||||
+65
-39
@@ -4,74 +4,77 @@ using System.Text;
|
||||
|
||||
using BCG.Utilities;
|
||||
|
||||
using bts;
|
||||
|
||||
using mbts;
|
||||
|
||||
namespace BCG {
|
||||
/// <summary>
|
||||
/// 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
|
||||
/// </summary>
|
||||
internal class SingleTagPubSubMap {
|
||||
private string myTag;
|
||||
internal class SingleTagConnectionData {
|
||||
private readonly string _tag;
|
||||
/// <summary>
|
||||
/// map of clientURL to client app (machine;app;pid;clientURL)
|
||||
/// map of client_url to client app (machine;app;pid;clientURL)
|
||||
/// </summary>
|
||||
Dictionary<string, string> mySubConnToApp = new Dictionary<string, string>();
|
||||
Dictionary<string, string> _sub_conn_to_app_map = new Dictionary<string, string>();
|
||||
/// <summary>
|
||||
/// map of all the active publishers (generally unique) to
|
||||
/// the publishing app
|
||||
/// </summary>
|
||||
Dictionary<string, string> myActivePubConnToApp = new Dictionary<string, string>();
|
||||
Dictionary<string, string> _active_pub_conn_to_app_map = new Dictionary<string, string>();
|
||||
/// <summary>
|
||||
/// list of dead publishers to the conn
|
||||
/// </summary>
|
||||
List<Pair<string, string>> myDeadPubConnToApp = new List<Pair<string, string>>();
|
||||
List<Pair<string, string>> _dead_pub_conn_to_app_map = new List<Pair<string, string>>();
|
||||
|
||||
public SingleTagPubSubMap(string tag) {
|
||||
myTag = tag;
|
||||
public SingleTagConnectionData(string tag) {
|
||||
_tag = tag;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list all the active connections that
|
||||
/// have subscribed to this tag
|
||||
/// </summary>
|
||||
public ICollection<string> subscriberConnections {
|
||||
get { return mySubConnToApp.Keys; }
|
||||
public ICollection<string> subscriber_connections {
|
||||
get { return _sub_conn_to_app_map.Keys; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of all the applications that have
|
||||
/// subscribed to this tag
|
||||
/// </summary>
|
||||
public ICollection<string> subscripingApps {
|
||||
get { return mySubConnToApp.Values; }
|
||||
public ICollection<string> subscriping_apps {
|
||||
get { return _sub_conn_to_app_map.Values; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a dictionary for all subsriber
|
||||
/// connections as key and the application
|
||||
/// as value
|
||||
/// TODO, make it readonlyDictionary
|
||||
/// </summary>
|
||||
public Dictionary<string, string> subscribers {
|
||||
get { return mySubConnToApp; }
|
||||
get { return _sub_conn_to_app_map; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a dictionary for all active publishing
|
||||
/// connection as key and the application as value
|
||||
/// </summary>
|
||||
public Dictionary<string, string> activePublishers {
|
||||
get { return myActivePubConnToApp; }
|
||||
public Dictionary<string, string> active_publishers {
|
||||
get { return _active_pub_conn_to_app_map; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of pair of dead connections and
|
||||
/// theur corresponding applications
|
||||
/// </summary>
|
||||
public ICollection<Pair<string, string>> deadPublishers {
|
||||
get { return myDeadPubConnToApp; }
|
||||
public ICollection<Pair<string, string>> dead_publishers {
|
||||
get { return _dead_pub_conn_to_app_map; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -82,20 +85,25 @@ namespace BCG {
|
||||
/// <param name="sub"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -106,17 +114,26 @@ namespace BCG {
|
||||
/// <param name="unsub"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 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 {
|
||||
/// </summary>
|
||||
/// <param name="subscriber"></param>
|
||||
/// <returns></returns>
|
||||
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<string, string>(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<string, string>(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 {
|
||||
/// <param name="pub"></param>
|
||||
/// <param name="publisherChanged"></param>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ using System.Text;
|
||||
using System.Collections.Generic;
|
||||
using BCG.Utilities;
|
||||
using mbts;
|
||||
using bts;
|
||||
|
||||
namespace BCG {
|
||||
/// <summary>
|
||||
@@ -13,39 +14,39 @@ namespace BCG {
|
||||
/// the data published for a tag
|
||||
/// </summary>
|
||||
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<Triplet<string, string, byte[]>> cbItems) {
|
||||
foreach (Triplet<string, string, byte[]> 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<Triplet<string, string, byte[]>> 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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user