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
//! This module represents a holochain application's inter-process-communication connection to an external p2p process.

use std::collections::{hash_map::Entry, HashMap};

use rmp_serde;
use serde;

use errors::*;
use message::*;
use msg_types::*;
use socket::{IpcSocket, ZmqIpcSocket};
use util::*;

/// A closure callback type def for getting acknowledgment when performing a `call`.
pub type CallResult = Box<FnMut(Result<Vec<u8>>) -> Result<()> + Send>;

/// IPC communication client structure. Allows connection to an external process that manages p2p communications.
///
/// This struct takes an abstract socket type mainly to facilitate unit testing. You will mainly instantiate the exported ZmqIpcClient type definition.
pub struct IpcClient<S: IpcSocket> {
    socket: Box<S>,
    next_id: u64,
    call_callbacks: HashMap<Vec<u8>, (f64, CallResult)>,
}

impl<S: IpcSocket> IpcClient<S> {
    /// Perform any underlying socket library cleanup. Call this before your application exits.
    pub fn destroy_context() -> Result<()> {
        S::destroy_context()?;
        Ok(())
    }

    /// Get a new IpcClient instance.
    pub fn new() -> Result<Self> {
        Ok(Self {
            socket: S::new()?,
            next_id: 0,
            call_callbacks: HashMap::new(),
        })
    }

    /// Close this specific IpcClient connection.
    pub fn close(mut self) -> Result<()> {
        self.socket.close()?;
        self.call_callbacks.clear();
        Ok(())
    }

    /// Connect this IpcClient to a p2p ipc server.
    pub fn connect(&mut self, endpoint: &str) -> Result<()> {
        let connect_start = get_millis();

        let mut wait_backoff: i64 = 1;

        self.socket.connect(endpoint)?;
        loop {
            if get_millis() - connect_start > 1000.0 {
                return Err(IpcError::Timeout.into());
            }

            println!("sending ping");
            self.ping()?;

            match self.process(wait_backoff)? {
                Some(msg) => match msg {
                    Message::Pong(pong) => {
                        println!(
                            "got pong: toServerMs: {}, roundTripMs: {}",
                            (pong.1 - pong.0).round() as i64,
                            (get_millis() - pong.0).round() as i64
                        );
                        break;
                    }
                    _ => {
                        panic!("cannot handle non-pongs during connect");
                    }
                },
                None => {
                    wait_backoff *= 2;
                    continue;
                }
            }
        }
        Ok(())
    }

    /// Send a heartbeat message to the ipc server.
    pub fn ping(&mut self) -> Result<()> {
        let snd = MsgPingSend(get_millis());
        self.priv_send(MSG_PING, &snd)?;
        Ok(())
    }

    /// invoke an RPC-style `call` on the ipc server
    pub fn call(&mut self, data: &[u8], cb: Option<CallResult>) -> Result<()> {
        let id = self.priv_get_id()?;
        if let Some(cb) = cb {
            self.call_callbacks.insert(id.clone(), (get_millis(), cb));
        }
        let snd = MsgCallSend(&id, data);
        self.priv_send(MSG_CALL, &snd)?;
        Ok(())
    }

    /// respond to a remote `call`
    pub fn respond(&mut self, message_id: &[u8], data: Result<&[u8]>) -> Result<()> {
        match data {
            Err(e) => {
                let e = format!("{}", e);
                let snd = MsgCallFailSend(message_id, e.as_bytes());
                self.priv_send(MSG_CALL_FAIL, &snd)?;
            }
            Ok(d) => {
                let snd = MsgCallOkSend(message_id, d);
                self.priv_send(MSG_CALL_OK, &snd)?;
            }
        }
        Ok(())
    }

    /// Allow IPC client to do any needed processing.
    /// This should be called regularly to make sure any maintenance tasks are executed properly, and to avoid incoming data backing up in memory.
    ///
    /// If there are no incoming messages waiting in the queue, `millis` indicates how long we should block waiting for one. It is perfectly valid to pass in `0` for `millis`.
    pub fn process(&mut self, millis: i64) -> Result<Option<Message>> {
        if !self.socket.poll(millis)? {
            return Ok(None);
        }

        // we have data, let's fetch it
        let res = self.socket.recv()?;
        if res.len() != 3 {
            bail_generic!("bad msg len: {}", res.len());
        }

        let (t, msg) = res[2].split_first().ok_or(IpcError::MissingDataError)?;
        match *t {
            MSG_PONG => {
                let pong: MsgPongRecv = rmp_serde::from_slice(msg)?;
                return Ok(Some(Message::Pong(pong)));
            }
            MSG_CALL => {
                let call: MsgCallRecv = rmp_serde::from_slice(msg)?;
                println!("got call: {:?}", call);
                return Ok(Some(Message::Call(call)));
            }
            MSG_CALL_OK => {
                let resp: MsgCallOkRecv = rmp_serde::from_slice(msg)?;
                if let Entry::Occupied(mut e) = self.call_callbacks.entry(resp.0.clone()) {
                    e.get_mut().1(Ok(resp.1.clone()))?;
                    e.remove();
                }
                return Ok(Some(Message::CallOk(resp)));
            }
            MSG_CALL_FAIL => {
                let resp: MsgCallFailRecv = rmp_serde::from_slice(msg)?;
                let id = resp.0;
                let resp = String::from_utf8_lossy(&resp.1).to_string();
                let resp = IpcError::GenericError { error: resp };
                if let Entry::Occupied(mut e) = self.call_callbacks.entry(id.clone()) {
                    e.get_mut().1(Err(resp.clone().into()))?;
                    e.remove();
                }
                return Err(resp.into());
            }
            _ => panic!("unexpected message type: 0x{:x}", t),
        }
    }

