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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt::Debug;
use std::io::Error as IoError;
use std::mem;
use std::net::{SocketAddr, UdpSocket};
use std::os::unix::net::UnixDatagram;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::thread::{self, JoinHandle};

use log;
use parking_lot::RwLock;
use serde::de::DeserializeOwned;
use serde::Serialize;

use crate::net::{self, Udp, UnixUdp};

// the callback type for passing messages into the callback
type Callback<T> = Box<dyn Send + Fn(T)>;

#[derive(Clone)]
enum SubscriberPort {
    UdpPort(u16),
    UnixDatagram(PathBuf),
}

const PUBSUB_ERROR: &str = "PubSubError";

trait PublishHandler<T>
where
    T: DeserializeOwned + Serialize,
{
    fn name(&self) -> &'static str;

    // Send a message
    fn send(&self, message: &T) -> Result<usize, IoError>;

    // return the UDP socket addr
    fn udp(&self) -> Option<SocketAddr> {
        None
    }

    // return the path of the Unix socket
    fn unix_datagram(&self) -> Option<PathBuf> {
        None
    }
}

// TODO have a publish queue
/// The Publisher publishes messages of type `T` to its subscribers.
pub struct Publisher<T> {
    name: String,
    handlers: HashMap<String, Box<dyn PublishHandler<T>>>,
}

fn sockaddr_string(addr: SocketAddr) -> String {
    format!("{}", addr)
}

fn path_string(path: &Path) -> String {
    format!("{}", path.to_str().unwrap_or(""))
}

impl<T> Publisher<T>
where
    T: DeserializeOwned + Serialize,
{
    /// Create a new publisher with a name.
    ///
    /// Args:
    /// * `name`: The name of the publisher.
    pub fn new(name: &str) -> Publisher<T> {
        Publisher {
            name: String::from(name),
            handlers: HashMap::new(),
        }
    }

    /// Publish a message to the subscribers.
    ///
    /// By default, the publisher has no subscribers. Subscribers must be added before the `publish`
    /// method does anything. Without subscribers, `publish` is a no-op.
    ///
    pub fn publish(&mut self, message: &T) {
        let tmp = mem::take(&mut self.handlers);
        for (key, handler) in tmp.into_iter() {
            if let Err(err) = handler.send(message) {
                log::error!(
                    "Failed to publish to '{}' publisher {} with error: {}",
                    self.name,
                    handler.name(),
                    err
                );
            } else {
                assert!(self.handlers.insert(key, handler).is_none());
            }
        }
    }

    /// Add a UDP endpoint to publish to.
    ///
    /// Args:
    /// * `addr`: The address of the UDP endpoint.
    pub fn add_udp_endpoint(&mut self, addr: SocketAddr) -> bool {
        let addr_str = sockaddr_string(addr);
        if self.handlers.contains_key(&addr_str) {
            log::warn!(
                "Pubsub handler for UDP endpoint already exists: {}",
                addr_str
            );
            return false;
        }
        assert!(self
            .handlers
            .insert(addr_str, Box::new(UdpPublisher { addr }))
            .is_none());
        true
    }

    /// Add a Unix Datagram endpoint to publish to.
    ///
    /// Args:
    /// * `path`: The Unix socket path to publish to.
    pub fn add_unix_datagram_endpoint(&mut self, path: &Path) -> bool {
        let addr_str = path_string(path);
        if self.handlers.contains_key(&addr_str) {
            log::warn!(
                "Pubsub handler for Unix datagram endpoint already exists: {}",
                addr_str
            );
            return false;
        }
        assert!(self
            .handlers
            .insert(addr_str, Box::new(UnixDatagramPublisher::new(path)))
            .is_none());
        true
    }

    /// Return the total number of Publish endpoints.
    pub fn num_endpoints(&self) -> usize {
        self.handlers.len()
    }
}

struct UdpPublisher {
    addr: SocketAddr,
}

