From c162449ecbdbdda06c50fdb9818eeb0a041aeae2 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Wed, 27 Jun 2018 17:28:14 -0400 Subject: updating examples --- ipfs-api/examples/add_file.rs | 16 ++++--- ipfs-api/examples/add_tar.rs | 34 +++++++------- ipfs-api/examples/bootstrap_default.rs | 76 ++++++++++++++++-------------- ipfs-api/examples/dns.rs | 44 ++++++++++-------- ipfs-api/examples/get_commands.rs | 15 +++--- ipfs-api/examples/get_stats.rs | 82 +++++++++++++++++--------------- ipfs-api/examples/get_swarm.rs | 54 +++++++++++---------- ipfs-api/examples/get_version.rs | 15 +++--- ipfs-api/examples/mfs.rs | 85 +++++++++++++++++++--------------- ipfs-api/examples/ping_peer.rs | 75 ++++++++++++++++-------------- ipfs-api/examples/pubsub.rs | 50 +++++++++----------- 11 files changed, 292 insertions(+), 254 deletions(-) diff --git a/ipfs-api/examples/add_file.rs b/ipfs-api/examples/add_file.rs index 66775f4..7784c89 100644 --- a/ipfs-api/examples/add_file.rs +++ b/ipfs-api/examples/add_file.rs @@ -6,12 +6,13 @@ // copied, modified, or distributed except according to those terms. // +extern crate futures; +extern crate hyper; extern crate ipfs_api; -extern crate tokio_core; use ipfs_api::IpfsClient; +use futures::Future; use std::fs::File; -use tokio_core::reactor::Core; // Creates an Ipfs client, and adds this source file to Ipfs. // @@ -19,11 +20,12 @@ fn main() { println!("note: this must be run in the root of the project repository"); println!("connecting to localhost:5001..."); - let mut core = Core::new().expect("expected event loop"); - let client = IpfsClient::default(&core.handle()); + let client = IpfsClient::default(); let file = File::open(file!()).expect("could not read source file"); - let req = client.add(file); - let add = core.run(req).expect("expected a valid response"); + let req = client + .add(file) + .map(|add| println!("added file: {:?}", add)) + .map_err(|e| eprintln!("{}", e)); - println!("added file: {:?}", add); + hyper::rt::run(req); } diff --git a/ipfs-api/examples/add_tar.rs b/ipfs-api/examples/add_tar.rs index 401c61d..1880712 100644 --- a/ipfs-api/examples/add_tar.rs +++ b/ipfs-api/examples/add_tar.rs @@ -7,15 +7,14 @@ // extern crate futures; +extern crate hyper; extern crate ipfs_api; extern crate tar; -extern crate tokio_core; -use futures::stream::Stream; +use futures::{Future, Stream}; use ipfs_api::IpfsClient; use std::io::Cursor; use tar::Builder; -use tokio_core::reactor::Core; // Creates an Ipfs client, and adds this source file to Ipfs. // @@ -23,8 +22,7 @@ fn main() { println!("note: this must be run in the root of the project repository"); println!("connecting to localhost:5001..."); - let mut core = Core::new().expect("expected event loop"); - let client = IpfsClient::default(&core.handle()); + let client = IpfsClient::default(); let mut buf = Vec::new(); @@ -40,15 +38,19 @@ fn main() { } let cursor = Cursor::new(buf); - let req = client.tar_add(cursor); - let add = core.run(req).expect("expected a valid response"); - - println!("added tar file: {:?}", add); - println!(); - - let req = client.tar_cat(&add.hash[..]).concat2(); - let cat = core.run(req).expect("expected a valid response"); - - println!("{}", String::from_utf8_lossy(&cat[..])); - println!(); + let req = client + .tar_add(cursor) + .and_then(move |add| { + println!("added tar file: {:?}", add); + println!(); + + client.tar_cat(&add.hash[..]).concat2() + }) + .map(|cat| { + println!("{}", String::from_utf8_lossy(&cat[..])); + println!(); + }) + .map_err(|e| eprintln!("{}", e)); + + hyper::rt::run(req) } diff --git a/ipfs-api/examples/bootstrap_default.rs b/ipfs-api/examples/bootstrap_default.rs index d236716..79c66ab 100644 --- a/ipfs-api/examples/bootstrap_default.rs +++ b/ipfs-api/examples/bootstrap_default.rs @@ -6,11 +6,12 @@ // copied, modified, or distributed except according to those terms. // +extern crate futures; +extern crate hyper; extern crate ipfs_api; -extern crate tokio_core; use ipfs_api::IpfsClient; -use tokio_core::reactor::Core; +use futures::Future; // Lists clients in bootstrap list, then adds the default list, then removes // them, and readds them. @@ -18,36 +19,43 @@ use tokio_core::reactor::Core; fn main() { println!("connecting to localhost:5001..."); - let mut core = Core::new().expect("expected event loop"); - let client = IpfsClient::default(&core.handle()); - - let bootstrap = client.bootstrap_list(); - let bootstrap = core.run(bootstrap).expect("expected a valid response"); - - println!("current bootstrap peers:"); - for peer in bootstrap.peers { - println!(" {}", peer); - } - - println!(); - println!("dropping all bootstrap peers..."); - - let drop = client.bootstrap_rm_all(); - let drop = core.run(drop).expect("expected a valid response"); - - println!("dropped:"); - for peer in drop.peers { - println!(" {}", peer); - } - - println!(); - println!("adding default peers..."); - - let add = client.bootstrap_add_default(); - let add = core.run(add).expect("expected a valid response"); - - println!("added:"); - for peer in add.peers { - println!(" {}", peer); - } + let client = IpfsClient::default(); + + let bootstrap = client.bootstrap_list().map(|bootstrap| { + println!("current bootstrap peers:"); + for peer in bootstrap.peers { + println!(" {}", peer); + } + }); + + let drop = client.bootstrap_rm_all().map(|drop| { + println!("dropped:"); + for peer in drop.peers { + println!(" {}", peer); + } + }); + + let add = client.bootstrap_add_default().map(|add| { + println!("added:"); + for peer in add.peers { + println!(" {}", peer); + } + }); + + hyper::rt::run( + bootstrap + .and_then(|_| { + println!(); + println!("dropping all bootstrap peers..."); + + drop + }) + .and_then(|_| { + println!(); + println!("adding default peers..."); + + add + }) + .map_err(|e| eprintln!("{}", e)), + ); } diff --git a/ipfs-api/examples/dns.rs b/ipfs-api/examples/dns.rs index ec574ab..78b4086 100644 --- a/ipfs-api/examples/dns.rs +++ b/ipfs-api/examples/dns.rs @@ -6,33 +6,37 @@ // copied, modified, or distributed except according to those terms. // +extern crate futures; +extern crate hyper; extern crate ipfs_api; -extern crate tokio_core; +use futures::Future; use ipfs_api::IpfsClient; -use tokio_core::reactor::Core; // Creates an Ipfs client, resolves ipfs.io, and lists the contents of it. // fn main() { println!("connecting to localhost:5001..."); - let mut core = Core::new().expect("expected event loop"); - let client = IpfsClient::default(&core.handle()); - - let req = client.dns("ipfs.io", false); - let dns = core.run(req).expect("dns should resolve"); - - println!("dns resolves to ({})", &dns.path); - println!(); - - let req = client.file_ls(&dns.path[..]); - let contents = core.run(req).expect("api should return path contents"); - - println!("found contents:"); - for directory in contents.objects.values() { - for file in directory.links.iter() { - println!("[{}] ({} bytes)", file.name, file.size); - } - } + let client = IpfsClient::default(); + + let req = client + .dns("ipfs.io", false) + .and_then(move |dns| { + println!("dns resolves to ({})", &dns.path); + println!(); + + client.file_ls(&dns.path[..]) + }) + .map(|contents| { + println!("found contents:"); + for directory in contents.objects.values() { + for file in directory.links.iter() { + println!("[{}] ({} bytes)", file.name, file.size); + } + } + }) + .map_err(|e| eprintln!("{}", e)); + + hyper::rt::run(req); } diff --git a/ipfs-api/examples/get_commands.rs b/ipfs-api/examples/get_commands.rs index d3f9852..b12ffa1 100644 --- a/ipfs-api/examples/get_commands.rs +++ b/ipfs-api/examples/get_commands.rs @@ -6,11 +6,12 @@ // copied, modified, or distributed except according to those terms. // +extern crate futures; +extern crate hyper; extern crate ipfs_api; -extern crate tokio_core; +use futures::Future; use ipfs_api::{response, IpfsClient}; -use tokio_core::reactor::Core; fn print_recursive(indent: usize, cmd: &response::CommandsResponse) { let cmd_indent = " ".repeat(indent * 4); @@ -39,9 +40,11 @@ fn print_recursive(indent: usize, cmd: &response::CommandsResponse) { fn main() { println!("connecting to localhost:5001..."); - let mut core = Core::new().expect("expected event loop"); - let client = IpfsClient::default(&core.handle()); - let req = client.commands(); + let client = IpfsClient::default(); + let req = client + .commands() + .map(|commands| print_recursive(0, &commands)) + .map_err(|e| eprintln!("{}", e)); - print_recursive(0, &core.run(req).expect("expected a valid response")); + hyper::rt::run(req); } diff --git a/ipfs-api/examples/get_stats.rs b/ipfs-api/examples/get_stats.rs index fe9f84f..41f2a5c 100644 --- a/ipfs-api/examples/get_stats.rs +++ b/ipfs-api/examples/get_stats.rs @@ -6,52 +6,58 @@ // copied, modified, or distributed except according to those terms. // +extern crate futures; +extern crate hyper; extern crate ipfs_api; -extern crate tokio_core; +use futures::Future; use ipfs_api::IpfsClient; -use tokio_core::reactor::Core; // Creates an Ipfs client, and gets some stats about the Ipfs server. // fn main() { println!("connecting to localhost:5001..."); - let mut core = Core::new().expect("expected event loop"); - let client = IpfsClient::default(&core.handle()); - - let bitswap_stats = client.stats_bitswap(); - let bitswap_stats = core.run(bitswap_stats).expect("expected a valid response"); - - let bw_stats = client.stats_bw(); - let bw_stats = core.run(bw_stats).expect("expected a valid response"); - - let repo_stats = client.stats_repo(); - let repo_stats = core.run(repo_stats).expect("expected a valid response"); - - println!("bitswap stats:"); - println!(" blocks recv: {}", bitswap_stats.blocks_received); - println!(" data recv: {}", bitswap_stats.data_received); - println!(" blocks sent: {}", bitswap_stats.blocks_sent); - println!(" data sent: {}", bitswap_stats.data_sent); - println!( - " peers: {}", - bitswap_stats.peers.join("\n ") - ); - println!( - " wantlist: {}", - bitswap_stats.wantlist.join("\n ") + let client = IpfsClient::default(); + + let bitswap_stats = client.stats_bitswap().map(|bitswap_stats| { + println!("bitswap stats:"); + println!(" blocks recv: {}", bitswap_stats.blocks_received); + println!(" data recv: {}", bitswap_stats.data_received); + println!(" blocks sent: {}", bitswap_stats.blocks_sent); + println!(" data sent: {}", bitswap_stats.data_sent); + println!( + " peers: {}", + bitswap_stats.peers.join("\n ") + ); + println!( + " wantlist: {}", + bitswap_stats.wantlist.join("\n ") + ); + println!(); + }); + + let bw_stats = client.stats_bw().map(|bw_stats| { + println!("bandwidth stats:"); + println!(" total in: {}", bw_stats.total_in); + println!(" total out: {}", bw_stats.total_out); + println!(" rate in: {}", bw_stats.rate_in); + println!(" rate out: {}", bw_stats.rate_out); + println!(); + }); + + let repo_stats = client.stats_repo().map(|repo_stats| { + println!("repo stats:"); + println!(" num objs: {}", repo_stats.num_objects); + println!(" repo size: {}", repo_stats.repo_size); + println!(" repo path: {}", repo_stats.repo_path); + println!(" version : {}", repo_stats.version); + }); + + hyper::rt::run( + bitswap_stats + .and_then(|_| bw_stats) + .and_then(|_| repo_stats) + .map_err(|e| eprintln!("{}", e)), ); - println!(); - println!("bandwidth stats:"); - println!(" total in: {}", bw_stats.total_in); - println!(" total out: {}", bw_stats.total_out); - println!(" rate in: {}", bw_stats.rate_in); - println!(" rate out: {}", bw_stats.rate_out); - println!(); - println!("repo stats:"); - println!(" num objs: {}", repo_stats.num_objects); - println!(" repo size: {}", repo_stats.repo_size); - println!(" repo path: {}", repo_stats.repo_path); - println!(" version : {}", repo_stats.version); } diff --git a/ipfs-api/examples/get_swarm.rs b/ipfs-api/examples/get_swarm.rs index 52e547a..2b9fd73 100644 --- a/ipfs-api/examples/get_swarm.rs +++ b/ipfs-api/examples/get_swarm.rs @@ -6,11 +6,12 @@ // copied, modified, or distributed except according to those terms. // +extern crate futures; +extern crate hyper; extern crate ipfs_api; -extern crate tokio_core; +use futures::Future; use ipfs_api::IpfsClient; -use tokio_core::reactor::Core; // Creates an Ipfs client, and gets information about your local address, and // connected peers. @@ -18,30 +19,33 @@ use tokio_core::reactor::Core; fn main() { println!("connecting to localhost:5001..."); - let mut core = Core::new().expect("expected event loop"); - let client = IpfsClient::default(&core.handle()); + let client = IpfsClient::default(); - let local = client.swarm_addrs_local(); - let local = core.run(local).expect("expected a valid response"); - - println!(); - println!("your addrs:"); - for addr in local.strings { - println!(" {}", addr); - } - - let connected = client.swarm_peers(); - let connected = core.run(connected).expect("expected a valid response"); + let local = client.swarm_addrs_local().map(|local| { + println!(); + println!("your addrs:"); + for addr in local.strings { + println!(" {}", addr); + } + }); - println!(); - println!("connected:"); - for peer in connected.peers { - let streams: Vec<&str> = peer.streams.iter().map(|s| &s.protocol[..]).collect(); - println!(" addr: {}", peer.addr); - println!(" peer: {}", peer.peer); - println!(" latency: {}", peer.latency); - println!(" muxer: {}", peer.muxer); - println!(" streams: {}", streams.join(", ")); + let connected = client.swarm_peers().map(|connected| { println!(); - } + println!("connected:"); + for peer in connected.peers { + let streams: Vec<&str> = peer.streams.iter().map(|s| &s.protocol[..]).collect(); + println!(" addr: {}", peer.addr); + println!(" peer: {}", peer.peer); + println!(" latency: {}", peer.latency); + println!(" muxer: {}", peer.muxer); + println!(" streams: {}", streams.join(", ")); + println!(); + } + }); + + hyper::rt::run( + local + .and_then(|_| connected) + .map_err(|e| eprintln!("{}", e)), + ); } diff --git a/ipfs-api/examples/get_version.rs b/ipfs-api/examples/get_version.rs index deea4e6..41a4ee5 100644 --- a/ipfs-api/examples/get_version.rs +++ b/ipfs-api/examples/get_version.rs @@ -6,21 +6,22 @@ // copied, modified, or distributed except according to those terms. // +extern crate futures; +extern crate hyper; extern crate ipfs_api; -extern crate tokio_core; +use futures::Future; use ipfs_api::IpfsClient; -use tokio_core::reactor::Core; // Creates an Ipfs client, and gets the version of the Ipfs server. // fn main() { println!("connecting to localhost:5001..."); - let mut core = Core::new().expect("expected event loop"); - let client = IpfsClient::default(&core.handle()); - let req = client.version(); - let version = core.run(req).expect("expected a valid response"); + let client = IpfsClient::default(); + let req = client + .version() + .map(|version| println!("version: {:?}", version.version)); - println!("version: {:?}", version.version); + hyper::rt::run(req.map_err(|e| eprintln!("{}", e))); } diff --git a/ipfs-api/examples/mfs.rs b/ipfs-api/examples/mfs.rs index c92e212..8bea192 100644 --- a/ipfs-api/examples/mfs.rs +++ b/ipfs-api/examples/mfs.rs @@ -6,12 +6,13 @@ // copied, modified, or distributed except according to those terms. // +extern crate futures; +extern crate hyper; extern crate ipfs_api; -extern crate tokio_core; +use futures::Future; use ipfs_api::{response, IpfsClient}; use std::fs::File; -use tokio_core::reactor::Core; fn print_stat(stat: response::FilesStatResponse) { println!(" type : {}", stat.typ); @@ -28,47 +29,55 @@ fn main() { println!("note: this must be run in the root of the project repository"); println!("connecting to localhost:5001..."); - let mut core = Core::new().expect("expected event loop"); - let client = IpfsClient::default(&core.handle()); + let client = IpfsClient::default(); println!("making /test..."); println!(); - let req = client.files_mkdir("/test", false); - core.run(req).expect("expected mkdir to succeed"); + let mkdir = client.files_mkdir("/test", false); + let mkdir_recursive = client.files_mkdir("/test/does/not/exist/yet", true); - println!("making dirs /test/does/not/exist/yet..."); - println!(); - - let req = client.files_mkdir("/test/does/not/exist/yet", true); - core.run(req).expect("expected mkdir -p to succeed"); - - println!("getting status of /test/does..."); - println!(); - - let req = client.files_stat("/test/does"); - let stat = core.run(req).expect("expected stat to succeed"); - - print_stat(stat); - - println!("writing source file to /test/mfs.rs"); - println!(); + let file_stat = client.files_stat("/test/does"); let src = File::open(file!()).expect("could not read source file"); - let req = client.files_write("/test/mfs.rs", true, true, src); - - core.run(req).expect("expected write to succed"); - - let req = client.files_stat("/test/mfs.rs"); - let stat = core.run(req).expect("expected stat to succeed"); - - print_stat(stat); - - println!("removing /test..."); - println!(); - - let req = client.files_rm("/test", true); - core.run(req).expect("expected rm to succeed"); - - println!("done!"); + let file_write = client.files_write("/test/mfs.rs", true, true, src); + + let file_write_stat = client.files_stat("/test/mfs.rs"); + + let file_rm = client.files_rm("/test", true); + + hyper::rt::run( + mkdir + .and_then(|_| { + println!("making dirs /test/does/not/exist/yet..."); + println!(); + + mkdir_recursive + }) + .and_then(|_| { + println!("getting status of /test/does..."); + println!(); + + file_stat + }) + .and_then(|stat| { + print_stat(stat); + + println!("writing source file to /test/mfs.rs"); + println!(); + + file_write + }) + .and_then(|_| file_write_stat) + .and_then(|stat| { + print_stat(stat); + + println!("removing /test..."); + println!(); + + file_rm + }) + .map(|_| println!("done!")) + .map_err(|e| eprintln!("{}", e)), + ) } diff --git a/ipfs-api/examples/ping_peer.rs b/ipfs-api/examples/ping_peer.rs index 3deb1e1..be55dd5 100644 --- a/ipfs-api/examples/ping_peer.rs +++ b/ipfs-api/examples/ping_peer.rs @@ -7,12 +7,11 @@ // extern crate futures; +extern crate hyper; extern crate ipfs_api; -extern crate tokio_core; -use futures::stream::Stream; -use ipfs_api::IpfsClient; -use tokio_core::reactor::Core; +use futures::{Future, Stream}; +use ipfs_api::{IpfsClient, response::PingResponse}; // Creates an Ipfs client, discovers a connected peer, and pings it using the // streaming Api, and by collecting it into a collection. @@ -20,39 +19,45 @@ use tokio_core::reactor::Core; fn main() { println!("connecting to localhost:5001..."); - let mut core = Core::new().expect("expected event loop"); - let client = IpfsClient::default(&core.handle()); + let client = IpfsClient::default(); println!(); println!("discovering connected peers..."); - let connected = client.swarm_peers(); - let connected = core.run(connected).expect("expected a valid response"); - - let peer = connected - .peers - .iter() - .next() - .expect("expected at least one peer"); - - println!(); - println!("discovered peer ({})", peer.peer); - println!(); - println!("streaming 10 pings..."); - let req = client.ping(&peer.peer[..], Some(10)); - - core.run(req.for_each(|ping| { - println!("{:?}", ping); - Ok(()) - })).expect("expected a valid response"); - - println!(); - println!("gathering 15 pings..."); - - let req = client.ping(&peer.peer[..], Some(15)); - let pings: Vec<_> = core.run(req.collect()).expect("expected a valid response"); - - for ping in pings.iter() { - println!("got response ({:?}) in ({})...", ping.text, ping.time); - } + let req = client + .swarm_peers() + .and_then(move |connected| { + let peer = connected + .peers + .iter() + .next() + .expect("expected at least one peer"); + + println!(); + println!("discovered peer ({})", peer.peer); + println!(); + println!("streaming 10 pings..."); + + let ping_stream = client.ping(&peer.peer[..], Some(10)).for_each(|ping| { + println!("{:?}", ping); + Ok(()) + }); + + let ping_gather = client.ping(&peer.peer[..], Some(15)).collect(); + + ping_stream.and_then(|_| { + println!(); + println!("gathering 15 pings..."); + + ping_gather + }) + }) + .map(|pings: Vec| { + for ping in pings.iter() { + println!("got response ({:?}) at ({})...", ping.text, ping.time); + } + }) + .map_err(|e| eprintln!("{}", e)); + + hyper::rt::run(req); } diff --git a/ipfs-api/examples/pubsub.rs b/ipfs-api/examples/pubsub.rs index 54a07ac..f760dcd 100644 --- a/ipfs-api/examples/pubsub.rs +++ b/ipfs-api/examples/pubsub.rs @@ -7,23 +7,21 @@ // extern crate futures; +extern crate hyper; extern crate ipfs_api; -extern crate tokio_core; extern crate tokio_timer; -use futures::stream::Stream; -use ipfs_api::{response, IpfsClient}; -use std::thread; -use std::time::Duration; -use tokio_core::reactor::{Core, Handle}; -use tokio_timer::Timer; +use futures::{Future, Stream}; +use ipfs_api::IpfsClient; +use std::{thread, time::{Duration, Instant}}; +use tokio_timer::Interval; static TOPIC: &'static str = "test"; -fn get_client(handle: &Handle) -> IpfsClient { +fn get_client() -> IpfsClient { println!("connecting to localhost:5001..."); - IpfsClient::default(handle) + IpfsClient::default() } // Creates an Ipfs client, and simultaneously publishes and reads from a pubsub @@ -34,43 +32,39 @@ fn main() { // a message to the "test" topic. // thread::spawn(move || { - let mut event_loop = Core::new().expect("expected event loop"); - let client = get_client(&event_loop.handle()); - let timer = Timer::default(); - let publish = timer - .interval(Duration::from_secs(1)) - .map_err(|_| response::Error::from("timeout error")) + let client = get_client(); + let publish = Interval::new(Instant::now(), Duration::from_secs(1)) + .map_err(|e| eprintln!("{}", e)) .for_each(move |_| { println!(); println!("publishing message..."); - client.pubsub_pub(TOPIC, "Hello World!") + client.pubsub_pub(TOPIC, "Hello World!").map_err(|e| eprintln!("{}", e)) }); println!(); println!("starting task to publish messages to ({})...", TOPIC); - event_loop - .run(publish) - .expect("expected the publish task to start"); + hyper::rt::run(publish); }); // This block will execute a future that suscribes to a topic, // and reads any incoming messages. // { - let mut event_loop = Core::new().expect("expected event loop"); - let client = get_client(&event_loop.handle()); + let client = get_client(); let req = client.pubsub_sub(TOPIC, false); println!(); println!("waiting for messages on ({})...", TOPIC); - event_loop - .run(req.take(5).for_each(|msg| { - println!(); - println!("received ({:?})", msg); + hyper::rt::run( + req.take(5) + .for_each(|msg| { + println!(); + println!("received ({:?})", msg); - Ok(()) - })) - .expect("expected a valid response"); + Ok(()) + }) + .map_err(|e| eprintln!("{}", e)), + ) } } -- cgit v1.2.3