fast-rpc 0.3.0

streaming JSON RPC over TCP
Documentation
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
// Copyright 2020 Joyent, Inc.

//! This module contains the types and functions used to encode and decode Fast
//! messages. The contents of this module are not needed for normal client or
//! server consumers of this crate, but they are exposed for the special case of
//! someone needing to implement custom client or server code.

use std::io::{Error, ErrorKind};
use std::sync::atomic::AtomicUsize;
use std::time::{SystemTime, UNIX_EPOCH};
use std::{io, str, usize};

use byteorder::{BigEndian, ByteOrder};
use bytes::{BufMut, BytesMut};
use crc16::*;
use num::{FromPrimitive, ToPrimitive};
use num_derive::{FromPrimitive, ToPrimitive};
use serde_derive::{Deserialize, Serialize};
use serde_json::Value;
use tokio_io::_tokio_codec::{Decoder, Encoder};

const FP_OFF_TYPE: usize = 0x1;
const FP_OFF_STATUS: usize = 0x2;
const FP_OFF_MSGID: usize = 0x3;
const FP_OFF_CRC: usize = 0x7;
const FP_OFF_DATALEN: usize = 0xb;
const FP_OFF_DATA: usize = 0xf;

/// The size of a Fast message header
pub const FP_HEADER_SZ: usize = FP_OFF_DATA;

const FP_VERSION_2: u8 = 0x2;
const FP_VERSION_CURRENT: u8 = FP_VERSION_2;

/// A data type representing a Fast message id that can safely be shard between
/// threads. The `next` associated function retrieves the next id value and
/// manages the circular message id space internally.
#[derive(Default)]
pub struct FastMessageId(AtomicUsize);

impl FastMessageId {
    /// Creates a new FastMessageId
    pub fn new() -> Self {
        FastMessageId(AtomicUsize::new(0x0))
    }
}

impl Iterator for FastMessageId {
    type Item = usize;

    /// Returns the next Fast message id and increments the value modulo the
    /// usize MAX_VALUE - 1.
    fn next(&mut self) -> Option<Self::Item> {
        // Increment our count. This is why we started at zero.
        let id_value = self.0.get_mut();
        let current = *id_value;
        *id_value = (*id_value + 1) % (usize::max_value() - 1);

        Some(current)
    }
}

/// An error type representing a failure to parse a buffer as a Fast message.
#[derive(Debug)]
pub enum FastParseError {
    NotEnoughBytes(usize),
    IOError(Error),
}

impl From<io::Error> for FastParseError {
    fn from(error: io::Error) -> Self {
        FastParseError::IOError(error)
    }
}

impl From<FastParseError> for Error {
    fn from(pfr: FastParseError) -> Self {
        match pfr {
            FastParseError::NotEnoughBytes(_) => {
                let msg = "Unable to parse message: not enough bytes";
                Error::new(ErrorKind::Other, msg)
            }
            FastParseError::IOError(e) => e,
        }
    }
}

/// An error type representing Fast error messages that may be returned from a
/// Fast server.
#[derive(Debug, Deserialize, Serialize)]
pub struct FastMessageServerError {
    pub name: String,
    pub message: String,
}

impl FastMessageServerError {
    pub fn new(name: &str, message: &str) -> Self {
        FastMessageServerError {
            name: String::from(name),
            message: String::from(message),
        }
    }
}

impl From<FastMessageServerError> for Error {
    fn from(err: FastMessageServerError) -> Self {
        Error::new(ErrorKind::Other, format!("{}: {}", err.name, err.message))
    }
}

/// Represents the Type field of a Fast message. Currently there is only one
/// valid value, JSON.
#[derive(Debug, FromPrimitive, ToPrimitive, PartialEq, Clone)]
pub enum FastMessageType {
    Json = 1,
}

/// Represents the Status field of a Fast message.
#[derive(Debug, FromPrimitive, ToPrimitive, PartialEq, Clone)]
pub enum FastMessageStatus {
    Data = 1,
    End = 2,
    Error = 3,
}

