private void TurtlesimSubscribe_Closing(object sender, System.ComponentModel.CancelEventArgs e)
 {
     if (_subscriber != null)
     {
         _subscriber.UnsubscribeAsync();
         _subscriber = null;
     }
 }
Example #2
0
        public static void WaitForAnyPublisher(this IRosSubscriber subscriber, CancellationToken token = default)
        {
            if (subscriber == null)
            {
                throw new ArgumentNullException(nameof(subscriber));
            }

            Task.Run(() => subscriber.WaitForAnyPublisherAsync(token), token).WaitAndRethrow();
        }
 public TurtlesimSubscribe(string topic, IMessageDispatcher messageDispatcher)
 {
     InitializeComponent();
     _viewModel  = new TurtlesimSubscribeViewModel(topic);
     _subscriber = new Subscriber <Twist>(topic, messageDispatcher, new RosMessageTypeAttributeHelper());
     _subscriber.RosMessageReceived += _subscriber_RosMessageReceived;
     _viewModel.Type = _subscriber.Type;
     Loaded         += TurtlesimSubscribe_Loaded;
     Closing        += TurtlesimSubscribe_Closing;
 }
Example #4
0
 public static void WaitForAnyPublisher(this IRosSubscriber subscriber, int timeoutInMs)
 {
     using CancellationTokenSource tokenSource = new(timeoutInMs);
     try
     {
         subscriber.WaitForAnyPublisher(tokenSource.Token);
     }
     catch (OperationCanceledException)
     {
         throw new TimeoutException("Wait for publisher timed out");
     }
 }
Example #5
0
        public static async Task WaitForAnyPublisherAsync(this IRosSubscriber subscriber,
                                                          CancellationToken token = default)
        {
            if (subscriber == null)
            {
                throw new ArgumentNullException(nameof(subscriber));
            }

            if (subscriber.NumActivePublishers != 0)
            {
                return;
            }

            using var linkedSource = CancellationTokenSource.CreateLinkedTokenSource(token, subscriber.CancellationToken);
            while (subscriber.NumActivePublishers == 0)
            {
                await Task.Delay(200, linkedSource.Token);
            }
        }
Example #6
0
        public async Task UnsubscribeAsync(CancellationToken token)
        {
            token.ThrowIfCancellationRequested();

            if (Subscriber == null)
            {
                return;
            }

            BagListener = null;

            if (subscriberId != null)
            {
                await Subscriber.UnsubscribeAsync(subscriberId, token);

                subscriberId = null;
            }

            Subscriber = null;
        }
Example #7
0
        public async Task SubscribeAsync(RosClient client, IListener listener, CancellationToken token)
        {
            if (listener != null)
            {
                listeners.Add((Listener <T>)listener);
            }

            token.ThrowIfCancellationRequested();
            if (client != null)
            {
                for (int t = 0; t < NumRetries; t++)
                {
                    try
                    {
                        IRosSubscriber subscriber;
                        (subscriberId, subscriber) = await client.SubscribeAsync <T>(topic, Callback, token : token,
                                                                                     transportHint : TransportHint);

                        if (bagListener != null)
                        {
                            bagId = subscriber.Subscribe(bagListener.EnqueueMessage);
                        }

                        Subscriber = subscriber;
                        break;
                    }
                    catch (RoslibException e)
                    {
                        Core.Logger.Error($"Failed to subscribe to service (try {t.ToString()}): ", e);
                        await Task.Delay(WaitBetweenRetriesInMs, token);
                    }
                }
            }
            else
            {
                Subscriber = null;
            }
        }