230 lines
8.9 KiB
C#
230 lines
8.9 KiB
C#
using System;
|
|
//using System.Messaging;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
using System.IO;
|
|
|
|
using mbts;
|
|
|
|
namespace BCG {
|
|
/// <summary>
|
|
/// Mantains all the state that there is in the server.
|
|
/// 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
|
|
/// </summary>
|
|
///
|
|
public class PubSubState {
|
|
|
|
public delegate void PubSubStateSingleTagChangeDelegate(PubSubSingleTagStateEvent theEvent);
|
|
public event PubSubStateSingleTagChangeDelegate onSingleTagChange;
|
|
public delegate void PubSubStateChangeDelegate(string[] tags);
|
|
public event PubSubStateChangeDelegate onTagMapChanged;
|
|
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>();
|
|
|
|
public PubSubState() {
|
|
SystemSubscriptionMapPublisher.init(this);
|
|
SystemStatsPublisher.init(this);
|
|
SystemHeartbeatPublisher.init();
|
|
}
|
|
|
|
#region subscription based state changes
|
|
|
|
/// <summary>
|
|
/// everytime a subscription is made, this gets called
|
|
/// by PubsubServer2. Adds a
|
|
/// TODO : subscibers sending sub twice from the client
|
|
/// </summary>
|
|
/// <param name="sub"></param>
|
|
/// <param name="md"></param>
|
|
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));
|
|
}
|
|
myTagToPubsubMap[tag].onSub(sub);
|
|
|
|
if (onSingleTagChange != null)
|
|
onSingleTagChange(new PubSubSingleTagStateEvent(tag, PubSubSingleTagStateEvent.EventType.subscription, sub.connection, sub.subscribingApp));
|
|
}
|
|
if (onTagMapChanged != null)
|
|
onTagMapChanged(sub.tags);
|
|
}
|
|
|
|
public void unsub(UnSubMetaData unsub) {
|
|
foreach (string tag in unsub.tags) {
|
|
if (myTagToPubsubMap.ContainsKey(tag)) {
|
|
///maybe check here, that if there are no subscribers
|
|
///and publishers for the tag then remove it.
|
|
myTagToPubsubMap[tag].onUnsub(unsub);
|
|
if (onSingleTagChange != null)
|
|
onSingleTagChange(new PubSubSingleTagStateEvent(tag, PubSubSingleTagStateEvent.EventType.unsubscription, unsub.connection, unsub.unsubscribingApp));
|
|
} else {
|
|
string warningMsg = String.Format("{0} was never published or subscribed on this pubsub, hence" +
|
|
" the unsub call by app {1} is voided", tag, unsub.unsubscribingApp);
|
|
//BlueEventLog.logEvent(warningMsg, BlueEventLevel.Warning);
|
|
}
|
|
}
|
|
if (onTagMapChanged != null)
|
|
onTagMapChanged(unsub.tags);
|
|
}
|
|
|
|
public void removeConnection(string connection) {
|
|
List<string> tagsEffected = new List<string>();
|
|
foreach (KeyValuePair<string, SingleTagPubSubMap> p in myTagToPubsubMap) {
|
|
///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 (onSingleTagChange != null) {
|
|
//we don't know the app or key qualifier or anything for these connection. that is unfortunate.
|
|
if (wasPublisher)
|
|
onSingleTagChange(new PubSubSingleTagStateEvent(tag, PubSubSingleTagStateEvent.EventType.publisherDie, connection, ""));
|
|
if (wasSubscriber)
|
|
onSingleTagChange(new PubSubSingleTagStateEvent(tag, PubSubSingleTagStateEvent.EventType.unsubscription, connection, ""));
|
|
|
|
}
|
|
}
|
|
if (onTagMapChanged != null)
|
|
onTagMapChanged(tagsEffected.ToArray());
|
|
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region data based state changes
|
|
/// <summary>
|
|
/// this is the only place in the state class where the
|
|
/// data is modified ; happens when data is published
|
|
/// </summary>
|
|
/// <param name="tag"></param>
|
|
/// <param name="msg"></param>
|
|
public void updatePubData(PubMetaData pub, Stream msg) {
|
|
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)) {
|
|
///first time data is published for this tag
|
|
myTagToData.Add(pub.tag, new SingleTagData(pub.tag, pub.type));
|
|
}
|
|
myTagToData[pub.tag].pubUpdate(msg);
|
|
|
|
if (!myTagToPubsubMap.ContainsKey(pub.tag)) {
|
|
myTagToPubsubMap.Add(pub.tag, new SingleTagPubSubMap(pub.tag));
|
|
}
|
|
bool pubChanged = false;
|
|
myTagToPubsubMap[pub.tag].onPub(pub, out pubChanged);
|
|
if (pubChanged) {
|
|
if (onTagMapChanged != null)
|
|
onTagMapChanged(new string[] { pub.tag });
|
|
|
|
if (onSingleTagChange != null)
|
|
onSingleTagChange(
|
|
new PubSubSingleTagStateEvent(pub.tag,
|
|
PubSubSingleTagStateEvent.EventType.publish,
|
|
pub.connection, pub.publishingApp));
|
|
}
|
|
if (onPublishStats != null)
|
|
onPublishStats(pub.tag, sz);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
public ICollection<string> getSubscriberConnectionsForTag(string tag) {
|
|
if (myTagToPubsubMap.ContainsKey(tag))
|
|
return myTagToPubsubMap[tag].subscriberConnections;
|
|
else
|
|
return new string[] { };
|
|
}
|
|
|
|
public ICollection<string> getSubscribingAppsForTag(string tag) {
|
|
if (myTagToPubsubMap.ContainsKey(tag))
|
|
return myTagToPubsubMap[tag].subscripingApps;
|
|
else
|
|
return new string[] { };
|
|
}
|
|
|
|
public byte[] getContent(string tag) {
|
|
if (!myTagToData.ContainsKey(tag))
|
|
return null;
|
|
else
|
|
return myTagToData[tag].getCurrentContent();
|
|
}
|
|
|
|
public string getTagType(string tag) {
|
|
if (!myTagToData.ContainsKey(tag))
|
|
return null;
|
|
else
|
|
return myTagToData[tag].tagType;
|
|
}
|
|
|
|
/// <summary>
|
|
/// returns all the applications 'connected' to one tag, which
|
|
/// includes a Dictionary of connection vs subscribers,
|
|
/// Dictionary of connections vs publishers and a connection, app
|
|
/// pair of all dead publishers
|
|
/// </summary>
|
|
/// <param name="tag"></param>
|
|
/// <param name="subscribers"></param>
|
|
/// <param name="activePublishers"></param>
|
|
/// <param name="deadPublishers"></param>
|
|
public void getAllConnections(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];
|
|
subscribers = map.subscribers;
|
|
activePublishers = map.activePublishers;
|
|
deadPublishers = map.deadPublishers;
|
|
} else {
|
|
subscribers = activePublishers = new Dictionary<string, string>();
|
|
deadPublishers = new List<Pair<string, string>>();
|
|
}
|
|
}
|
|
|
|
public ICollection<string> allTags {
|
|
get { return myTagToPubsubMap.Keys; }
|
|
}
|
|
|
|
public ICollection<string> tagsWithData {
|
|
get { return myTagToData.Keys; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// given a list of tags, constructs a Dictionary of
|
|
/// subscribers vs tags
|
|
/// </summary>
|
|
/// <param name="tags"></param>
|
|
/// <returns></returns>
|
|
public Dictionary<string, List<string>> getSubscriberConnectionsForTags(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 (!retVal.ContainsKey(sub))
|
|
retVal.Add(sub, new List<string>());
|
|
retVal[sub].Add(tag);
|
|
}
|
|
}
|
|
}
|
|
return retVal;
|
|
}
|
|
|
|
}
|
|
} |