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
use crate::{
    addr::Endpoint, auth::*, core::*, error::*, Ctx, CtxHandle, Group,
    GroupSlice,
};
use libzmq_sys as sys;
use sys::errno;

use serde::{Deserialize, Serialize};

use std::{
    ffi::c_void,
    str,
    sync::{Arc, Mutex},
};

fn join(socket_mut_ptr: *mut c_void, group: &GroupSlice) -> Result<(), Error> {
    let rc =
        unsafe { sys::zmq_join(socket_mut_ptr, group.as_c_str().as_ptr()) };

    if rc == -1 {
        let errno = unsafe { sys::zmq_errno() };
        let err = match errno {
            errno::EINVAL => {
                Error::new(ErrorKind::InvalidInput("cannot join group twice"))
            }
            errno::ETERM => Error::new(ErrorKind::InvalidCtx),
            errno::EINTR => Error::new(ErrorKind::Interrupted),
            errno::ENOTSOCK => panic!("invalid socket"),
            errno::EMTHREAD => panic!("no i/o thread available"),
            _ => panic!(msg_from_errno(errno)),
        };

        Err(err)
    } else {
        Ok(())
    }
}

fn leave(socket_mut_ptr: *mut c_void, group: &GroupSlice) -> Result<(), Error> {
    let rc =
        unsafe { sys::zmq_leave(socket_mut_ptr, group.as_c_str().as_ptr()) };

    if rc == -1 {
        let errno = unsafe { sys::zmq_errno() };
        let err = match errno {
            errno::EINVAL => Error::new(ErrorKind::InvalidInput(
                "cannot leave a group that wasn't joined",
            )),
            errno::ETERM => Error::new(ErrorKind::InvalidCtx),
            errno::EINTR => Error::new(ErrorKind::Interrupted),
            errno::ENOTSOCK => panic!("invalid socket"),
            errno::EMTHREAD => panic!("no i/o thread available"),
            _ => panic!(msg_from_errno(errno)),
        };

        Err(err)
    } else {
        Ok(())
    }
}

/// A `Dish` socket is used by a subscriber to subscribe to groups distributed
/// by a [`Radio`].
///
/// Initially a ZMQ_DISH socket is not subscribed to any groups, use [`join`]
/// to join a group.
///
/// # Summary of Characteristics
/// | Characteristic            | Value          |
/// |:-------------------------:|:--------------:|
/// | Compatible peer sockets   | [`Radio`]      |
/// | Direction                 | Unidirectional |
/// | Send/receive pattern      | Receive only   |
/// | Incoming routing strategy | Fair-queued    |
/// | Outgoing routing strategy | N/A            |
///
/// # Example
/// ```
/// # fn main() -> Result<(), anyhow::Error> {
/// use libzmq::{prelude::*, *};
/// use std::{thread, time::Duration};
///
/// let addr: TcpAddr = "127.0.0.1:*".try_into()?;
///
/// let radio = RadioBuilder::new()
///     .bind(addr)
///     .build()?;
///
/// let bound = radio.last_endpoint()?;
/// let a: Group = "group a".try_into()?;
///
/// let dish = DishBuilder::new()
///     .connect(bound)
///     .join(&a)
///     .build()?;
///
/// // Start the feed. It has no conceptual start nor end, thus we
/// // don't synchronize with the subscribers.
/// thread::spawn(move || {
///     let a: Group = "group a".try_into().unwrap();
///     let b: Group = "group b".try_into().unwrap();
///     let mut count = 0;
///     loop {
///         let mut msg = Msg::new();
///         // Alternate between the two groups.
///         let group = if count % 2 == 0 {
///             &a
///         } else {
///             &b
///         };
///
///         radio.transmit(msg, group).unwrap();
///
///         thread::sleep(Duration::from_millis(1));
///         count += 1;
///     }
/// });
///
/// // The dish exclusively receives messages from the groups it joined.
/// let msg = dish.recv_msg()?;
/// assert_eq!(msg.group().unwrap(), &a);
///
/// let msg = dish.recv_msg()?;
/// assert_eq!(msg.group().unwrap(), &a);
/// #
/// #     Ok(())
/// # }
/// ```
///
/// [`Radio`]: struct.Radio.html
/// [`join`]: #method.join
#[derive(Debug, Clone)]
pub struct Dish {
    inner: Arc<RawSocket>,
    groups: Arc<Mutex<Vec<Group>>>,
}

