Esempio n. 1
0
 /// <summary>
 ///     Checks if the given topic is valid.
 /// </summary>
 /// <typeparam name="T">Advertise Options </typeparam>
 /// <param name="ops"></param>
 /// <returns></returns>
 private bool IsValid <T>(AdvertiseOptions <T> ops) where T : RosMessage, new()
 {
     if (ops.dataType == "*")
     {
         throw new Exception("Advertising with * as the datatype is not allowed.  Topic [" + ops.topic + "]");
     }
     if (ops.md5Sum == "*")
     {
         throw new Exception("Advertising with * as the md5sum is not allowed.  Topic [" + ops.topic + "]");
     }
     if (ops.md5Sum == "")
     {
         throw new Exception("Advertising on topic [" + ops.topic + "] with an empty md5sum");
     }
     if (ops.dataType == "")
     {
         throw new Exception("Advertising on topic [" + ops.topic + "] with an empty datatype");
     }
     if (string.IsNullOrEmpty(ops.messageDefinition))
     {
         this.logger.LogWarning(
             "Advertising on topic [" + ops.topic +
             "] with an empty message definition. Some tools may not work correctly"
             );
     }
     return(true);
 }
Esempio n. 2
0
        /// <summary>
        ///     Creates a publisher with the given advertise options
        /// </summary>
        /// <typeparam name="M">Type of topic</typeparam>
        /// <param name="ops">Advertise options</param>
        /// <returns>A publisher with the specified options</returns>
        public Publisher <M> advertise <M>(AdvertiseOptions <M> ops) where M : RosMessage, new()
        {
            ops.topic = resolveName(ops.topic);
            if (ops.callbackQueue == null)
            {
                ops.callbackQueue = Callback;
            }
            var callbacks = new SubscriberCallbacks(ops.connectCB, ops.disconnectCB, ops.callbackQueue);

            if (TopicManager.Instance.advertise(ops, callbacks))
            {
                var pub = new Publisher <M>(ops.topic, ops.md5Sum, ops.dataType, this, callbacks);
                lock ( gate )
                {
                    collection.Publishers.Add(pub);
                }
                return(pub);
            }
            ROS.Error()($"[{ThisNode.Name}] Advertisement of publisher has failed");
            return(null);
        }
Esempio n. 3
0
        /// <summary>
        ///     Creates a publisher with the given advertise options
        /// </summary>
        /// <typeparam name="M">Type of topic</typeparam>
        /// <param name="ops">Advertise options</param>
        /// <returns>A publisher with the specified options</returns>
        public async Task <Publisher <M> > AdvertiseAsync <M>(AdvertiseOptions <M> ops)
            where M : RosMessage, new()
        {
            ops.topic = ResolveName(ops.topic);
            if (ops.callbackQueue == null)
            {
                ops.callbackQueue = Callback;
            }
            var callbacks = new SubscriberCallbacks(ops.connectCB, ops.disconnectCB, ops.callbackQueue);

            if (await TopicManager.Instance.Advertise(ops, callbacks))
            {
                var pub = new Publisher <M>(ops.topic, ops.md5Sum, ops.dataType, this, callbacks);
                lock (gate)
                {
                    collection.Publishers.Add(pub);
                }
                return(pub);
            }
            logger.LogError("Advertisement of publisher has failed");
            return(null);
        }
