Skip to content

Commit bbbb636

Browse files
example: simplify transfer example (#53)
## Description <!-- A summary of what this pull request achieves and a rough list of changes. --> ## Breaking Changes <!-- Optional, if there are any breaking changes document them, including how to migrate older code. --> ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR. --> ## Change checklist - [ ] Self-review. - [ ] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [ ] Tests if relevant. - [ ] All breaking changes documented. --------- Co-authored-by: Philipp Krüger <[email protected]>
1 parent d8d2b48 commit bbbb636

File tree

1 file changed

+43
-23
lines changed

1 file changed

+43
-23
lines changed

examples/transfer.rs

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
use std::{path::PathBuf, str::FromStr};
1+
use std::path::PathBuf;
22

33
use anyhow::Result;
44
use iroh::{protocol::Router, Endpoint};
55
use iroh_blobs::{
66
net_protocol::Blobs,
7-
rpc::client::blobs::{ReadAtLen, WrapOption},
7+
rpc::client::blobs::WrapOption,
8+
store::{ExportFormat, ExportMode},
89
ticket::BlobTicket,
910
util::SetTagOption,
1011
};
@@ -19,41 +20,53 @@ async fn main() -> Result<()> {
1920

2021
// Now we build a router that accepts blobs connections & routes them
2122
// to the blobs protocol.
22-
let node = Router::builder(endpoint)
23+
let router = Router::builder(endpoint)
2324
.accept(iroh_blobs::ALPN, blobs.clone())
2425
.spawn()
2526
.await?;
2627

27-
let blobs = blobs.client();
28+
// We use a blobs client to interact with the blobs protocol we're running locally:
29+
let blobs_client = blobs.client();
2830

29-
let args = std::env::args().collect::<Vec<_>>();
30-
match &args.iter().map(String::as_str).collect::<Vec<_>>()[..] {
31-
[_cmd, "send", path] => {
32-
let abs_path = PathBuf::from_str(path)?.canonicalize()?;
31+
// Grab all passed in arguments, the first one is the binary itself, so we skip it.
32+
let args: Vec<String> = std::env::args().skip(1).collect();
33+
// Convert to &str, so we can pattern-match easily:
34+
let arg_refs: Vec<&str> = args.iter().map(String::as_str).collect();
3335

34-
println!("Analyzing file.");
36+
match arg_refs.as_slice() {
37+
["send", filename] => {
38+
let filename: PathBuf = filename.parse()?;
39+
let abs_path = std::path::absolute(&filename)?;
3540

36-
let blob = blobs
37-
.add_from_path(abs_path, true, SetTagOption::Auto, WrapOption::NoWrap)
41+
println!("Hashing file.");
42+
43+
// keep the file in place and link it, instead of copying it into the in-memory blobs database
44+
let in_place = true;
45+
let blob = blobs_client
46+
.add_from_path(abs_path, in_place, SetTagOption::Auto, WrapOption::NoWrap)
3847
.await?
3948
.finish()
4049
.await?;
4150

42-
let node_id = node.endpoint().node_id();
51+
let node_id = router.endpoint().node_id();
4352
let ticket = BlobTicket::new(node_id.into(), blob.hash, blob.format)?;
4453

45-
println!("File analyzed. Fetch this file by running:");
46-
println!("cargo run --example transfer -- receive {ticket} {path}");
54+
println!("File hashed. Fetch this file by running:");
55+
println!(
56+
"cargo run --example transfer -- receive {ticket} {}",
57+
filename.display()
58+
);
4759

4860
tokio::signal::ctrl_c().await?;
4961
}
50-
[_cmd, "receive", ticket, path] => {
51-
let path_buf = PathBuf::from_str(path)?;
52-
let ticket = BlobTicket::from_str(ticket)?;
62+
["receive", ticket, filename] => {
63+
let filename: PathBuf = filename.parse()?;
64+
let abs_path = std::path::absolute(filename)?;
65+
let ticket: BlobTicket = ticket.parse()?;
5366

5467
println!("Starting download.");
5568

56-
blobs
69+
blobs_client
5770
.download(ticket.hash(), ticket.node_addr().clone())
5871
.await?
5972
.finish()
@@ -62,14 +75,21 @@ async fn main() -> Result<()> {
6275
println!("Finished download.");
6376
println!("Copying to destination.");
6477

65-
let mut file = tokio::fs::File::create(path_buf).await?;
66-
let mut reader = blobs.read_at(ticket.hash(), 0, ReadAtLen::All).await?;
67-
tokio::io::copy(&mut reader, &mut file).await?;
78+
blobs_client
79+
.export(
80+
ticket.hash(),
81+
abs_path,
82+
ExportFormat::Blob,
83+
ExportMode::Copy,
84+
)
85+
.await?
86+
.finish()
87+
.await?;
6888

6989
println!("Finished copying.");
7090
}
7191
_ => {
72-
println!("Couldn't parse command line arguments.");
92+
println!("Couldn't parse command line arguments: {args:?}");
7393
println!("Usage:");
7494
println!(" # to send:");
7595
println!(" cargo run --example transfer -- send [FILE]");
@@ -82,7 +102,7 @@ async fn main() -> Result<()> {
82102

83103
// Gracefully shut down the node
84104
println!("Shutting down.");
85-
node.shutdown().await?;
105+
router.shutdown().await?;
86106

87107
Ok(())
88108
}

0 commit comments

Comments
 (0)