impl Dish {
    /// Create a `Dish` socket from the [`global context`]
    ///
    /// # Returned Error Variants
    /// * [`InvalidCtx`]
    /// * [`SocketLimit`]
    ///
    /// [`InvalidCtx`]: enum.ErrorKind.html#variant.InvalidCtx
    /// [`SocketLimit`]: enum.ErrorKind.html#variant.SocketLimit
    /// [`global context`]: struct.Ctx.html#method.global
    pub fn new() -> Result<Self, Error> {
        let inner = Arc::new(RawSocket::new(RawSocketType::Dish)?);

        Ok(Self {
            inner,
            groups: Arc::default(),
        })
    }

    /// Create a `Dish` socket associated with a specific context
    /// from a `CtxHandle`.
    ///
    /// # Returned Error Variants
    /// * [`InvalidCtx`]
    /// * [`SocketLimit`]
    ///
    /// [`InvalidCtx`]: enum.ErrorKind.html#variant.InvalidCtx
    /// [`SocketLimit`]: enum.ErrorKind.html#variant.SocketLimit
    pub fn with_ctx(handle: CtxHandle) -> Result<Self, Error> {
        let inner = Arc::new(RawSocket::with_ctx(RawSocketType::Dish, handle)?);

        Ok(Self {
            inner,
            groups: Arc::default(),
        })
    }

    /// Returns a reference to the context of the socket.
    pub fn ctx(&self) -> CtxHandle {
        self.inner.ctx()
    }
    /// Joins the specified group.
    ///
    /// # Usage Contract
    /// * A group can be joined at most once.
    ///
    /// # Returned Error Variants
    /// * [`InvalidCtx`]
    /// * [`Interrupted`]
    /// * [`InvalidInput`] (if group was already joined)
    ///
    /// # Example
    /// ```
    /// # fn main() -> Result<(), anyhow::Error> {
    /// use libzmq::{prelude::*, Dish, Group};
    ///
    /// let group: Group = "some group".try_into()?;
    /// let dish = Dish::new()?;
    /// dish.join(group)?;
    /// #
    /// #     Ok(())
    /// # }
    /// ```
    ///
    /// [`InvalidCtx`]: enum.ErrorKind.html#variant.InvalidCtx
    /// [`Interrupted`]: enum.ErrorKind.html#variant.Interrupted
    /// [`InvalidInput`]: enum.ErrorKind.html#variant.InvalidInput
    pub fn join<G>(&self, group: G) -> Result<(), Error>
    where
        G: Into<Group>,
    {
        let mut guard = self.groups.lock().unwrap();
        let group = group.into();
        join(self.raw_socket().as_mut_ptr(), &group)?;
        guard.push(group);
        Ok(())
    }

    /// Returns a snapshot of the list of joined groups.
    ///
    /// The list might be modified by another thread after it is returned.
    ///
    /// # Example
    /// ```
    /// # fn main() -> Result<(), anyhow::Error> {
    /// use libzmq::{prelude::*, Dish, Group};
    ///
    /// let first: Group = "group name".try_into()?;
    ///
    /// let dish = Dish::new()?;
    /// assert!(dish.joined().is_empty());
    ///
    /// dish.join(first)?;
    /// assert_eq!(dish.joined().len(), 1);
    /// #
    /// #     Ok(())
    /// # }
    /// ```
    pub fn joined(&self) -> Vec<Group> {
        self.groups.lock().unwrap().to_owned()
    }

