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
use std::convert::TryInto;
use std::sync::Arc;
use std::ops::Deref;

//Message used in event bus in standalone instance and cluster
#[derive(Clone, Default, Debug)]
pub struct Message {
    //Destination sub address
    pub(crate) address: Option<String>,
    //Replay sub address
    pub(crate) replay: Option<String>,
    //Binary body content
    pub(crate) body: Arc<Body>,
    //Protocol version
    pub(crate) protocol_version: i32,
    //System codec id
    pub(crate) system_codec_id: i32,
    //Port to replay message
    pub(crate) port: i32,
    //Host to replay message
    pub(crate) host: String,
    //Headers
    pub(crate) headers: i32,
    //Message send as publish to all nodes in sub
    pub(crate) publish: bool,
}

#[derive(Clone, Debug)]
pub enum Body {

    Byte(u8),
    Short(i16),
    Int(i32),
    Long(i64),
    Float(f32),
    Double(f64),
    String(String),
    ByteArray(Vec<u8>),
    Boolean(bool),
    Char(char),
    Null,
    Ping

}

impl Default for Body {
    fn default() -> Self {
        Self::Null
    }
}

impl Message {
    #[inline]
    pub fn body(&self) -> Arc<Body> {
        return self.body.clone();
    }

    //Reply message to event bus
    #[inline]
    pub fn reply(&mut self, data: Body) {
        self.body = Arc::new(data);
        self.address = self.replay.clone();
        self.replay = None;
    }

    pub fn generate() -> Message {
        Message {
            address: Some("test.01".to_string()),
            replay: Some(format!(
                "__vertx.reply.{}",
                uuid::Uuid::new_v4().to_string()
            )),
            body: Arc::new(Body::String(uuid::Uuid::new_v4().to_string())),
            port: 44532 as i32,
            host: "localhost".to_string(),
            ..Default::default()
        }
    }
}

//Implementation of deserialize byte array to message
impl From<Vec<u8>> for Message {
    #[inline]
    fn from(msg: Vec<u8>) -> Self {
        let mut idx = 1;
        let system_codec_id = i8::from_be_bytes(msg[idx..idx + 1].try_into().unwrap()) as i32;
        idx += 2;
        let len_addr = i32::from_be_bytes(msg[idx..idx + 4].try_into().unwrap()) as usize;
        idx += 4;
        let address = String::from_utf8(msg[idx..idx + len_addr].to_vec()).unwrap();
        idx += len_addr;
        let len_replay = i32::from_be_bytes(msg[idx..idx + 4].try_into().unwrap()) as usize;
        idx += 4;
        let mut replay = None;
        if len_replay > 0 {
            let replay_str = String::from_utf8(msg[idx..idx + len_replay].to_vec()).unwrap();
            idx += len_replay;
            replay = Some(replay_str);
        }
        let port = i32::from_be_bytes(msg[idx..idx + 4].try_into().unwrap());
        idx += 4;
        let len_host = i32::from_be_bytes(msg[idx..idx + 4].try_into().unwrap()) as usize;
        idx += 4;
        let host = String::from_utf8(msg[idx..idx + len_host].to_vec()).unwrap();
        idx += len_host;
        let headers = i32::from_be_bytes(msg[idx..idx + 4].try_into().unwrap());
        idx += 4;
        let body;
        match system_codec_id {
            0 => {
                body = Body::Null
            },
            1 => {
                body = Body::Ping
            }
            2 => {
                body = Body::Byte(u8::from_be_bytes(msg[idx..idx + 1].try_into().unwrap()))
            }
            3 => {
                body = Body::Boolean(i8::from_be_bytes(msg[idx..idx + 1].try_into().unwrap()) == 1)
            },
            4 => {
                body = Body::Short(i16::from_be_bytes(msg[idx..idx + 2].try_into().unwrap()))
            }
            5 => {
                body = Body::Int(i32::from_be_bytes(msg[idx..idx + 4].try_into().unwrap()))
            },
            6 => {
                body = Body::Long(i64::from_be_bytes(msg[idx..idx + 8].try_into().unwrap()))
            },
            7 => {
                body = Body::Float(f32::from_be_bytes(msg[idx..idx + 4].try_into().unwrap()))
            },
            8 => {
                body = Body::Double(f64::from_be_bytes(msg[idx..idx + 8].try_into().unwrap()))
            },
            9 => {
                let len_body = i32::from_be_bytes(msg[idx..idx + 4].try_into().unwrap()) as usize;
                idx += 4;
                let body_array = msg[idx..idx + len_body].to_vec();
                body = Body::String(String::from_utf8(body_array).unwrap())
            },
            10 => {
                body = Body::Char(char::from_u32(i16::from_be_bytes(msg[idx..idx + 2].try_into().unwrap()) as u32).unwrap())
            }
            12 => {
                let len_body = i32::from_be_bytes(msg[idx..idx + 4].try_into().unwrap()) as usize;
                idx += 4;
                let body_array = msg[idx..idx + len_body].to_vec();
                body = Body::ByteArray(body_array)
            },
            _ => panic!("system_codec_id: {} not supported", system_codec_id)
        }

        Message {
            address: Some(address.to_string()),
            replay,
            port,
            host,
            headers,
            body: Arc::new(body),
            system_codec_id,
            ..Default::default()
        }
    }
}

