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
//! Implements the [muxrpc protocol](https://github.com/ssbc/muxrpc) in rust.
//!
//! All futures, sinks and streams with an error type of `Option<io::Error>` use `None` to signal
//! that an error happend on the underlying transport, but on another handle to the transport.
#![deny(missing_docs)]

extern crate atm_async_utils;
#[macro_use]
extern crate futures_core;
extern crate futures_sink;
extern crate futures_io;
extern crate futures_util;
extern crate packet_stream;
extern crate serde;
extern crate serde_json;
#[macro_use(Serialize, Deserialize)]
extern crate serde_derive;

#[cfg(test)]
extern crate async_ringbuffer;
#[cfg(test)]
extern crate futures;

mod errors;
mod common;
mod async;
mod sync;
mod source;
mod sink;
mod duplex;

use std::convert::From;
use std::io;

use futures_core::{Future, Stream, Poll};
use futures_core::Async::Ready;
use futures_core::task::Context;
use futures_io::{AsyncRead, AsyncWrite};
use packet_stream::{packet_stream, PsIn, PsOut, IncomingPacket, PacketType, ClosePs,
                    Done as PsDone};
use serde_json::Value;
use serde_json::{to_vec, from_slice};
use serde::Serialize;
use serde::de::DeserializeOwned;

pub use errors::*;
use common::*;
use async::{new_peer_async, new_async, new_async_response};
pub use async::{Async, PeerAsyncResponse, PeerAsync, AsyncResponse};
use sync::{new_peer_sync, new_sync, new_sync_response};
pub use sync::{Sync, PeerSyncResponse, PeerSync, SyncResponse};
use source::{new_rpc_stream, new_source, new_source_cancelable};
pub use source::{Source, SourceCancelable, RpcStream, CancelSource};
use sink::{new_sink, new_rpc_sink};
pub use sink::{MuxSink, RpcSink};
use duplex::new_duplex;
pub use duplex::Duplex;

#[derive(Serialize)]
#[serde(rename_all = "lowercase")]
struct OutRpc<'a, 'b, A: 'b> {
    name: &'a [&'a str],
    #[serde(rename = "type")]
    type_: RpcType,
    args: &'b A,
}

impl<'a, 'b, A: Serialize + 'b> OutRpc<'a, 'b, A> {
    fn new(name: &'a [&'a str], type_: RpcType, args: &'b A) -> OutRpc<'a, 'b, A> {
        OutRpc { name, type_, args }
    }
}

#[derive(Deserialize)]
#[serde(rename_all = "lowercase")]
struct InRpc {
    name: Box<[String]>,
    #[serde(rename = "type")]
    type_: RpcType,
    args: Box<[Value]>,
}

// The different rpc types.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
enum RpcType {
    Source,
    Sink,
    Duplex,
    Async,
    Sync,
}

/// Implementors of this trait are rpcs that can be sent to the peer. The serilize implementation
/// should provide the argument value(s).
pub trait Rpc: Serialize {
    /// The names for this rpc.
    fn names() -> &'static [&'static str];
}

/// A future that emits the wrapped writer of a muxrpc connection once the outgoing half of the
/// has been fully closed.
pub struct Done<W>(PsDone<W, Box<[u8]>>);

impl<W> Future for Done<W> {
    type Item = W;
    /// This can only be emitted if a handle to the underlying transport has been polled but was
    /// dropped before it was done. If all handles are polled/closed properly, this error is never
    /// emitted.
    type Error = W;

    fn poll(&mut self, cx: &mut Context) -> Poll<Self::Item, Self::Error> {
        self.0.poll(cx)
    }
}

/// Take ownership of an AsyncRead and an AsyncWrite to create the two halves of
/// a muxrpc connection.
///
/// `R` is the `AsyncRead` for reading bytes from the peer, `W` is the
/// `AsyncWrite` for writing bytes to the peer, and `B` is the type that is used
/// as input for sending data.
pub fn muxrpc<R: AsyncRead, W: AsyncWrite>(r: R, w: W) -> (RpcIn<R, W>, RpcOut<R, W>, Done<W>) {
    let (ps_in, ps_out, done) = packet_stream(r, w);
    (RpcIn::new(ps_in), RpcOut::new(ps_out), Done(done))
}

/// A stream of incoming rpcs from the peer.
pub struct RpcIn<R: AsyncRead, W>(PsIn<R, W, Box<[u8]>>);

