-
Notifications
You must be signed in to change notification settings - Fork 14
docs: Update the transfer example to iroh 0.90 #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use std::path::PathBuf; | ||
|
||
use iroh::{protocol::Router, Endpoint}; | ||
use iroh_blobs::{net_protocol::Blobs, store::mem::MemStore, ticket::BlobTicket}; | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
// Create an endpoint, it allows creating and accepting | ||
// connections in the iroh p2p world | ||
let endpoint = Endpoint::builder().discovery_n0().bind().await?; | ||
|
||
// We initialize an in-memory backing store for iroh-blobs | ||
let store = MemStore::new(); | ||
// Then we initialize a struct that can accept blobs requests over iroh connections | ||
let blobs = Blobs::new(&store, endpoint.clone(), None); | ||
|
||
// Grab all passed in arguments, the first one is the binary itself, so we skip it. | ||
let args: Vec<String> = std::env::args().skip(1).collect(); | ||
// Convert to &str, so we can pattern-match easily: | ||
let arg_refs: Vec<&str> = args.iter().map(String::as_str).collect(); | ||
|
||
match arg_refs.as_slice() { | ||
["send", filename] => { | ||
let filename: PathBuf = filename.parse()?; | ||
let abs_path = std::path::absolute(&filename)?; | ||
|
||
println!("Hashing file."); | ||
|
||
// When we import a blob, we get back a "tag" that refers to said blob in the store | ||
// and allows us to control when/if it gets garbage-collected | ||
let tag = store.blobs().add_path(abs_path).await?; | ||
|
||
let node_id = endpoint.node_id(); | ||
let ticket = BlobTicket::new(node_id.into(), tag.hash, tag.format); | ||
|
||
println!("File hashed. Fetch this file by running:"); | ||
println!( | ||
"cargo run --example transfer -- receive {ticket} {}", | ||
filename.display() | ||
); | ||
|
||
// For sending files we build a router that accepts blobs connections & routes them | ||
// to the blobs protocol. | ||
let router = Router::builder(endpoint) | ||
.accept(iroh_blobs::ALPN, blobs) | ||
.spawn(); | ||
|
||
tokio::signal::ctrl_c().await?; | ||
|
||
// Gracefully shut down the node | ||
println!("Shutting down."); | ||
router.shutdown().await?; | ||
} | ||
["receive", ticket, filename] => { | ||
let filename: PathBuf = filename.parse()?; | ||
let abs_path = std::path::absolute(filename)?; | ||
let ticket: BlobTicket = ticket.parse()?; | ||
|
||
// For receiving files, we create a "downloader" that allows us to fetch files | ||
// from other nodes via iroh connections | ||
let downloader = store.downloader(&endpoint); | ||
|
||
println!("Starting download."); | ||
|
||
downloader | ||
.download(ticket.hash(), Some(ticket.node_addr().node_id)) | ||
.await?; | ||
|
||
println!("Finished download."); | ||
println!("Copying to destination."); | ||
|
||
store.blobs().export(ticket.hash(), abs_path).await?; | ||
|
||
println!("Finished copying."); | ||
|
||
// Gracefully shut down the node | ||
println!("Shutting down."); | ||
endpoint.close().await; | ||
} | ||
_ => { | ||
println!("Couldn't parse command line arguments: {args:?}"); | ||
println!("Usage:"); | ||
println!(" # to send:"); | ||
println!(" cargo run --example transfer -- send [FILE]"); | ||
println!(" # this will print a ticket."); | ||
println!(); | ||
println!(" # to receive:"); | ||
println!(" cargo run --example transfer -- receive [TICKET] [FILE]"); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.