/// This type encapsulates the header of a Fast message.
pub struct FastMessageHeader {
    /// The Type field of the Fast message
    msg_type: FastMessageType,
    /// The Status field of the Fast message
    status: FastMessageStatus,
    /// The Fast message identifier
    id: u32,
    /// The CRC16 check value of the Fast message data payload
    crc: u32,
    /// The length in bytes of the Fast message data payload
    data_len: usize,
}

/// Represents the metadata about a `FastMessage` data payload. This includes a
/// timestamp and an RPC method name.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct FastMessageMetaData {
    pub uts: u64,
    pub name: String,
}

impl FastMessageMetaData {
    pub fn new(n: String) -> FastMessageMetaData {
        let now = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
        let now_micros =
            now.as_secs() * 1_000_000 + u64::from(now.subsec_micros());

        FastMessageMetaData {
            uts: now_micros,
            name: n,
        }
    }
}

/// Encapsulates the Fast message metadata and the JSON formatted message data.
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub struct FastMessageData {
    pub m: FastMessageMetaData,
    pub d: Value,
}

impl FastMessageData {
    pub fn new(n: String, d: Value) -> FastMessageData {
        FastMessageData {
            m: FastMessageMetaData::new(n),
            d,
        }
    }
}

/// Represents a Fast message including the header and data payload
#[derive(Debug, Clone)]
pub struct FastMessage {
    /// The Type field of the Fast message
    pub msg_type: FastMessageType,
    /// The Status field of the Fast message
    pub status: FastMessageStatus,
    /// The Fast message identifier
    pub id: u32,
    /// The length in bytes of the Fast message data payload
    pub msg_size: Option<usize>,
    /// The data payload of the Fast message
    pub data: FastMessageData,
}

impl PartialEq for FastMessage {
    fn eq(&self, other: &FastMessage) -> bool {
        self.msg_type == other.msg_type
            && self.status == other.status
            && self.id == other.id
            && self.msg_size == other.msg_size
            && self.data == other.data
    }
}

impl FastMessage {
    /// Parse a byte buffer into a `FastMessage`. Returns a `FastParseError` if
    /// the available bytes cannot be parsed to a `FastMessage`.
    pub fn parse(buf: &[u8]) -> Result<FastMessage, FastParseError> {
        FastMessage::check_buffer_size(buf)?;
        let header = FastMessage::parse_header(buf)?;

        FastMessage::validate_data_length(buf, header.data_len)?;
        let raw_data = &buf[FP_OFF_DATA..FP_OFF_DATA + header.data_len];
        FastMessage::validate_crc(raw_data, header.crc)?;
        let data = FastMessage::parse_data(raw_data)?;

        let msg_size = match header.status {
            FastMessageStatus::End => None,
            _ => Some(FP_OFF_DATA + header.data_len),
        };

        Ok(FastMessage {
            msg_type: header.msg_type,
            status: header.status,
            id: header.id,
            msg_size,
            data,
        })
    }

    /// Check that the provided byte buffer contains at least `FP_HEADER_SZ`
    /// bytes.  Returns a `FastParseError` if this is not the case.
    pub fn check_buffer_size(buf: &[u8]) -> Result<(), FastParseError> {
        if buf.len() < FP_HEADER_SZ {
            Err(FastParseError::NotEnoughBytes(buf.len()))
        } else {
            Ok(())
        }
    }

    /// Parse a portion of a byte buffer into a `FastMessageHeader`. Returns a
    /// `FastParseError` if the available bytes cannot be parsed to a
    /// `FastMessageHeader`.
    pub fn parse_header(
        buf: &[u8],
    ) -> Result<FastMessageHeader, FastParseError> {
        let msg_type =
            FromPrimitive::from_u8(buf[FP_OFF_TYPE]).ok_or_else(|| {
                let msg = "Failed to parse message type";
                FastParseError::IOError(Error::new(ErrorKind::Other, msg))
            })?;
        let status =
            FromPrimitive::from_u8(buf[FP_OFF_STATUS]).ok_or_else(|| {
                let msg = "Failed to parse message status";
                FastParseError::IOError(Error::new(ErrorKind::Other, msg))
            })?;
        let msg_id = BigEndian::read_u32(&buf[FP_OFF_MSGID..FP_OFF_MSGID + 4]);
        let expected_crc =
            BigEndian::read_u32(&buf[FP_OFF_CRC..FP_OFF_CRC + 4]);
        let data_len =
            BigEndian::read_u32(&buf[FP_OFF_DATALEN..FP_OFF_DATALEN + 4])
                as usize;

        Ok(FastMessageHeader {
            msg_type,
            status,
            id: msg_id,
            crc: expected_crc,
            data_len,
        })
    }