impl<R: AsyncRead, W> RpcIn<R, W> {
    fn new(ps_in: PsIn<R, W, Box<[u8]>>) -> RpcIn<R, W> {
        RpcIn(ps_in)
    }
}

impl<R: AsyncRead, W: AsyncWrite> Stream for RpcIn<R, W> {
    /// Name(s) and args of the rpc, and a handle for reacting to the rpc
    type Item = (Box<[String]>, Box<[Value]>, IncomingRpc<R, W>);
    type Error = RpcError;

    fn poll_next(&mut self, cx: &mut Context) -> Poll<Option<Self::Item>, Self::Error> {
        match try_ready!(self.0.poll_next(cx)) {
            None => Ok(Ready(None)),

            Some((data, metadata, handle)) => {
                if metadata.packet_type == PacketType::Json {
                    let rpc = from_slice::<InRpc>(&data)?;

                    match handle {
                        IncomingPacket::Request(req) => {
                            match rpc.type_ {
                                RpcType::Sync => {
                                    Ok(Ready(Some((rpc.name,
                                                   rpc.args,
                                                   IncomingRpc::Sync(new_peer_sync(req))))))
                                }

                                RpcType::Async => {
                                    Ok(Ready(Some((rpc.name,
                                                   rpc.args,
                                                   IncomingRpc::Async(new_peer_async(req))))))
                                }

                                _ => Err(RpcError::NotJson),
                            }
                        }

                        IncomingPacket::Duplex(ps_sink, ps_stream) => {
                            match rpc.type_ {
                                RpcType::Source => {
                                    Ok(Ready(Some((rpc.name,
                                                   rpc.args,
                                                   IncomingRpc::Source(new_rpc_sink(ps_sink))))))
                                }

                                RpcType::Sink => {
                                    Ok(Ready(Some((rpc.name,
                                                   rpc.args,
                                                   IncomingRpc::Sink(new_rpc_stream(ps_stream))))))
                                }

                                RpcType::Duplex => Ok(Ready(Some((rpc.name, rpc.args, IncomingRpc::Duplex(new_rpc_sink(ps_sink), new_rpc_stream(ps_stream)))))),

                                _ => Err(RpcError::NotJson),
                            }
                        }
                    }
                } else {
                    Err(RpcError::NotJson)
                }
            }
        }
    }
}

fn unwrap_serialize<S: Serialize>(s: S) -> Box<[u8]> {
    match to_vec(&s) {
        Ok(data) => data.into_boxed_slice(),
        Err(_) => panic!("Muxrpc serialization to bytes failed"),
    }
}

/// Allows sending rpcs to the peer.
pub struct RpcOut<R: AsyncRead, W>(PsOut<R, W, Box<[u8]>>);

impl<R: AsyncRead, W> RpcOut<R, W> {
    fn new(ps_out: PsOut<R, W, Box<[u8]>>) -> RpcOut<R, W> {
        RpcOut(ps_out)
    }
}

