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
use super::clock::Clock;
use super::error::Result;
use super::master::Master;
use super::slave::Slave;
use crate::api::SystemState;
use crate::error::ErrorKind;
use crate::rosxmlrpc::Response;
use crate::tcpros::{Message, PublisherStream, ServicePair, ServiceResult};
use crate::{RawMessageDescription, SubscriptionHandler};
use log::error;
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;

#[derive(Clone)]
pub struct Publisher<T: Message> {
    clock: Arc<dyn Clock>,
    seq: Arc<AtomicUsize>,
    stream: PublisherStream<T>,
    raii: Arc<InteractorRaii<PublisherInfo>>,
}

impl<T: Message> Publisher<T> {
    pub(crate) fn new(
        master: Arc<Master>,
        slave: Arc<Slave>,
        clock: Arc<dyn Clock>,
        hostname: &str,
        name: &str,
        queue_size: usize,
        message_description: Option<RawMessageDescription>,
    ) -> Result<Self> {
        let message_description =
            message_description.unwrap_or_else(RawMessageDescription::from_message::<T>);
        let stream =
            slave.add_publication::<T>(hostname, name, queue_size, message_description.clone())?;

        let raii = Arc::new(InteractorRaii::new(PublisherInfo {
            master,
            slave,
            name: name.into(),
        }));

        raii.interactor
            .master
            .register_publisher(name, &message_description.msg_type)
            .map_err(|err| {
                error!("Failed to register publisher for topic '{}': {}", name, err);
                err
            })?;

        Ok(Self {
            stream,
            clock,
            seq: Arc::new(AtomicUsize::new(0)),
            raii,
        })
    }

    #[inline]
    pub fn subscriber_count(&self) -> usize {
        self.stream.subscriber_count()
    }

    #[inline]
    pub fn subscriber_names(&self) -> Vec<String> {
        self.stream.subscriber_names()
    }

    #[inline]
    pub fn set_latching(&mut self, latching: bool) {
        self.stream.set_latching(latching);
    }

    #[inline]
    pub fn set_queue_size(&mut self, queue_size: usize) {
        self.stream.set_queue_size(queue_size);
    }

    /// Wait until all the subscribers reported by rosmaster have connected
    #[inline]
    pub fn wait_for_subscribers(&self, timeout: Option<std::time::Duration>) -> Result<()> {
        let timeout = timeout.map(|v| std::time::Instant::now() + v);
        let iteration_time = std::time::Duration::from_millis(50);
        loop {
            let system_state: SystemState = self.raii.interactor.master.get_system_state()?.into();
            let mut master_subs = system_state
                .subscribers
                .into_iter()
                .find(|v| v.name == self.raii.interactor.name)
                .map(|v| v.connections)
                .unwrap_or_default();
            master_subs.sort();
            let mut local_subs = self.subscriber_names();
            local_subs.sort();
            if master_subs == local_subs {
                return Ok(());
            }
            let now = std::time::Instant::now();
            let mut wait_time = iteration_time;
            if let Some(timeout) = &timeout {
                let time_left = timeout
                    .checked_duration_since(now)
                    .ok_or(ErrorKind::TimeoutError)?;
                wait_time = wait_time.min(time_left);
            }
            std::thread::sleep(wait_time);
        }
    }

    #[inline]
    pub fn send(&self, mut message: T) -> Result<()> {
        message.set_header(&self.clock, &self.seq);
        self.stream.send(&message).map_err(Into::into)
    }
}

struct PublisherInfo {
    master: Arc<Master>,
    slave: Arc<Slave>,
    name: String,
}

impl Interactor for PublisherInfo {
    fn unregister(&mut self) -> Response<()> {
        self.slave.remove_publication(&self.name);
        self.master.unregister_publisher(&self.name).map(|_| ())
    }
}

#[derive(Clone)]
pub struct Subscriber {
    info: Arc<InteractorRaii<SubscriberInfo>>,
}

impl Subscriber {
    pub(crate) fn new<T, H>(
        master: Arc<Master>,
        slave: Arc<Slave>,
        name: &str,
        queue_size: usize,
        handler: H,
    ) -> Result<Self>
    where
        T: Message,
        H: SubscriptionHandler<T>,
    {
        let id = slave.add_subscription::<T, H>(name, queue_size, handler)?;

        let info = Arc::new(InteractorRaii::new(SubscriberInfo {
            master,
            slave,
            name: name.into(),
            id,
        }));

        let publishers = info
            .interactor
            .master
            .register_subscriber(name, &T::msg_type())?;

        if let Err(err) = info
            .interactor
            .slave
            .add_publishers_to_subscription(name, publishers.into_iter())
        {
            error!(
                "Failed to subscribe to all publishers of topic '{}': {}",
                name, err
            );
        }

        Ok(Self { info })
    }

    #[inline]
    pub fn publisher_count(&self) -> usize {
        self.info
            .interactor
            .slave
            .get_publisher_count_of_subscription(&self.info.interactor.name)
    }

    #[inline]
    pub fn publisher_uris(&self) -> Vec<String> {
        self.info
            .interactor
            .slave
            .get_publisher_uris_of_subscription(&self.info.interactor.name)
    }
}

struct SubscriberInfo {
    master: Arc<Master>,
    slave: Arc<Slave>,
    name: String,
    id: usize,
}

impl Interactor for SubscriberInfo {
    fn unregister(&mut self) -> Response<()> {
        self.slave.remove_subscription(&self.name, self.id);
        self.master.unregister_subscriber(&self.name).map(|_| ())
    }
}

#[derive(Clone)]
pub struct Service {
    _raii: Arc<InteractorRaii<ServiceInfo>>,
}

impl Service {
    pub(crate) fn new<T, F>(
        master: Arc<Master>,
        slave: Arc<Slave>,
        hostname: &str,
        bind_address: &str,
        name: &str,
        handler: F,
    ) -> Result<Self>
    where
        T: ServicePair,
        F: Fn(T::Request) -> ServiceResult<T::Response> + Send + Sync + 'static,
    {
        let api = slave.add_service::<T, F>(hostname, bind_address, name, handler)?;

        let raii = Arc::new(InteractorRaii::new(ServiceInfo {
            master,
            slave,
            api,
            name: name.into(),
        }));

        raii.interactor
            .master
            .register_service(name, &raii.interactor.api)?;
        Ok(Self { _raii: raii })
    }
}

struct ServiceInfo {
    master: Arc<Master>,
    slave: Arc<Slave>,
    name: String,
    api: String,
}

impl Interactor for ServiceInfo {
    fn unregister(&mut self) -> Response<()> {
        self.slave.remove_service(&self.name);
        self.master
            .unregister_service(&self.name, &self.api)
            .map(|_| ())
    }
}

trait Interactor {
    fn unregister(&mut self) -> Response<()>;
}

struct InteractorRaii<I: Interactor> {
    pub interactor: I,
}

impl<I: Interactor> InteractorRaii<I> {
    pub fn new(interactor: I) -> InteractorRaii<I> {
        Self { interactor }
    }
}

impl<I: Interactor> Drop for InteractorRaii<I> {
    fn drop(&mut self) {
        if let Err(e) = self.interactor.unregister() {
            error!("Error while unloading: {:?}", e);
        }
    }
}