impl Message {
    //Serialize message to byte array
    #[inline]
    pub fn to_vec(&self) -> Result<Vec<u8>, &str> {
        let mut data = vec![];
        data.push(1);

        let mut b0 = vec![];
        match self.body.deref() {
            Body::Int(b) => {
                data.push(5);
                b0.extend_from_slice(b.to_be_bytes().as_slice());
            }
            Body::Long(b) => {
                data.push(6);
                b0.extend_from_slice(b.to_be_bytes().as_slice());
            }
            Body::Float(b) => {
                data.push(7);
                b0.extend_from_slice(b.to_be_bytes().as_slice());
            }
            Body::Double(b) => {
                data.push(8);
                b0.extend_from_slice(b.to_be_bytes().as_slice());
            }
            Body::String(b) => {
                data.push(9);
                b0.extend_from_slice(&(b.len() as i32).to_be_bytes());
                b0.extend_from_slice(b.as_bytes());
            }
            Body::ByteArray(b) => {
                data.push(12);
                b0.extend_from_slice(&(b.len() as i32).to_be_bytes());
                b0.extend_from_slice(b.as_slice());
            }
            Body::Boolean(b) => {
                data.push(3);
                if *b {
                    b0.extend_from_slice((1 as i8).to_be_bytes().as_slice())
                } else {
                    b0.extend_from_slice((0 as i8).to_be_bytes().as_slice())
                }
            }
            Body::Null => {
                data.push(0);
            }
            Body::Byte(b) => {
                data.push(2);
                b0.push(*b);
            }
            Body::Short(b) => {
                data.push(4);
                b0.extend_from_slice(b.to_be_bytes().as_slice());
            }
            Body::Char(b) => {
                data.push(10);
                b0.extend_from_slice((((*b) as u32) as i16).to_be_bytes().as_slice());
            }
            Body::Ping => {
                data.push(1);
            }
        };

        data.push(0);
        let address = self.address.clone().expect("Replay message not found!");
        data.extend_from_slice(&(address.len() as i32).to_be_bytes());
        data.extend_from_slice(address.as_bytes());
        match self.replay.clone() {
            Some(addr) => {
                data.extend_from_slice(&(addr.len() as i32).to_be_bytes());
                data.extend_from_slice(addr.as_bytes());
            }
            None => {
                data.extend_from_slice(&(0 as i32).to_be_bytes());
            }
        }
        data.extend_from_slice(&self.port.to_be_bytes());
        data.extend_from_slice(&(self.host.len() as i32).to_be_bytes());
        data.extend_from_slice(self.host.as_bytes());
        data.extend_from_slice(&(4 as i32).to_be_bytes());
        // data.extend_from_slice(&(self.body.len() as i32).to_be_bytes());
        data.extend_from_slice(b0.as_slice());

        let len = ((data.len()) as i32).to_be_bytes();
        for idx in 0..4 {
            data.insert(idx, len[idx]);
        }
        return Ok(data);
    }
}