impl<R, W> RpcOut<R, W>
    where R: AsyncRead,
          W: AsyncWrite
{
    /// Send an async request to the peer.
    ///
    /// The `Async` Future must be polled to actually start sending the request.
    /// The `AsyncResponse` Future can be polled to receive the response.
    ///
    /// `Res` is the type of a successful response, `E` is the type of an error response.
    pub fn async<RPC: Rpc, Res: DeserializeOwned, E: DeserializeOwned>
        (&mut self,
         rpc: &RPC)
         -> (Async<W>, AsyncResponse<R, Res, E>) {
        let out_rpc = OutRpc::new(RPC::names(), RpcType::Async, rpc);

        let (out_req, in_res) = self.0
            .request(unwrap_serialize(&out_rpc), PacketType::Json);
        (new_async(out_req), new_async_response(in_res))
    }

    /// Send a sync request to the peer.
    ///
    /// The `Sync` Future must be polled to actually start sending the request.
    /// The `SyncResponse` Future can be polled to receive the response.
    ///
    /// `Res` is the type of a successful response, `E` is the type of an error response.
    pub fn sync<RPC: Rpc, Res: DeserializeOwned, E: DeserializeOwned>
        (&mut self,
         rpc: &RPC)
         -> (Sync<W>, SyncResponse<R, Res, E>) {
        let out_rpc = OutRpc::new(RPC::names(), RpcType::Sync, rpc);

        let (out_req, in_res) = self.0
            .request(unwrap_serialize(&out_rpc), PacketType::Json);
        (new_sync(out_req), new_sync_response(in_res))
    }

    /// Send a source request to the peer.
    ///
    /// The `Source` Future must be polled to actually start sending the request.
    /// The `RpcStream` can be polled to receive the responses.
    ///
    /// `I` is the type of the responses, `E` is the type of an error response.
    pub fn source<RPC: Rpc, I: DeserializeOwned, E: DeserializeOwned>
        (&mut self,
         rpc: &RPC)
         -> (Source<W>, RpcStream<R, I, E>) {
        let out_rpc = OutRpc::new(RPC::names(), RpcType::Source, rpc);

        let (ps_sink, ps_stream) = self.0.duplex();
        (new_source(ps_sink, unwrap_serialize(&out_rpc)), new_rpc_stream(ps_stream))
    }

    /// Send a source request to the peer, that may be cancelled at any time.
    ///
    /// The `Source` Future must be polled to actually start sending the request, and it yields
    /// a handle for cancelling the source. Note that if the handle is not used for cancelling, it
    /// *must* still be closed.
    /// The `InSink` can be polled to receive the responses.
    ///
    /// `I` is the type of the responses, `E` is the type of an error response.
    pub fn source_cancelable<RPC: Rpc, I: DeserializeOwned, E: DeserializeOwned>
        (&mut self,
         rpc: &RPC)
         -> (SourceCancelable<W>, RpcStream<R, I, E>) {
        let out_rpc = OutRpc::new(RPC::names(), RpcType::Source, rpc);

        let (ps_sink, ps_stream) = self.0.duplex();
        (new_source_cancelable(ps_sink, unwrap_serialize(&out_rpc)), new_rpc_stream(ps_stream))
    }

    /// Send a sink request to the peer.
    ///
    /// The `MuxSink` Future must be polled to actually start sending the request, and it yields
    /// a sink for sending more data to the peer.
    pub fn sink<RPC: Rpc>(&mut self, rpc: &RPC) -> MuxSink<W> {
        let out_rpc = OutRpc::new(RPC::names(), RpcType::Sink, rpc);

        let (ps_sink, _) = self.0.duplex();
        new_sink(ps_sink, unwrap_serialize(&out_rpc))
    }

    /// Send a duplex request to the peer.
    ///
    /// The `OutSDuplex` Future must be polled to actually start sending the request, and it yields
    /// a sink for sending more data to the peer.
    /// The `RpcStream` can be polled to receive the responses.
    ///
    /// `I` is the type of the responses, `E` is the type of an error response.
    pub fn duplex<RPC: Rpc, I: DeserializeOwned, E: DeserializeOwned>
        (&mut self,
         rpc: &RPC)
         -> (Duplex<W>, RpcStream<R, I, E>) {
        let out_rpc = OutRpc::new(RPC::names(), RpcType::Duplex, rpc);

        let (ps_sink, ps_stream) = self.0.duplex();
        (new_duplex(ps_sink, unwrap_serialize(&out_rpc)), new_rpc_stream(ps_stream))
    }

    /// Close the muxrpc session. If there are still active handles to the underlying transport,
    /// it is not closed immediately. It will get closed once the last of them is done.
    pub fn close(self) -> CloseRpc<R, W> {
        CloseRpc(self.0.close())
    }
}

/// A future for closing the muxrpc session. If there are still active handles to the underlying transport,
/// it is not closed immediately. It will get closed once the last of them is done.
pub struct CloseRpc<R: AsyncRead, W>(ClosePs<R, W, Box<[u8]>>);

impl<R: AsyncRead, W: AsyncWrite> Future for CloseRpc<R, W> {
    type Item = ();
    type Error = Option<io::Error>;

    fn poll(&mut self, cx: &mut Context) -> Poll<Self::Item, Self::Error> {
        self.0.poll(cx)
    }
}

