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