Esempio n. 4
0
        /// <summary>
        ///     Register as a publisher on a topic.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ops"></param>
        /// <param name="callbacks"></param>
        /// <returns></returns>
        public bool advertise <T>(AdvertiseOptions <T> ops, SubscriberCallbacks callbacks) where T : RosMessage, new()
        {
            if (!isValid(ops))
            {
                return(false);
            }

            Publication pub = null;

            lock ( advertisedTopicsMutex )
            {
                if (shuttingDown)
                {
                    return(false);
                }
                pub = lookupPublicationWithoutLock(ops.topic);
                if (pub != null)
                {
                    if (pub.Md5sum != ops.md5Sum)
                    {
                        ROS.Error()($"[{ThisNode.Name}] Tried to advertise on topic [{ops.topic}] with md5sum [{ops.md5Sum}] and datatype [{ops.dataType}], but the topic is already advertised as md5sum [{pub.Md5sum}] and datatype [{pub.DataType}]");
                        return(false);
                    }
                }
                else
                {
                    pub = new Publication(ops.topic, ops.dataType, ops.md5Sum, ops.messageDefinition, ops.queueSize,
                                          ops.latch, ops.hasHeader);
                }
                pub.addCallbacks(callbacks);
                advertisedTopics.Add(pub);
            }

            bool         found = false;
            Subscription sub   = null;

            lock ( subcriptionsMutex )
            {
                foreach (Subscription s in subscriptions)
                {
                    if (s.name == ops.topic && md5sumsMatch(s.md5sum, ops.md5Sum) && !s.IsDropped)
                    {
                        found = true;
                        sub   = s;
                        break;
                    }
                }
            }

            if (found)
            {
                sub.addLocalConnection(pub);
            }

            var args    = new XmlRpcValue(ThisNode.Name, ops.topic, ops.dataType, XmlRpcManager.Instance.Uri);
            var result  = new XmlRpcValue();
            var payload = new XmlRpcValue();

            if (!Master.execute("registerPublisher", args, result, payload, true))
            {
                ROS.Error()($"[{ThisNode.Name}] RPC \"registerService\" for service {ops.topic} failed.");
                return(false);
            }

            return(true);
        }
Esempio n. 5
0
 public Publisher <M> Advertise <M>(AdvertiseOptions <M> ops) where M : RosMessage, new() =>
 AdvertiseAsync <M>(ops).Result;
Esempio n. 6
0
        /// <summary>
        /// Register as a publisher on a topic.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ops"></param>
        /// <param name="callbacks"></param>
        /// <returns></returns>
        public async Task <bool> Advertise <T>(AdvertiseOptions <T> ops, SubscriberCallbacks callbacks) where T : RosMessage, new()
        {
            if (!IsValid(ops))
            {
                return(false);
            }

            Publication pub = null;

            lock (gate)
            {
                if (shuttingDown)
                {
                    return(false);
                }

                pub = LookupPublicationWithoutLock(ops.topic);
                if (pub != null)
                {
                    if (pub.Md5Sum != ops.md5Sum)
                    {
                        this.logger.LogError(
                            "Tried to advertise on topic [{0}] with md5sum [{1}] and datatype [{2}], but the topic is already advertised as md5sum [{3}] and datatype [{4}]",
                            ops.topic,
                            ops.md5Sum,
                            ops.dataType,
                            pub.Md5Sum,
                            pub.DataType
                            );
                        return(false);
                    }
                }
                else
                {
                    pub = new Publication(
                        ops.topic,
                        ops.dataType,
                        ops.md5Sum,
                        ops.messageDefinition,
                        ops.queueSize,
                        ops.Latch,
                        ops.hasHeader
                        );
                }
                pub.AddCallbacks(callbacks);
                advertisedTopics.Add(pub);
            }

            bool         found = false;
            Subscription sub   = null;

            lock (gate)
            {
                foreach (Subscription s in subscriptions)
                {
                    if (s.Name == ops.topic && Md5SumsMatch(s.Md5Sum, ops.md5Sum) && !s.IsDisposed)
                    {
                        found = true;
                        sub   = s;
                        break;
                    }
                }
            }

            if (found)
            {
                sub.AddLocalConnection(pub);
            }

            var args    = new XmlRpcValue(ThisNode.Name, ops.topic, ops.dataType, XmlRpcManager.Instance.Uri);
            var result  = new XmlRpcValue();
            var payload = new XmlRpcValue();

            if (!await Master.ExecuteAsync("registerPublisher", args, result, payload, true))
            {
                this.logger.LogError($"RPC \"registerPublisher\" for topic '{ops.topic}' failed.");
                return(false);
            }

            return(true);
        }