mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-30 10:11:33 +00:00
examples/*: Migrate to async await (#2356)
* Adapt examples to async style loop * Adapt async style loop for chat.rs * Adapt async style loop for distributed-key-value-store.rs * Adapt async style loop for gossibsub-chat.rs * Adapt async style loop for ipfs-private.rs * Adapt ping to use async * Update tutorial crate to reflect new changes Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
committed by
GitHub
parent
75ae7b0461
commit
2c75fbe812
@ -40,19 +40,18 @@
|
||||
//! The two nodes establish a connection, negotiate the ping protocol
|
||||
//! and begin pinging each other.
|
||||
|
||||
use futures::executor::block_on;
|
||||
use futures::prelude::*;
|
||||
use libp2p::swarm::{Swarm, SwarmEvent};
|
||||
use libp2p::{identity, ping, Multiaddr, PeerId};
|
||||
use std::error::Error;
|
||||
use std::task::Poll;
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
#[async_std::main]
|
||||
async fn main() -> Result<(), Box<dyn Error>> {
|
||||
let local_key = identity::Keypair::generate_ed25519();
|
||||
let local_peer_id = PeerId::from(local_key.public());
|
||||
println!("Local peer id: {:?}", local_peer_id);
|
||||
|
||||
let transport = block_on(libp2p::development_transport(local_key))?;
|
||||
let transport = libp2p::development_transport(local_key).await?;
|
||||
|
||||
// Create a ping network behaviour.
|
||||
//
|
||||
@ -75,17 +74,11 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
println!("Dialed {}", addr)
|
||||
}
|
||||
|
||||
block_on(future::poll_fn(move |cx| loop {
|
||||
match swarm.poll_next_unpin(cx) {
|
||||
Poll::Ready(Some(event)) => match event {
|
||||
SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {:?}", address),
|
||||
SwarmEvent::Behaviour(event) => println!("{:?}", event),
|
||||
_ => {}
|
||||
},
|
||||
Poll::Ready(None) => return Poll::Ready(()),
|
||||
Poll::Pending => return Poll::Pending,
|
||||
loop {
|
||||
match swarm.select_next_some().await {
|
||||
SwarmEvent::NewListenAddr { address, .. } => println!("Listening on {:?}", address),
|
||||
SwarmEvent::Behaviour(event) => println!("{:?}", event),
|
||||
_ => {}
|
||||
}
|
||||
}));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user