impl<T> PublishHandler<T> for UdpPublisher
where
    T: DeserializeOwned + Serialize,
{
    fn name(&self) -> &'static str {
        "UDP"
    }

    fn send(&self, message: &T) -> Result<usize, IoError> {
        // TODO bring this into a new() function
        let udp = UdpSocket::bind("0.0.0.0:0").unwrap();
        let msg_bytes = net::construct_message(message);
        udp.send_to(&msg_bytes, self.addr)
    }

    fn udp(&self) -> Option<SocketAddr> {
        Some(self.addr)
    }
}

struct UnixDatagramPublisher {
    path: PathBuf,
    socket: UnixDatagram,
    path_seen: RefCell<bool>,
}

impl UnixDatagramPublisher {
    fn new(path: &Path) -> UnixDatagramPublisher {
        let socket = UnixDatagram::unbound().unwrap();
        UnixDatagramPublisher {
            path: PathBuf::from(path),
            socket,
            path_seen: RefCell::new(false),
        }
    }
}

impl<T> PublishHandler<T> for UnixDatagramPublisher
where
    T: DeserializeOwned + Serialize,
{
    fn name(&self) -> &'static str {
        "Unix Datagram"
    }

    fn send(&self, message: &T) -> Result<usize, IoError> {
        // sometimes the subscriber endpoint is slow to show up. don't treat it as an error to send
        // when the endpoint doesn't exist yet. That said, if the endpoint did exist, assume it
        // always does exist until there's a fatal OS error writing to it.
        if !(self.path.exists() || *self.path_seen.borrow()) {
            return Ok(0);
        } else if self.path.exists() && !*self.path_seen.borrow() {
            *self.path_seen.borrow_mut() = true;
        }

        let msg_bytes = net::construct_message(message);
        self.socket.send_to(&msg_bytes, &self.path)
    }

    fn unix_datagram(&self) -> Option<PathBuf> {
        Some(self.path.clone())
    }
}

/// The Subscriber receives messages of type `T` and processes them with a callback.
pub struct Subscriber {
    name: String,
    subscribe_port: SubscriberPort,
    stop_requested: Arc<RwLock<bool>>,
    thread: Option<JoinHandle<()>>,
}

impl Subscriber {
    fn new<T>(
        name: &'static str,
        subscribe_port: SubscriberPort,
        callback: Callback<T>,
    ) -> Subscriber
    where
        T: Debug + DeserializeOwned + Serialize + 'static,
    {
        let stop_requested = Arc::new(RwLock::new(false));
        let is_stop_requested = stop_requested.clone();
        let thread_subscribe_port = subscribe_port.clone();

        let thread = thread::spawn(move || {
            let subscriber: Box<dyn SubscribeHandler<T>> = match thread_subscribe_port {
                SubscriberPort::UdpPort(port) => Box::new(UdpSubscriber::new(port)),
                SubscriberPort::UnixDatagram(path) => {
                    Box::new(UnixDatagramSubscriber::new(Path::new(&path)))
                }
            };
            while !*is_stop_requested.read() {
                match subscriber.recv() {
                    Ok(msg) => (*callback)(msg),
                    Err(err) => {
                        log::debug!("recv error on Subscriber '{}' with error:\n{}", name, err)
                    }
                };
            }
        });

        Subscriber {
            name: String::from(name),
            subscribe_port,
            stop_requested,
            thread: Some(thread),
        }
    }

