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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
use super::super::rosxmlrpc::Response;
use super::clock::{Clock, Rate, RealClock, SimulatedClock};
use super::error::{Error, ErrorKind, Result, ResultExt};
use super::master::{self, Master, Topic};
use super::naming::{self, Resolver};
use super::raii::{Publisher, Service, Subscriber};
use super::resolve;
use super::slave::Slave;
use crate::api::clock::Delay;
use crate::api::handlers::CallbackSubscriptionHandler;
use crate::api::ShutdownManager;
use crate::msg::rosgraph_msgs::{Clock as ClockMsg, Log};
use crate::msg::std_msgs::Header;
use crate::tcpros::{Client, Message, ServicePair, ServiceResult};
use crate::util::FAILED_TO_LOCK;
use crate::{RawMessage, RawMessageDescription, SubscriptionHandler};
use error_chain::bail;
use lazy_static::lazy_static;
use log::error;
use ros_message::{Duration, Time};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::ops::Deref;
use std::sync::Arc;
use std::sync::Mutex;
use std::thread::sleep;
use std::time::Instant;
use xml_rpc;
use yaml_rust::{Yaml, YamlLoader};

pub struct Ros {
    master: Arc<Master>,
    slave: Arc<Slave>,
    hostname: String,
    bind_address: String,
    resolver: Resolver,
    name: String,
    clock: Arc<dyn Clock>,
    static_subs: Vec<Subscriber>,
    logger: Arc<Mutex<Option<Publisher<Log>>>>,
    shutdown_manager: Arc<ShutdownManager>,
}

impl Ros {
    pub fn new(name: &str) -> Result<Ros> {
        let mut namespace = resolve::namespace();
        if !namespace.starts_with('/') {
            namespace = format!("/{}", namespace);
        }
        let master_uri = resolve::master();
        let hostname = resolve::hostname();
        let name = resolve::name(name);
        let mut ros = Ros::new_raw(&master_uri, &hostname, &namespace, &name)?;
        for (src, dest) in resolve::mappings() {
            ros.map(&src, &dest)?;
        }
        for (src, dest) in resolve::params() {
            let data = YamlLoader::load_from_str(&dest)
                .chain_err(|| ErrorKind::BadYamlData(dest.clone()))?
                .into_iter()
                .next()
                .ok_or_else(|| ErrorKind::BadYamlData(dest.clone()))?;
            let param = ros.param(&src).ok_or(ErrorKind::CannotResolveName(src))?;
            param.set_raw(yaml_to_xmlrpc(data)?)?;
        }

        if ros
            .param("/use_sim_time")
            .and_then(|v| v.get().ok())
            .unwrap_or(false)
        {
            let clock = Arc::new(SimulatedClock::default());
            let ros_clock = Arc::clone(&clock);
            let sub = ros
                .subscribe::<ClockMsg, _>("/clock", 1, move |v| clock.trigger(v.clock))
                .chain_err(|| {
                    ErrorKind::CommunicationIssue("Failed to subscribe to simulated clock".into())
                })?;
            ros.static_subs.push(sub);
            ros.clock = ros_clock;
        }

        *ros.logger.lock().unwrap() = Some(ros.publish("/rosout", 100)?);

        Ok(ros)
    }

    fn new_raw(master_uri: &str, hostname: &str, namespace: &str, name: &str) -> Result<Ros> {
        let namespace = namespace.trim_end_matches('/');

        if name.contains('/') {
            bail!(ErrorKind::Naming(
                naming::error::ErrorKind::IllegalCharacter(name.into()),
            ));
        }

        let bind_host = {
            if hostname == "localhost" || hostname.starts_with("127.") {
                hostname
            } else {
                "0.0.0.0"
            }
        };

        let name = format!("{}/{}", namespace, name);
        let resolver = Resolver::new(&name)?;

        let logger = Arc::new(Mutex::new(None));
        let shutdown_manager = Arc::new(ShutdownManager::new({
            let logger = Arc::clone(&logger);
            move || drop(logger.lock().unwrap().take())
        }));

        let slave = Slave::new(
            master_uri,
            hostname,
            bind_host,
            0,
            &name,
            Arc::clone(&shutdown_manager),
        )?;
        let master = Master::new(master_uri, &name, slave.uri())?;

        Ok(Ros {
            master: Arc::new(master),
            slave: Arc::new(slave),
            hostname: String::from(hostname),
            bind_address: String::from(bind_host),
            resolver,
            name,
            clock: Arc::new(RealClock::default()),
            static_subs: Vec::new(),
            logger,
            shutdown_manager,
        })
    }

