Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/custom-protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ async fn listen(text: Vec<String>) -> Result<()> {
proto.insert_and_index(text).await?;
}
// Build the iroh-blobs protocol handler, which is used to download blobs.
let blobs = BlobsProtocol::new(&store, endpoint.clone(), None);
let blobs = BlobsProtocol::builder(&store).build(&endpoint);

// create a router that handles both our custom protocol and the iroh-blobs protocol.
let node = Router::builder(endpoint)
Expand Down
2 changes: 1 addition & 1 deletion examples/mdns-discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async fn accept(path: &Path) -> Result<()> {
.await?;
let builder = Router::builder(endpoint.clone());
let store = MemStore::new();
let blobs = BlobsProtocol::new(&store, endpoint.clone(), None);
let blobs = BlobsProtocol::builder(&store).build(&endpoint);
let builder = builder.accept(iroh_blobs::ALPN, blobs.clone());
let node = builder.spawn();

Expand Down
4 changes: 3 additions & 1 deletion examples/random_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,9 @@ async fn provide(args: ProvideArgs) -> anyhow::Result<()> {
.bind()
.await?;
let (dump_task, events_tx) = dump_provider_events(args.allow_push);
let blobs = iroh_blobs::BlobsProtocol::new(&store, endpoint.clone(), Some(events_tx));
let blobs = iroh_blobs::BlobsProtocol::builder(&store)
.events(events_tx)
.build(&endpoint);
let router = iroh::protocol::Router::builder(endpoint.clone())
.accept(iroh_blobs::ALPN, blobs)
.spawn();
Expand Down
45 changes: 44 additions & 1 deletion src/net_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
//! # }
//! ```

use std::{fmt::Debug, future::Future, ops::Deref, sync::Arc};
use std::{fmt::Debug, future::Future, ops::Deref, path::Path, sync::Arc};

use iroh::{
endpoint::Connection,
Expand All @@ -49,6 +49,7 @@ use tracing::error;
use crate::{
api::Store,
provider::{Event, EventSender},
store::{fs::FsStore, mem::MemStore},
ticket::BlobTicket,
HashAndFormat,
};
Expand All @@ -66,6 +67,32 @@ pub struct BlobsProtocol {
pub(crate) inner: Arc<BlobsInner>,
}

/// A builder for the blobs protocol handler.
pub struct BlobsProtocolBuilder {
pub store: Store,
pub events: Option<mpsc::Sender<Event>>,
}

impl BlobsProtocolBuilder {
fn new(store: Store) -> Self {
Self {
store,
events: None,
}
}

/// Set provider events.
pub fn events(mut self, events: mpsc::Sender<Event>) -> Self {
self.events = Some(events);
self
}

/// Build the blobs protocol handler.
pub fn build(self, endpoint: &Endpoint) -> BlobsProtocol {
BlobsProtocol::new(&self.store, endpoint.clone(), self.events)
}
}

impl Deref for BlobsProtocol {
type Target = Store;

Expand All @@ -85,6 +112,22 @@ impl BlobsProtocol {
}
}

/// Create a new Blobs protocol handler builder, given a store.
pub fn builder(store: &Store) -> BlobsProtocolBuilder {
BlobsProtocolBuilder::new(store.clone())
}

/// Create a new memory-backed Blobs protocol handler.
pub fn memory() -> BlobsProtocolBuilder {
Self::builder(&MemStore::new())
}

/// Load a persistent Blobs protocol handler from a path.
pub async fn persistent(path: impl AsRef<Path>) -> anyhow::Result<BlobsProtocolBuilder> {
let store = FsStore::load(path).await?;
Ok(Self::builder(&store))
}

pub fn store(&self) -> &Store {
&self.inner.store
}
Expand Down
Loading