Skip to content

Commit

Permalink
feat: refactor distributed-key-value-store example (#5652)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
kamuik16 authored Oct 29, 2024
1 parent 4e7ff3e commit 9586071
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions examples/distributed-key-value-store/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ license = "MIT"
release = false

[dependencies]
async-std = { version = "1.12", features = ["attributes"] }
tokio = { workspace = true, features = ["full"] }
async-trait = "0.1"
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
21 changes: 13 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,13 @@ use libp2p::{
};
use std::error::Error;
use std::time::Duration;
use tokio::{
io::{self, 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 +47,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 +63,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 +75,17 @@ 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();
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")),
Ok(Some(line)) = stdin.next_line() => {
handle_input_line(&mut swarm.behaviour_mut().kademlia, line);
}
event = swarm.select_next_some() => match event {
SwarmEvent::NewListenAddr { address, .. } => {
println!("Listening in {address:?}");
Expand Down

0 comments on commit 9586071

Please sign in to comment.