    fn map(&mut self, source: &str, destination: &str) -> Result<()> {
        self.resolver.map(source, destination).map_err(Into::into)
    }

    #[inline]
    pub fn uri(&self) -> &str {
        self.slave.uri()
    }

    #[inline]
    pub fn name(&self) -> &str {
        &self.name
    }

    #[inline]
    pub fn hostname(&self) -> &str {
        &self.hostname
    }

    #[inline]
    pub fn bind_address(&self) -> &str {
        &self.bind_address
    }

    #[inline]
    pub fn now(&self) -> Time {
        self.clock.now()
    }

    #[inline]
    pub fn delay(&self, d: Duration) -> Delay {
        self.clock.await_init();
        Delay::new(Arc::clone(&self.clock), d)
    }

    #[inline]
    pub fn shutdown_sender(&self) -> Arc<ShutdownManager> {
        Arc::clone(&self.shutdown_manager)
    }

    pub fn rate(&self, rate: f64) -> Rate {
        self.clock.await_init();
        let nanos = 1_000_000_000.0 / rate;
        Rate::new(Arc::clone(&self.clock), Duration::from_nanos(nanos as i64))
    }

    #[inline]
    pub fn is_ok(&self) -> bool {
        !self.shutdown_manager.awaiting_shutdown()
    }

    #[inline]
    pub fn spin(&self) -> Spinner {
        Spinner {
            shutdown_manager: Arc::clone(&self.shutdown_manager),
        }
    }

    pub fn param(&self, name: &str) -> Option<Parameter> {
        self.resolver.translate(name).ok().map(|v| Parameter {
            master: Arc::clone(&self.master),
            name: v,
        })
    }

    pub fn parameters(&self) -> Response<Vec<String>> {
        self.master.get_param_names()
    }

    pub fn state(&self) -> Response<master::SystemState> {
        self.master.get_system_state().map(Into::into)
    }

    pub fn topics(&self) -> Response<Vec<Topic>> {
        self.master
            .get_topic_types()
            .map(|v| v.into_iter().map(Into::into).collect())
    }

    pub fn client<T: ServicePair>(&self, service: &str) -> Result<Client<T>> {
        let name = self.resolver.translate(service)?;
        Ok(Client::new(Arc::clone(&self.master), &self.name, &name))
    }

    pub fn wait_for_service(
        &self,
        service: &str,
        timeout: Option<std::time::Duration>,
    ) -> Result<()> {
        let timeout = timeout.map(|v| std::time::Instant::now() + v);
        let client = self.client::<RawMessage>(service)?;

        loop {
            let iteration_limit = std::time::Duration::from_secs(10);
            let leftover_timeout = match timeout {
                Some(t) => t
                    .checked_duration_since(Instant::now())
                    .ok_or_else(|| Error::from(ErrorKind::TimeoutError))?,
                None => iteration_limit,
            }
            .min(iteration_limit);
            if client.probe(leftover_timeout).is_ok() {
                return Ok(());
            }
            sleep(std::time::Duration::from_millis(100));
        }
    }

    pub fn service<T, F>(&self, service: &str, handler: F) -> Result<Service>
    where
        T: ServicePair,
        F: Fn(T::Request) -> ServiceResult<T::Response> + Send + Sync + 'static,
    {
        let name = self.resolver.translate(service)?;
        Service::new::<T, F>(
            Arc::clone(&self.master),
            Arc::clone(&self.slave),
            &self.hostname,
            &self.bind_address,
            &name,
            handler,
        )
    }

    #[inline]
    pub fn subscribe<T, F>(&self, topic: &str, queue_size: usize, callback: F) -> Result<Subscriber>
    where
        T: Message,
        F: Fn(T) + Send + 'static,
    {
        self.subscribe_with_ids(topic, queue_size, move |data, _| callback(data))
    }

    pub fn subscribe_with_ids<T, F>(
        &self,
        topic: &str,
        queue_size: usize,
        callback: F,
    ) -> Result<Subscriber>
    where
        T: Message,
        F: Fn(T, &str) + Send + 'static,
    {
        self.subscribe_with_ids_and_headers(
            topic,
            queue_size,
            callback,
            |_: HashMap<String, String>| (),
        )
    }