    /// Create a subscriber that listens on a UDP port.
    ///
    /// Args:
    /// * `name` The name to refer to the subscriber.
    /// * `port`: The UDP port to listen for new messages on.
    /// * `callback`: The function to call on incoming data.
    pub fn with_udp_port<T>(name: &'static str, port: u16, callback: Callback<T>) -> Subscriber
    where
        T: Debug + DeserializeOwned + Serialize + 'static,
    {
        Subscriber::new(name, SubscriberPort::UdpPort(port), callback)
    }

    /// Create a subscriber that listens on a Unix Datagram socket.
    ///
    /// Args:
    /// * `name` The name to refer to the subscriber.
    /// * `path`: The unix socket path to bind the server to.
    /// * `callback`: The function to call on incoming data.
    pub fn with_unix_datagram<T>(
        name: &'static str,
        path: &Path,
        callback: Callback<T>,
    ) -> Subscriber
    where
        T: Debug + DeserializeOwned + Serialize + 'static,
    {
        Subscriber::new(
            name,
            SubscriberPort::UnixDatagram(PathBuf::from(path)),
            callback,
        )
    }

    /// Check if the Subscriber is running.
    pub fn is_running(&self) -> bool {
        self.thread.is_some()
    }

    /// Stop the Subscriber
    pub fn stop(&mut self) {
        if self.is_running() {
            log::debug!("Stopping Subscriber: {}", self.name);
            *self.stop_requested.write() = true;
            self.send_stop_signal();
            self.thread.take().unwrap().join().unwrap();
        }
    }

    fn send_stop_signal(&self) {
        match &self.subscribe_port {
            SubscriberPort::UdpPort(port) => self.send_stop_signal_udp(*port),
            SubscriberPort::UnixDatagram(path) => self.send_stop_signal_unix(&path),
        }
    }

    fn send_stop_signal_udp(&self, port: u16) {
        let addr = format!("127.0.0.1:{}", port);
        let mut udp = Udp::new(UdpSocket::bind("0.0.0.0:0").unwrap());
        udp.set_write_addr(addr.parse().unwrap());
        net::write_stop_signal(Ok(udp), &self.name);
    }

    fn send_stop_signal_unix(&self, path: &Path) {
        let mut unix = UnixUdp::new(UnixDatagram::unbound().unwrap());
        unix.set_path(path);
        net::write_stop_signal(Ok(unix), &self.name);
    }
}

impl Drop for Subscriber {
    fn drop(&mut self) {
        self.stop();
    }
}

trait SubscribeHandler<T>
where
    T: DeserializeOwned + Serialize,
{
    fn recv(&self) -> Result<T, IoError>;
}

struct UdpSubscriber {
    udp: RefCell<Option<Udp>>,
}

impl UdpSubscriber {
    fn new(port: u16) -> UdpSubscriber {
        let addr: SocketAddr = format!("0.0.0.0:{}", port).parse().unwrap();
        let listener = UdpSocket::bind(addr).expect(&format!("Cannot bind to UDP port: {}", port));

        UdpSubscriber {
            udp: RefCell::new(Some(Udp::new(listener))),
        }
    }
}

impl<T> SubscribeHandler<T> for UdpSubscriber
where
    T: DeserializeOwned + Serialize,
{
    fn recv(&self) -> Result<T, IoError> {
        let mut udp = self.udp.borrow_mut().take().unwrap();
        let response = net::recv(&mut udp, None, PUBSUB_ERROR, false);
        *self.udp.borrow_mut() = Some(udp);
        response
    }
}

struct UnixDatagramSubscriber {
    unix: RefCell<Option<UnixUdp>>,
}

impl UnixDatagramSubscriber {
    fn new(path: &Path) -> UnixDatagramSubscriber {
        UnixDatagramSubscriber {
            unix: RefCell::new(Some(UnixUdp::new(
                UnixDatagram::bind(path)
                    .expect(&format!("Cannot bind to Unix datagram socket: {:?}", path)),
            ))),
        }
    }
}

impl<T> SubscribeHandler<T> for UnixDatagramSubscriber
where
    T: DeserializeOwned + Serialize,
{
    fn recv(&self) -> Result<T, IoError> {
        let mut unix = self.unix.borrow_mut().take().unwrap();
        let response = net::recv(&mut unix, None, PUBSUB_ERROR, false);
        *self.unix.borrow_mut() = Some(unix);
        response
    }
}

#[cfg(test)]
mod tests {
    use std::io::Write;
    use std::sync::Arc;
    use std::time::{Duration, Instant};

    use parking_lot::Mutex;
    use portpicker;
    use serde::Deserialize;
    use tempfile;

    use super::*;

