Skip to content

Commit 9586071

Browse files
authored
feat: refactor distributed-key-value-store example (#5652)
## Description ref #4449 Refactored distributed-key-value-store example to use `tokio` instead of `async-std` ## Change checklist <!-- Please add a Changelog entry in the appropriate crates and bump the crate versions if needed. See <https://github.com/libp2p/rust-libp2p/blob/master/docs/release.md#development-between-releases>--> - [x] I have performed a self-review of my own code - [x] I have made corresponding changes to the documentation - [x] I have added tests that prove my fix is effective or that my feature works - [x] A changelog entry has been made in the appropriate crates
1 parent 4e7ff3e commit 9586071

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

Cargo.lock

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

examples/distributed-key-value-store/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ license = "MIT"
99
release = false
1010

1111
[dependencies]
12-
async-std = { version = "1.12", features = ["attributes"] }
12+
tokio = { workspace = true, features = ["full"] }
1313
async-trait = "0.1"
1414
futures = { workspace = true }
15-
libp2p = { path = "../../libp2p", features = [ "async-std", "dns", "kad", "mdns", "noise", "macros", "tcp", "yamux"] }
15+
libp2p = { path = "../../libp2p", features = [ "tokio", "dns", "kad", "mdns", "noise", "macros", "tcp", "yamux"] }
1616
tracing = { workspace = true }
1717
tracing-subscriber = { workspace = true, features = ["env-filter"] }
1818

examples/distributed-key-value-store/src/main.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

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

23-
use async_std::io;
24-
use futures::{prelude::*, select};
23+
use futures::stream::StreamExt;
2524
use libp2p::kad;
2625
use libp2p::kad::store::MemoryStore;
2726
use libp2p::kad::Mode;
@@ -32,9 +31,13 @@ use libp2p::{
3231
};
3332
use std::error::Error;
3433
use std::time::Duration;
34+
use tokio::{
35+
io::{self, AsyncBufReadExt},
36+
select,
37+
};
3538
use tracing_subscriber::EnvFilter;
3639

37-
#[async_std::main]
40+
#[tokio::main]
3841
async fn main() -> Result<(), Box<dyn Error>> {
3942
let _ = tracing_subscriber::fmt()
4043
.with_env_filter(EnvFilter::from_default_env())
@@ -44,11 +47,11 @@ async fn main() -> Result<(), Box<dyn Error>> {
4447
#[derive(NetworkBehaviour)]
4548
struct Behaviour {
4649
kademlia: kad::Behaviour<MemoryStore>,
47-
mdns: mdns::async_io::Behaviour,
50+
mdns: mdns::tokio::Behaviour,
4851
}
4952

5053
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
51-
.with_async_std()
54+
.with_tokio()
5255
.with_tcp(
5356
tcp::Config::default(),
5457
noise::Config::new,
@@ -60,7 +63,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
6063
key.public().to_peer_id(),
6164
MemoryStore::new(key.public().to_peer_id()),
6265
),
63-
mdns: mdns::async_io::Behaviour::new(
66+
mdns: mdns::tokio::Behaviour::new(
6467
mdns::Config::default(),
6568
key.public().to_peer_id(),
6669
)?,
@@ -72,15 +75,17 @@ async fn main() -> Result<(), Box<dyn Error>> {
7275
swarm.behaviour_mut().kademlia.set_mode(Some(Mode::Server));
7376

7477
// Read full lines from stdin
75-
let mut stdin = io::BufReader::new(io::stdin()).lines().fuse();
78+
let mut stdin = io::BufReader::new(io::stdin()).lines();
7679

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

8083
// Kick it off.
8184
loop {
8285
select! {
83-
line = stdin.select_next_some() => handle_input_line(&mut swarm.behaviour_mut().kademlia, line.expect("Stdin not to close")),
86+
Ok(Some(line)) = stdin.next_line() => {
87+
handle_input_line(&mut swarm.behaviour_mut().kademlia, line);
88+
}
8489
event = swarm.select_next_some() => match event {
8590
SwarmEvent::NewListenAddr { address, .. } => {
8691
println!("Listening in {address:?}");

0 commit comments

Comments
 (0)