    pub fn subscribe_with_ids_and_headers<T, F, G>(
        &self,
        topic: &str,
        mut queue_size: usize,
        on_message: F,
        on_connect: G,
    ) -> Result<Subscriber>
    where
        T: Message,
        F: Fn(T, &str) + Send + 'static,
        G: Fn(HashMap<String, String>) + Send + 'static,
    {
        if queue_size == 0 {
            queue_size = usize::max_value();
        }
        let name = self.resolver.translate(topic)?;
        Subscriber::new::<T, _>(
            Arc::clone(&self.master),
            Arc::clone(&self.slave),
            &name,
            queue_size,
            CallbackSubscriptionHandler::new(on_message, on_connect),
        )
    }

    pub fn subscribe_with<T, H>(
        &self,
        topic: &str,
        mut queue_size: usize,
        handler: H,
    ) -> Result<Subscriber>
    where
        T: Message,
        H: SubscriptionHandler<T>,
    {
        if queue_size == 0 {
            queue_size = usize::max_value();
        }
        let name = self.resolver.translate(topic)?;
        Subscriber::new::<T, H>(
            Arc::clone(&self.master),
            Arc::clone(&self.slave),
            &name,
            queue_size,
            handler,
        )
    }

    pub fn publish<T>(&self, topic: &str, queue_size: usize) -> Result<Publisher<T>>
    where
        T: Message,
    {
        self.publish_common(topic, queue_size, None)
    }

    pub fn publish_with_description<T>(
        &self,
        topic: &str,
        queue_size: usize,
        message_description: RawMessageDescription,
    ) -> Result<Publisher<T>>
    where
        T: Message,
    {
        self.publish_common(topic, queue_size, Some(message_description))
    }

    fn publish_common<T>(
        &self,
        topic: &str,
        mut queue_size: usize,
        message_description: Option<RawMessageDescription>,
    ) -> Result<Publisher<T>>
    where
        T: Message,
    {
        if queue_size == 0 {
            queue_size = usize::max_value();
        }
        let name = self.resolver.translate(topic)?;
        Publisher::new(
            Arc::clone(&self.master),
            Arc::clone(&self.slave),
            Arc::clone(&self.clock),
            &self.bind_address,
            &name,
            queue_size,
            message_description,
        )
    }

    fn log_to_terminal(&self, level: i8, msg: &str, file: &str, line: u32) {
        use colored::{Color, Colorize};

        let format_string =
            |prefix, color| format!("[{} @ {}:{}]: {}", prefix, file, line, msg).color(color);

        match level {
            Log::DEBUG => println!("{}", format_string("DEBUG", Color::White)),
            Log::INFO => println!("{}", format_string("INFO", Color::White)),
            Log::WARN => eprintln!("{}", format_string("WARN", Color::Yellow)),
            Log::ERROR => eprintln!("{}", format_string("ERROR", Color::Red)),
            Log::FATAL => eprintln!("{}", format_string("FATAL", Color::Red)),
            _ => {}
        }
    }

    pub fn log(&self, level: i8, msg: String, file: &str, line: u32) {
        self.log_to_terminal(level, &msg, file, line);
        let topics = self.slave.publications.get_topic_names();
        let message = Log {
            header: Header::default(),
            level,
            msg,
            name: self.name.clone(),
            line,
            file: file.into(),
            function: String::default(),
            topics,
        };
        let maybe_logger = self.logger.lock().unwrap();
        if let Some(logger) = maybe_logger.deref() {
            if let Err(err) = logger.send(message) {
                error!("Logging error: {}", err);
            }
        }
    }

    pub fn log_once(&self, level: i8, msg: String, file: &str, line: u32) {
        lazy_static! {
            static ref UNIQUE_LOGS: Mutex<HashSet<String>> = Mutex::new(HashSet::new());
        }
        let key = format!("{}:{}", file, line);
        let mut unique_logs = UNIQUE_LOGS.lock().expect(FAILED_TO_LOCK);
        if !unique_logs.contains(&key) {
            unique_logs.insert(key);
            self.log(level, msg, file, line);
        }
    }

    pub fn log_throttle(&self, period: f64, level: i8, msg: String, file: &str, line: u32) {
        lazy_static! {
            static ref PERIODIC_LOGS: Mutex<HashMap<String, Time>> = Mutex::new(HashMap::new());
        }
        let now = self.now();
        let key = format!("{}:{}", file, line);
        let get_next_log_time = |now, period| now + Duration::from_nanos((period * 1e9) as i64);
        let mut period_logs = PERIODIC_LOGS.lock().expect(FAILED_TO_LOCK);
        match period_logs.get_mut(&key) {
            Some(next_log_time) => {
                if now >= *next_log_time {
                    *next_log_time = get_next_log_time(*next_log_time, period);
                    self.log(level, msg, file, line);
                }
            }
            None => {
                period_logs.insert(key, get_next_log_time(now, period));
                self.log(level, msg, file, line);
            }
        }
    }

