use futures::{future, TryStreamExt};
use ipfs_api::{response::PingResponse, IpfsClient};
#[cfg_attr(feature = "with-actix", actix_rt::main)]
#[cfg_attr(feature = "with-hyper", tokio::main)]
async fn main() {
tracing_subscriber::fmt::init();
eprintln!("connecting to localhost:5001...");
let client = IpfsClient::default();
eprintln!();
eprintln!("discovering connected peers...");
let peer = match client.swarm_peers().await {
Ok(connected) => connected
.peers
.into_iter()
.next()
.expect("expected at least one peer"),
Err(e) => {
eprintln!("error getting connected peers: {}", e);
return;
}
};
eprintln!();
eprintln!("discovered peer ({})", peer.peer);
eprintln!();
eprintln!("streaming 10 pings...");
if let Err(e) = client
.ping(&peer.peer[..], Some(10))
.try_for_each(|ping| {
eprintln!("{:?}", ping);
future::ok(())
})
.await
{
eprintln!("error streaming pings: {}", e);
}
eprintln!();
eprintln!("gathering 15 pings...");
match client
.ping(&peer.peer[..], Some(15))
.try_collect::<Vec<PingResponse>>()
.await
{
Ok(pings) => {
for ping in pings.iter() {
eprintln!("got response ({:?}) at ({})...", ping.text, ping.time);
}
}
Err(e) => eprintln!("error collecting pings: {}", e),
}
}