public void RemoveSubscriber_Success() { 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.RemoveSubscriber("sub1").Is(true); container.GetSubscriber("sub1", out sub).Is(false); container.RemoveSubscriber("sub1").Is(false); }
/// <summary> /// Callback from master of current publisher list for specified topic. /// </summary> /// <param name="callerId"> ROS caller ID. </param> /// <param name="topic"> Topic name. </param> /// <param name="publishers"> List of current publishers for topic in the form of XMLRPC URIs </param> /// <returns> /// [0] = int: code <br/> /// [1] = str: status message <br/> /// [2] = int: ignore /// </returns> public object[] PublisherUpdate(string callerId, string topic, string[] publishers) { _logger.Debug(m => m("PublisherUpdate(callerId={0},topic={1},publishers={2})" , callerId, topic, publishers)); if (_topicContainer.HasSubscriber(topic)) { //TODO: TryGet? var subs = _topicContainer.GetSubscribers().First(s => s.TopicName == topic); subs.UpdatePublishers(publishers.Select(x => new Uri(x)).ToList()); } return(new object[] { StatusCode.Success, "Publisher update received.", 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); }