Ejemplo n.º 1
0
 public PropagatedUnsubscribeMessage(UnsubscribeMessage msg, string site)
     : base(msg, site)
 {
 }
Ejemplo n.º 2
0
 public void unsubscribe(string topic)
 {
     lock (thisLock)
     {
         if (!_subscribedTopics.Contains(topic))
         {
             // do nothing or throw exception?
             return;
         }
         _subscribedTopics.Remove(topic);
         // TODO LOG
         // TODO make all calls assyncs
         UnsubscribeMessage msg = new UnsubscribeMessage() { sub = this, seqnum = _seqnum, topic = topic, uri = getURI() };
         log(string.Format("Unsubscribe. '{0}'", msg));
         _broker.unsubscribe(msg);
         _seqnum += 1;
     }
 }
Ejemplo n.º 3
0
 public void unsubscribe(UnsubscribeMessage msg)
 {
     log(string.Format("[Unsubscribe] Received event '{0}'", msg));
     //if (isDuplicate(msg)) return;
     // should we have FIFO order here?
     lock (_topicSubscribers)
     {
         if (_topicSubscribers.ContainsKey(msg.topic))
         {
             _topicSubscribers[msg.topic].Remove(msg.uri);
             if (_topicSubscribers[msg.topic].Count == 0)
             {
                 _topicSubscribers.Remove(msg.topic);
             }
         }
     }
     PropagatedUnsubscribeMessage pmsg = new PropagatedUnsubscribeMessage(msg, _site);
     // propagate unsubscribe only to parent, taking advantage of tree strucure
     lock (_parentSiteLock)
     {
         if (_parentSite != null)
         {
             foreach (Broker b in _parentSite.brokers)
             {
                 log(string.Format("[Unsubscribe] senting '{0}' to parent site '{1}'", pmsg, _parentSite.name));
                 // TODO assyncronous
                 b.propagateUnsubscribe(pmsg);
             }
         }
     }
 }
Ejemplo n.º 4
0
        public void unsubscribe(UnsubscribeMessage msg)
        {
            string origin_site = msg.interested_site;
            int en = getEventnum();

            //We should only propagate the unsubscriveMessage if there is no more sites
            //or subscribers that subscribe it

            if (origin_site == null)
            {
                lock (_topicSubscribers)
                {
                    if (_topicSubscribers.ContainsKey(msg.topic))
                    {
                        if (_topicSubscribers[msg.topic].Contains(msg.uri))
                        {
                            _topicSubscribers[msg.topic].Remove(msg.uri);
                        }
                        else
                        {
                            //discart duplicated message
                            return;
                        }

                        if (_topicSubscribers[msg.topic].Count == 0)
                        {
                            _topicSubscribers.Remove(msg.topic);
                        }
                    }
                    else
                    {
                        //If you unsubscribe something you are not subscribed does not make sense propagate the message
                        return;
                    }
                }
            }
            else
            {
                lock (_topicSites)
                {
                    if (_topicSites.ContainsKey(msg.topic))
                    {
                        if (_topicSites[msg.topic].Contains(msg.interested_site))
                        {
                            _topicSites[msg.topic].Remove(msg.interested_site);
                        }
                        else
                        {
                            //discart duplicated message
                            return;
                        }

                        if (_topicSites[msg.topic].Count == 0)
                        {
                            _topicSites.Remove(msg.topic);
                        }
                    }
                    else
                    {
                        //If you unsubscribe something you are not subscribed does not make sense propagate the message
                        return;
                    }
                }
            }

            //Doon't print discarted msg
            if (origin_site == null)
            {
                log(en, "[Unsubscribe] received" + msg.ToString());
            }
            else
            {
                log(en, "[Propagate unsubscribe] 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))
                            {

                                var num = _siteToPropagatedSub[_parentSite.name][msg.topic];
                                num--;

                                _siteToPropagatedSub[_parentSite.name][msg.topic] = num;

                                if (_siteToPropagatedSub[_parentSite.name][msg.topic] == 0)
                                {
                                    _siteToPropagatedSub[_parentSite.name].Remove(msg.topic);
                                }

                                //NO More interested sites in this topic can propagate to parent
                                if (!_siteToPropagatedSub[_parentSite.name].ContainsKey(msg.topic))
                                {
                                    log(en, string.Format("[Unsubscribe] Sending {0} to parent site {1}", msg, _parentSite.name));
                                    foreach (var b in _parentSite.brokers)
                                    {
                                        UnsubscribeDelegate sd = new UnsubscribeDelegate(b.unsubscribe);
                                        sd.BeginInvoke(msg, null, null);
                                    }
                                }
                            }

                        }
                    }
                }
            }

            lock (_childSites)
            {
                foreach (var s in _childSites)
                {

                    lock (s)
                    {
                        if (origin_site == null || s.name != origin_site)
                        {
                            lock (_siteToPropagatedSub)
                            {
                                if (_siteToPropagatedSub.ContainsKey(s.name))
                                {

                                    var num = _siteToPropagatedSub[s.name][msg.topic];
                                    num--;

                                    _siteToPropagatedSub[s.name][msg.topic] = num;

                                    if (_siteToPropagatedSub[s.name][msg.topic] == 0)
                                    {
                                        _siteToPropagatedSub[s.name].Remove(msg.topic);
                                    }

                                    //NO More interested sites in this topic can propagate to child
                                    if (!_siteToPropagatedSub[s.name].ContainsKey(msg.topic))
                                    {
                                        log(en, string.Format("[Unsubscribe] Sending {0} to site {1}", msg, s.name));
                                        foreach (var b in s.brokers)
                                        {
                                            UnsubscribeDelegate sd = new UnsubscribeDelegate(b.unsubscribe);
                                            sd.BeginInvoke(msg, null, null);
                                        }
                                    }
                                }
                            }
                        }

                    }
                }
            }
        }