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
//! Crypto-bank pub-sub module publisher.

use crossbeam::channel::Receiver;

use cxmr_tectonic::{Command, SerializedCommand};

use super::Error;

/// Starts synchronous ZeroMQ publisher.
pub fn start_publisher(addr: String, rx: Receiver<Command>) -> Result<(), Error> {
    let context = zmq::Context::new();

    // Connect to task ventilator
    let publisher = context.socket(zmq::PUB)?;
    publisher.bind(&addr)?;

    for cmd in rx.iter() {
        match cmd.serialize()? {
            SerializedCommand::Single(msg) => publisher.send(&msg, 0)?,
            SerializedCommand::Many(msgs) => {
                for msg in msgs {
                    publisher.send(&msg, 0)?;
                }
            }
        }
    }

    Ok(())
}