    /// Leave the specified group.
    ///
    /// # Usage Contract
    /// * The group must be already joined.
    ///
    /// # Returned Error Variants
    /// * [`InvalidCtx`]
    /// * [`Interrupted`]
    /// * [`InvalidInput`] (if group not already joined)
    ///
    /// # Example
    /// ```
    /// # fn main() -> Result<(), anyhow::Error> {
    /// use libzmq::{prelude::*, Dish, Group};
    ///
    /// let group: Group = "some group".to_owned().try_into()?;
    ///
    /// let dish = Dish::new()?;
    /// assert!(dish.joined().is_empty());
    ///
    /// dish.join(&group)?;
    /// assert_eq!(dish.joined().len(), 1);
    ///
    /// dish.leave(&group)?;
    /// assert!(dish.joined().is_empty());
    /// #
    /// #     Ok(())
    /// # }
    /// ```
    /// [`InvalidCtx`]: enum.ErrorKind.html#variant.InvalidCtx
    /// [`Interrupted`]: enum.ErrorKind.html#variant.Interrupted
    /// [`InvalidInput`]: enum.ErrorKind.html#variant.InvalidInput
    pub fn leave<G>(&self, group: G) -> Result<(), Error>
    where
        G: AsRef<GroupSlice>,
    {
        let mut guard = self.groups.lock().unwrap();
        let group = group.as_ref();

        leave(self.raw_socket().as_mut_ptr(), group)?;

        let position = guard.iter().position(|g| g == group).unwrap();
        guard.remove(position);
        Ok(())
    }
}

impl PartialEq for Dish {
    fn eq(&self, other: &Dish) -> bool {
        self.inner == other.inner
    }
}

impl Eq for Dish {}

impl GetRawSocket for Dish {
    fn raw_socket(&self) -> &RawSocket {
        &self.inner
    }
}

impl Socket for Dish {}
impl RecvMsg for Dish {}

unsafe impl Send for Dish {}
unsafe impl Sync for Dish {}

/// A configuration for a `Dish`.
///
/// Especially helpfull in config files.
// We can't derive and use #[serde(flatten)] because of this issue:
// https://github.com/serde-rs/serde/issues/1346
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(from = "FlatDishConfig")]
#[serde(into = "FlatDishConfig")]
pub struct DishConfig {
    socket_config: SocketConfig,
    recv_config: RecvConfig,
    groups: Option<Vec<Group>>,
}

impl DishConfig {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn build(&self) -> Result<Dish, Error> {
        self.with_ctx(Ctx::global())
    }

    pub fn with_ctx(&self, handle: CtxHandle) -> Result<Dish, Error> {
        let dish = Dish::with_ctx(handle)?;
        self.apply(&dish)?;

        Ok(dish)
    }

    pub fn groups(&self) -> Option<&[Group]> {
        self.groups.as_deref()
    }

    pub fn set_groups<I>(&mut self, maybe_groups: Option<I>)
    where
        I: IntoIterator<Item = Group>,
    {
        let groups = maybe_groups.map(|g| g.into_iter().collect());
        self.groups = groups;
    }

    pub fn apply(&self, dish: &Dish) -> Result<(), Error> {
        if let Some(ref groups) = self.groups {
            for group in groups {
                dish.join(group)?;
            }
        }
        self.recv_config.apply(dish)?;
        self.socket_config.apply(dish)?;

        Ok(())
    }
}

#[derive(Clone, Serialize, Deserialize)]
struct FlatDishConfig {
    connect: Option<Vec<Endpoint>>,
    bind: Option<Vec<Endpoint>>,
    recv_hwm: HighWaterMark,
    recv_timeout: Period,
    groups: Option<Vec<Group>>,
    mechanism: Option<Mechanism>,
}

impl From<DishConfig> for FlatDishConfig {
    fn from(config: DishConfig) -> Self {
        let socket_config = config.socket_config;
        let recv_config = config.recv_config;
        Self {
            connect: socket_config.connect,
            bind: socket_config.bind,
            mechanism: socket_config.mechanism,
            recv_hwm: recv_config.recv_hwm,
            recv_timeout: recv_config.recv_timeout,
            groups: config.groups,
        }
    }
}

