Merge #390
390: Example tuneup r=koivunej a=koivunej Removes the examples: - client1 and client2 - ipfs_bitswap_test Also removes the stray file `examples/block.data`. It used to be used by something. I wrote a longer one but the short gist is that fetch_and_cat has superceded both, except for the ipld stuff, which will now have zero examples but there are tests. It's better to have one clear example than to have multiple confusing and out of date ones. Co-authored-by: Joonas Koivunen <joonas@equilibrium.co> Co-authored-by: Joonas Koivunen <joonas.koivunen@gmail.com>
This commit is contained in:
commit
a7cca369d5
@ -1 +0,0 @@
|
||||
Here is some data
|
@ -1,21 +0,0 @@
|
||||
use futures::join;
|
||||
use ipfs::{make_ipld, Ipfs, TestTypes, UninitializedIpfs};
|
||||
use tokio::task;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let (ipfs, fut): (Ipfs<TestTypes>, _) =
|
||||
UninitializedIpfs::default().await.start().await.unwrap();
|
||||
task::spawn(fut);
|
||||
|
||||
let f1 = ipfs.put_dag(make_ipld!("block1"));
|
||||
let f2 = ipfs.put_dag(make_ipld!("block2"));
|
||||
let (res1, res2) = join!(f1, f2);
|
||||
|
||||
let root = make_ipld!([res1.unwrap(), res2.unwrap()]);
|
||||
ipfs.put_dag(root).await.unwrap();
|
||||
|
||||
ipfs.exit_daemon().await;
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
use futures::join;
|
||||
use ipfs::{Ipfs, IpfsPath, TestTypes, UninitializedIpfs};
|
||||
use std::str::FromStr;
|
||||
use tokio::task;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let path =
|
||||
IpfsPath::from_str("/ipfs/zdpuB1caPcm4QNXeegatVfLQ839Lmprd5zosXGwRUBJHwj66X").unwrap();
|
||||
|
||||
let (ipfs, fut): (Ipfs<TestTypes>, _) =
|
||||
UninitializedIpfs::default().await.start().await.unwrap();
|
||||
task::spawn(fut);
|
||||
|
||||
let f1 = ipfs.get_dag(path.sub_path("0").unwrap());
|
||||
let f2 = ipfs.get_dag(path.sub_path("1").unwrap());
|
||||
let (res1, res2) = join!(f1, f2);
|
||||
println!("Received block with contents: {:?}", res1.unwrap());
|
||||
println!("Received block with contents: {:?}", res2.unwrap());
|
||||
|
||||
ipfs.exit_daemon().await;
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
use futures::pin_mut;
|
||||
use futures::stream::StreamExt; // needed for StreamExt::next
|
||||
use ipfs::{Ipfs, IpfsOptions, IpfsPath, MultiaddrWithPeerId, TestTypes, UninitializedIpfs};
|
||||
use ipfs::{Error, Ipfs, IpfsOptions, IpfsPath, MultiaddrWithPeerId, TestTypes, UninitializedIpfs};
|
||||
use std::env;
|
||||
use std::process::exit;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
@ -9,21 +9,18 @@ use tokio::io::AsyncWriteExt;
|
||||
async fn main() {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
// this example will wait forever attempting to fetch a CID provided at command line. It is
|
||||
// expected to be used by connecting another ipfs peer to it and providing the blocks from that
|
||||
// peer.
|
||||
// This example attempts to fetch a CID provided at command line. It is expected to be used by
|
||||
// either:
|
||||
//
|
||||
// - connecting another ipfs peer to it
|
||||
// - be given the other peers address as the last argument
|
||||
//
|
||||
// The other connecting or connected peer must be providing the requested CID or this will hang
|
||||
// forever.
|
||||
|
||||
let path = match env::args().nth(1).map(|s| s.parse::<IpfsPath>()) {
|
||||
Some(Ok(cid)) => cid,
|
||||
Some(Err(e)) => {
|
||||
eprintln!(
|
||||
"Failed to parse {} as IpfsPath: {}",
|
||||
env::args().nth(1).unwrap(),
|
||||
e
|
||||
);
|
||||
exit(1);
|
||||
}
|
||||
None => {
|
||||
let (path, target) = match parse_options() {
|
||||
Ok(Some(tuple)) => tuple,
|
||||
Ok(None) => {
|
||||
eprintln!("Usage: fetch_and_cat <IPFS_PATH | CID> [MULTIADDR]");
|
||||
eprintln!(
|
||||
"Example will accept connections and print all bytes of the unixfs file to \
|
||||
@ -33,28 +30,32 @@ async fn main() {
|
||||
peer_id. The given Multiaddr will be connected to instead of awaiting an incoming connection.");
|
||||
exit(0);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Invalid argument: {:?}", e);
|
||||
exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
if path.root().cid().is_none() {
|
||||
eprintln!(
|
||||
"Unsupported path: ipns resolution is not available yet: {}",
|
||||
path
|
||||
);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
let target = env::args()
|
||||
.nth(2)
|
||||
.map(|s| s.parse::<MultiaddrWithPeerId>().unwrap());
|
||||
|
||||
// Start daemon and initialize repo
|
||||
// Initialize the repo and start a daemon.
|
||||
//
|
||||
// Here we are using the IpfsOptions::inmemory_with_generated_keys, which creates a new random
|
||||
// key and in-memory storage for blocks and pins.
|
||||
let mut opts = IpfsOptions::inmemory_with_generated_keys();
|
||||
|
||||
// Disable MDNS to explicitly connect or be connected just in case there are multiple IPFS
|
||||
// nodes running.
|
||||
opts.mdns = false;
|
||||
|
||||
// UninitializedIpfs will handle starting up the repository and return the facade (ipfs::Ipfs)
|
||||
// and the background task (ipfs::IpfsFuture).
|
||||
let (ipfs, fut): (Ipfs<TestTypes>, _) = UninitializedIpfs::new(opts, None)
|
||||
.await
|
||||
.start()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// The background task must be spawned to use anything other than the repository; most notably,
|
||||
// the libp2p.
|
||||
tokio::task::spawn(fut);
|
||||
|
||||
if let Some(target) = target {
|
||||
@ -72,14 +73,18 @@ async fn main() {
|
||||
eprintln!();
|
||||
}
|
||||
|
||||
// Calling Ipfs::cat_unixfs returns a future of a stream, because the path resolving
|
||||
// and the initial block loading will require at least one async call before any actual file
|
||||
// content can be *streamed*.
|
||||
let stream = ipfs.cat_unixfs(path, None).await.unwrap_or_else(|e| {
|
||||
eprintln!("Error: {}", e);
|
||||
exit(1);
|
||||
});
|
||||
|
||||
// The stream needs to be pinned on the stack to be used with StreamExt::next
|
||||
pin_mut!(stream);
|
||||
let mut stdout = tokio::io::stdout();
|
||||
|
||||
let mut stdout = tokio::io::stdout();
|
||||
let mut total = 0;
|
||||
|
||||
loop {
|
||||
@ -105,3 +110,28 @@ async fn main() {
|
||||
|
||||
eprintln!("Total received: {} bytes", total);
|
||||
}
|
||||
|
||||
fn parse_options() -> Result<Option<(IpfsPath, Option<MultiaddrWithPeerId>)>, Error> {
|
||||
let mut args = env::args().skip(1);
|
||||
|
||||
let path = if let Some(path) = args.next() {
|
||||
path.parse::<IpfsPath>()
|
||||
.map_err(|e| e.context(format!("failed to parse {:?} as IpfsPath", path)))?
|
||||
} else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
let target = if let Some(multiaddr) = args.next() {
|
||||
let ma = multiaddr.parse::<MultiaddrWithPeerId>().map_err(|e| {
|
||||
Error::new(e).context(format!(
|
||||
"failed to parse {:?} as MultiaddrWithPeerId",
|
||||
multiaddr
|
||||
))
|
||||
})?;
|
||||
Some(ma)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
Ok(Some((path, target)))
|
||||
}
|
||||
|
@ -1,67 +0,0 @@
|
||||
#![recursion_limit = "512"]
|
||||
|
||||
use cid::{Cid, Codec};
|
||||
use ipfs::{Block, Ipfs, TestTypes, UninitializedIpfs};
|
||||
use multihash::Sha2_256;
|
||||
use tokio::{
|
||||
io::{stdin, AsyncBufReadExt, BufReader},
|
||||
task,
|
||||
};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
// this example demonstrates
|
||||
// - block building
|
||||
// - local swarm communication with go-ipfs
|
||||
|
||||
// Start daemon and initialize repo
|
||||
let (ipfs, fut): (Ipfs<TestTypes>, _) =
|
||||
UninitializedIpfs::default().await.start().await.unwrap();
|
||||
task::spawn(fut);
|
||||
|
||||
let data = b"block-want\n".to_vec().into_boxed_slice();
|
||||
let wanted = Cid::new_v1(Codec::Raw, Sha2_256::digest(&data));
|
||||
|
||||
let (public_key, addresses) = ipfs.identity().await.unwrap();
|
||||
assert!(!addresses.is_empty(), "Zero listening addresses");
|
||||
|
||||
eprintln!("Please connect an ipfs node having {} to:\n", wanted);
|
||||
|
||||
let peer_id = public_key.into_peer_id().to_string();
|
||||
|
||||
for address in addresses {
|
||||
eprintln!(" - {}/p2p/{}", address, peer_id);
|
||||
}
|
||||
|
||||
eprintln!();
|
||||
eprintln!("The block wanted in this example can be created on the other node:");
|
||||
eprintln!(" echo block-want | ipfs block put -f raw");
|
||||
eprintln!();
|
||||
|
||||
// Create a Block
|
||||
let data = b"block-provide\n".to_vec().into_boxed_slice();
|
||||
let cid = Cid::new_v1(Codec::Raw, Sha2_256::digest(&data));
|
||||
let provided = ipfs.put_block(Block::new(data, cid)).await.unwrap();
|
||||
|
||||
eprintln!(
|
||||
"After connecting the node, it can be used to get block: {}",
|
||||
provided
|
||||
);
|
||||
eprintln!("This should print out \"block-provide\\n\":");
|
||||
eprintln!(" ipfs block get {}", provided);
|
||||
eprintln!();
|
||||
|
||||
// Retrive a Block
|
||||
let block = ipfs.get_block(&wanted).await.unwrap();
|
||||
let contents = std::str::from_utf8(block.data()).unwrap();
|
||||
eprintln!("Block retrieved: {:?}", contents);
|
||||
|
||||
eprintln!();
|
||||
eprintln!("Press enter or CTRL-C to exit this example.");
|
||||
|
||||
let _ = BufReader::new(stdin()).read_line(&mut String::new()).await;
|
||||
|
||||
ipfs.exit_daemon().await;
|
||||
}
|
Loading…
Reference in New Issue
Block a user