mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-14 18:41:22 +00:00
refactor(examples): change chat from async-std to tokio
Related: #4449. Pull-Request: #4539.
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -804,11 +804,11 @@ dependencies = [
|
|||||||
name = "chat-example"
|
name = "chat-example"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-std",
|
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"env_logger 0.10.0",
|
"env_logger 0.10.0",
|
||||||
"futures",
|
"futures",
|
||||||
"libp2p",
|
"libp2p",
|
||||||
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -6,8 +6,8 @@ publish = false
|
|||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
async-std = { version = "1.12", features = ["attributes"] }
|
tokio = { version = "1.32", features = ["full"] }
|
||||||
async-trait = "0.1"
|
async-trait = "0.1"
|
||||||
env_logger = "0.10.0"
|
env_logger = "0.10.0"
|
||||||
futures = "0.3.28"
|
futures = "0.3.28"
|
||||||
libp2p = { path = "../../libp2p", features = ["async-std", "gossipsub", "mdns", "noise", "macros", "tcp", "yamux", "quic"] }
|
libp2p = { path = "../../libp2p", features = ["tokio", "gossipsub", "mdns", "noise", "macros", "tcp", "yamux", "quic"] }
|
||||||
|
@ -20,8 +20,7 @@
|
|||||||
|
|
||||||
#![doc = include_str!("../README.md")]
|
#![doc = include_str!("../README.md")]
|
||||||
|
|
||||||
use async_std::io;
|
use futures::{future::Either, stream::StreamExt};
|
||||||
use futures::{future::Either, prelude::*, select};
|
|
||||||
use libp2p::{
|
use libp2p::{
|
||||||
core::{muxing::StreamMuxerBox, transport::OrTransport, upgrade},
|
core::{muxing::StreamMuxerBox, transport::OrTransport, upgrade},
|
||||||
gossipsub, identity, mdns, noise, quic,
|
gossipsub, identity, mdns, noise, quic,
|
||||||
@ -33,15 +32,16 @@ use std::collections::hash_map::DefaultHasher;
|
|||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
use tokio::{io, io::AsyncBufReadExt, select};
|
||||||
|
|
||||||
// We create a custom network behaviour that combines Gossipsub and Mdns.
|
// We create a custom network behaviour that combines Gossipsub and Mdns.
|
||||||
#[derive(NetworkBehaviour)]
|
#[derive(NetworkBehaviour)]
|
||||||
struct MyBehaviour {
|
struct MyBehaviour {
|
||||||
gossipsub: gossipsub::Behaviour,
|
gossipsub: gossipsub::Behaviour,
|
||||||
mdns: mdns::async_io::Behaviour,
|
mdns: mdns::tokio::Behaviour,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_std::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
// Create a random PeerId
|
// Create a random PeerId
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
@ -49,13 +49,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
let local_peer_id = PeerId::from(id_keys.public());
|
let local_peer_id = PeerId::from(id_keys.public());
|
||||||
|
|
||||||
// Set up an encrypted DNS-enabled TCP Transport over the yamux protocol.
|
// Set up an encrypted DNS-enabled TCP Transport over the yamux protocol.
|
||||||
let tcp_transport = tcp::async_io::Transport::new(tcp::Config::default().nodelay(true))
|
let tcp_transport = tcp::tokio::Transport::new(tcp::Config::default().nodelay(true))
|
||||||
.upgrade(upgrade::Version::V1Lazy)
|
.upgrade(upgrade::Version::V1Lazy)
|
||||||
.authenticate(noise::Config::new(&id_keys).expect("signing libp2p-noise static keypair"))
|
.authenticate(noise::Config::new(&id_keys).expect("signing libp2p-noise static keypair"))
|
||||||
.multiplex(yamux::Config::default())
|
.multiplex(yamux::Config::default())
|
||||||
.timeout(std::time::Duration::from_secs(20))
|
.timeout(std::time::Duration::from_secs(20))
|
||||||
.boxed();
|
.boxed();
|
||||||
let quic_transport = quic::async_std::Transport::new(quic::Config::new(&id_keys));
|
let quic_transport = quic::tokio::Transport::new(quic::Config::new(&id_keys));
|
||||||
let transport = OrTransport::new(quic_transport, tcp_transport)
|
let transport = OrTransport::new(quic_transport, tcp_transport)
|
||||||
.map(|either_output, _| match either_output {
|
.map(|either_output, _| match either_output {
|
||||||
Either::Left((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
|
Either::Left((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
|
||||||
@ -91,13 +91,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
|
|
||||||
// Create a Swarm to manage peers and events
|
// Create a Swarm to manage peers and events
|
||||||
let mut swarm = {
|
let mut swarm = {
|
||||||
let mdns = mdns::async_io::Behaviour::new(mdns::Config::default(), local_peer_id)?;
|
let mdns = mdns::tokio::Behaviour::new(mdns::Config::default(), local_peer_id)?;
|
||||||
let behaviour = MyBehaviour { gossipsub, mdns };
|
let behaviour = MyBehaviour { gossipsub, mdns };
|
||||||
SwarmBuilder::with_async_std_executor(transport, behaviour, local_peer_id).build()
|
SwarmBuilder::with_tokio_executor(transport, behaviour, local_peer_id).build()
|
||||||
};
|
};
|
||||||
|
|
||||||
// Read full lines from stdin
|
// 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
|
// Listen on all interfaces and whatever port the OS assigns
|
||||||
swarm.listen_on("/ip4/0.0.0.0/udp/0/quic-v1".parse()?)?;
|
swarm.listen_on("/ip4/0.0.0.0/udp/0/quic-v1".parse()?)?;
|
||||||
@ -108,13 +108,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
// Kick it off
|
// Kick it off
|
||||||
loop {
|
loop {
|
||||||
select! {
|
select! {
|
||||||
line = stdin.select_next_some() => {
|
Ok(Some(line)) = stdin.next_line() => {
|
||||||
if let Err(e) = swarm
|
if let Err(e) = swarm
|
||||||
.behaviour_mut().gossipsub
|
.behaviour_mut().gossipsub
|
||||||
.publish(topic.clone(), line.expect("Stdin not to close").as_bytes()) {
|
.publish(topic.clone(), line.as_bytes()) {
|
||||||
println!("Publish error: {e:?}");
|
println!("Publish error: {e:?}");
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
event = swarm.select_next_some() => match event {
|
event = swarm.select_next_some() => match event {
|
||||||
SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(mdns::Event::Discovered(list))) => {
|
SwarmEvent::Behaviour(MyBehaviourEvent::Mdns(mdns::Event::Discovered(list))) => {
|
||||||
for (peer_id, _multiaddr) in list {
|
for (peer_id, _multiaddr) in list {
|
||||||
|
Reference in New Issue
Block a user