Ejemplo n.º 1
0
        /// <summary>
        ///   Publisher node API method called by a subscriber node. <br/>
        ///   This requests that source allocate a channel for communication. <br/>
        ///   Subscriber provides a list of desired protocols for communication. <br/>
        ///   Publisher returns the selected protocol along with any additional params required for establishing connection. <br/>
        ///   For example, for a TCP/IP-based connection, the source node may return a port number of TCP/IP server.
        /// </summary>
        /// <param name="callerId"> ROS caller ID. </param>
        /// <param name="topic"> Topic name. </param>
        /// <param name="protocols"> List of desired protocols for communication in order of preference. Each protocol is a list of the form [ProtocolName, ProtocolParam1, ProtocolParam2...N] </param>
        /// <returns>
        /// [0] = int: code <br/>
        /// [1] = str: status message <br/>
        /// [2] = protocolParams may be an empty list if there are no compatible protocols.
        /// </returns>
        public object[] RequestTopic(string callerId, string topic, object[] protocols)
        {
            _logger.Debug(m => m("RequestTopic(callerId={0},topic={1},protocols={2})"
                                 , callerId, topic, protocols));

            if (!_topicContainer.HasPublisher(topic))
            {
                _logger.Warn(m => m("No publishers for topic: ", topic));
                return(new object[]
                {
                    StatusCode.Error,
                    "No publishers for topic: " + topic,
                    "null"
                });
            }

            foreach (string[] protocol in protocols)
            {
                string protocolName = protocol[0];

                if (protocolName != "TCPROS")
                {
                    continue;
                }

                if (!_tcpRosListener.ContainsKey(topic))
                {
                    _logger.Warn(m => m("No publishers for topic: ", topic));
                    return(new object[]
                    {
                        StatusCode.Error,
                        "No publishers for topic: " + topic,
                        "null"
                    });
                }
                var listener = _tcpRosListener[topic];
                var address  = listener.EndPoint;

                return(new object[]
                {
                    StatusCode.Success,
                    "Protocol<" + protocolName + ", AdvertiseAddress<" + Ros.HostName + ", " + address.Port + ">>",
                    new object[]
                    {
                        protocolName,
                        Ros.HostName,
                        address.Port
                    }
                });
            }

            _logger.Warn("No supported protocols specified.");

            return(new object[]
            {
                StatusCode.Error,
                "No supported protocols specified.",
                "null"
            });
        }
Ejemplo n.º 2
0
        public void AddPublisher_AlreadyAdded()
        {
            var        container = new TopicContainer();
            IPublisher pub;

            container.HasPublisher("pub1").Is(false);
            container.GetPublishers().Count.Is(0);

            container.AddPublisher(new Publisher <std_msgs.String>("pub1", "test")).Is(true);
            container.HasPublisher("pub1").Is(true);
            container.GetPublishers().Count.Is(1);
            container.GetPublisher("pub1", out pub).Is(true);

            container.AddPublisher(new Publisher <std_msgs.String>("pub1", "test")).Is(false);
            container.HasPublisher("pub1").Is(true);
            container.GetPublishers().Count.Is(1);
            container.GetPublisher("pub1", out pub).Is(true);
        }
Ejemplo n.º 3
0
        /// <summary>
        ///   Create a ROS Topic Publisher
        /// </summary>
        /// <typeparam name="TMessage"> Topic Message Type </typeparam>
        /// <param name="topicName"> Topic Name </param>
        /// <param name="latching"> true: send the latest published message when subscribed topic </param>
        /// <returns> Publisher </returns>
        public Task <Publisher <TMessage> > PublisherAsync <TMessage>(string topicName, bool latching = false)
            where TMessage : IMessage, new()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("Node");
            }

            if (_topicContainer.HasPublisher(topicName))
            {
                throw new InvalidOperationException(topicName + " is already created.");
            }

            _logger.InfoFormat("Create Publisher: {0}", topicName);

            var publisher = new Publisher <TMessage>(topicName, NodeId, latching);

            _topicContainer.AddPublisher(publisher);
            publisher.Disposing += DisposePublisherAsync;

            var tcpRosListener = new TcpRosListener(0);

            _slaveServer.AddListener(topicName, tcpRosListener);

            var acceptDisposable = tcpRosListener.AcceptAsync()
                                   .Do(_ => _logger.Debug("Accepted for Publisher"))
                                   .Subscribe(socket => publisher.AddTopic(socket),
                                              ex => _logger.Error("Accept Error", ex));

            _publisherDisposables.Add(topicName, acceptDisposable);

            var tcs = new TaskCompletionSource <Publisher <TMessage> >();

            _logger.Debug("RegisterPublisher");
            _masterClient
            .RegisterPublisherAsync(NodeId, topicName, publisher.MessageType, _slaveServer.SlaveUri)
            .ContinueWith(task =>
            {
                _logger.Debug("Registered Publisher");

                if (task.Status == TaskStatus.RanToCompletion)
                {
                    publisher.UpdateSubscribers(task.Result);
                    tcs.SetResult(publisher);
                }
                else if (task.Status == TaskStatus.Faulted)
                {
                    _logger.Error("RegisterPublisher: Failure", task.Exception.InnerException);
                    tcs.SetException(task.Exception.InnerException);
                }
            });

            return(tcs.Task);
        }