From 1dbab207ece88a47f92b951a83ef8d95dc40fe1c Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Tue, 23 Feb 2021 23:26:48 -0500 Subject: migrate examples to separate crate --- ipfs-api-examples/examples/ping_peer.rs | 70 +++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 ipfs-api-examples/examples/ping_peer.rs (limited to 'ipfs-api-examples/examples/ping_peer.rs') diff --git a/ipfs-api-examples/examples/ping_peer.rs b/ipfs-api-examples/examples/ping_peer.rs new file mode 100644 index 0000000..5c5de44 --- /dev/null +++ b/ipfs-api-examples/examples/ping_peer.rs @@ -0,0 +1,70 @@ +// Copyright 2017 rust-ipfs-api Developers +// +// Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be +// copied, modified, or distributed except according to those terms. +// + +use futures::{future, TryStreamExt}; +use ipfs_api_examples::ipfs_api::{response::PingResponse, IpfsApi, IpfsClient}; + +// Creates an Ipfs client, discovers a connected peer, and pings it using the +// streaming Api, and by collecting it into a collection. +// +#[ipfs_api_examples::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::>() + .await + { + Ok(pings) => { + for ping in pings.iter() { + eprintln!("got response ({:?}) at ({})...", ping.text, ping.time); + } + } + Err(e) => eprintln!("error collecting pings: {}", e), + } +} -- cgit v1.2.3