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
//!

mod error;
mod config;

pub use crate::error::{Error, Result};
pub use crate::config::Config;
use std::path::Path;
use serde::{Deserialize, Serialize};


pub struct Ctx {
    ctx: zmq::Context,
    cfg: Config,
}

impl Ctx {
    pub fn new<P: AsRef<Path>>(toml_path: P) -> Result<Self> {
        Ok(Self {
            ctx: zmq::Context::new(),
            cfg: Config::parse_toml(toml_path)?,
        })
    }

    pub fn with_config(config: Config) -> Self {
        Self {
            ctx: zmq::Context::new(),
            cfg: config,
        }
    }

    pub fn channel(&self) -> Result<(Source, Sink)> {
        let   sink =   self.sink()?; // NOTE: first initialize the sink ...
        let source = self.source()?; // NOTE: ... and only then the source
        Ok((source, sink))
    }

    fn source(&self) -> Result<Source> {
        let source = Source(self.ctx.socket(zmq::REQ)?);
        source.0.connect(&format!("tcp://{}:{}", self.cfg.host, self.cfg.port))?;
        Ok(source)
    }

    fn sink(&self) -> Result<Sink> {
        let sink = Sink(self.ctx.socket(zmq::REP)?);
        sink.0.bind(&format!("tcp://*:{}", self.cfg.port))?;
        Ok(sink)
    }
}


/// It's a little passive-aggressive, but it'll work.
const ACK: &str = "K";


pub struct Source(zmq::Socket);

impl Source {
    /// Send a value of type `V`.
    /// Return `Ok(())` if the value was sent successfully;
    /// Otherwise return an error.
    pub fn send<V>(&mut self, value: &V) -> Result<()>
    where V: ?Sized + Serialize {
        imp::send(&mut self.0, value)?;
        let reply: String = imp::recv(&mut self.0)?;
        debug_assert_eq!(reply, ACK);
        Ok(())
    }
}


pub struct Sink(zmq::Socket);

impl Sink {
    pub fn recv<V>(&mut self) -> Result<V>
    where V: for<'de> Deserialize<'de> {
        let msg: V = imp::recv(&mut self.0)?;
        imp::send(&mut self.0, ACK)?;
        Ok(msg)
    }
}


mod imp {
    use super::*;

    const NO_FLAGS: i32 = 0;

    #[inline(always)]
    pub(super) fn send<V>(socket: &mut zmq::Socket, value: &V) -> Result<()>
    where V: ?Sized + Serialize {
        let s: String = serde_json::to_string(value)?;
        socket.send(&s, NO_FLAGS)?;
        Ok(())
    }

    #[inline(always)]
    pub(super) fn recv<V>(socket: &mut zmq::Socket) -> Result<V>
    where V: for<'de> Deserialize<'de> {
        match socket.recv_string(NO_FLAGS)? {
            Ok(s) => Ok(serde_json::from_str::<V>(&s)?),
            Err(bytes) => Err(Error::NotUtf8Error(bytes)),
        }
    }
}



#[cfg(test)]
mod tests {
    use crate::error::Result;
    use serde_derive::{Deserialize, Serialize};
    use super::*;

    #[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
    struct Foo(String, usize);

    #[test]
    fn send_and_receive_msg() -> Result<()> {
        let ctx = Ctx::with_config(Config {
            host: "127.0.0.1".to_string(),
            port: 11001, // test-specific port
        });
        let (mut source, mut sink): (Source, Sink) = ctx.channel()?;
        let thread_guard = std::thread::spawn(move || {
            let msg0: String = sink.recv().expect("Sink failed to receive MSG0");
            assert_eq!(msg0, "Hello World! 0");
            let msg1: String = sink.recv().expect("Sink failed to receive MSG1");
            assert_eq!(msg1, "Hello World! 1");
            let msg2: Foo = sink.recv().expect("Sink failed to receive MSG2");
            assert_eq!(msg2, Foo("Hello World! 2".to_string(), 42));
        });
        source.send("Hello World! 0")?;
        source.send("Hello World! 1")?;
        source.send(&Foo("Hello World! 2".to_string(), 42))?;
        thread_guard.join().unwrap();
        Ok(())
    }

    #[test]
    fn multiple_senders() -> Result<()> {
        let ctx = Ctx::with_config(Config {
            host: "127.0.0.1".to_string(),
            port: 11002, // test-specific port
        });
        let (mut source0, mut sink): (Source, Sink) = ctx.channel()?;
        let mut source1 = ctx.source()?;
        let thread_guard = std::thread::spawn(move || {
            let msg0: String = sink.recv().expect("Sink failed to receive MSG0");
            assert_eq!(msg0, "Hello World! 0");
            let msg1: String = sink.recv().expect("Sink failed to receive MSG1");
            assert_eq!(msg1, "Hello World! 1");
            let msg2: Foo = sink.recv().expect("Sink failed to receive MSG2");
            assert_eq!(msg2, Foo("Hello World! 2".to_string(), 42));
        });
        source0.send("Hello World! 0")?;
        source1.send("Hello World! 1")?;
        source0.send(&Foo("Hello World! 2".to_string(), 42))?;
        thread_guard.join().unwrap();
        Ok(())
    }

    #[test]
    fn read_config_file() -> Result<()> {
        let ctx = Ctx::new("ipc-chan.toml")?;
        let cfg = Config::default();
        assert_eq!(ctx.cfg.host, cfg.host);
        assert_eq!(ctx.cfg.port, cfg.port);
        Ok(())
    }

}