    fn setup_logging() {
        let _ = env_logger::builder()
            .format(|buf, record| {
                writeln!(
                    buf,
                    "{}:{} [{}] - {}",
                    record.file().unwrap_or("unknown"),
                    record.line().unwrap_or(0),
                    record.level(),
                    record.args()
                )
            })
            .is_test(true)
            .try_init();
    }

    #[test]
    fn start_stop_subscriber_udp() {
        setup_logging();
        let port: u16 = portpicker::pick_unused_port().unwrap();
        let mut subscriber: Subscriber =
            Subscriber::with_udp_port::<i32>("test", port, Box::new(|_| {}));
        assert!(subscriber.is_running());
        subscriber.stop();
        assert!(!subscriber.is_running());
    }

    #[test]
    fn start_stop_subscriber_unix_datagram() {
        setup_logging();
        let tempdir = tempfile::tempdir().unwrap();
        let socket = tempdir.path().join("socket");
        let mut subscriber: Subscriber =
            Subscriber::with_unix_datagram::<i32>("test", &socket, Box::new(|_| {}));
        assert!(subscriber.is_running());
        subscriber.stop();
        assert!(!subscriber.is_running());
    }

    #[test]
    fn one_publisher_many_subscribers() {
        #[derive(Deserialize, Serialize, Debug, Clone)]
        struct TestMessage {
            name: String,
            value: u32,
        }

        impl TestMessage {
            fn eq(&self, other: &TestMessage) -> bool {
                self.name == other.name && self.value == other.value
            }
        }

        let null_message = TestMessage {
            name: String::new(),
            value: 0,
        };

        let test_message = TestMessage {
            name: String::from("test_message"),
            value: 123,
        };

        let mut publisher = Publisher::new("test");

        let udp_port: u16 = portpicker::pick_unused_port().unwrap();
        let udp_sub_msg = Arc::new(Mutex::new(null_message.clone()));
        let udp_msg_clone = Arc::clone(&udp_sub_msg);
        let mut udp_subscriber: Subscriber = Subscriber::with_udp_port::<TestMessage>(
            "test",
            udp_port,
            Box::new(move |msg| {
                let mut data = udp_msg_clone.lock();
                *data = msg.clone();
            }),
        );
        assert!(publisher.add_udp_endpoint(format!("127.0.0.1:{}", udp_port).parse().unwrap()));
        assert!(udp_subscriber.is_running());

        let unix_dgram_sub_msg = Arc::new(Mutex::new(null_message.clone()));
        let unix_dgram_msg_clone = Arc::clone(&unix_dgram_sub_msg);
        let tempdir = tempfile::tempdir().unwrap();
        let socket = tempdir.path().join("socket");
        //let socket = socket.as_path().to_str().unwrap();
        let mut unix_subsciber = Subscriber::with_unix_datagram::<TestMessage>(
            "test",
            &socket,
            Box::new(move |msg| {
                let mut data = unix_dgram_msg_clone.lock();
                *data = msg.clone();
            }),
        );
        assert!(publisher.add_unix_datagram_endpoint(&socket));
        assert!(unix_subsciber.is_running());

        let n_subscribers = 2;

        let start = Instant::now();
        let timeout = Duration::from_secs(5);
        while start.elapsed() < timeout {
            publisher.publish(&test_message);

            let udp_data = udp_sub_msg.lock();
            let unix_dgram_data = unix_dgram_sub_msg.lock();
            if udp_data.eq(&test_message) && unix_dgram_data.eq(&test_message) {
                break;
            }
            if publisher.num_endpoints() != n_subscribers {
                break;
            }

            thread::sleep(Duration::from_millis(10));
        }
        let timed_out = start.elapsed() >= timeout;
        udp_subscriber.stop();
        unix_subsciber.stop();

        assert_eq!(publisher.num_endpoints(), n_subscribers);
        assert!(!timed_out);

        assert!(!udp_subscriber.is_running());
        assert!(!unix_subsciber.is_running());
    }

