using System; //using System.Messaging; using System.Collections; using System.Collections.Generic; using System.Reflection; using System.IO; using mbts; namespace BCG { /// /// 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 /// /// 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 myTagToData = new Dictionary(); private Dictionary myTagToPubsubMap = new Dictionary(); public PubSubState() { SystemSubscriptionMapPublisher.init(this); SystemStatsPublisher.init(this); SystemHeartbeatPublisher.init(); } #region subscription based state changes /// /// everytime a subscription is made, this gets called /// by PubsubServer2. Adds a /// TODO : subscibers sending sub twice from the client /// /// /// 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 tagsEffected = new List(); foreach (KeyValuePair 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 /// /// this is the only place in the state class where the /// data is modified ; happens when data is published /// /// /// 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 getSubscriberConnectionsForTag(string tag) { if (myTagToPubsubMap.ContainsKey(tag)) return myTagToPubsubMap[tag].subscriberConnections; else return new string[] { }; } public ICollection 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; } /// /// 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 /// /// /// /// /// public void getAllConnections(string tag, out Dictionary subscribers, out Dictionary activePublishers, out ICollection> deadPublishers) { if (myTagToPubsubMap.ContainsKey(tag)) { SingleTagPubSubMap map = myTagToPubsubMap[tag]; subscribers = map.subscribers; activePublishers = map.activePublishers; deadPublishers = map.deadPublishers; } else { subscribers = activePublishers = new Dictionary(); deadPublishers = new List>(); } } public ICollection allTags { get { return myTagToPubsubMap.Keys; } } public ICollection tagsWithData { get { return myTagToData.Keys; } } /// /// given a list of tags, constructs a Dictionary of /// subscribers vs tags /// /// /// public Dictionary> getSubscriberConnectionsForTags(IEnumerable tags) { Dictionary> retVal = new Dictionary>(); 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()); retVal[sub].Add(tag); } } } return retVal; } } }