    fn validate_data_length(
        buf: &[u8],
        data_length: usize,
    ) -> Result<(), FastParseError> {
        if buf.len() < (FP_HEADER_SZ + data_length) {
            Err(FastParseError::NotEnoughBytes(buf.len()))
        } else {
            Ok(())
        }
    }

    fn validate_crc(data_buf: &[u8], crc: u32) -> Result<(), FastParseError> {
        let calculated_crc = u32::from(State::<ARC>::calculate(data_buf));
        if crc != calculated_crc {
            let msg = "Calculated CRC does not match the provided CRC";
            Err(FastParseError::IOError(Error::new(ErrorKind::Other, msg)))
        } else {
            Ok(())
        }
    }

    fn parse_data(data_buf: &[u8]) -> Result<FastMessageData, FastParseError> {
        match str::from_utf8(data_buf) {
            Ok(data_str) => serde_json::from_str(data_str).map_err(|_e| {
                let msg = "Failed to parse data payload as JSON";
                FastParseError::IOError(Error::new(ErrorKind::Other, msg))
            }),
            Err(_) => {
                let msg = "Failed to parse data payload as UTF-8";
                Err(FastParseError::IOError(Error::new(ErrorKind::Other, msg)))
            }
        }
    }

    /// Returns a `FastMessage` that represents a Fast protocol `DATA` message
    /// with the provided message identifer and data payload.
    pub fn data(msg_id: u32, data: FastMessageData) -> FastMessage {
        FastMessage {
            msg_type: FastMessageType::Json,
            status: FastMessageStatus::Data,
            id: msg_id,
            msg_size: None,
            data,
        }
    }

    /// Returns a `FastMessage` that represents a Fast protocol `END` message
    /// with the provided message identifer. The method parameter is used in the
    /// otherwise empty data payload.
    pub fn end(msg_id: u32, method: String) -> FastMessage {
        FastMessage {
            msg_type: FastMessageType::Json,
            status: FastMessageStatus::End,
            id: msg_id,
            msg_size: None,
            data: FastMessageData::new(method, Value::Array(vec![])),
        }
    }

    /// Returns a `FastMessage` that represents a Fast protocol `ERROR` message
    /// with the provided message identifer and data payload.
    pub fn error(msg_id: u32, data: FastMessageData) -> FastMessage {
        FastMessage {
            msg_type: FastMessageType::Json,
            status: FastMessageStatus::Error,
            id: msg_id,
            msg_size: None,
            data,
        }
    }
}

/// This type implements the functions necessary for the Fast protocl framing.
pub struct FastRpc;

impl Decoder for FastRpc {
    type Item = Vec<FastMessage>;
    type Error = Error;

    fn decode(
        &mut self,
        buf: &mut BytesMut,
    ) -> Result<Option<Self::Item>, Error> {
        let mut msgs: Self::Item = Vec::new();
        let mut done = false;

        while !done && !buf.is_empty() {
            // Make sure there is room in msgs to fit a message
            if msgs.len() + 1 > msgs.capacity() {
                msgs.reserve(1);
            }

            match FastMessage::parse(&buf) {
                Ok(parsed_msg) => {
                    // TODO: Handle the error case here!
                    let data_str =
                        serde_json::to_string(&parsed_msg.data).unwrap();
                    let data_len = data_str.len();
                    buf.advance(FP_HEADER_SZ + data_len);
                    msgs.push(parsed_msg);
                    Ok(())
                }
                Err(FastParseError::NotEnoughBytes(_)) => {
                    // Not enough bytes available yet so we need to return
                    // Ok(None) to let the Framed instance know to read more
                    // data before calling this function again.
                    done = true;
                    Ok(())
                }
                Err(err) => {
                    let msg = format!(
                        "failed to parse Fast request: {}",
                        Error::from(err)
                    );
                    Err(Error::new(ErrorKind::Other, msg))
                }
            }?
        }

        if msgs.is_empty() {
            Ok(None)
        } else {
            Ok(Some(msgs))
        }
    }
}

