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

feat: refactor distributed-key-value-store example #5652

Merged
merged 1 commit into from
Oct 29, 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
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
Loading