Skip to content

Instantly share code, notes, and snippets.

@rklaehn
Last active July 14, 2023 13:23
Show Gist options
  • Save rklaehn/e1f44fac66ceb098605552140f42714e to your computer and use it in GitHub Desktop.
Save rklaehn/e1f44fac66ceb098605552140f42714e to your computer and use it in GitHub Desktop.
/// Run a get request that is complete (no dealing with resume) and hardcoded to the (name, hash) iroh collection
async fn run_get_request_collection(
opts: iroh::dial::Options,
hash: Hash,
) -> anyhow::Result<Stats> {
let request = GetRequest::all(hash).into();
let connection = iroh::dial::dial(opts).await?;
let initial = fsm::start(connection, request);
let connected = initial.next().await?;
// on connected callback
use fsm::*;
let ConnectedNext::StartRoot(sc) = connected.next().await? else {
anyhow::bail!("request did not include collection");
};
let (end, data) = sc.next().concatenate_into_vec().await?;
let collection = Collection::from_bytes(&data)?;
// on collection callback
let mut next = end.next();
let finishing = loop {
let start = match next {
EndBlobNext::MoreChildren(start) => start,
EndBlobNext::Closing(finishing) => break finishing,
};
let blob = &collection.blobs()[start.child_offset() as usize];
let (done, data) = start.next(blob.hash).concatenate_into_vec().await?;
// on blob callback
next = done.next();
};
let stats = finishing.next().await?;
Ok(stats)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment