1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//! Subscribe topics

use std::sync::Arc;

use anyhow::Result;
use futures::channel::mpsc;
use rclrust_msg::_core::MessageT;

use crate::{
    internal::worker::{ReceiveWorker, WorkerMessage},
    node::Node,
    qos::QoSProfile,
};

pub mod rcl_wrapper;
pub use rcl_wrapper::RclSubscription;

pub mod invoker;
pub use invoker::{SubscriptionInvoker, SubscriptionInvokerBase};

/// Subscription
pub struct Subscription<T>
where
    T: MessageT,
{
    handle: Arc<RclSubscription>,
    worker: ReceiveWorker<Arc<T::Raw>>,
}

impl<T> Subscription<T>
where
    T: MessageT,
{
    pub(crate) fn new<F>(
        node: &Node,
        topic_name: &str,
        callback: F,
        qos: &QoSProfile,
    ) -> Result<Self>
    where
        T::Raw: 'static,
        F: Fn(Arc<T::Raw>) + Send + 'static,
    {
        let handle = Arc::new(RclSubscription::new::<T>(
            node.clone_handle(),
            topic_name,
            qos,
        )?);

        Ok(Self {
            handle,
            worker: ReceiveWorker::new(callback),
        })
    }

    /// Get the topic name which this subscritpion subscibes to.
    ///
    /// #  Examples
    ///
    /// ```
    /// # use anyhow::Result;
    /// # use rclrust::qos::QoSProfile;
    /// # use rclrust_msg::std_msgs::msg::Int32;
    /// #
    /// # #[tokio::main]
    /// # async fn main() -> Result<()> {
    /// # let ctx = rclrust::init()?;
    /// # let mut node = ctx.create_node("node")?;
    /// # let callback = |_| ();
    /// let subscription =
    ///     node.create_subscription::<Int32, _>("message", callback, &QoSProfile::default())?;
    /// assert_eq!(&subscription.topic_name().unwrap(), "/message");
    /// # Ok(())
    /// # }
    /// ```
    pub fn topic_name(&self) -> Option<String> {
        self.handle.topic_name()
    }

    /// Check if this publisher is valid or not. Normally, a return value should be `true`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use anyhow::Result;
    /// # use rclrust::qos::QoSProfile;
    /// # use rclrust_msg::std_msgs::msg::Int32;
    /// #
    /// # #[tokio::main]
    /// # async fn main() -> Result<()> {
    /// # let ctx = rclrust::init()?;
    /// # let mut node = ctx.create_node("node")?;
    /// # let callback = |_| ();
    /// let subscription =
    ///     node.create_subscription::<Int32, _>("message", callback, &QoSProfile::default())?;
    /// assert!(subscription.is_valid());
    /// # Ok(())
    /// # }
    /// ```
    pub fn is_valid(&self) -> bool {
        self.handle.is_valid()
    }

    /// Get how many publisher are publishing the topic which this subscription subscribes to.
    ///
    /// # Examples
    ///
    /// ```
    /// # use anyhow::Result;
    /// # use rclrust::qos::QoSProfile;
    /// # use rclrust_msg::std_msgs::msg::Int32;
    /// #
    /// # #[tokio::main]
    /// # async fn main() -> Result<()> {
    /// # let ctx = rclrust::init()?;
    /// # let mut node = ctx.create_node("node")?;
    /// # let callback = |_| ();
    /// let subscription =
    ///     node.create_subscription::<Int32, _>("message", callback, &QoSProfile::default())?;
    /// println!("{}", subscription.publisher_count()?);
    /// # Ok(())
    /// # }
    /// ```
    pub fn publisher_count(&self) -> Result<usize> {
        self.handle.publisher_count()
    }

    /// Get the actual QoS settings, after the defaults have been determined.
    ///
    /// # Examples
    ///
    /// ```
    /// # use anyhow::Result;
    /// # use rclrust::qos::QoSProfile;
    /// # use rclrust_msg::std_msgs::msg::Int32;
    /// #
    /// # #[tokio::main]
    /// # async fn main() -> Result<()> {
    /// # let ctx = rclrust::init()?;
    /// # let mut node = ctx.create_node("node")?;
    /// # let callback = |_| ();
    /// let subscription =
    ///     node.create_subscription::<Int32, _>("message", callback, &QoSProfile::default())?;
    /// println!("{:?}", subscription.actual_qos().unwrap());
    /// # Ok(())
    /// # }
    /// ```
    pub fn actual_qos(&self) -> Option<QoSProfile> {
        self.handle.actual_qos()
    }

    pub(crate) fn create_invoker(&self) -> SubscriptionInvoker<T> {
        SubscriptionInvoker::new_from_target(self)
    }

    pub(crate) fn clone_handle(&self) -> Arc<RclSubscription> {
        Arc::clone(&self.handle)
    }

    pub(crate) fn clone_tx(&self) -> mpsc::Sender<WorkerMessage<Arc<T::Raw>>> {
        self.worker.clone_tx()
    }
}

#[cfg(test)]
mod test {
    use rclrust_msg::std_msgs::msg::Int32;

    use super::*;

    fn random_name() -> String {
        use rand::{distributions::Alphanumeric, thread_rng, Rng};

        thread_rng()
            .sample_iter(&Alphanumeric)
            .map(char::from)
            .filter(|c| c.is_alphabetic())
            .take(20)
            .collect()
    }

    #[tokio::test]
    async fn pub_sub() -> Result<()> {
        use std::{
            sync::{
                atomic::{AtomicU32, Ordering},
                Arc,
            },
            time::Duration,
        };

        let ctx = crate::init()?;
        let pub_node = ctx.create_node(&random_name())?;
        let mut sub_node = ctx.create_node(&random_name())?;

        let topic_name = random_name();

        let counter = Arc::new(AtomicU32::new(0));
        let _subscriber = {
            let counter = Arc::clone(&counter);
            sub_node.create_subscription(
                &topic_name,
                move |topic: Arc<Int32>| {
                    counter.fetch_add(1, Ordering::Relaxed);
                    assert_eq!(topic.data, 42);
                    println!("called");
                },
                &QoSProfile::default(),
            )?
        };

        let publisher = pub_node.create_publisher::<Int32>(&topic_name, &QoSProfile::default())?;
        publisher.publish(&Int32 { data: 42 })?;
        tokio::time::sleep(Duration::from_millis(100)).await;

        assert_eq!(counter.load(Ordering::Relaxed), 1);

        Ok(())
    }

    #[tokio::test]
    async fn subscription_topic_name() -> Result<()> {
        let ctx = crate::init()?;
        let mut node = ctx.create_node(&random_name())?;
        let subscription =
            node.create_subscription::<Int32, _>("message", |_| (), &QoSProfile::default())?;
        assert_eq!(subscription.topic_name().unwrap(), "/message");

        Ok(())
    }

    #[tokio::test]
    async fn subscription_is_valid() -> Result<()> {
        let ctx = crate::init()?;
        let mut node = ctx.create_node(&random_name())?;
        let subscription =
            node.create_subscription::<Int32, _>("message", |_| (), &QoSProfile::default())?;
        assert!(subscription.is_valid());

        Ok(())
    }

    #[tokio::test]
    async fn subscription_publisher_count() -> Result<()> {
        let ctx = crate::init()?;
        let mut node = ctx.create_node(&random_name())?;
        let subscription =
            node.create_subscription::<Int32, _>("message", |_| (), &QoSProfile::default())?;

        assert_eq!(subscription.publisher_count()?, 0);

        let _pub = node.create_publisher::<Int32>("message", &QoSProfile::default())?;

        assert_eq!(subscription.publisher_count()?, 1);

        Ok(())
    }

    #[tokio::test]
    async fn subscription_actual_qos() -> Result<()> {
        use std::time::Duration;

        use crate::qos::LivelinessPolicy;

        let ctx = crate::init()?;
        let mut node = ctx.create_node(&random_name())?;
        let qos = QoSProfile::sensor_data()
            .deadline(Duration::from_millis(5))
            .lifespan(Duration::from_millis(10))
            .liveliness(LivelinessPolicy::Automatic)
            .liveliness_lease_duration(Duration::from_millis(15));
        let subscription = node.create_subscription::<Int32, _>("message", |_| (), &qos)?;

        let actual_qos = subscription.actual_qos().unwrap();
        assert_eq!(actual_qos.history, qos.history);
        assert_eq!(actual_qos.depth, qos.depth);
        assert_eq!(actual_qos.reliability, qos.reliability);
        assert_eq!(actual_qos.durability, qos.durability);
        assert_eq!(actual_qos.deadline, qos.deadline);
        // TODO: Do not match when using Cyclone DDS
        // assert_eq!(actual_qos.lifespan, qos.lifespan);
        assert_eq!(actual_qos.liveliness, qos.liveliness);
        assert_eq!(
            actual_qos.liveliness_lease_duration,
            qos.liveliness_lease_duration
        );
        assert_eq!(
            actual_qos.avoid_ros_namespace_conventions,
            qos.avoid_ros_namespace_conventions
        );

        Ok(())
    }
}