iroh_blobs/
net_protocol.rs1use std::{fmt::Debug, ops::Deref, sync::Arc};
40
41use iroh::{
42 endpoint::Connection,
43 protocol::{AcceptError, ProtocolHandler},
44 Endpoint, Watcher,
45};
46use tracing::error;
47
48use crate::{api::Store, provider::events::EventSender, ticket::BlobTicket, HashAndFormat};
49
50#[derive(Debug)]
51pub(crate) struct BlobsInner {
52 pub(crate) store: Store,
53 pub(crate) endpoint: Endpoint,
54 pub(crate) events: EventSender,
55}
56
57#[derive(Debug, Clone)]
59pub struct BlobsProtocol {
60 pub(crate) inner: Arc<BlobsInner>,
61}
62
63impl Deref for BlobsProtocol {
64 type Target = Store;
65
66 fn deref(&self) -> &Self::Target {
67 &self.inner.store
68 }
69}
70
71impl BlobsProtocol {
72 pub fn new(store: &Store, endpoint: Endpoint, events: Option<EventSender>) -> Self {
73 Self {
74 inner: Arc::new(BlobsInner {
75 store: store.clone(),
76 endpoint,
77 events: events.unwrap_or(EventSender::DEFAULT),
78 }),
79 }
80 }
81
82 pub fn store(&self) -> &Store {
83 &self.inner.store
84 }
85
86 pub fn endpoint(&self) -> &Endpoint {
87 &self.inner.endpoint
88 }
89
90 pub async fn ticket(&self, content: impl Into<HashAndFormat>) -> anyhow::Result<BlobTicket> {
95 let content = content.into();
96 let addr = self.inner.endpoint.node_addr().initialized().await;
97 let ticket = BlobTicket::new(addr, content.hash, content.format);
98 Ok(ticket)
99 }
100}
101
102impl ProtocolHandler for BlobsProtocol {
103 async fn accept(&self, conn: Connection) -> std::result::Result<(), AcceptError> {
104 let store = self.store().clone();
105 let events = self.inner.events.clone();
106 crate::provider::handle_connection(conn, store, events).await;
107 Ok(())
108 }
109
110 async fn shutdown(&self) {
111 if let Err(cause) = self.store().shutdown().await {
112 error!("error shutting down store: {:?}", cause);
113 }
114 }
115}