    pub fn log_throttle_identical(
        &self,
        period: f64,
        level: i8,
        msg: String,
        file: &str,
        line: u32,
    ) {
        lazy_static! {
            static ref IDENTICAL_LOGS: Mutex<HashMap<String, (Time, String)>> =
                Mutex::new(HashMap::new());
        }
        let now = self.now();
        let key = format!("{}:{}", file, line);
        let get_next_log_time = |now, period| now + Duration::from_nanos((period * 1e9) as i64);
        let mut identical_logs = IDENTICAL_LOGS.lock().expect(FAILED_TO_LOCK);
        match identical_logs.get_mut(&key) {
            Some((next_log_time, previous_msg)) => {
                if &msg != previous_msg {
                    *previous_msg = msg.clone();
                    *next_log_time = get_next_log_time(now, period);
                    self.log(level, msg, file, line);
                } else if now >= *next_log_time {
                    *next_log_time = get_next_log_time(*next_log_time, period);
                    self.log(level, msg, file, line);
                }
            }
            None => {
                identical_logs.insert(key, (get_next_log_time(now, period), msg.clone()));
                self.log(level, msg, file, line);
            }
        }
    }
}

pub struct Parameter {
    master: Arc<Master>,
    name: String,
}

impl Parameter {
    pub fn name(&self) -> &str {
        &self.name
    }

    pub fn get<'b, T: Deserialize<'b>>(&self) -> Response<T> {
        self.master.get_param::<T>(&self.name)
    }

    pub fn get_raw(&self) -> Response<xml_rpc::Value> {
        self.master.get_param_any(&self.name)
    }

    pub fn set<T: Serialize>(&self, value: &T) -> Response<()> {
        self.master.set_param::<T>(&self.name, value).and(Ok(()))
    }

    pub fn set_raw(&self, value: xml_rpc::Value) -> Response<()> {
        self.master.set_param_any(&self.name, value).and(Ok(()))
    }

    pub fn delete(&self) -> Response<()> {
        self.master.delete_param(&self.name).and(Ok(()))
    }

    pub fn exists(&self) -> Response<bool> {
        self.master.has_param(&self.name)
    }

    pub fn search(&self) -> Response<String> {
        self.master.search_param(&self.name)
    }
}

fn yaml_to_xmlrpc(val: Yaml) -> Result<xml_rpc::Value> {
    Ok(match val {
        Yaml::Real(v) => xml_rpc::Value::Double(
            v.parse()
                .chain_err(|| ErrorKind::BadYamlData("Failed to parse float".into()))?,
        ),
        Yaml::Integer(v) => xml_rpc::Value::Int(v as i32),
        Yaml::String(v) => xml_rpc::Value::String(v),
        Yaml::Boolean(v) => xml_rpc::Value::Bool(v),
        Yaml::Array(v) => {
            xml_rpc::Value::Array(v.into_iter().map(yaml_to_xmlrpc).collect::<Result<_>>()?)
        }
        Yaml::Hash(v) => xml_rpc::Value::Struct(
            v.into_iter()
                .map(|(k, v)| Ok((yaml_to_string(k)?, yaml_to_xmlrpc(v)?)))
                .collect::<Result<_>>()?,
        ),
        Yaml::Alias(_) => bail!(ErrorKind::BadYamlData("Alias is not supported".into())),
        Yaml::Null => bail!(ErrorKind::BadYamlData("Illegal null value".into())),
        Yaml::BadValue => bail!(ErrorKind::BadYamlData("Bad value provided".into())),
    })
}

fn yaml_to_string(val: Yaml) -> Result<String> {
    Ok(match val {
        Yaml::Real(v) | Yaml::String(v) => v,
        Yaml::Integer(v) => v.to_string(),
        Yaml::Boolean(true) => "true".into(),
        Yaml::Boolean(false) => "false".into(),
        _ => bail!(ErrorKind::BadYamlData(
            "Hash keys need to be strings".into()
        )),
    })
}

pub struct Spinner {
    shutdown_manager: Arc<ShutdownManager>,
}

impl Drop for Spinner {
    fn drop(&mut self) {
        while !self.shutdown_manager.awaiting_shutdown() {
            sleep(std::time::Duration::from_millis(100));
        }
    }
}