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): distributed-key-value-store identify and rendezvous #5571

Closed
wants to merge 8 commits into from
Closed
5 changes: 2 additions & 3 deletions examples/distributed-key-value-store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ license = "MIT"
release = false

[dependencies]
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", "dns", "kad", "mdns", "noise", "macros", "tcp", "yamux"] }
libp2p = { path = "../../libp2p", features = [ "tokio", "dns", "kad", "mdns", "noise", "macros", "tcp", "yamux"] }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }

Expand Down
17 changes: 9 additions & 8 deletions examples/distributed-key-value-store/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

#![doc = include_str!("../README.md")]

use async_std::io;
use futures::{prelude::*, select};
use futures::stream::StreamExt;
use libp2p::kad;
use libp2p::kad::store::MemoryStore;
use libp2p::kad::Mode;
Expand All @@ -32,9 +31,10 @@ use libp2p::{
};
use std::error::Error;
use std::time::Duration;
use tokio::{io, io::AsyncBufReadExt, select};
use tracing_subscriber::EnvFilter;

#[async_std::main]
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let _ = tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
Expand All @@ -44,11 +44,11 @@ async fn main() -> Result<(), Box<dyn Error>> {
#[derive(NetworkBehaviour)]
struct Behaviour {
kademlia: kad::Behaviour<MemoryStore>,
mdns: mdns::async_io::Behaviour,
mdns: mdns::tokio::Behaviour,
}

let mut swarm = libp2p::SwarmBuilder::with_new_identity()
.with_async_std()
.with_tokio()
.with_tcp(
tcp::Config::default(),
noise::Config::new,
Expand All @@ -60,7 +60,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
key.public().to_peer_id(),
MemoryStore::new(key.public().to_peer_id()),
),
mdns: mdns::async_io::Behaviour::new(
mdns: mdns::tokio::Behaviour::new(
mdns::Config::default(),
key.public().to_peer_id(),
)?,
Expand All @@ -72,15 +72,16 @@ async fn main() -> Result<(), Box<dyn Error>> {
swarm.behaviour_mut().kademlia.set_mode(Some(Mode::Server));

// Read full lines from stdin
let mut stdin = io::BufReader::new(io::stdin()).lines().fuse();
Copy link
Member

@jxs jxs Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we keep std::futures::select! and therefore this fuse?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jxs i did as just line following other example implementations with tokios as the following:
chat/src/main.rs:88: let mut stdin = io::BufReader::new(io::stdin()).lines();
ipfs-private/src/main.rs:172: let mut stdin = io::BufReader::new(io::stdin()).lines();

but I can try your approach later tonight.

let mut stdin = io::BufReader::new(io::stdin()).lines();

// Listen on all interfaces and whatever port the OS assigns.
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;

// Kick it off.
loop {
select! {
line = stdin.select_next_some() => handle_input_line(&mut swarm.behaviour_mut().kademlia, line.expect("Stdin not to close")),
line = stdin.next_line() => handle_input_line(&mut swarm.behaviour_mut().kademlia,
line.expect("Stdin not to close").expect("EOL")),
event = swarm.select_next_some() => match event {
SwarmEvent::NewListenAddr { address, .. } => {
println!("Listening in {address:?}");
Expand Down
5 changes: 2 additions & 3 deletions examples/identify/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ license = "MIT"
release = false

[dependencies]
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", "dns", "dcutr", "identify", "macros", "noise", "ping", "relay", "rendezvous", "tcp", "tokio","yamux"] }
libp2p = { path = "../../libp2p", features = ["identify", "noise", "tcp", "tokio", "yamux"] }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }

Expand Down
4 changes: 2 additions & 2 deletions examples/identify/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ use libp2p::{core::multiaddr::Multiaddr, identify, noise, swarm::SwarmEvent, tcp
use std::{error::Error, time::Duration};
use tracing_subscriber::EnvFilter;

#[async_std::main]
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let _ = tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.try_init();

let mut swarm = libp2p::SwarmBuilder::with_new_identity()
.with_async_std()
.with_tokio()
.with_tcp(
tcp::Config::default(),
noise::Config::new,
Expand Down
4 changes: 1 addition & 3 deletions examples/rendezvous/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ license = "MIT"
release = false

[dependencies]
async-std = { version = "1.12", features = ["attributes"] }
async-trait = "0.1"
futures = { workspace = true }
libp2p = { path = "../../libp2p", features = [ "async-std", "identify", "macros", "noise", "ping", "rendezvous", "tcp", "tokio", "yamux"] }
libp2p = { path = "../../libp2p", features = ["identify", "macros", "noise", "ping", "rendezvous", "tcp", "tokio", "yamux"] }
tokio = { workspace = true, features = ["rt-multi-thread", "macros", "time"] }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
Expand Down
Loading