bind_and_connect/
bind_and_connect.rs1use nseqe::Node;
2use nseqe::action::Sleep;
3use nseqe::protocol::ip::{
4 Bind, Connect, ConnectPredicate, MessagesPredicate, ReceivePredicate, Send, SendMode, Wait,
5 WaitEvent,
6};
7use tokio::task::JoinSet;
8use tracing::{Instrument, span};
9
10#[tokio::main]
11async fn main() {
12 tracing_subscriber::fmt()
14 .with_max_level(tracing::Level::DEBUG)
15 .init();
16
17 let mut node_1 = create_node_1();
18 let mut node_2 = create_node_2();
19
20 let mut set = JoinSet::new();
21
22 set.spawn(
23 async move {
24 node_1.start().await;
25 }
26 .instrument(span!(tracing::Level::INFO, "node_1")),
27 );
28
29 set.spawn(
30 async move {
31 node_2.start().await;
32 }
33 .instrument(span!(tracing::Level::INFO, "node_2")),
34 );
35
36 while let Some(res) = set.join_next().await {
37 match res {
38 Ok(()) => {
39 tracing::info!("Node finished successfully");
40 }
41 Err(e) => {
42 tracing::error!("Node failed: {:?}", e);
43 }
44 }
45 }
46}
47
48fn create_node_1() -> Node {
49 let mut node = Node::new("test-node-1");
50 let bind_action = Bind::new("192.168.1.10:3000".parse().unwrap());
51 let sleep_action = Sleep::new(1000);
52 let connection_action = Connect::new(
53 "192.168.1.10:0".parse().unwrap(),
54 "192.168.1.11:4000".parse().unwrap(),
55 1000,
56 );
57 let send_unicast_action = Send::new(
58 SendMode::Unicast,
59 "192.168.1.10:3000".parse().unwrap(),
60 "192.168.1.11:4000".parse().unwrap(),
61 vec![1, 2, 3, 4],
62 );
63
64 let send_broadcast_action = Send::new(
65 SendMode::Broadcast,
66 "192.168.1.10:0".parse().unwrap(),
67 "192.168.1.255:49999".parse().unwrap(),
68 vec![1, 2, 3, 4],
69 );
70
71 let wait_connection_action = Wait::new(WaitEvent::Connection(ConnectPredicate::new(
72 "192.168.1.11:0".parse().unwrap(),
73 "192.168.1.10:3000".parse().unwrap(),
74 )));
75
76 let wait_message_action = Wait::new(WaitEvent::Messages(ReceivePredicate::new(vec![
77 MessagesPredicate {
78 from: "192.168.1.11:0".parse().unwrap(),
79 to: "192.168.1.10:3000".parse().unwrap(),
80 buffer: vec![1, 1],
81 },
82 ])));
83
84 node.add_action(bind_action);
85 node.add_action(sleep_action.clone());
86 node.add_action(send_broadcast_action);
87 node.add_action(wait_connection_action);
88 node.add_action(wait_message_action);
89 node.add_action(connection_action);
90 node.add_action(send_unicast_action);
91 node.add_action(sleep_action);
92
93 node
94}
95
96fn create_node_2() -> Node {
97 let mut node = Node::new("test-node-2");
98 let bind_action = Bind::new("192.168.1.11:4000".parse().unwrap());
99 let sleep_action = Sleep::new(5000);
100 let connection_action = Connect::new(
101 "192.168.1.11:0".parse().unwrap(),
102 "192.168.1.10:3000".parse().unwrap(),
103 1000,
104 );
105
106 let send_unicast_action = Send::new(
107 SendMode::Unicast,
108 "192.168.1.11:4000".parse().unwrap(),
109 "192.168.1.10:3000".parse().unwrap(),
110 vec![1, 1],
111 );
112
113 node.add_action(bind_action);
114 node.add_action(sleep_action.clone());
115 node.add_action(connection_action);
116 node.add_action(sleep_action);
117 node.add_action(send_unicast_action);
118
119 node
120}