1
- /*
2
1
mod common;
3
2
4
3
use async_std:: {
5
4
channel,
6
5
task,
7
6
} ;
7
+ use chrono:: { DateTime , Utc } ;
8
8
use common:: {
9
9
action_dispatcher,
10
10
deserialize,
11
11
PeerEvent ,
12
12
PeerManager ,
13
13
} ;
14
14
use tp2p:: {
15
- Sync ,
15
+ event :: Event ,
16
16
message:: MessageWrapped ,
17
17
peer:: { Keypair , PeeringConfig , ConfirmationMode , SubscriptionConfig } ,
18
+ sync:: Sync ,
18
19
} ;
19
20
21
+ fn now ( ) -> DateTime < Utc > {
22
+ Utc :: now ( )
23
+ }
24
+
20
25
macro_rules! main_loop {
21
- ($sync:expr, $peer:expr, $tx:expr, $rx:expr, $name:expr) => {
26
+ ( $sync: expr, $peer: expr, $tx: expr, $rx: expr, $name: expr, $U : ty , $T : ty , $S : ty ) => {
22
27
loop {
23
28
match $rx. try_recv( ) {
24
29
Ok ( PeerEvent :: Connect ( from) ) => {
25
30
println!( concat!( $name, ": new connection! {:?}" ) , from) ;
26
31
}
27
32
Ok ( PeerEvent :: Recv ( from, message_bytes) ) => {
28
33
let msg: MessageWrapped = deserialize( message_bytes. as_ref( ) ) ?;
29
- //println!(concat!($name, ": recv: {:?} -- {:?}"), from, msg); // DEBUG: remove
30
- let actions = $sync.process_incoming_message(&msg, from)
31
- .map_err(|err| format!("process: {:?}", err))?;
34
+ let actions = match & msg {
35
+ MessageWrapped :: Init ( pubkey) => {
36
+ $sync. process_init_message( & msg, & from, & now( ) ) ?
37
+ }
38
+ MessageWrapped :: Sealed ( sealed) => {
39
+ let message_opened = $sync. unwrap_incoming_message( sealed) ?;
40
+ match message_opened. body( ) {
41
+ Event :: Hello => {
42
+ $sync. process_event_hello( & message_opened, sealed. pubkey_sender( ) , & from, & now( ) ) ?
43
+ }
44
+ Event :: PeerInit { .. } => {
45
+ $sync. process_event_peer_init( & message_opened, sealed. pubkey_sender( ) , & from, & now( ) ) ?
46
+ }
47
+ Event :: PeerConfirm { .. } => {
48
+ $sync. process_event_peer_confirm( & message_opened, sealed. pubkey_sender( ) , & now( ) ) ?
49
+ }
50
+ Event :: Ping => {
51
+ $sync. process_event_ping( & message_opened, sealed. pubkey_sender( ) , & now( ) ) ?
52
+ }
53
+ Event :: Pong => {
54
+ $sync. process_event_pong( & message_opened, sealed. pubkey_sender( ) , & now( ) ) ?
55
+ }
56
+ Event :: QueryMessagesByID { ids } => {
57
+ let messages = {
58
+ drop( ids) ;
59
+ vec![ ]
60
+ } ;
61
+ $sync. process_event_query_messages_by_id( & message_opened, sealed. pubkey_sender( ) , & messages) ?
62
+ }
63
+ Event :: QueryMessagesByDepth { topic, depth } => {
64
+ let messages = {
65
+ drop( topic) ;
66
+ drop( depth) ;
67
+ vec![ ]
68
+ } ;
69
+ $sync. process_event_query_messages_by_depth( & message_opened, sealed. pubkey_sender( ) , & messages) ?
70
+ }
71
+ Event :: Subscribe ( ..) => {
72
+ }
73
+ _ => panic!( "oi" ) ,
74
+ }
75
+ }
76
+ } ;
77
+
32
78
for action in actions {
33
79
println!( concat!( $name, ": action -- {:?}" ) , action) ;
34
- action_dispatcher(&mut $sync, &$tx, action).await?;
80
+ action_dispatcher:: <$U , $T , $S> ( & mut $sync, & $tx, action) . await ?;
35
81
}
36
82
}
37
83
Err ( channel:: TryRecvError :: Closed ) => {
@@ -46,7 +92,6 @@ macro_rules! main_loop {
46
92
47
93
#[ async_std:: test]
48
94
async fn peer_connect ( ) -> Result < ( ) , String > {
49
- env_logger::init();
50
95
let peer1_task = task:: spawn ( async move {
51
96
let keypair = Keypair :: new_random ( ) ;
52
97
let peering_config = PeeringConfig :: new (
@@ -61,7 +106,7 @@ async fn peer_connect() -> Result<(), String> {
61
106
let peer_task = task:: spawn ( async move {
62
107
peer. start ( "127.0.0.1" , 50020 ) . await . expect ( "error running peer" ) ;
63
108
} ) ;
64
- main_loop! { sync , peer, tx, rx, "peer1" }
109
+ main_loop ! { sync , peer, tx, rx, "peer1" , ( ) , ( ) , ( ) }
65
110
66
111
peer_task. await ;
67
112
let res: Result < ( ) , String > = Ok ( ( ) ) ;
@@ -84,12 +129,12 @@ async fn peer_connect() -> Result<(), String> {
84
129
peer. start ( "127.0.0.1" , 50021 ) . await . expect ( "error running peer" ) ;
85
130
} ) ;
86
131
87
- let actions = sync.init_comm( "127.0.0.1:50020").expect("peer_init failed");
132
+ let actions = sync. init_comm :: < ( ) , ( ) > ( "127.0.0.1:50020" , & now ( ) ) . expect ( "peer_init failed" ) ;
88
133
for action in actions {
89
134
println ! ( "peer2: action -- {:?}" , action) ;
90
135
action_dispatcher ( & mut sync, & tx, action) . await ?;
91
136
}
92
- main_loop! { sync , peer, tx, rx, "peer2" }
137
+ main_loop ! { sync , peer, tx, rx, "peer2" , ( ) , ( ) , ( ) }
93
138
94
139
peer_task. await ;
95
140
let res: Result < ( ) , String > = Ok ( ( ) ) ;
@@ -101,5 +146,4 @@ async fn peer_connect() -> Result<(), String> {
101
146
102
147
Ok ( ( ) )
103
148
}
104
- */
105
149
0 commit comments