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
//! A non-blocking IO and easy-to-use Rust crate for constructing simple distributed preject. The crate contains several building blocks that can be use to construct a distributed system program.
//! The establish methods in the `node` module can establish connection between two nodes. Once established the connection, the nodes add each other into neighbors list and continuesly send `heartbeat` message to each other to check for activity.
//! You could defined the logic on their your based on the `senduserdefinedmsg` methods easily. You just need to register a handler methods with a particular name and you can define specific logic of handling logic with this specific name. Of course you can choose `sendecdsaencmsg` or `sendrsaencmsg` to achive the same function with a safer message communication.
//!
//! # Example
//! This is an example code for initiating the node instance. This example takes 2 arguments, the
//! first is the id of node you choose, the second is the local network port you choose. For
//! example, 0 127.0.0.1:8080 means you choose to the id as 0 for the node and bind the node to
//! port 8080 on this machine. This is also the default input(which means you don't input any
//! argument) of this code.
//! ````
//! #[tokio::main()]
//! async fn main() -> std::io::Result<()> {
//! // The first argument is the id of node.
//! // The second is the addr of node.
//! let args: Vec<String> = env::args().collect();
//!
//! // Initiate the env_logger.
//! env_logger::init();
//!
//! // If there is no argument, use the configuration in config.rs
//! let node = if args.len() > 2 {
//! Arc::new(Node::new(
//! args[1]
//! .parse::<u64>()
//! .expect("The first argument must can be converted to u64."),
//! &args[2],
//! ))
//! } else {
//! Arc::new(Node::default())
//! };
//!
//! // Run the node.
//! node.run().await;
//!
//! tokio::signal::ctrl_c()
//! .await
//! .expect("Failed to listen for ctrl_c");
//!
//! Ok(())
//! }
//! ````
//! This is an example using handler.
//! ````
//! #[derive(Serialize, Deserialize, Debug)]
//! struct Point {
//! id: u64,
//! x: i32,
//! y: i32,
//! }
//!
//! pub struct CustomHandler;
//!
//! #[async_trait]
//! impl UserMessageHandler for CustomHandler {
//! async fn handle(&self, message: UserDefined, connection: Arc<Node>) -> EasyDist::Result<()> {
//! let point: Point = serde_json::from_slice(&message.get_data()).unwrap();
//! println!("ping received: {:?}.", point);
//! let data = vec![];
//! connection
//! .send_ecdsa_signed_msg(point.id, "OK", &data)
//! .await?;
//! Ok(())
//! }
//! }
//!
//! #[tokio::main()]
//! async fn main() -> std::io::Result<()> {
//! // The first argument is the id of node.
//! // The second is the addr of node.
//! let args: Vec<String> = env::args().collect();
//!
//! // Initiate the env_logger.
//! env_logger::init();
//!
//! // If there is no argument, use the configuration in config.rs
//! let node = if args.len() > 2 {
//! Arc::new(Node::new(
//! args[1]
//! .parse::<u64>()
//! .expect("The first argument must can be converted to u64."),
//! &args[2],
//! ))
//! } else {
//! Arc::new(Node::default())
//! };
//!
//! let handle = Arc::new(CustomHandler {});
//! let _ = node.clone().register_user_handler("ping", handle).await;
//!
//! // Run the node.
//! node.run().await;
//!
//! tokio::signal::ctrl_c()
//! .await
//! .expect("Failed to listen for ctrl_c");
//! println!("Shutting down...");
//!
//! Ok(())
//! }
//! ````
pub use Connection;
pub use Message;
pub use Frame;
use ;
use Db;
pub use State;
pub use server;
pub use client;
pub use Node;
pub type Error = ;
pub type Result<T> = Result;