Пример #1
0
        public void AddSubscriber_AlreadyAdded()
        {
            var         container = new TopicContainer();
            ISubscriber sub;

            container.HasSubscriber("sub1").Is(false);
            container.GetSubscribers().Count.Is(0);

            container.AddSubscriber(new Subscriber <std_msgs.String>("sub1", "test")).Is(true);
            container.HasSubscriber("sub1").Is(true);
            container.GetSubscribers().Count.Is(1);
            container.GetSubscriber("sub1", out sub).Is(true);

            container.AddSubscriber(new Subscriber <std_msgs.String>("sub1", "test")).Is(false);
            container.HasSubscriber("sub1").Is(true);
            container.GetSubscribers().Count.Is(1);
            container.GetSubscriber("sub1", out sub).Is(true);
        }
Пример #2
0
        /// <summary>
        ///   Create a ROS Topic Subscriber
        /// </summary>
        /// <typeparam name="TMessage"> Topic Message Type </typeparam>
        /// <param name="topicName"> Topic Name </param>
        /// <param name="nodelay"> false: Socket uses the Nagle algorithm </param>
        /// <returns> Subscriber </returns>
        public Task <Subscriber <TMessage> > SubscriberAsync <TMessage>(string topicName, bool nodelay = true)
            where TMessage : IMessage, new()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("Node");
            }

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

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

            var subscriber = new Subscriber <TMessage>(topicName, NodeId, nodelay);

            _topicContainer.AddSubscriber(subscriber);
            subscriber.Disposing += DisposeSubscriberAsync;

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

            _logger.Debug("RegisterSubscriber");
            _masterClient
            .RegisterSubscriberAsync(NodeId, topicName, subscriber.MessageType, _slaveServer.SlaveUri)
            .ContinueWith(task =>
            {
                _logger.Debug("Registered Subscriber");

                if (task.Status == TaskStatus.RanToCompletion)
                {
                    ((ISubscriber)subscriber).UpdatePublishers(task.Result);
                    tcs.SetResult(subscriber);
                }
                else if (task.Status == TaskStatus.Faulted)
                {
                    tcs.SetException(task.Exception.InnerException);
                    _logger.Error("RegisterSubscriber: Failure", task.Exception.InnerException);
                }
            });

            return(tcs.Task);
        }