Skip to content

Commit

Permalink
refactor(examples): change ipfs-private from async-std to tokio
Browse files Browse the repository at this point in the history
Related: libp2p#4449.

Pull-Request: libp2p#4591.
  • Loading branch information
randommm authored Oct 4, 2023
1 parent d862b40 commit 0e94bc2
Show file tree
Hide file tree
Showing 3 changed files with 11 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/ipfs-private/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ publish = false
license = "MIT"

[dependencies]
async-std = { version = "1.12", features = ["attributes"] }
tokio = { version = "1.32", features = ["rt-multi-thread", "macros", "io-std"] }
async-trait = "0.1"
either = "1.9"
env_logger = "0.10"
futures = "0.3.28"
libp2p = { path = "../../libp2p", features = ["async-std", "gossipsub", "dns", "identify", "kad", "macros", "noise", "ping", "pnet", "tcp", "websocket", "yamux"] }
libp2p = { path = "../../libp2p", features = ["tokio", "gossipsub", "dns", "identify", "kad", "macros", "noise", "ping", "pnet", "tcp", "websocket", "yamux"] }
16 changes: 8 additions & 8 deletions examples/ipfs-private/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@

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

use async_std::io;
use either::Either;
use futures::{prelude::*, select};
use futures::prelude::*;
use libp2p::{
core::{muxing::StreamMuxerBox, transport, transport::upgrade::Version},
gossipsub, identify, identity,
Expand All @@ -33,6 +32,7 @@ use libp2p::{
tcp, yamux, Multiaddr, PeerId, Transport,
};
use std::{env, error::Error, fs, path::Path, str::FromStr, time::Duration};
use tokio::{io, io::AsyncBufReadExt, select};

/// Builds the transport that serves as a common ground for all connections.
pub fn build_transport(
Expand All @@ -42,7 +42,7 @@ pub fn build_transport(
let noise_config = noise::Config::new(&key_pair).unwrap();
let yamux_config = yamux::Config::default();

let base_transport = tcp::async_io::Transport::new(tcp::Config::default().nodelay(true));
let base_transport = tcp::tokio::Transport::new(tcp::Config::default().nodelay(true));
let maybe_encrypted = match psk {
Some(psk) => Either::Left(
base_transport.and_then(move |socket, _| PnetConfig::new(psk).handshake(socket)),
Expand Down Expand Up @@ -108,7 +108,7 @@ fn parse_legacy_multiaddr(text: &str) -> Result<Multiaddr, Box<dyn Error>> {
Ok(res)
}

#[async_std::main]
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();

Expand Down Expand Up @@ -186,7 +186,7 @@ async fn main() -> Result<(), Box<dyn Error>> {

println!("Subscribing to {gossipsub_topic:?}");
behaviour.gossipsub.subscribe(&gossipsub_topic).unwrap();
SwarmBuilder::with_async_std_executor(transport, behaviour, local_peer_id).build()
SwarmBuilder::with_tokio_executor(transport, behaviour, local_peer_id).build()
};

// Reach out to other nodes if specified
Expand All @@ -197,19 +197,19 @@ async fn main() -> Result<(), Box<dyn Error>> {
}

// 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() => {
Ok(Some(line)) = stdin.next_line() => {
if let Err(e) = swarm
.behaviour_mut()
.gossipsub
.publish(gossipsub_topic.clone(), line.expect("Stdin not to close").as_bytes())
.publish(gossipsub_topic.clone(), line.as_bytes())
{
println!("Publish error: {e:?}");
}
Expand Down

0 comments on commit 0e94bc2

Please sign in to comment.