Exemple #1
0
        public bool subscribe <T>(SubscribeOptions <T> ops) where T : IRosMessage, new()
        {
            lock (subs_mutex)
            {
                if (addSubCallback(ops))
                {
                    return(true);
                }
                if (shutting_down)
                {
                    return(false);
                }
            }
            if (ops.md5sum == "")
            {
                throw subscribeFail(ops, "with an empty md5sum");
            }
            if (ops.datatype == "")
            {
                throw subscribeFail(ops, "with an empty datatype");
            }
            if (ops.helper == null)
            {
                throw subscribeFail(ops, "without a callback");
            }
            string       md5sum   = ops.md5sum;
            string       datatype = ops.datatype;
            Subscription s        = new Subscription(ops.topic, md5sum, datatype);

            s.addCallback(ops.helper, ops.md5sum, ops.callback_queue, ops.queue_size, ops.allow_concurrent_callbacks, ops.topic);
            if (!registerSubscriber(s, ops.datatype))
            {
                EDB.WriteLine("Couldn't register subscriber on topic [{0}]", ops.topic);
                s.shutdown();
                return(false);
            }

            lock (subs_mutex)
            {
                subscriptions.Add(s);
            }
            return(true);
        }
        public bool addSubCallback <M>(SubscribeOptions <M> ops) where M : IRosMessage, new()
        {
            bool         found       = false;
            bool         found_topic = false;
            Subscription sub         = null;

            if (shutting_down)
            {
                return(false);
            }
            foreach (Subscription s in subscriptions)
            {
                sub = s;
                if (!sub.IsDropped && sub.name == ops.topic)
                {
                    found_topic = true;
                    if (md5sumsMatch(ops.md5sum, sub.md5sum))
                    {
                        found = true;
                    }
                    break;
                }
            }
            if (found_topic && !found)
            {
                throw new Exception
                          ("Tried to subscribe to a topic with the same name but different md5sum as a topic that was already subscribed [" +
                          ops.datatype + "/" + ops.md5sum + " vs. " + sub.datatype + "/" +
                          sub.md5sum + "]");
            }
            if (found)
            {
                if (
                    !sub.addCallback(ops.helper, ops.md5sum, ops.callback_queue, ops.queue_size,
                                     ops.allow_concurrent_callbacks, ops.topic))
                {
                    return(false);
                }
            }
            return(found);
        }