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
//! This module contains message serialization structures.

use serde_bytes;

/// Send a ping to the IPC server
/// This message is an array of 1 `f64` millisecond epoch timestamp value.
/// - index 0 : the current system timestamp of this system
#[derive(Serialize, Debug, Clone, PartialEq)]
pub struct MsgPingSend(pub f64);

/// A server response to a client-intiated `ping` message.
/// This message is an array of 2 `f64` millisecond epoch timestamp values.
/// - index 0 : the echoed initiation time of the originating `ping` message
/// - index 1 : the timestamp at which the server received / responded to the originating `ping` message
#[derive(Deserialize, Debug, Clone, PartialEq)]
pub struct MsgPongRecv(pub f64, pub f64);

/// Client wishes to send a `call` message to another node.
/// This message is an array of 2 `&[u8]` slices.
/// - index 0 : message identifier
/// - index 1 : message data
#[derive(Serialize, Debug, Clone, PartialEq)]
pub struct MsgCallSend<'a>(
    #[serde(with = "serde_bytes")] pub &'a [u8],
    #[serde(with = "serde_bytes")] pub &'a [u8],
);

/// This message represents this client receiving a `call` message.
/// This message is an array of 2 `Vec<u8>` values.
/// - index 0 : message identifier
/// - index 1 : message data
#[derive(Deserialize, Debug, Clone, PartialEq)]
pub struct MsgCallRecv(
    #[serde(with = "serde_bytes")] pub Vec<u8>,
    #[serde(with = "serde_bytes")] pub Vec<u8>,
);

/// Client wishes to respond with success to a `call` message.
/// This message is an array of 2 `&[u8]` slices.
/// - index 0 : message identifier
/// - index 1 : message data
#[derive(Serialize, Debug, Clone, PartialEq)]
pub struct MsgCallOkSend<'a>(
    #[serde(with = "serde_bytes")] pub &'a [u8],
    #[serde(with = "serde_bytes")] pub &'a [u8],
);

/// This message represents this client receiving a success response message.
/// This message is an array of 2 `Vec<u8>` values.
/// - index 0 : message identifier
/// - index 1 : message data
#[derive(Deserialize, Debug, Clone, PartialEq)]
pub struct MsgCallOkRecv(
    #[serde(with = "serde_bytes")] pub Vec<u8>,
    #[serde(with = "serde_bytes")] pub Vec<u8>,
);

/// Client wishes to respond with an error to a `call` message.
/// This message is an array of 2 `&[u8]` slices.
/// - index 0 : message identifier
/// - index 1 : message data
#[derive(Serialize, Debug, Clone, PartialEq)]
pub struct MsgCallFailSend<'a>(
    #[serde(with = "serde_bytes")] pub &'a [u8],
    #[serde(with = "serde_bytes")] pub &'a [u8],
);

/// This message represents this client receiving an error response message.
/// This message is an array of 2 `Vec<u8>` values.
/// - index 0 : message identifier
/// - index 1 : message data
#[derive(Deserialize, Debug, Clone, PartialEq)]
pub struct MsgCallFailRecv(
    #[serde(with = "serde_bytes")] pub Vec<u8>,
    #[serde(with = "serde_bytes")] pub Vec<u8>,
);

/// This enum is an amalgomation of all the server-sent message types to be used as a return type when receiving messages.
#[derive(Debug, Clone, PartialEq)]
pub enum Message {
    Pong(MsgPongRecv),
    Call(MsgCallRecv),
    CallOk(MsgCallOkRecv),
    CallFail(MsgCallFailRecv),
}

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

    #[derive(Deserialize, Debug, Clone, PartialEq)]
    pub struct MsgPingRecv(pub f64);

    #[test]
    fn it_msg_ping_round_trip() {
        let snd = MsgPingSend(42.0);
        let wire = rmp_serde::to_vec(&snd).unwrap();
        let res: MsgPingRecv = rmp_serde::from_slice(&wire).unwrap();
        assert!(42.0 == res.0);
    }

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

    #[test]
    fn it_msg_pong_round_trip() {
        let snd = MsgPongSend(42.0, 42.0);
        let wire = rmp_serde::to_vec(&snd).unwrap();
        let res: MsgPongRecv = rmp_serde::from_slice(&wire).unwrap();
        assert!(42.0 == res.0);
        assert!(42.0 == res.1);
    }

    #[test]
    fn it_msg_call_round_trip() {
        let data = vec![42];
        let snd = MsgCallSend(&data, &data);
        let wire = rmp_serde::to_vec(&snd).unwrap();
        let res: MsgCallRecv = rmp_serde::from_slice(&wire).unwrap();
        assert_eq!(vec![42], res.0);
        assert_eq!(vec![42], res.1);
    }

    #[test]
    fn it_msg_call_ok_round_trip() {
        let data = vec![42];
        let snd = MsgCallOkSend(&data, &data);
        let wire = rmp_serde::to_vec(&snd).unwrap();
        let res: MsgCallOkRecv = rmp_serde::from_slice(&wire).unwrap();
        assert_eq!(vec![42], res.0);
        assert_eq!(vec![42], res.1);
    }

    #[test]
    fn it_msg_call_fail_round_trip() {
        let data = vec![42];
        let snd = MsgCallFailSend(&data, &data);
        let wire = rmp_serde::to_vec(&snd).unwrap();
        let res: MsgCallFailRecv = rmp_serde::from_slice(&wire).unwrap();
        assert_eq!(vec![42], res.0);
        assert_eq!(vec![42], res.1);
    }
}