コード例 #1
0
ファイル: XSynqSubscriber.cs プロジェクト: jesumarquez/lt
        private void Subscribe()
        {
            if (ConnectionState != ConnctionStates.Init)
            {
                throw new InvalidOperationException("Estado incorrecto, no puede suscribir.");
            }
            ConnectionState = ConnctionStates.Trying;
            var d = new SubscribeDelegate(AsyncSubscribe);

            d.BeginInvoke(AsyncSubscribeComplete, d);
        }
コード例 #2
0
ファイル: Broker.cs プロジェクト: arturfonseca/DAD
        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");
             }*/
        }