use std::{fmt::Debug, ops::Deref, sync::Arc};
use iroh::{
endpoint::Connection,
protocol::{AcceptError, ProtocolHandler},
};
use tracing::error;
use crate::{api::Store, provider::events::EventSender};
#[derive(Debug)]
pub(crate) struct BlobsInner {
store: Store,
events: EventSender,
}
#[derive(Debug, Clone)]
pub struct BlobsProtocol {
inner: Arc<BlobsInner>,
}
impl Deref for BlobsProtocol {
type Target = Store;
fn deref(&self) -> &Self::Target {
&self.inner.store
}
}
impl BlobsProtocol {
pub fn new(store: &Store, events: Option<EventSender>) -> Self {
Self {
inner: Arc::new(BlobsInner {
store: store.clone(),
events: events.unwrap_or(EventSender::DEFAULT),
}),
}
}
pub fn store(&self) -> &Store {
&self.inner.store
}
}
impl ProtocolHandler for BlobsProtocol {
async fn accept(&self, conn: Connection) -> std::result::Result<(), AcceptError> {
let store = self.store().clone();
let events = self.inner.events.clone();
crate::provider::handle_connection(conn, store, events).await;
Ok(())
}
async fn shutdown(&self) {
if let Err(cause) = self.store().shutdown().await {
error!("error shutting down store: {:?}", cause);
}
}
}