Skip to content

Commit ebada19

Browse files
committed
feat: use block_on from futures
1 parent 842d685 commit ebada19

File tree

3 files changed

+82
-73
lines changed

3 files changed

+82
-73
lines changed

examples/dcutr/Cargo.lock

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/dcutr/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dcutr-example"
3-
version = "0.0.0"
3+
version = "0.1.0"
44
authors = ["Calimero Limited <info@calimero.network>"]
55
edition = "2021"
66
repository = "https://github.com/calimero-network/relay-server"
@@ -10,7 +10,8 @@ license = "MIT OR Apache-2.0"
1010
camino = "1.1.6"
1111
clap = { version = "4.5.4", features = ["derive", "env"] }
1212
eyre = "0.6.12"
13-
futures-util = "0.3.30"
13+
futures = "0.3.30"
14+
futures-timer = "3.0"
1415
libp2p = { version = "0.53.2", features = [
1516
"dcutr",
1617
"dns",

examples/dcutr/src/main.rs

+77-70
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::str::FromStr;
22
use std::{error::Error, time::Duration};
33

44
use clap::Parser;
5-
use libp2p::futures::prelude::*;
5+
use futures::{executor::block_on, future::FutureExt, stream::StreamExt};
66
use libp2p::swarm::{NetworkBehaviour, SwarmEvent};
77
use libp2p::{dcutr, identify, identity, noise, ping, relay, yamux, Multiaddr, PeerId};
88
use multiaddr::Protocol;
@@ -99,54 +99,61 @@ async fn main() -> Result<(), Box<dyn Error>> {
9999
.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap())
100100
.unwrap();
101101

102-
// Reference: https://github.com/libp2p/rust-libp2p/blob/60fd566a955a33c42a6ab6eefc1f0fedef9f8b83/examples/dcutr/src/main.rs#L118
103-
loop {
104-
tokio::select! {
105-
Some(event) = swarm.next() => {
106-
match event {
107-
SwarmEvent::NewListenAddr { address, .. } => {
108-
info!(%address, "Listening on address");
102+
// Wait to listen on all interfaces.
103+
block_on(async {
104+
let mut delay = futures_timer::Delay::new(std::time::Duration::from_secs(1)).fuse();
105+
loop {
106+
futures::select! {
107+
event = swarm.next() => {
108+
match event.unwrap() {
109+
SwarmEvent::NewListenAddr { address, .. } => {
110+
info!(%address, "Listening on address");
111+
}
112+
event => panic!("{event:?}"),
109113
}
110-
event => panic!("{event:?}"),
111114
}
112-
}
113-
_ = tokio::time::sleep(Duration::from_secs(1)) => {
114-
// Likely listening on all interfaces now, thus continuing by breaking the loop.
115-
break;
115+
_ = delay => {
116+
// Likely listening on all interfaces now, thus continuing by breaking the loop.
117+
break;
118+
}
116119
}
117120
}
118-
}
121+
});
119122

120123
// Connect to the relay server. Not for the reservation or relayed connection, but to (a) learn
121124
// our local public address and (b) enable a freshly started relay to learn its public address.
122125
swarm.dial(opt.relay_address.clone()).unwrap();
123-
124-
let mut learned_observed_addr = false;
125-
let mut told_relay_observed_addr = false;
126-
loop {
127-
match swarm.next().await.unwrap() {
128-
SwarmEvent::NewListenAddr { .. } => {}
129-
SwarmEvent::Dialing { .. } => {}
130-
SwarmEvent::ConnectionEstablished { .. } => {}
131-
SwarmEvent::Behaviour(BehaviourEvent::Ping(_)) => {}
132-
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Sent { .. })) => {
133-
info!("Told relay its public address");
134-
told_relay_observed_addr = true;
135-
}
136-
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Received {
137-
info: identify::Info { observed_addr, .. },
138-
..
139-
})) => {
140-
info!(address=%observed_addr, "Relay told us our observed address");
141-
learned_observed_addr = true;
126+
block_on(async {
127+
let mut learned_observed_addr = false;
128+
let mut told_relay_observed_addr = false;
129+
130+
loop {
131+
match swarm.next().await.unwrap() {
132+
SwarmEvent::NewListenAddr { .. } => {}
133+
SwarmEvent::Dialing { .. } => {}
134+
SwarmEvent::ConnectionEstablished { .. } => {}
135+
SwarmEvent::Behaviour(BehaviourEvent::Ping(_)) => {}
136+
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Sent {
137+
..
138+
})) => {
139+
info!("Told relay its public address");
140+
told_relay_observed_addr = true;
141+
}
142+
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Received {
143+
info: identify::Info { observed_addr, .. },
144+
..
145+
})) => {
146+
info!(address=%observed_addr, "Relay told us our observed address");
147+
learned_observed_addr = true;
148+
}
149+
event => panic!("{event:?}"),
142150
}
143-
event => panic!("{event:?}"),
144-
}
145151

146-
if learned_observed_addr && told_relay_observed_addr {
147-
break;
152+
if learned_observed_addr && told_relay_observed_addr {
153+
break;
154+
}
148155
}
149-
}
156+
});
150157

