Skip to content

Commit 8395e98

Browse files
committed
Add back builder for blobs protocol
1 parent 73ca073 commit 8395e98

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

examples/custom-protocol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ async fn listen(text: Vec<String>) -> Result<()> {
100100
proto.insert_and_index(text).await?;
101101
}
102102
// Build the iroh-blobs protocol handler, which is used to download blobs.
103-
let blobs = BlobsProtocol::new(&store, endpoint.clone(), None);
103+
let blobs = BlobsProtocol::builder(&store).build(&endpoint);
104104

105105
// create a router that handles both our custom protocol and the iroh-blobs protocol.
106106
let node = Router::builder(endpoint)

examples/mdns-discovery.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ async fn accept(path: &Path) -> Result<()> {
6868
.await?;
6969
let builder = Router::builder(endpoint.clone());
7070
let store = MemStore::new();
71-
let blobs = BlobsProtocol::new(&store, endpoint.clone(), None);
71+
let blobs = BlobsProtocol::builder(&store).build(&endpoint);
7272
let builder = builder.accept(iroh_blobs::ALPN, blobs.clone());
7373
let node = builder.spawn();
7474

examples/random_store.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,9 @@ async fn provide(args: ProvideArgs) -> anyhow::Result<()> {
237237
.bind()
238238
.await?;
239239
let (dump_task, events_tx) = dump_provider_events(args.allow_push);
240-
let blobs = iroh_blobs::BlobsProtocol::new(&store, endpoint.clone(), Some(events_tx));
240+
let blobs = iroh_blobs::BlobsProtocol::builder(&store)
241+
.events(events_tx)
242+
.build(&endpoint);
241243
let router = iroh::protocol::Router::builder(endpoint.clone())
242244
.accept(iroh_blobs::ALPN, blobs)
243245
.spawn();

src/net_protocol.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
//! # }
3737
//! ```
3838
39-
use std::{fmt::Debug, future::Future, ops::Deref, sync::Arc};
39+
use std::{fmt::Debug, future::Future, ops::Deref, path::Path, sync::Arc};
4040

4141
use iroh::{
4242
endpoint::Connection,
@@ -49,6 +49,7 @@ use tracing::error;
4949
use crate::{
5050
api::Store,
5151
provider::{Event, EventSender},
52+
store::{fs::FsStore, mem::MemStore},
5253
ticket::BlobTicket,
5354
HashAndFormat,
5455
};
@@ -66,6 +67,32 @@ pub struct BlobsProtocol {
6667
pub(crate) inner: Arc<BlobsInner>,
6768
}
6869

70+
/// A builder for the blobs protocol handler.
71+
pub struct BlobsProtocolBuilder {
72+
pub store: Store,
73+
pub events: Option<mpsc::Sender<Event>>,
74+
}
75+
76+
impl BlobsProtocolBuilder {
77+
fn new(store: Store) -> Self {
78+
Self {
79+
store,
80+
events: None,
81+
}
82+
}
83+
84+
/// Set provider events.
85+
pub fn events(mut self, events: mpsc::Sender<Event>) -> Self {
86+
self.events = Some(events);
87+
self
88+
}
89+
90+
/// Build the blobs protocol handler.
91+
pub fn build(self, endpoint: &Endpoint) -> BlobsProtocol {
92+
BlobsProtocol::new(&self.store, endpoint.clone(), self.events)
93+
}
94+
}
95+
6996
impl Deref for BlobsProtocol {
7097
type Target = Store;
7198

@@ -85,6 +112,22 @@ impl BlobsProtocol {
85112
}
86113
}
87114

115+
/// Create a new Blobs protocol handler builder, given a store.
116+
pub fn builder(store: &Store) -> BlobsProtocolBuilder {
117+
BlobsProtocolBuilder::new(store.clone())
118+
}
119+
120+
/// Create a new memory-backed Blobs protocol handler.
121+
pub fn memory() -> BlobsProtocolBuilder {
122+
Self::builder(&MemStore::new())
123+
}
124+
125+
/// Load a persistent Blobs protocol handler from a path.
126+
pub async fn persistent(path: impl AsRef<Path>) -> anyhow::Result<BlobsProtocolBuilder> {
127+
let store = FsStore::load(path).await?;
128+
Ok(Self::builder(&store))
129+
}
130+
88131
pub fn store(&self) -> &Store {
89132
&self.inner.store
90133
}

0 commit comments

Comments
 (0)