impl From<FlatDishConfig> for DishConfig {
    fn from(flat: FlatDishConfig) -> Self {
        let socket_config = SocketConfig {
            connect: flat.connect,
            bind: flat.bind,
            mechanism: flat.mechanism,
        };
        let recv_config = RecvConfig {
            recv_hwm: flat.recv_hwm,
            recv_timeout: flat.recv_timeout,
        };
        Self {
            socket_config,
            recv_config,
            groups: flat.groups,
        }
    }
}
impl GetSocketConfig for DishConfig {
    fn socket_config(&self) -> &SocketConfig {
        &self.socket_config
    }

    fn socket_config_mut(&mut self) -> &mut SocketConfig {
        &mut self.socket_config
    }
}

impl ConfigureSocket for DishConfig {}

impl GetRecvConfig for DishConfig {
    fn recv_config(&self) -> &RecvConfig {
        &self.recv_config
    }

    fn recv_config_mut(&mut self) -> &mut RecvConfig {
        &mut self.recv_config
    }
}

impl ConfigureRecv for DishConfig {}

/// A builder for a `Dish`.
///
/// Allows for ergonomic one line socket configuration.
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct DishBuilder {
    inner: DishConfig,
}

impl DishBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn build(&self) -> Result<Dish, Error> {
        self.inner.build()
    }

    pub fn with_ctx(&self, handle: CtxHandle) -> Result<Dish, Error> {
        self.inner.with_ctx(handle)
    }

    pub fn join<I, G>(&mut self, groups: I) -> &mut Self
    where
        I: IntoIterator<Item = G>,
        G: Into<Group>,
    {
        let groups: Vec<Group> = groups.into_iter().map(G::into).collect();
        self.inner.set_groups(Some(groups));
        self
    }
}

impl GetSocketConfig for DishBuilder {
    fn socket_config(&self) -> &SocketConfig {
        self.inner.socket_config()
    }

    fn socket_config_mut(&mut self) -> &mut SocketConfig {
        self.inner.socket_config_mut()
    }
}

impl BuildSocket for DishBuilder {}

impl GetRecvConfig for DishBuilder {
    fn recv_config(&self) -> &RecvConfig {
        self.inner.recv_config()
    }

    fn recv_config_mut(&mut self) -> &mut RecvConfig {
        self.inner.recv_config_mut()
    }
}

impl BuildRecv for DishBuilder {}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_ser_de() {
        let config = DishConfig::new();

        let ron = serde_yaml::to_string(&config).unwrap();
        let de: DishConfig = serde_yaml::from_str(&ron).unwrap();
        assert_eq!(config, de);
    }

    #[test]
    fn test_dish() {
        use crate::{prelude::*, TcpAddr, *};
        use std::{thread, time::Duration};

        let addr: TcpAddr = "127.0.0.1:*".try_into().unwrap();

        let radio = RadioBuilder::new().bind(addr).build().unwrap();

        let bound = radio.last_endpoint().unwrap();
        let a: Group = "group a".try_into().unwrap();

        let dish = DishBuilder::new().connect(bound).join(&a).build().unwrap();

        // Start the feed. It has no conceptual start nor end, thus we
        // don't synchronize with the subscribers.
        thread::spawn(move || {
            let a: Group = "group a".try_into().unwrap();
            let b: Group = "group b".try_into().unwrap();
            let mut count = 0;
            loop {
                let mut msg = Msg::new();
                // Alternate between the two groups.
                let group = if count % 2 == 0 { &a } else { &b };

                msg.set_group(group);
                radio.send(msg).unwrap();

                std::thread::sleep(Duration::from_millis(1));
                count += 1;
            }
        });

        // The dish exclusively receives messages from the groups it joined.
        let msg = dish.recv_msg().unwrap();
        assert_eq!(msg.group().unwrap(), &a);

        let msg = dish.recv_msg().unwrap();
        assert_eq!(msg.group().unwrap(), &a);
    }
}