    // -- private -- //

    /// message ids in this module are just sequential u64-s
    fn priv_get_id(&mut self) -> Result<Vec<u8>> {
        self.next_id += 1;
        return Ok(rmp_serde::to_vec(&(self.next_id - 1))?);
    }

    /// send a raw message to the ipc server
    fn priv_send<T>(&mut self, t: u8, data: &T) -> Result<()>
    where
        T: serde::Serialize,
    {
        let mut data = rmp_serde::to_vec(data)?;
        data.insert(0, t);
        // with two zmq "ROUTER" sockets, one side must have a well-known id
        // for the holochain ipc protocol, the server is always 4 0x24 bytes
        self.socket.send(&[&[0x24, 0x24, 0x24, 0x24], &[], &data])?;
        Ok(())
    }
}

/// The ZeroMQ implementation of IpcClient.
pub type ZmqIpcClient = IpcClient<ZmqIpcSocket>;

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

    #[derive(Serialize, Debug, Clone, PartialEq)]
    pub struct MsgPongSend(pub f64, pub f64);

    impl IpcClient<MockIpcSocket> {
        fn priv_test_inject(&mut self, data: Vec<Vec<u8>>) {
            self.socket.inject_response(data);
        }

        fn priv_test_sent_count(&mut self) -> usize {
            self.socket.sent_count()
        }

        fn priv_test_next_sent(&mut self) -> Vec<Vec<u8>> {
            self.socket.next_sent()
        }
    }

    #[test]
    fn it_can_construct_and_destroy_sockets() {
        let ipc: IpcClient<MockIpcSocket> = IpcClient::new().unwrap();
        ipc.close().unwrap();
    }

    #[test]
    fn it_can_connect() {
        let mut ipc: IpcClient<MockIpcSocket> = IpcClient::new().unwrap();

        let pong = MsgPongSend(get_millis() - 4.0, get_millis() - 2.0);
        let mut data = rmp_serde::to_vec(&pong).unwrap();
        data.insert(0, MSG_PONG);

        ipc.priv_test_inject(vec![vec![], vec![], data]);

        ipc.connect("test-garbage").unwrap();
        ipc.close().unwrap();
    }

    #[test]
    fn it_can_receive_calls() {
        let mut ipc: IpcClient<MockIpcSocket> = IpcClient::new().unwrap();
        let data = MsgCallSend(b"", &[0x42]);
        let mut data = rmp_serde::to_vec(&data).unwrap();
        data.insert(0, MSG_CALL);
        ipc.priv_test_inject(vec![vec![], vec![], data]);
        let result = ipc.process(0);
        let result = result.unwrap().unwrap();
        let result = match result {
            Message::Call(s) => s,
            _ => panic!("bad message type"),
        };

        assert_eq!(vec![0x42], result.1);

        ipc.close().unwrap();
    }

    #[test]
    fn it_can_receive_call_oks() {
        let mut ipc: IpcClient<MockIpcSocket> = IpcClient::new().unwrap();
        let data = MsgCallOkSend(b"", &[0x42]);
        let mut data = rmp_serde::to_vec(&data).unwrap();
        data.insert(0, MSG_CALL_OK);
        ipc.priv_test_inject(vec![vec![], vec![], data]);
        let result = ipc.process(0);
        let result = result.unwrap().unwrap();
        let result = match result {
            Message::CallOk(s) => s,
            _ => panic!("bad message type"),
        };

        assert_eq!(vec![0x42], result.1);

        ipc.close().unwrap();
    }

    #[test]
    fn it_can_receive_call_fails() {
        let mut ipc: IpcClient<MockIpcSocket> = IpcClient::new().unwrap();
        let data = MsgCallFailSend(b"", &[0x42]);
        let mut data = rmp_serde::to_vec(&data).unwrap();
        data.insert(0, MSG_CALL_FAIL);
        ipc.priv_test_inject(vec![vec![], vec![], data]);
        let result = ipc.process(0);
        let result = result.expect_err("should have been an Err result");
        let result = format!("{}", result);
        assert_eq!("IpcError: B".to_string(), result);

        ipc.close().unwrap();
    }

    #[test]
    fn it_can_publish_ok_responses() {
        let mut ipc: IpcClient<MockIpcSocket> = IpcClient::new().unwrap();
        ipc.respond(&[0x42], Ok(&[0x99])).unwrap();
        assert_eq!(1, ipc.priv_test_sent_count());
        let mut sent = ipc.priv_test_next_sent();
        assert_eq!(3, sent.len());
        let sent = sent.remove(2);
        let sent: MsgCallOkRecv = rmp_serde::from_slice(&sent[1..]).unwrap();
        assert_eq!(vec![0x42], sent.0);
        assert_eq!(vec![0x99], sent.1);
        ipc.close().unwrap();
    }

    #[test]
    fn it_can_publish_fail_responses() {
        let mut ipc: IpcClient<MockIpcSocket> = IpcClient::new().unwrap();
        ipc.respond(
            &[0x42],
            Err(IpcError::GenericError {
                error: "test".to_string(),
            }.into()),
        ).unwrap();
        assert_eq!(1, ipc.priv_test_sent_count());
        let mut sent = ipc.priv_test_next_sent();
        assert_eq!(3, sent.len());
        let sent = sent.remove(2);
        let sent: MsgCallFailRecv = rmp_serde::from_slice(&sent[1..]).unwrap();
        assert_eq!(vec![0x42], sent.0);
        assert_eq!(b"IpcError: test".to_vec(), sent.1);
        ipc.close().unwrap();
    }
}