151158
match opt.mode {
152159
Mode::Dial => {
@@ -165,40 +172,40 @@ async fn main() -> Result<(), Box<dyn Error>> {
165172
}
166173
}
167174

168-
loop {
169-
match swarm.next().await.unwrap() {
170-
SwarmEvent::NewListenAddr { address, .. } => {
171-
info!(%address, "Listening on address");
172-
}
173-
SwarmEvent::Behaviour(BehaviourEvent::RelayClient(
174-
relay::client::Event::ReservationReqAccepted { .. },
175-
)) => {
176-
assert!(opt.mode == Mode::Listen);
177-
info!("Relay accepted our reservation request");
178-
}
179-
SwarmEvent::Behaviour(BehaviourEvent::RelayClient(event)) => {
180-
info!(?event)
181-
}
182-
SwarmEvent::Behaviour(BehaviourEvent::Dcutr(event)) => {
183-
info!(?event)
184-
}
185-
SwarmEvent::Behaviour(BehaviourEvent::Identify(event)) => {
186-
info!(?event)
187-
}
188-
SwarmEvent::Behaviour(BehaviourEvent::Ping(event)) => {
189-
info!(?event)
190-
}
191-
SwarmEvent::ConnectionEstablished {
192-
peer_id, endpoint, ..
193-
} => {
194-
info!(peer=%peer_id, ?endpoint, "Established new connection");
195-
}
196-
SwarmEvent::OutgoingConnectionError { peer_id, error, .. } => {
197-
info!(peer=?peer_id, "Outgoing connection failed: {error}");
175+
block_on(async {
176+
loop {
177+
match swarm.next().await.unwrap() {
178+
SwarmEvent::NewListenAddr { address, .. } => {
179+
info!(%address, "Listening on address");
180+
}
181+
SwarmEvent::Behaviour(BehaviourEvent::RelayClient(
182+
relay::client::Event::ReservationReqAccepted { .. },
183+
)) => {
184+
assert!(opt.mode == Mode::Listen);
185+
info!("Relay accepted our reservation request");
186+
}
187+
SwarmEvent::Behaviour(BehaviourEvent::RelayClient(event)) => {
188+
info!(?event)
189+
}
190+
SwarmEvent::Behaviour(BehaviourEvent::Dcutr(event)) => {
191+
info!(?event)
192+
}
193+
SwarmEvent::Behaviour(BehaviourEvent::Identify(event)) => {
194+
info!(?event)
195+
}
196+
SwarmEvent::Behaviour(BehaviourEvent::Ping(_)) => {}
197+
SwarmEvent::ConnectionEstablished {
198+
peer_id, endpoint, ..
199+
} => {
200+
info!(peer=%peer_id, ?endpoint, "Established new connection");
201+
}
202+
SwarmEvent::OutgoingConnectionError { peer_id, error, .. } => {
203+
info!(peer=?peer_id, "Outgoing connection failed: {error}");
204+
}
205+
_ => {}
198206
}
199-
_ => {}
200207
}
201-
}
208+
})
202209
}
203210

204211
fn generate_ed25519(secret_key_seed: u8) -> identity::Keypair {

0 commit comments

Comments
 (0)