Skip to content

Commit

Permalink
feat: use block_on from futures
Browse files Browse the repository at this point in the history
  • Loading branch information
fbozic committed May 7, 2024
1 parent 842d685 commit ebada19
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 73 deletions.
3 changes: 2 additions & 1 deletion examples/dcutr/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions examples/dcutr/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dcutr-example"
version = "0.0.0"
version = "0.1.0"
authors = ["Calimero Limited <info@calimero.network>"]
edition = "2021"
repository = "https://github.com/calimero-network/relay-server"
Expand All @@ -10,7 +10,8 @@ license = "MIT OR Apache-2.0"
camino = "1.1.6"
clap = { version = "4.5.4", features = ["derive", "env"] }
eyre = "0.6.12"
futures-util = "0.3.30"
futures = "0.3.30"
futures-timer = "3.0"
libp2p = { version = "0.53.2", features = [
"dcutr",
"dns",
Expand Down
147 changes: 77 additions & 70 deletions examples/dcutr/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::str::FromStr;
use std::{error::Error, time::Duration};

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

// Reference: https://github.com/libp2p/rust-libp2p/blob/60fd566a955a33c42a6ab6eefc1f0fedef9f8b83/examples/dcutr/src/main.rs#L118
loop {
tokio::select! {
Some(event) = swarm.next() => {
match event {
SwarmEvent::NewListenAddr { address, .. } => {
info!(%address, "Listening on address");
// Wait to listen on all interfaces.
block_on(async {
let mut delay = futures_timer::Delay::new(std::time::Duration::from_secs(1)).fuse();
loop {
futures::select! {
event = swarm.next() => {
match event.unwrap() {
SwarmEvent::NewListenAddr { address, .. } => {
info!(%address, "Listening on address");
}
event => panic!("{event:?}"),
}
event => panic!("{event:?}"),
}
}
_ = tokio::time::sleep(Duration::from_secs(1)) => {
// Likely listening on all interfaces now, thus continuing by breaking the loop.
break;
_ = delay => {
// Likely listening on all interfaces now, thus continuing by breaking the loop.
break;
}
}
}
}
});

// Connect to the relay server. Not for the reservation or relayed connection, but to (a) learn
// our local public address and (b) enable a freshly started relay to learn its public address.
swarm.dial(opt.relay_address.clone()).unwrap();

let mut learned_observed_addr = false;
let mut told_relay_observed_addr = false;
loop {
match swarm.next().await.unwrap() {
SwarmEvent::NewListenAddr { .. } => {}
SwarmEvent::Dialing { .. } => {}
SwarmEvent::ConnectionEstablished { .. } => {}
SwarmEvent::Behaviour(BehaviourEvent::Ping(_)) => {}
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Sent { .. })) => {
info!("Told relay its public address");
told_relay_observed_addr = true;
}
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Received {
info: identify::Info { observed_addr, .. },
..
})) => {
info!(address=%observed_addr, "Relay told us our observed address");
learned_observed_addr = true;
block_on(async {
let mut learned_observed_addr = false;
let mut told_relay_observed_addr = false;

loop {
match swarm.next().await.unwrap() {
SwarmEvent::NewListenAddr { .. } => {}
SwarmEvent::Dialing { .. } => {}
SwarmEvent::ConnectionEstablished { .. } => {}
SwarmEvent::Behaviour(BehaviourEvent::Ping(_)) => {}
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Sent {
..
})) => {
info!("Told relay its public address");
told_relay_observed_addr = true;
}
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Received {
info: identify::Info { observed_addr, .. },
..
})) => {
info!(address=%observed_addr, "Relay told us our observed address");
learned_observed_addr = true;
}
event => panic!("{event:?}"),
}
event => panic!("{event:?}"),
}

if learned_observed_addr && told_relay_observed_addr {
break;
if learned_observed_addr && told_relay_observed_addr {
break;
}
}
}
});

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

loop {
match swarm.next().await.unwrap() {
SwarmEvent::NewListenAddr { address, .. } => {
info!(%address, "Listening on address");
}
SwarmEvent::Behaviour(BehaviourEvent::RelayClient(
relay::client::Event::ReservationReqAccepted { .. },
)) => {
assert!(opt.mode == Mode::Listen);
info!("Relay accepted our reservation request");
}
SwarmEvent::Behaviour(BehaviourEvent::RelayClient(event)) => {
info!(?event)
}
SwarmEvent::Behaviour(BehaviourEvent::Dcutr(event)) => {
info!(?event)
}
SwarmEvent::Behaviour(BehaviourEvent::Identify(event)) => {
info!(?event)
}
SwarmEvent::Behaviour(BehaviourEvent::Ping(event)) => {
info!(?event)
}
SwarmEvent::ConnectionEstablished {
peer_id, endpoint, ..
} => {
info!(peer=%peer_id, ?endpoint, "Established new connection");
}
SwarmEvent::OutgoingConnectionError { peer_id, error, .. } => {
info!(peer=?peer_id, "Outgoing connection failed: {error}");
block_on(async {
loop {
match swarm.next().await.unwrap() {
SwarmEvent::NewListenAddr { address, .. } => {
info!(%address, "Listening on address");
}
SwarmEvent::Behaviour(BehaviourEvent::RelayClient(
relay::client::Event::ReservationReqAccepted { .. },
)) => {
assert!(opt.mode == Mode::Listen);
info!("Relay accepted our reservation request");
}
SwarmEvent::Behaviour(BehaviourEvent::RelayClient(event)) => {
info!(?event)
}
SwarmEvent::Behaviour(BehaviourEvent::Dcutr(event)) => {
info!(?event)
}
SwarmEvent::Behaviour(BehaviourEvent::Identify(event)) => {
info!(?event)
}
SwarmEvent::Behaviour(BehaviourEvent::Ping(_)) => {}
SwarmEvent::ConnectionEstablished {
peer_id, endpoint, ..
} => {
info!(peer=%peer_id, ?endpoint, "Established new connection");
}
SwarmEvent::OutgoingConnectionError { peer_id, error, .. } => {
info!(peer=?peer_id, "Outgoing connection failed: {error}");
}
_ => {}
}
_ => {}
}
}
})
}

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

0 comments on commit ebada19

Please sign in to comment.