p2panda_blobs/
protocol.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! Blobs protocol handler implementation for accepting inbound network connections.
4use std::sync::Arc;
5
6use anyhow::Result;
7use futures_lite::future::Boxed as BoxedFuture;
8use iroh_blobs::protocol::ALPN;
9use iroh_blobs::provider::{self, EventSender};
10use iroh_blobs::store::Store;
11use iroh_blobs::util::local_pool::LocalPoolHandle;
12use iroh_net::endpoint::Connecting;
13use p2panda_net::ProtocolHandler;
14
15/// Application-Layer Protocol Negotiation (ALPN) identifier for blobs.
16pub const BLOBS_ALPN: &[u8] = ALPN;
17
18/// Blobs connection handler.
19#[derive(Debug)]
20pub struct BlobsProtocol<S> {
21    rt: LocalPoolHandle,
22    store: S,
23}
24
25impl<S: Store> BlobsProtocol<S> {
26    /// Returns a new instance of `BlobsProtocol` using the given store and local task pool.
27    ///
28    /// `BlobsProtocol` implements the `ProtocolHandler` trait, allowing it to accept inbound
29    /// network connections for the purposes of blob sync.
30    pub fn new(store: S, rt: LocalPoolHandle) -> Self {
31        Self { rt, store }
32    }
33}
34
35impl<S: Store> ProtocolHandler for BlobsProtocol<S> {
36    fn accept(self: Arc<Self>, conn: Connecting) -> BoxedFuture<Result<()>> {
37        Box::pin(async move {
38            provider::handle_connection(
39                conn.await?,
40                self.store.clone(),
41                EventSender::default(),
42                self.rt.clone(),
43            )
44            .await;
45            Ok(())
46        })
47    }
48}