|
| 1 | +use std::path::PathBuf; |
| 2 | + |
| 3 | +use iroh::{protocol::Router, Endpoint}; |
| 4 | +use iroh_blobs::{net_protocol::Blobs, store::mem::MemStore, ticket::BlobTicket}; |
| 5 | + |
| 6 | +#[tokio::main] |
| 7 | +async fn main() -> anyhow::Result<()> { |
| 8 | + // Create an endpoint, it allows creating and accepting |
| 9 | + // connections in the iroh p2p world |
| 10 | + let endpoint = Endpoint::builder().discovery_n0().bind().await?; |
| 11 | + |
| 12 | + // We initialize an in-memory backing store for iroh-blobs |
| 13 | + let store = MemStore::new(); |
| 14 | + // Then we initialize a struct that can accept blobs requests over iroh connections |
| 15 | + let blobs = Blobs::new(&store, endpoint.clone(), None); |
| 16 | + |
| 17 | + // Grab all passed in arguments, the first one is the binary itself, so we skip it. |
| 18 | + let args: Vec<String> = std::env::args().skip(1).collect(); |
| 19 | + // Convert to &str, so we can pattern-match easily: |
| 20 | + let arg_refs: Vec<&str> = args.iter().map(String::as_str).collect(); |
| 21 | + |
| 22 | + match arg_refs.as_slice() { |
| 23 | + ["send", filename] => { |
| 24 | + let filename: PathBuf = filename.parse()?; |
| 25 | + let abs_path = std::path::absolute(&filename)?; |
| 26 | + |
| 27 | + println!("Hashing file."); |
| 28 | + |
| 29 | + // When we import a blob, we get back a "tag" that refers to said blob in the store |
| 30 | + // and allows us to control when/if it gets garbage-collected |
| 31 | + let tag = store.blobs().add_path(abs_path).await?; |
| 32 | + |
| 33 | + let node_id = endpoint.node_id(); |
| 34 | + let ticket = BlobTicket::new(node_id.into(), tag.hash, tag.format); |
| 35 | + |
| 36 | + println!("File hashed. Fetch this file by running:"); |
| 37 | + println!( |
| 38 | + "cargo run --example transfer -- receive {ticket} {}", |
| 39 | + filename.display() |
| 40 | + ); |
| 41 | + |
| 42 | + // For sending files we build a router that accepts blobs connections & routes them |
| 43 | + // to the blobs protocol. |
| 44 | + let router = Router::builder(endpoint) |
| 45 | + .accept(iroh_blobs::ALPN, blobs) |
| 46 | + .spawn(); |
| 47 | + |
| 48 | + tokio::signal::ctrl_c().await?; |
| 49 | + |
| 50 | + // Gracefully shut down the node |
| 51 | + println!("Shutting down."); |
| 52 | + router.shutdown().await?; |
| 53 | + } |
| 54 | + ["receive", ticket, filename] => { |
| 55 | + let filename: PathBuf = filename.parse()?; |
| 56 | + let abs_path = std::path::absolute(filename)?; |
| 57 | + let ticket: BlobTicket = ticket.parse()?; |
| 58 | + |
| 59 | + // For receiving files, we create a "downloader" that allows us to fetch files |
| 60 | + // from other nodes via iroh connections |
| 61 | + let downloader = store.downloader(&endpoint); |
| 62 | + |
| 63 | + println!("Starting download."); |
| 64 | + |
| 65 | + downloader |
| 66 | + .download(ticket.hash(), Some(ticket.node_addr().node_id)) |
| 67 | + .await?; |
| 68 | + |
| 69 | + println!("Finished download."); |
| 70 | + println!("Copying to destination."); |
| 71 | + |
| 72 | + store.blobs().export(ticket.hash(), abs_path).await?; |
| 73 | + |
| 74 | + println!("Finished copying."); |
| 75 | + |
| 76 | + // Gracefully shut down the node |
| 77 | + println!("Shutting down."); |
| 78 | + endpoint.close().await; |
| 79 | + } |
| 80 | + _ => { |
| 81 | + println!("Couldn't parse command line arguments: {args:?}"); |
| 82 | + println!("Usage:"); |
| 83 | + println!(" # to send:"); |
| 84 | + println!(" cargo run --example transfer -- send [FILE]"); |
| 85 | + println!(" # this will print a ticket."); |
| 86 | + println!(); |
| 87 | + println!(" # to receive:"); |
| 88 | + println!(" cargo run --example transfer -- receive [TICKET] [FILE]"); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + Ok(()) |
| 93 | +} |
0 commit comments