    #[test]
    fn many_publishers_one_subscriber_udp() {
        let mut publisher = Publisher::new("test");
        let capture = Arc::new(Mutex::new(HashMap::new()));
        let cb_capture_a = Arc::clone(&capture);
        let cb_capture_b = Arc::clone(&capture);

        let udp_port_a: u16 = portpicker::pick_unused_port().unwrap();
        let udp_subscriber_a: Subscriber = Subscriber::with_udp_port::<u32>(
            "test",
            udp_port_a,
            Box::new(move |msg| {
                let mut data = cb_capture_a.lock();
                data.insert(0, msg);
            }),
        );
        assert!(publisher.add_udp_endpoint(format!("127.0.0.1:{}", udp_port_a).parse().unwrap()));

        let udp_port_b: u16 = portpicker::pick_unused_port().unwrap();
        let udp_subscriber_b: Subscriber = Subscriber::with_udp_port::<u32>(
            "test",
            udp_port_b,
            Box::new(move |msg| {
                let mut data = cb_capture_b.lock();
                data.insert(1, msg);
            }),
        );
        assert!(publisher.add_udp_endpoint(format!("127.0.0.1:{}", udp_port_b).parse().unwrap()));

        assert!(udp_subscriber_a.is_running());
        assert!(udp_subscriber_b.is_running());
        let num_endpoints = publisher.num_endpoints();

        let start = Instant::now();
        let timeout = Duration::from_secs(5);
        while start.elapsed() < timeout {
            publisher.publish(&0);
            if capture.lock().len() >= num_endpoints {
                break;
            }

            thread::sleep(Duration::from_millis(10));
        }

        assert!(capture.lock().len() == num_endpoints);
        assert!(start.elapsed() < timeout);
    }

    #[test]
    fn many_publishers_one_subscriber_unix_datagram() {
        let mut publisher = Publisher::new("test");
        let capture = Arc::new(Mutex::new(HashMap::new()));
        let cb_capture_a = Arc::clone(&capture);
        let cb_capture_b = Arc::clone(&capture);

        let tempdir_a = tempfile::tempdir().unwrap();
        let socket_a = tempdir_a.path().join("socket");
        let unix_subscriber_a: Subscriber = Subscriber::with_unix_datagram::<u32>(
            "test",
            &socket_a,
            Box::new(move |msg| {
                let mut data = cb_capture_a.lock();
                data.insert(0, msg);
            }),
        );
        assert!(publisher.add_unix_datagram_endpoint(&socket_a));

        let tempdir_b = tempfile::tempdir().unwrap();
        let socket_b = tempdir_b.path().join("socket");
        let unix_subscriber_b: Subscriber = Subscriber::with_unix_datagram::<u32>(
            "test",
            &socket_b,
            Box::new(move |msg| {
                let mut data = cb_capture_b.lock();
                data.insert(1, msg);
            }),
        );
        assert!(publisher.add_unix_datagram_endpoint(&socket_b));

        assert!(unix_subscriber_a.is_running());
        assert!(unix_subscriber_b.is_running());
        let num_endpoints = publisher.num_endpoints();
        assert_eq!(num_endpoints, 2);

        let start = Instant::now();
        let timeout = Duration::from_secs(5);
        while start.elapsed() < timeout {
            publisher.publish(&0);
            if capture.lock().len() >= num_endpoints {
                break;
            }

            thread::sleep(Duration::from_millis(10));
        }

        assert!(capture.lock().len() == num_endpoints);
        assert!(start.elapsed() < timeout);
    }

    #[test]
    fn no_duplicate_publishers() {
        let mut publisher: Publisher<u32> = Publisher::new("test");

        let udp_port: u16 = portpicker::pick_unused_port().unwrap();
        assert!(publisher.add_udp_endpoint(format!("127.0.0.1:{}", udp_port).parse().unwrap()));
        assert!(!publisher.add_udp_endpoint(format!("127.0.0.1:{}", udp_port).parse().unwrap()));

        let tempdir = tempfile::tempdir().unwrap();
        let socket = tempdir.path().join("socket");
        assert!(publisher.add_unix_datagram_endpoint(&socket));
        assert!(!publisher.add_unix_datagram_endpoint(&socket));

        assert_eq!(publisher.num_endpoints(), 2);
    }
}