ndn_app/
connection.rs

1use bytes::Bytes;
2
3use ndn_faces::local::InProcHandle;
4use ndn_ipc::ForwarderClient;
5use ndn_packet::Name;
6
7use crate::AppError;
8
9/// Unified NDN connection — either an embedded engine face or an external forwarder.
10///
11/// Both [`send`](Self::send) and [`recv`](Self::recv) take `&self`, so an
12/// `Arc<NdnConnection>` can be shared across tasks for concurrent send/recv.
13pub enum NdnConnection {
14    /// In-process connection via [`InProcHandle`] (embedded engine).
15    Embedded(InProcHandle),
16    /// External connection via [`ForwarderClient`] (Unix socket + optional SHM).
17    External(ForwarderClient),
18}
19
20impl NdnConnection {
21    /// Send a packet.
22    pub async fn send(&self, pkt: Bytes) -> Result<(), AppError> {
23        match self {
24            NdnConnection::Embedded(h) => h.send(pkt).await.map_err(|_| AppError::Closed),
25            NdnConnection::External(c) => c.send(pkt).await.map_err(AppError::Connection),
26        }
27    }
28
29    /// Receive a packet. Returns `None` if the channel is closed.
30    pub async fn recv(&self) -> Option<Bytes> {
31        match self {
32            NdnConnection::Embedded(h) => h.recv().await,
33            NdnConnection::External(c) => c.recv().await,
34        }
35    }
36
37    /// Register a prefix (only meaningful for External connections;
38    /// embedded faces use engine FIB directly).
39    pub async fn register_prefix(&self, prefix: &Name) -> Result<(), AppError> {
40        match self {
41            NdnConnection::Embedded(_) => Ok(()), // no-op for embedded
42            NdnConnection::External(c) => c
43                .register_prefix(prefix)
44                .await
45                .map_err(AppError::Connection),
46        }
47    }
48}