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
// Copyright (c) 2015-2017 Contributors as noted in the AUTHORS file.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>
// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// This file may not be copied, modified, or distributed except according to those terms.

pub mod async;
pub mod tcp;
pub mod ipc;
pub mod endpoint;
pub mod pipe;
pub mod acceptor;

use std::io::Result;

pub struct Destination<'a> {
    pub addr: &'a str,
    pub pids: (u16, u16),
    pub tcp_no_delay: bool,
    pub recv_max_size: u64
}

pub trait Transport {
    fn connect(&self, dest: &Destination) -> Result<Box<pipe::Pipe>>;
    fn bind(&self, dest: &Destination) -> Result<Box<acceptor::Acceptor>>;
}

#[cfg(test)]
mod tests {
    use std::fmt;

    use mio;

    use transport::*;

    pub struct TestPipeContext {
        registrations: Vec<(mio::Ready, mio::PollOpt)>,
        reregistrations: Vec<(mio::Ready, mio::PollOpt)>,
        deregistrations: usize,
        raised_events: Vec<pipe::Event>
    }

    impl TestPipeContext {
        pub fn new() -> TestPipeContext {
            TestPipeContext {
                registrations: Vec::new(),
                reregistrations: Vec::new(),
                deregistrations: 0,
                raised_events: Vec::new()
            }
        }
        pub fn get_registrations(&self) -> &[(mio::Ready, mio::PollOpt)] { &self.registrations }
        pub fn get_reregistrations(&self) -> &[(mio::Ready, mio::PollOpt)] { &self.reregistrations }
        pub fn get_deregistrations(&self) -> usize { self.deregistrations }
        pub fn get_raised_events(&self) -> &[pipe::Event] { &self.raised_events }
    }

    impl endpoint::EndpointRegistrar for TestPipeContext {
        fn register(&mut self, _: &mio::Evented, interest: mio::Ready, opt: mio::PollOpt) {
            self.registrations.push((interest, opt));
        }
        fn reregister(&mut self, _: &mio::Evented, interest: mio::Ready, opt: mio::PollOpt) {
            self.reregistrations.push((interest, opt));
        }
        fn deregister(&mut self, _: &mio::Evented) {
            self.deregistrations += 1;
        }
    }

    impl fmt::Debug for TestPipeContext {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            write!(f, "TestPipeContext")
        }
    }

    impl pipe::Context for TestPipeContext {
        fn raise(&mut self, evt: pipe::Event) {
            self.raised_events.push(evt);
        }
    }
}