Ejemplo n.º 1
0
 public PropagatedPublishMessage(PublishMessage msg, string site)
 {
     publisherURI = msg.publisherURI;
     seqnum = msg.seqnum;
     topic = msg.topic;
     content = msg.content;
     origin_site = site;
 }
Ejemplo n.º 2
0
 public PublishMessage(PublishMessage msg, string site)
 {
     publisherName = msg.publisherName;
     originalSeqnum = msg.originalSeqnum;
     publisherURI = msg.publisherURI;
     seqnum = msg.seqnum;
     topic = msg.topic;
     content = msg.content;
     originSite = site;
     eventnum = msg.eventnum;
 }
Ejemplo n.º 3
0
        private void routing(PublishMessage msg)
        {
            PropagatedPublishMessage pmsg = new PropagatedPublishMessage(msg, _site);
            pmsg.origin_site = _site;

            seq++;
            Console.WriteLine(seq);

            if (_routingPolicy == RoutingPolicy.flooding)
            {
                lock (_childSites)
                {
                    foreach (var s in _childSites)
                    {
                        lock (s)
                        {
                            foreach (var broker in s.brokers)
                            {
                                // TODO broker.getURI() is slow, we should use a cache
                                log(string.Format("[Flooding Routing] sent event '{0}' to '{1}'", msg, broker.getURI()));

                                PropagatePublishDelegate d = new PropagatePublishDelegate(broker.propagatePublish);
                                d.BeginInvoke(pmsg, null, null);

                                if (_loggingLevel == LoggingLevel.full)
                                    c.reportEvent(EventType.BroEvent, getURI(), msg.publisherURI, msg.topic, msg.seqnum);
                            }
                        }

                    }
                }

            }
            else // routing policy is filtering
            {
                List<string> sentSites = new List<string>();

                lock (_topicSites)
                {
                    foreach (var subscribedTopic in _topicSites.Keys)
                    {
                        if (!equivalentTopic(msg.topic, subscribedTopic))
                            continue;
                        foreach (var site_name in _topicSites[subscribedTopic])
                        {

                            if (sentSites.Contains(site_name))
                                continue;
                            var site = _nameToSite[site_name];
                            //translate seq_num of the message to send to child_sites

                            if (_orderingPolicy == OrderingPolicy.fifo)
                            {
                                lock (_siteToFifoStruct)
                                {

                                    if (!_siteToFifoStruct.ContainsKey(site_name))
                                    {
                                        _siteToFifoStruct.Add(site_name, new List<FIFOstruct>());
                                    }

                                    int index = _siteToFifoStruct[site_name].FindIndex(item => item._publhisherURI == pmsg.publisherURI);

                                    if (index < 0)
                                    {
                                        // element does not exists
                                        _siteToFifoStruct[site_name].Add(new FIFOstruct(msg.publisherURI, 0));
                                        //getIndex Now
                                        index = _siteToFifoStruct[site_name].FindIndex(item => item._publhisherURI == pmsg.publisherURI);
                                    }
                                    var fifo = _siteToFifoStruct[site_name][index];

                                    //create a new message for each site interested sites with possibly a different seqnum
                                    PropagatedPublishMessage local_pmsg = new PropagatedPublishMessage(msg, _site);
                                    local_pmsg.origin_site = _site;

                                    local_pmsg.seqnum = fifo._seq_num;
                                    fifo.listOfmessages.Add(local_pmsg);

                                    fifo._seq_num++;

                                    //It's not necessary to register the message in the listOfmessages because the message is imediately sent
                                    //But you could have to do that way with an assync approach
                                }
                            }

                            lock (site)
                            {
                                foreach (var broker in site.brokers)
                                {
                                    // using broker.getURI() increases network traffic
                                    if (_orderingPolicy == OrderingPolicy.fifo)
                                    {
                                        int index = _siteToFifoStruct[site_name].FindIndex(item => item._publhisherURI == pmsg.publisherURI);
                                        if (index >= 0)
                                        {
                                            foreach (PropagatedPublishMessage _pmsg in _siteToFifoStruct[site.name][index].listOfmessages.ToList())
                                            {
                                                PropagatePublishDelegate d = new PropagatePublishDelegate(broker.propagatePublish);
                                                log(string.Format("[filtering routing] sent event '{0}' to '{1}'", _pmsg, broker.getURI()));
                                                d.BeginInvoke(_pmsg, null, null);

                                                if (_loggingLevel == LoggingLevel.full)
                                                    c.reportEvent(EventType.BroEvent, getURI(), _pmsg.publisherURI, _pmsg.topic, msg.seqnum);

                                                _siteToFifoStruct[site.name][index].listOfmessages.Remove(_pmsg);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        PropagatePublishDelegate d = new PropagatePublishDelegate(broker.propagatePublish);
                                        log(string.Format("[filtering routing] sent event '{0}' to '{1}'", pmsg, broker.getURI()));
                                        d.BeginInvoke(pmsg, null, null);

                                        if (_loggingLevel == LoggingLevel.full)
                                            c.reportEvent(EventType.BroEvent, getURI(), pmsg.publisherURI, pmsg.topic, msg.seqnum);

                                    }
                                }
                            }

                        }
                    }
                }

            }

            // send to parent site brokers
            // always send publish to parent, doesnt matter if interested in topic
            lock (_parentSiteLock)
            {
                if (_parentSite != null)
                {
                    foreach (Broker broker in _parentSite.brokers)
                    {
                        // TODO broker.getURI() is slow, we should use a cache
                        PropagatePublishDelegate d = new PropagatePublishDelegate(broker.propagatePublish);
                        log(string.Format("[Routing] sent event '{0}' to parent broker '{1}'", pmsg, broker.getURI()));
                        d.BeginInvoke(pmsg, null, null);

                        if (_loggingLevel == LoggingLevel.full)
                            c.reportEvent(EventType.BroEvent, getURI(), pmsg.publisherURI, pmsg.topic, msg.seqnum);

                    }
                }
            }
        }
Ejemplo n.º 4
0
        private void deliver(PublishMessage msg)
        {
            // to avoid sending two times, we use a list
            List<string> sentUris = new List<string>();

            // List of delegates that know the interested subcribers
            List<ReceiveDelegate> remoteDelegates = new List<ReceiveDelegate>();

            // First phase we filter site subscribers and fillin
            lock (_topicSubscribers)
            {
                foreach (var subscribedTopic in _topicSubscribers.Keys)
                {
                    if (!equivalentTopic(msg.topic, subscribedTopic))
                        continue;
                    foreach (var uri in _topicSubscribers[subscribedTopic])
                    {
                        if (sentUris.Contains(uri))
                            continue;
                        Subscriber s = _uriToSubs[uri];
                        // TODO assync
                        ReceiveDelegate rd = new ReceiveDelegate(s.receive);
                        remoteDelegates.Add(rd);
                        //MUDAR ISTO...
                        // c.reportEvent(EventType.SubEvent, uri, msg.publisherURI, msg.topic, msg.total_seqnum);
                        log(string.Format("[Deliver] sent event '{0}' to '{1}'", msg, uri));

                    }
                }
            }
            List<IAsyncResult> results = new List<IAsyncResult>();

            foreach (ReceiveDelegate subDelegate in remoteDelegates)
            {
                IAsyncResult result = subDelegate.BeginInvoke(msg, null, null);
                results.Add(result);
            }
            List<WaitHandle> handlesLst = new List<WaitHandle>();
            //TODO ASK PROFESSOR IF WE NEED TO RESEND LOST MESSAGES
        }
Ejemplo n.º 5
0
        public void publishWork(PublishMessage msg)
        {
            // FLOODING implementation
            // TODO discart if duplicate message
            // TODO make all calls assyncs

            log(string.Format("[Publish] Received event '{0}'", msg));

            if (_orderingPolicy == OrderingPolicy.fifo)
            {
                //FIFO
                lock (_fifostructs)
                {

                    int index = _fifostructs.FindIndex(item => item._publhisherURI == msg.publisherURI);

                    if (index < 0)
                    {
                        // element does not exists
                        _fifostructs.Add(new FIFOstruct(msg.publisherURI, 0));
                        //getIndex Now
                        index = _fifostructs.FindIndex(item => item._publhisherURI == msg.publisherURI);
                    }
                    var fifo = _fifostructs[index];
                    //TODO Verify duplicates
                    fifo.listOfmessages.Add(msg);
                    fifo.listOfmessages.OrderBy(item => item.seqnum).ToList();

                    //DEBUG ListOfMessages
                    foreach (PublishMessage _msg in fifo.listOfmessages)
                    {
                        log(string.Format("[ListOfMessage] Publisher: '{0}' msg '{1}'", _msg.publisherURI, _msg));
                    }

                    foreach (PublishMessage _msg in fifo.listOfmessages.ToList())
                    {

                        if (_msg.seqnum == fifo._seq_num)
                        {
                            //Prepare to send the msg to interested sites
                            deliver(_msg);
                            routing(_msg);

                            //Message sent , increment seq_num and delete delivered message
                            fifo._seq_num++;
                            fifo.listOfmessages.Remove(_msg);
                        }
                        else
                            break;
                    }
                }
            }
            else
            {
                deliver(msg);
                routing(msg);
            }
        }
Ejemplo n.º 6
0
        public void publish(PublishMessage msg)
        {
            bool freezed = false;

            lock (_freezedLock)
            {
                if (_freezed)
                {
                    freezed = true;
                    log(string.Format("[Publish] freezed"));
                    _freezedPublishMessages.Add(msg);
                }
            }
            if (!freezed)
            {
                publishWork(msg);
            }
        }
Ejemplo n.º 7
0
        public void receive(PublishMessage p)
        {
            bool freezed =false;
            lock (_freezedLock)
            {
                if (_freezed)
                {
                    freezed = true;
                    _freezedReceives.Add(p);
                }

            }
            if (!freezed)
            {
                receive_job(p);
            }
        }
Ejemplo n.º 8
0
        private void publish_work(string topic, string content)
        {
            lock (thisLock)
            {
                int total_seqnum = _total_seqnum;
                _total_seqnum += 1;
                string cc = "";
                cc = string.Format("[Content]PublisherURI:'{0}' seqnum:{1} timestamp:{2}",getURI(), total_seqnum, DateTime.Now.ToString());

                var msg = new PublishMessage() { publisherURI = getURI(), seqnum = total_seqnum, topic = topic, content = cc };
                log(string.Format("[publish] {0}", msg));
                // TODO make all calls assyncs

                c.reportEvent(EventType.PubEvent, getURI(), getURI(), topic, total_seqnum);
                _broker.publish(msg);
            }
        }
Ejemplo n.º 9
0
 public void receive_job(PublishMessage m)
 {
     //TEM DE FICAR AQUI O LOG
     c.reportEvent(EventType.SubEvent, getURI(), m.publisherURI, m.topic, m.seqnum);
     log(string.Format("Received. topic:'{0}' content:'{1}'", m.topic, m.content));
 }
Ejemplo n.º 10
0
        private void deliver(int en, PublishMessage rmsg)
        {
            var msg = new PublishMessage(rmsg, _site);
            // to avoid sending two times, we use a list
            List<string> sentURIs = new List<string>();

            // Dictionary of delegates per subcribers
            Dictionary<string, ReceiveDelegate> remoteDelegate = new Dictionary<string, ReceiveDelegate>();

            List<string> receivingSubscribers = new List<string>();
            // First phase we filter site subscribers and fillin
            lock (_topicSubscribers)
            {
                foreach (var subscribedTopic in _topicSubscribers.Keys)
                {
                    if (!equivalentTopic(msg.topic, subscribedTopic))
                        continue;
                    if (_orderingPolicy == OrderingPolicy.total)
                    {
                        lock (_TOQueue)
                        {
                            _TOQueue.Add(msg);
                        }
                        break;
                    }
                    foreach (var uri in _topicSubscribers[subscribedTopic]) // subscriber uri
                    {
                        if (receivingSubscribers.Contains(uri))
                            continue;
                        receivingSubscribers.Add(uri);
                    }
                }
            }

            if (_orderingPolicy == OrderingPolicy.total)
            {
                TODeliver(en);
                return;
            }

            foreach (var uri in receivingSubscribers)
            {
                Subscriber s = _uriToSubs[uri];
                // TODO assync
                ReceiveDelegate rd = new ReceiveDelegate(s.receive);
                if (_orderingPolicy == OrderingPolicy.fifo)
                {
                    lock (_subToFifoStruct)
                    {
                        if (!_subToFifoStruct.ContainsKey(uri))
                        {
                            _subToFifoStruct.Add(uri, new List<FIFOstruct>());
                        }
                        int index = _subToFifoStruct[uri].FindIndex(item => item._publhisherURI == msg.publisherURI);
                        if (index < 0)
                        {
                            // element does not exists
                            _subToFifoStruct[uri].Add(new FIFOstruct(msg.publisherURI, 0));
                            //getIndex Now
                            index = _subToFifoStruct[uri].FindIndex(item => item._publhisherURI == msg.publisherURI);
                        }
                        var fifo = _subToFifoStruct[uri][index];
                        //create a new message for each site interested sites with possibly a different seqnum
                        var x = msg.seqnum;
                        msg.seqnum = fifo._seq_num;
                        fifo._seq_num++;
                        log(en, string.Format("FIFO Deliver.For subscriber {2} mapping seqnum from {0} to {1}", x, msg.seqnum, uri));
                        rd.BeginInvoke(msg, null, null);
                    }
                }
                else
                { // No ordering
                    log(en, "NO ordering. no seqnum mapping");
                    rd.BeginInvoke(msg, null, null);
                    log(en, string.Format("Delivered message to {0}", uri));
                }
            }
        }
Ejemplo n.º 11
0
        private void filter(int en, PublishMessage receivedMessage, PublishMessage sendingMessage)
        {
            log(en, "[Filter] Routing");
            var sentSites = new List<string>();
            lock (_topicSites)
            {
                foreach (var subscribedTopic in _topicSites.Keys)
                {
                    if (!equivalentTopic(receivedMessage.topic, subscribedTopic))
                        continue;
                    foreach (var site_name in _topicSites[subscribedTopic])
                    {
                        if (site_name == receivedMessage.originSite)
                            continue;
                        if (sentSites.Contains(site_name))
                        {
                            continue;
                        }
                        sentSites.Add(site_name);

                        var site = _nameToSite[site_name];
                        if (_orderingPolicy == OrderingPolicy.fifo)
                        {
                            //translate seq_num of the message to send to child_sites
                            lock (_siteToFifoStruct)
                            {
                                if (!_siteToFifoStruct.ContainsKey(site_name))
                                {
                                    _siteToFifoStruct.Add(site_name, new List<FIFOstruct>());
                                }
                                int index = _siteToFifoStruct[site_name].FindIndex(item => item._publhisherURI == sendingMessage.publisherURI);
                                if (index < 0)
                                {
                                    // element does not exists
                                    _siteToFifoStruct[site_name].Add(new FIFOstruct(receivedMessage.publisherURI, 0));
                                    //getIndex Now
                                    index = _siteToFifoStruct[site_name].FindIndex(item => item._publhisherURI == sendingMessage.publisherURI);
                                }
                                var fifo = _siteToFifoStruct[site_name][index];
                                sendingMessage.seqnum = fifo._seq_num;
                                log(en, string.Format("[Filter][FIFO] For site {2}, mapping seqnum from {0} to {1}",
                                    receivedMessage.seqnum, sendingMessage.seqnum, site_name));
                                fifo._seq_num++;
                            }
                        }

                        lock (site)
                        {
                            foreach (var broker in site.brokers)
                            {
                                PublishDelegate d = new PublishDelegate(broker.publish);
                                if (_orderingPolicy == OrderingPolicy.total)
                                {
                                    log(en, "Routing: received " + receivedMessage);
                                    log(en, "Routing sending " + sendingMessage);
                                    d.BeginInvoke(sendingMessage, null, null);
                                    log(en, string.Format("[Filter][TOTAL] routed to site {0}", site.name));
                                }
                                else if (_orderingPolicy == OrderingPolicy.fifo)
                                {
                                    d.BeginInvoke(sendingMessage, null, null);
                                    log(en, string.Format("[Filter][FIFO] routed to site {0}", site.name));
                                }
                                else
                                {
                                    d.BeginInvoke(sendingMessage, null, null);
                                    log(en, string.Format("[FILTER][NO ORDER] routed to {0}", site.name));
                                }
                                if (_loggingLevel == LoggingLevel.full)
                                    c.reportEvent(EventType.BroEvent, getURI(), sendingMessage.publisherURI, sendingMessage.topic, receivedMessage.seqnum);
                            }
                        }

                    }
                }
            }
        }
Ejemplo n.º 12
0
        public void publishWork(PublishMessage receivingMessage)
        {
            // FLOODING implementation
            // TODO discart if duplicate message
            // TODO make all calls assyncs

            //TODO LOCKS HERE

            lock (_receivedLock)
            {
                if (receivedMsg.ContainsKey(receivingMessage.publisherName))
                {
                    if (!receivedMsg[receivingMessage.publisherName].Contains(receivingMessage.originalSeqnum))
                        receivedMsg[receivingMessage.publisherName].Add(receivingMessage.originalSeqnum);
                    else
                        return;
                }
                else
                {
                    receivedMsg.Add(receivingMessage.publisherName, new HashSet<int>());
                    receivedMsg[receivingMessage.publisherName].Add(receivingMessage.originalSeqnum);
                }
            }

            int en = getEventnum();
            log(en, "Processing " + receivingMessage);
            if (_orderingPolicy == OrderingPolicy.total)
            {
                deliver(en, receivingMessage);
                routing(en, receivingMessage);
            }
            else if (_orderingPolicy == OrderingPolicy.fifo)
            {
                //FIFO
                lock (_fifostructs)
                {

                    int index = _fifostructs.FindIndex(item => item._publhisherURI == receivingMessage.publisherURI);
                    if (index < 0)
                    {
                        // element does not exists
                        _fifostructs.Add(new FIFOstruct(receivingMessage.publisherURI, 0));
                        //getIndex Now
                        index = _fifostructs.FindIndex(item => item._publhisherURI == receivingMessage.publisherURI);
                    }
                    var fifo = _fifostructs[index];
                    /// Discard duplicates
                    /*
                    if(msg.seqnum < fifo._seq_num)
                    {
                        log(string.Format("[Publish] Received duplicate message pub:{0} seqnum:{1}", msg.publisherURI, msg.seqnum));
                        return;
                    }
                    */
                    fifo.listOfmessages.Add(receivingMessage);
                    fifo.listOfmessages = fifo.listOfmessages.OrderBy(item => item.seqnum).ToList();
                    var queueList = fifo.listOfmessages.Select(x => "" + x.seqnum);
                    var qlstr = string.Join(",", queueList);
                    log(en, string.Format("FIFO seqnum:{0} queue:{1}", fifo._seq_num, qlstr));
                    foreach (PublishMessage _msg in fifo.listOfmessages.ToList())
                    {
                        if (_msg.seqnum == fifo._seq_num)
                        {
                            //Prepare to send the msg to interested sites
                            deliver(en, _msg);
                            routing(en, _msg);

                            //Message sent , increment seq_num and delete delivered message
                            fifo._seq_num++;
                            fifo.listOfmessages.Remove(_msg);
                        }
                        else
                            break;
                    }
                }
            }
            else
            {
                //TODO DISCARD DUPLICATES
                deliver(en, receivingMessage);
                routing(en, receivingMessage);
            }
        }
Ejemplo n.º 13
0
 public void publish(PublishMessage msg)
 {
     bool freezed = false;
     try
     {
         lock (_freezedLock)
         {
             if (_freezed)
             {
                 freezed = true;
                 log(string.Format("[Publish] freezed"));
                 _freezedPublishMessages.Add(msg);
             }
         }
         if (!freezed)
         {
             publishWork(msg);
         }
     }
     catch (Exception e)
     {
         log(e.ToString());
     }
 }
Ejemplo n.º 14
0
 private void routing(int en, PublishMessage receivedMessage)
 {
     PublishMessage sendingMessage = new PublishMessage(receivedMessage, _site);
     log(en, "Routing: received " + receivedMessage);
     log(en, "Routing sending " + sendingMessage);
     if (_routingPolicy == RoutingPolicy.flooding)
     {
         flooding(en, receivedMessage, sendingMessage);
     }
     else // routing policy is filtering
     {
         filter(en, receivedMessage, sendingMessage);
     }
 }
Ejemplo n.º 15
0
 private void flooding(PublishMessage msg)
 {
 }
Ejemplo n.º 16
0
        private void flooding(int en, PublishMessage receivedMessage, PublishMessage sendingMessage)
        {
            lock (_childSites)
            {
                foreach (var site in _childSites)
                {

                    lock (site)
                    {
                        if (site.name != receivedMessage.originSite)
                        {
                            foreach (var broker in site.brokers)
                            {
                                PublishDelegate d = new PublishDelegate(broker.publish);
                                d.BeginInvoke(sendingMessage, null, null);
                                log(en, string.Format("[Flooding] to child site {0}", site.name));
                                if (_loggingLevel == LoggingLevel.full)
                                    c.reportEvent(EventType.BroEvent, getURI(), receivedMessage.publisherURI, receivedMessage.topic, receivedMessage.seqnum);
                            }
                        }
                    }
                }
            }

            lock (_parentSiteLock)
            {
                if (_parentSite != null && _parentSite.name != receivedMessage.originSite)
                {
                    foreach (Broker broker in _parentSite.brokers)
                    {
                        PublishDelegate d = new PublishDelegate(broker.publish);
                        log(en, string.Format("[Flooding] to parent site {0}", _parentSite.name));
                        d.BeginInvoke(sendingMessage, null, null);
                        if (_loggingLevel == LoggingLevel.full)
                            c.reportEvent(EventType.BroEvent, getURI(), sendingMessage.publisherURI, sendingMessage.topic, receivedMessage.seqnum);
                    }
                }
            }
        }