示例#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);
        }
示例#2
0
        public bool unsubscribe(string topic, ISubscriptionCallbackHelper sbch)
        {
            Subscription sub = null;

            lock (subs_mutex)
            {
                if (shutting_down)
                {
                    return(false);
                }
                foreach (Subscription s in subscriptions)
                {
                    if (s.name == topic)
                    {
                        sub = s;
                        break;
                    }
                }
            }
            if (sub == null)
            {
                return(false);
            }
            sub.removeCallback(sbch);
            if (sub.NumCallbacks == 0)
            {
                lock (subs_mutex)
                {
                    subscriptions.Remove(sub);

                    if (!unregisterSubscriber(topic))
                    {
                        EDB.WriteLine("Couldn't unregister subscriber for topic [" + topic + "]");
                    }
                }

                sub.shutdown();
                return(true);
            }
            return(true);
        }