@@ -2,7 +2,7 @@ use std::str::FromStr;
2
2
use std:: { error:: Error , time:: Duration } ;
3
3
4
4
use clap:: Parser ;
5
- use futures :: { executor :: block_on , future :: FutureExt , stream :: StreamExt } ;
5
+ use libp2p :: futures :: prelude :: * ;
6
6
use libp2p:: swarm:: { NetworkBehaviour , SwarmEvent } ;
7
7
use libp2p:: { dcutr, identify, identity, noise, ping, relay, yamux, Multiaddr , PeerId } ;
8
8
use multiaddr:: Protocol ;
@@ -101,60 +101,54 @@ async fn main() -> Result<(), Box<dyn Error>> {
101
101
. unwrap ( ) ;
102
102
103
103
// Wait to listen on all interfaces.
104
- block_on ( async {
105
- let mut delay = futures_timer:: Delay :: new ( std:: time:: Duration :: from_secs ( 1 ) ) . fuse ( ) ;
106
- loop {
107
- futures:: select! {
108
- event = swarm. next( ) => {
109
- match event. unwrap( ) {
110
- SwarmEvent :: NewListenAddr { address, .. } => {
111
- info!( %address, "Listening on address" ) ;
112
- }
113
- event => panic!( "{event:?}" ) ,
104
+ loop {
105
+ tokio:: select! {
106
+ Some ( event) = swarm. next( ) => {
107
+ match event {
108
+ SwarmEvent :: NewListenAddr { address, .. } => {
109
+ info!( %address, "Listening on address" ) ;
114
110
}
111
+ event => panic!( "{event:?}" ) ,
115
112
}
116
- _ = delay => {
117
- // Likely listening on all interfaces now, thus continuing by breaking the loop.
118
- break ;
119
- }
113
+ }
114
+ _ = tokio :: time :: sleep ( Duration :: from_secs ( 1 ) ) => {
115
+ // Likely listening on all interfaces now, thus continuing by breaking the loop.
116
+ break ;
120
117
}
121
118
}
122
- } ) ;
119
+ }
123
120
124
121
// Connect to the relay server. Not for the reservation or relayed connection, but to (a) learn
125
122
// our local public address and (b) enable a freshly started relay to learn its public address.
126
123
swarm. dial ( opt. relay_address . clone ( ) ) . unwrap ( ) ;
127
- block_on ( async {
128
- let mut learned_observed_addr = false ;
129
- let mut told_relay_observed_addr = false ;
130
-
131
- loop {
132
- match swarm. next ( ) . await . unwrap ( ) {
133
- SwarmEvent :: NewListenAddr { .. } => { }
134
- SwarmEvent :: Dialing { .. } => { }
135
- SwarmEvent :: ConnectionEstablished { .. } => { }
136
- SwarmEvent :: Behaviour ( BehaviourEvent :: Ping ( _) ) => { }
137
- SwarmEvent :: Behaviour ( BehaviourEvent :: Identify ( identify:: Event :: Sent {
138
- ..
139
- } ) ) => {
140
- info ! ( "Told relay its public address" ) ;
141
- told_relay_observed_addr = true ;
142
- }
143
- SwarmEvent :: Behaviour ( BehaviourEvent :: Identify ( identify:: Event :: Received {
144
- info : identify:: Info { observed_addr, .. } ,
145
- ..
146
- } ) ) => {
147
- info ! ( address=%observed_addr, "Relay told us our observed address" ) ;
148
- learned_observed_addr = true ;
149
- }
150
- event => panic ! ( "{event:?}" ) ,
151
- }
152
124
153
- if learned_observed_addr && told_relay_observed_addr {
154
- break ;
125
+ let mut learned_observed_addr = false ;
126
+ let mut told_relay_observed_addr = false ;
127
+
128
+ loop {
129
+ match swarm. next ( ) . await . unwrap ( ) {
130
+ SwarmEvent :: NewListenAddr { .. } => { }
131
+ SwarmEvent :: Dialing { .. } => { }
132
+ SwarmEvent :: ConnectionEstablished { .. } => { }
133
+ SwarmEvent :: Behaviour ( BehaviourEvent :: Ping ( _) ) => { }
134
+ SwarmEvent :: Behaviour ( BehaviourEvent :: Identify ( identify:: Event :: Sent { .. } ) ) => {
135
+ info ! ( "Told relay its public address" ) ;
136
+ told_relay_observed_addr = true ;
137
+ }
138
+ SwarmEvent :: Behaviour ( BehaviourEvent :: Identify ( identify:: Event :: Received {
139
+ info : identify:: Info { observed_addr, .. } ,
140
+ ..
141
+ } ) ) => {
142
+ info ! ( address=%observed_addr, "Relay told us our observed address" ) ;
143
+ learned_observed_addr = true ;
155
144
}
145
+ event => panic ! ( "{event:?}" ) ,
156
146
}
157
- } ) ;
147
+
148
+ if learned_observed_addr && told_relay_observed_addr {
149
+ break ;
150
+ }
151
+ }
158
152
159
153
match opt. mode {
160
154
Mode :: Dial => {
@@ -173,63 +167,61 @@ async fn main() -> Result<(), Box<dyn Error>> {
173
167
}
174
168
}
175
169
176
- block_on ( async {
177
- let mut stdin = tokio:: io:: BufReader :: new ( tokio:: io:: stdin ( ) ) . lines ( ) ;
178
-
179
- loop {
180
- let event = tokio:: select! {
181
- Some ( event) = swarm. next( ) => event,
182
- Ok ( Some ( line) ) = stdin. next_line( ) => {
183
- match line. trim( ) {
184
- "peers" => {
185
- info!( "Connected peers: {}" , swarm. network_info( ) . num_peers( ) ) ;
186
- for peer in swarm. connected_peers( ) {
187
- info!( peer=%peer, "Connected peer" ) ;
188
- }
170
+ let mut stdin = tokio:: io:: BufReader :: new ( tokio:: io:: stdin ( ) ) . lines ( ) ;
171
+
172
+ loop {
173
+ let event = tokio:: select! {
174
+ Some ( event) = swarm. next( ) => event,
175
+ Ok ( Some ( line) ) = stdin. next_line( ) => {
176
+ match line. trim( ) {
177
+ "peers" => {
178
+ info!( "Connected peers: {}" , swarm. network_info( ) . num_peers( ) ) ;
179
+ for peer in swarm. connected_peers( ) {
180
+ info!( peer=%peer, "Connected peer" ) ;
189
181
}
190
- _ => info!( "Unknown command" ) ,
191
182
}
192
- continue ;
183
+ _ => info! ( "Unknown command" ) ,
193
184
}
194
- } ;
185
+ continue ;
186
+ }
187
+ } ;
195
188
196
- match event {
197
- SwarmEvent :: NewListenAddr { address, .. } => {
198
- info ! ( %address, "Listening on address" ) ;
199
- }
200
- SwarmEvent :: Behaviour ( BehaviourEvent :: RelayClient (
201
- relay:: client:: Event :: ReservationReqAccepted { .. } ,
202
- ) ) => {
203
- assert ! ( opt. mode == Mode :: Listen ) ;
204
- info ! ( "Relay accepted our reservation request" ) ;
205
- }
206
- SwarmEvent :: Behaviour ( BehaviourEvent :: RelayClient ( event) ) => {
207
- info ! ( ?event, "\x1b [33mrelay\x1b [0m" ) ;
208
- }
209
- SwarmEvent :: Behaviour ( BehaviourEvent :: Dcutr ( event) ) => {
210
- info ! ( ?event, "\x1b [32mdcutr\x1b [0m" ) ;
211
- }
212
- SwarmEvent :: Behaviour ( BehaviourEvent :: Identify ( event) ) => {
213
- info ! ( ?event, "\x1b [34midentify\x1b [0m" ) ;
214
- }
215
- SwarmEvent :: Behaviour ( BehaviourEvent :: Ping ( _) ) => { }
216
- SwarmEvent :: ConnectionEstablished {
217
- peer_id, endpoint, ..
218
- } => {
219
- info ! ( peer=%peer_id, ?endpoint, "Established new connection" ) ;
220
- }
221
- SwarmEvent :: ConnectionClosed {
222
- peer_id, endpoint, ..
223
- } => {
224
- info ! ( peer=%peer_id, ?endpoint, "Closed connection" ) ;
225
- }
226
- SwarmEvent :: OutgoingConnectionError { peer_id, error, .. } => {
227
- info ! ( peer=?peer_id, "Outgoing connection failed: {error}" ) ;
228
- }
229
- _ => { }
189
+ match event {
190
+ SwarmEvent :: NewListenAddr { address, .. } => {
191
+ info ! ( %address, "Listening on address" ) ;
192
+ }
193
+ SwarmEvent :: Behaviour ( BehaviourEvent :: RelayClient (
194
+ relay:: client:: Event :: ReservationReqAccepted { .. } ,
195
+ ) ) => {
196
+ assert ! ( opt. mode == Mode :: Listen ) ;
197
+ info ! ( "Relay accepted our reservation request" ) ;
230
198
}
199
+ SwarmEvent :: Behaviour ( BehaviourEvent :: RelayClient ( event) ) => {
200
+ info ! ( ?event, "\x1b [33mrelay\x1b [0m" ) ;
201
+ }
202
+ SwarmEvent :: Behaviour ( BehaviourEvent :: Dcutr ( event) ) => {
203
+ info ! ( ?event, "\x1b [32mdcutr\x1b [0m" ) ;
204
+ }
205
+ SwarmEvent :: Behaviour ( BehaviourEvent :: Identify ( event) ) => {
206
+ info ! ( ?event, "\x1b [34midentify\x1b [0m" ) ;
207
+ }
208
+ SwarmEvent :: Behaviour ( BehaviourEvent :: Ping ( _) ) => { }
209
+ SwarmEvent :: ConnectionEstablished {
210
+ peer_id, endpoint, ..
211
+ } => {
212
+ info ! ( peer=%peer_id, ?endpoint, "Established new connection" ) ;
213
+ }
214
+ SwarmEvent :: ConnectionClosed {
215
+ peer_id, endpoint, ..
216
+ } => {
217
+ info ! ( peer=%peer_id, ?endpoint, "Closed connection" ) ;
218
+ }
219
+ SwarmEvent :: OutgoingConnectionError { peer_id, error, .. } => {
220
+ info ! ( peer=?peer_id, "Outgoing connection failed: {error}" ) ;
221
+ }
222
+ _ => { }
231
223
}
232
- } )
224
+ }
233
225
}
234
226
235
227
fn generate_ed25519 ( secret_key_seed : u8 ) -> identity:: Keypair {
0 commit comments