/// An incoming packet, initiated by the peer.
pub enum IncomingRpc<R: AsyncRead, W: AsyncWrite> {
    /// A source request. You get a sink, the peer got a stream.
    Source(RpcSink<W>),
    /// A sink request. You get a stream, the peer got a sink.
    Sink(RpcStream<R, Value, Value>),
    /// A duplex request. Both peers get a stream and a sink.
    Duplex(RpcSink<W>, RpcStream<R, Value, Value>),
    /// An async request. You get an PeerAsync, the peer got an AsyncResponse.
    Async(PeerAsync<W>),
    /// A sync request. You get an PeerSync, the peer got an AsyncResponse.
    Sync(PeerSync<W>),
}

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

    use async_ringbuffer::*;
    use futures::prelude::*;
    use futures::future::ok;
    use futures::stream::iter_ok;
    use futures::sink::close;
    use futures::executor::block_on;

    #[derive(Serialize)]
    struct TestRpc([u8; 8]);

    const NAMES: [&'static str; 2] = ["foo", "bar"];

    impl Rpc for TestRpc {
        fn names() -> &'static [&'static str] {
            &NAMES
        }
    }

    #[test]
    fn test_async() {
        let (writer_a, reader_a) = ring_buffer(2);
        let (writer_b, reader_b) = ring_buffer(2);

        let (a_in, mut a_out, _) = muxrpc(reader_a, writer_b);
        let (b_in, b_out, _) = muxrpc(reader_b, writer_a);

        let echo = b_in.for_each(|(names, args, in_rpc)| {
                assert_eq!(names,
                           vec!["foo".to_string(), "bar".to_string()].into_boxed_slice());
                match in_rpc {
                    IncomingRpc::Async(in_async) => {
                        in_async.respond(&args).map_err(|_| unreachable!())
                    }
                    _ => unreachable!(),
                }
            })
            .and_then(|_| b_out.close().map_err(|_| unreachable!()));

        let consume_a = a_in.for_each(|_| ok(()));

        let (req0, res0) = a_out.async::<_, [u8; 8], i32>(&TestRpc([0, 1, 2, 3, 4, 5, 6, 7]));
        let (req1, res1) = a_out.async::<_, [u8; 8], i32>(&TestRpc([8, 9, 10, 11, 12, 13, 14, 15]));
        let (req2, res2) = a_out.async::<_, [u8; 8], i32>(&TestRpc([16, 17, 18, 19, 20, 21, 22,
                                                                    23]));

        let send_all = req0.join3(req1, req2).and_then(|_| a_out.close());

        let receive_all = res0.join3(res1, res2)
            .map(|(r0_data, r1_data, r2_data)| {
                     return r0_data == [0, 1, 2, 3, 4, 5, 6, 7] &&
                            r1_data == [8, 9, 10, 11, 12, 13, 14, 15] &&
                            r2_data == [16, 17, 18, 19, 20, 21, 22, 23];
                 });

        assert!(block_on(echo.join4(consume_a.map_err(|_| unreachable!()),
                                    send_all.map_err(|_| unreachable!()),
                                    receive_all.map_err(|_| unreachable!()))
                             .map(|(_, _, _, worked)| assert!(worked)))
                        .is_ok());
    }

    #[test]
    fn test_sync() {
        let (writer_a, reader_a) = ring_buffer(2);
        let (writer_b, reader_b) = ring_buffer(2);

        let (a_in, mut a_out, _) = muxrpc(reader_a, writer_b);
        let (b_in, b_out, _) = muxrpc(reader_b, writer_a);

        let echo = b_in.for_each(|(names, args, in_rpc)| {
                assert_eq!(names,
                           vec!["foo".to_string(), "bar".to_string()].into_boxed_slice());
                match in_rpc {
                    IncomingRpc::Sync(in_sync) => {
                        in_sync.respond(&args).map_err(|_| unreachable!())
                    }
                    _ => unreachable!(),
                }
            })
            .and_then(|_| b_out.close().map_err(|_| unreachable!()));

        let consume_a = a_in.for_each(|_| ok(()));

        let (req0, res0) = a_out.sync::<_, [u8; 8], i32>(&TestRpc([0, 1, 2, 3, 4, 5, 6, 7]));
        let (req1, res1) = a_out.sync::<_, [u8; 8], i32>(&TestRpc([8, 9, 10, 11, 12, 13, 14, 15]));
        let (req2, res2) = a_out.sync::<_, [u8; 8], i32>(&TestRpc([16, 17, 18, 19, 20, 21, 22,
                                                                   23]));

        let send_all = req0.join3(req1, req2).and_then(|_| a_out.close());

        let receive_all = res0.join3(res1, res2)
            .map(|(r0_data, r1_data, r2_data)| {
                     return r0_data == [0, 1, 2, 3, 4, 5, 6, 7] &&
                            r1_data == [8, 9, 10, 11, 12, 13, 14, 15] &&
                            r2_data == [16, 17, 18, 19, 20, 21, 22, 23];
                 });

        assert!(block_on(echo.join4(consume_a.map_err(|_| unreachable!()),
                                    send_all.map_err(|_| unreachable!()),
                                    receive_all.map_err(|_| unreachable!()))
                             .map(|(_, _, _, worked)| assert!(worked)))
                        .is_ok());
    }

    #[test]
    fn test_sink() {
        let (writer_a, reader_a) = ring_buffer(2);
        let (writer_b, reader_b) = ring_buffer(2);

        let (a_in, mut a_out, _) = muxrpc(reader_a, writer_b);
        let (b_in, b_out, _) = muxrpc(reader_b, writer_a);

        let echo = b_in.fold(true, |acc, (names, _, in_rpc)| {
                assert_eq!(names,
                           vec!["foo".to_string(), "bar".to_string()].into_boxed_slice());
                match in_rpc {
                    IncomingRpc::Sink(rpc_stream) => {
                        rpc_stream
                            .collect()
                            .map(move |data| {
                                     acc && data == vec![Value::Bool(true), Value::Bool(false)]
                                 })
                            .map_err(|_| RpcError::NotJson)
                    }
                    _ => unreachable!(),
                }
            })
            .and_then(|worked| {
                          b_out
                              .close()
                              .map(move |_| worked)
                              .map_err(|_| unreachable!())
                      });

        let consume_a = a_in.for_each(|_| ok(()));

        let out_sink0 = a_out.sink(&TestRpc([0, 1, 2, 3, 4, 5, 6, 7]));
        let out0 =
            out_sink0.and_then(|rpc_sink| {
                                   rpc_sink.send_all(iter_ok::<_, Option<io::Error>>(vec![Ok(Value::Bool(true)),
                                                                          Ok(Value::Bool(false))]))
                                                                          .and_then(|(rpc_sink, _)| close(rpc_sink))
                               });
        let out_sink1 = a_out.sink(&TestRpc([0, 1, 2, 3, 4, 5, 6, 99]));
        let out1 =
            out_sink1.and_then(|rpc_sink| {
                                   rpc_sink.send_all(iter_ok::<_, Option<io::Error>>(vec![Ok(Value::Bool(true)),
                                                                          Ok(Value::Bool(false))]))
                                                                          .and_then(|(rpc_sink, _)| close(rpc_sink))
                               });

        let send_all = out0.join(out1).and_then(|_| a_out.close());

        assert!(block_on(echo.join3(consume_a.map_err(|_| unreachable!()),
                                    send_all.map_err(|_| unreachable!()))
                             .map(|(worked, _, _)| worked))
                        .is_ok());
    }

    #[test]
    fn test_source() {
        let (writer_a, reader_a) = ring_buffer(2);
        let (writer_b, reader_b) = ring_buffer(2);

        let (a_in, mut a_out, _) = muxrpc(reader_a, writer_b);
        let (b_in, b_out, _) = muxrpc(reader_b, writer_a);

        let echo = b_in.for_each(|(names, _, in_rpc)| {
                assert_eq!(names,
                           vec!["foo".to_string(), "bar".to_string()].into_boxed_slice());
                match in_rpc {
                    IncomingRpc::Source(rpc_sink) => {
                        rpc_sink
                            .send_all(iter_ok::<_, Option<io::Error>>(vec![Ok(Value::Bool(true)),
                                                                           Ok(Value::Bool(false))]))
                            .and_then(|(rpc_sink, _)| close(rpc_sink))
                            .map_err(|_| unreachable!())
                            .map(|_| ())
                    }
                    _ => unreachable!(),
                }
            })
            .and_then(|_| b_out.close().map_err(|_| unreachable!()));

        let consume_a = a_in.for_each(|_| ok(()));

        let (out_source0, stream0) =
            a_out.source::<_, bool, i32>(&TestRpc([0, 1, 2, 3, 4, 5, 6, 7]));
        let stream0 = stream0.collect().map(|data| data == vec![true, false]);

        let (out_source1, stream1) =
            a_out.source::<_, bool, i32>(&TestRpc([0, 1, 2, 3, 4, 5, 6, 99]));
        let stream1 = stream1.collect().map(|data| data == vec![true, false]);

        let send_all = out_source0.join(out_source1).and_then(|_| a_out.close());
        let process_all = stream0.join(stream1);

        assert!(block_on(echo.join4(consume_a.map_err(|_| unreachable!()),
                                    send_all.map_err(|_| unreachable!()),
                                    process_all.map_err(|err| panic!("{:?}", err)))
                             .map(|(_, _, _, (worked0, worked1))| {
                                      assert!(worked0 && worked1)
                                  }))
                        .is_ok());
    }
}