public PropagatedSubcribeMessage(SubscribeMessage msg, string site) { seqnum = msg.seqnum; topic = msg.topic; uri = msg.uri; interested_site = site; }
public void subscribe(string topic) { lock (thisLock) { // TODO LOG if (_subscribedTopics.Contains(topic)) { // do nothing, should we? keep-alive in the future? who knows... return; } _subscribedTopics.Add(topic); // TODO make all calls assyncs SubscribeMessage msg = new SubscribeMessage() { sub = this, seqnum = _seqnum, topic = topic, uri = getURI() }; log(string.Format("Subscribe. '{0}'", msg)); _broker.subscribe(msg); _seqnum += 1; } }
public void subscribe(SubscribeMessage msg) { log(string.Format("[Subscribe] Received event '{0}'", msg)); //if (isDuplicate(msg)) return; // should we have FIFO order here? lock (_topicSubscribers) { if (!_topicSubscribers.ContainsKey(msg.topic)) { _topicSubscribers.Add(msg.topic, new List<string>()); } _topicSubscribers[msg.topic].Add(msg.uri); } PropagatedSubcribeMessage pmsg = new PropagatedSubcribeMessage(msg, _site); // propagate subscribe only to parent, taking advantage of tree strucure lock (_parentSiteLock) { if (_parentSite != null) { foreach (Broker b in _parentSite.brokers) { //TODO assyncronous log(string.Format("[subscribe] senting '{0}' to parent site '{1}'", pmsg, _parentSite.name)); b.propagateSubscribe(pmsg); } } } }
public void subscribe(SubscribeMessage msg) { string origin_site = msg.interested_site; int en = getEventnum(); if (origin_site == null) { lock (_topicSubscribers) { if (!_topicSubscribers.ContainsKey(msg.topic)) { _topicSubscribers.Add(msg.topic, new List<string>()); } //Discart possible duplicates if (_topicSubscribers[msg.topic].Contains(msg.uri)) return; else { _topicSubscribers[msg.topic].Add(msg.uri); } } } else { lock (_topicSites) { if (!_topicSites.ContainsKey(msg.topic)) { _topicSites.Add(msg.topic, new List<string>()); } //Discart possible duplicates if (_topicSites[msg.topic].Contains(msg.interested_site)) return; else { _topicSites[msg.topic].Add(msg.interested_site); } } } //Doon't print discarted msg if (origin_site == null) { log(en, "[Subscribe] received" + msg.ToString()); } else { log(en, "[Propagate subscribe] received" + msg.ToString()); } msg.interested_site = _site; // propagate subscribe to parent, taking advantage of tree strucure lock (_parentSiteLock) { if (_parentSite != null) { if (origin_site == null || _parentSite.name != origin_site) { lock (_siteToPropagatedSub) { if (!_siteToPropagatedSub.ContainsKey(_parentSite.name)) { _siteToPropagatedSub.Add(_parentSite.name, new Dictionary<string, int>()); } if (!_siteToPropagatedSub[_parentSite.name].ContainsKey(msg.topic)) { _siteToPropagatedSub[_parentSite.name].Add(msg.topic, 1); log(en, string.Format("[Subscribe] Sending {0} to parent site {1}", msg, _parentSite.name)); foreach (var b in _parentSite.brokers) { SubscribeDelegate sd = new SubscribeDelegate(b.subscribe); sd.BeginInvoke(msg, null, null); } } else { var num = _siteToPropagatedSub[_parentSite.name][msg.topic]; num++; _siteToPropagatedSub[_parentSite.name][msg.topic] = num; } } } } } lock (_childSites) { foreach (var s in _childSites) { lock (s) { if (origin_site == null || s.name != origin_site) { lock (_siteToPropagatedSub) { if (!_siteToPropagatedSub.ContainsKey(s.name)) { _siteToPropagatedSub.Add(s.name, new Dictionary<string, int>()); } if (!_siteToPropagatedSub[s.name].ContainsKey(msg.topic)) { _siteToPropagatedSub[s.name].Add(msg.topic, 1); log(en, string.Format("[Subscribe] Sending {0} to child site {1}", msg, s.name)); foreach (var b in s.brokers) { SubscribeDelegate sd = new SubscribeDelegate(b.subscribe); sd.BeginInvoke(msg, null, null); } } else { var num = _siteToPropagatedSub[s.name][msg.topic]; num++; _siteToPropagatedSub[s.name][msg.topic] = num; } } } } } } /* if (origin_site == null) { log(en, "Subscribe finished"); } else { log(en, "Propagate subscribe finished"); }*/ }