impl Encoder for FastRpc {
    type Item = Vec<FastMessage>;
    //TODO: Create custom FastMessage error type
    type Error = io::Error;
    fn encode(
        &mut self,
        item: Self::Item,
        buf: &mut BytesMut,
    ) -> Result<(), io::Error> {
        let results: Vec<Result<(), String>> =
            item.iter().map(|x| encode_msg(x, buf)).collect();
        let result: Result<Vec<()>, String> = results.iter().cloned().collect();
        match result {
            Ok(_) => Ok(()),
            Err(errs) => Err(Error::new(ErrorKind::Other, errs)),
        }
    }
}

/// Encode a `FastMessage` into a byte buffer. The `Result` contains a unit type
/// on success and an error string on failure.
pub(crate) fn encode_msg(
    msg: &FastMessage,
    buf: &mut BytesMut,
) -> Result<(), String> {
    let m_msg_type_u8 = msg.msg_type.to_u8();
    let m_status_u8 = msg.status.to_u8();
    match (m_msg_type_u8, m_status_u8) {
        (Some(msg_type_u8), Some(status_u8)) => {
            // TODO: Handle the error case here!
            let data_str = serde_json::to_string(&msg.data).unwrap();
            let data_len = data_str.len();
            let buf_capacity = buf.capacity();
            if buf.len() + FP_HEADER_SZ + data_len > buf_capacity {
                buf.reserve(FP_HEADER_SZ + data_len as usize);
            }
            buf.put_u8(FP_VERSION_CURRENT);
            buf.put_u8(msg_type_u8);
            buf.put_u8(status_u8);
            buf.put_u32_be(msg.id);
            buf.put_u32_be(u32::from(State::<ARC>::calculate(
                data_str.as_bytes(),
            )));
            buf.put_u32_be(data_str.len() as u32);
            buf.put(data_str);
            Ok(())
        }
        (None, Some(_)) => Err(String::from("Invalid message type")),
        (Some(_), None) => Err(String::from("Invalid status")),
        (None, None) => Err(String::from("Invalid message type and status")),
    }
}

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

    use std::iter;

    use quickcheck::{quickcheck, Arbitrary, Gen};
    use rand::distributions::Alphanumeric;
    use rand::seq::SliceRandom;
    use rand::Rng;
    use serde_json::Map;

    fn random_string<G: Gen>(g: &mut G, len: usize) -> String {
        iter::repeat(())
            .map(|()| g.sample(Alphanumeric))
            .take(len)
            .collect()
    }

    fn nested_object<G: Gen>(g: &mut G) -> Value {
        let k_len = g.gen::<u8>() as usize;
        let v_len = g.gen::<u8>() as usize;
        let k = random_string(g, k_len);
        let v = random_string(g, v_len);
        let count = g.gen::<u64>();
        let mut inner_obj = Map::new();
        let mut outer_obj = Map::new();
        let _ = inner_obj.insert(k, Value::String(v));
        outer_obj
            .insert(String::from("value"), Value::Object(inner_obj))
            .and_then(|_| {
                outer_obj.insert(String::from("count"), count.into())
            });
        Value::Object(outer_obj)
    }

    #[derive(Clone, Debug)]
    struct MessageCount(u8);

    impl Arbitrary for MessageCount {
        fn arbitrary<G: Gen>(g: &mut G) -> MessageCount {
            let mut c = 0;
            while c == 0 {
                c = g.gen::<u8>()
            }

            MessageCount(c)
        }
    }

    impl Arbitrary for FastMessageStatus {
        fn arbitrary<G: Gen>(g: &mut G) -> FastMessageStatus {
            let choices = [
                FastMessageStatus::Data,
                FastMessageStatus::End,
                FastMessageStatus::Error,
            ];

            choices.choose(g).unwrap().clone()
        }
    }

    impl Arbitrary for FastMessageType {
        fn arbitrary<G: Gen>(g: &mut G) -> FastMessageType {
            let choices = [FastMessageType::Json];

            choices.choose(g).unwrap().clone()
        }
    }

    impl Arbitrary for FastMessageMetaData {
        fn arbitrary<G: Gen>(g: &mut G) -> FastMessageMetaData {
            let name = random_string(g, 10);
            FastMessageMetaData::new(name)
        }
    }

    impl Arbitrary for FastMessageData {
        fn arbitrary<G: Gen>(g: &mut G) -> FastMessageData {
            let md = FastMessageMetaData::arbitrary(g);

            let choices = [
                Value::Array(vec![]),
                Value::Object(Map::new()),
                nested_object(g),
                Value::Array(vec![nested_object(g)]),
            ];

            let value = choices.choose(g).unwrap().clone();

            FastMessageData { m: md, d: value }
        }
    }

    impl Arbitrary for FastMessage {
        fn arbitrary<G: Gen>(g: &mut G) -> FastMessage {
            let msg_type = FastMessageType::arbitrary(g);
            let status = FastMessageStatus::arbitrary(g);
            let id = g.gen::<u32>();

            let data = FastMessageData::arbitrary(g);
            let data_str = serde_json::to_string(&data).unwrap();
            let msg_sz = match status {
                FastMessageStatus::End => None,
                _ => Some(FP_OFF_DATA + data_str.len()),
            };

            FastMessage {
                msg_type,
                status,
                id,
                msg_size: msg_sz,
                data,
            }
        }
    }

    quickcheck! {
        fn prop_fast_message_roundtrip(msg: FastMessage) -> bool {
            let mut write_buf = BytesMut::new();
            match encode_msg(&msg, &mut write_buf) {
                Ok(_) => {
                    match FastMessage::parse(&write_buf) {
                        Ok(decoded_msg) => decoded_msg == msg,
                        Err(_) => false
                    }
                },
                Err(_) => false
            }
        }
    }

    quickcheck! {
        fn prop_fast_message_bundling(msg: FastMessage, msg_count: MessageCount) -> bool {
            let mut write_buf = BytesMut::new();
            let mut error_occurred = false;
            for _ in 0..msg_count.0 {
                match encode_msg(&msg, &mut write_buf) {
                    Ok(_) => (),
                    Err(_) => {
                        error_occurred = true;
                    }
                }
            }

            if error_occurred {
                return false;
            }

            let msg_size = write_buf.len() / msg_count.0 as usize;
            let mut offset = 0;
            for _ in 0..msg_count.0 {
                match FastMessage::parse(&write_buf[offset..offset+msg_size]) {
                    Ok(decoded_msg) => error_occurred = decoded_msg != msg,
                    Err(_) => error_occurred = true
                }
                offset += msg_size;
            }

            !error_occurred
        }
    }

    quickcheck! {
        fn prop_fast_message_decoding(msg: FastMessage, msg_count: MessageCount) -> bool {
            let mut write_buf = BytesMut::new();
            let mut error_occurred = false;
            let mut fast_msgs: Vec<FastMessage> =
                Vec::with_capacity(msg_count.0 as usize);

            (0..msg_count.0).for_each(|_| {
                fast_msgs.push(msg.clone());
            });

            let mut fast_rpc = FastRpc;
            let encode_res = fast_rpc.encode(fast_msgs, &mut write_buf);

            if encode_res.is_err() {
                return false;
            }

            let decode_result = fast_rpc.decode(&mut write_buf);
            if decode_result.is_err() {
                return false;
            }

            let m_decoded_msgs = decode_result.unwrap();


            if m_decoded_msgs.is_none() {
                return false;
            }

            let decoded_msgs = m_decoded_msgs.unwrap();
            if decoded_msgs.len() != msg_count.0 as usize {
                return false;
            }


            for decoded_msg in decoded_msgs {
                error_occurred = decoded_msg != msg;
            }

            !error_occurred
        }
    }
}