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
//! # A future based vert.x tcp eventbus client implementation for Rust.
//! `Eventbus` is the core struct to communicate with [vert.x](https://vertx.io/) eventbus.
//! Any further operation can be represented by a stream or future.
//! The body of a message is a json value [serde_json::value](https://docs.serde.rs/serde_json/value/enum.Value.html)
//! # Example
//! ```
//! let task = future::Eventbus::connect(IpAddr::from_str("127.0.0.1").unwrap(), 12345);
//! let task = task.and_then(|(eventbus, readstream, writestream)| {
//!     tokio::spawn(readstream.into_future().map(|_| ()).map_err(|e| ()));
//!     tokio::spawn(writestream.into_future().map(|_| ()).map_err(|e| ()));
//!     futures::future::ok(eventbus)
//! });
//! let task = task.and_then(|eventbus: Eventbus| {
//!     let test_reply = eventbus.send_reply("test".to_string(), json!({
//!         "aaaa":"bbbb"
//!     })).unwrap().and_then(|response| {
//!         println!("{:?}", response);
//!         futures::future::ok(())
//!     });
//!     test_reply
//! });
//! tokio::run(task.map_err(|e| ()));
//! ```
extern crate byteorder;
extern crate bytes;
extern crate crossbeam;
#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate serde_json;

pub mod response;
pub mod request;
pub mod future;
pub mod codec;

#[cfg(test)]
mod tests {
    use std::net::IpAddr;
    use std::str::FromStr;

    use futures::future::{Future, IntoFuture};
    use tokio::prelude::stream::Stream;

    use crate::future;
    use crate::future::Eventbus;

    /// Current Output:
        /// running 1 test
        /// MessageFail(ResponseFailObject { failureCode: 1, failureType: "RECIPIENT_FAILURE", message: "test fail message", sourceAddress: "test" })
    #[test]
    fn test_send() {
        let task = future::Eventbus::connect(IpAddr::from_str("127.0.0.1").unwrap(), 12345);
        let task = task.and_then(|(eventbus, readstream, writestream)| {
            tokio::spawn(readstream.into_future().map(|_| ()).map_err(|e| ()));
            tokio::spawn(writestream.into_future().map(|_| ()).map_err(|e| ()));
            futures::future::ok(eventbus)
        });
        let task = task.and_then(|eventbus: Eventbus| {
            let test_reply = eventbus.send_reply("test".to_string(), json!({
                "aaaa":"bbbb"
            })).unwrap().and_then(|response| {
                println!("{:?}", response);
                futures::future::ok(())
            });
            test_reply
        });
        tokio::run(task.map_err(|e| ()));
    }
}