Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(examples): use tokio instead of async-std in relay-server #5600

Merged
merged 7 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.lock

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

5 changes: 2 additions & 3 deletions examples/relay-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ release = false

[dependencies]
clap = { version = "4.5.6", features = ["derive"] }
async-std = { version = "1.12", features = ["attributes"] }
async-trait = "0.1"
tokio = { version = "1.37.0", features = ["full"] }
futures = { workspace = true }
libp2p = { path = "../../libp2p", features = [ "async-std", "noise", "macros", "ping", "tcp", "identify", "yamux", "relay", "quic"] }
libp2p = { path = "../../libp2p", features = ["tokio", "noise", "macros", "ping", "tcp", "identify", "yamux", "relay", "quic"] }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }

Expand Down
42 changes: 20 additions & 22 deletions examples/relay-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
#![doc = include_str!("../README.md")]

use clap::Parser;
use futures::executor::block_on;
use futures::stream::StreamExt;
use futures::StreamExt;
use libp2p::{
core::multiaddr::Protocol,
core::Multiaddr,
Expand All @@ -35,7 +34,8 @@ use std::error::Error;
use std::net::{Ipv4Addr, Ipv6Addr};
use tracing_subscriber::EnvFilter;

fn main() -> Result<(), Box<dyn Error>> {
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let _ = tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.try_init();
Expand All @@ -46,7 +46,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let local_key: identity::Keypair = generate_ed25519(opt.secret_key_seed);

let mut swarm = libp2p::SwarmBuilder::with_existing_identity(local_key)
.with_async_std()
.with_tokio()
.with_tcp(
tcp::Config::default(),
noise::Config::new,
Expand Down Expand Up @@ -81,27 +81,25 @@ fn main() -> Result<(), Box<dyn Error>> {
.with(Protocol::QuicV1);
swarm.listen_on(listen_addr_quic)?;

block_on(async {
loop {
match swarm.next().await.expect("Infinite Stream.") {
SwarmEvent::Behaviour(event) => {
if let BehaviourEvent::Identify(identify::Event::Received {
info: identify::Info { observed_addr, .. },
..
}) = &event
{
swarm.add_external_address(observed_addr.clone());
}

println!("{event:?}")
loop {
match swarm.next().await.expect("Infinite Stream.") {
SwarmEvent::Behaviour(event) => {
if let BehaviourEvent::Identify(identify::Event::Received {
info: identify::Info { observed_addr, .. },
..
}) = &event
{
swarm.add_external_address(observed_addr.clone());
}
SwarmEvent::NewListenAddr { address, .. } => {
println!("Listening on {address:?}");
}
_ => {}

println!("{event:?}")
}
SwarmEvent::NewListenAddr { address, .. } => {
println!("Listening on {address:?}");
}
_ => {}
}
})
}
}

#[derive(NetworkBehaviour)]
Expand Down
Loading