Skip to main content

node_device_session/
traits.rs

1//! C-5 transport and host-bus traits.
2
3use crate::messages::OpenOk;
4use crate::types::{Refusal, Session};
5use async_trait::async_trait;
6use std::io;
7use tokio::io::{AsyncRead, AsyncWrite};
8
9/// Boxed write half of one bidirectional stream.
10pub type BoxSend = Box<dyn AsyncWrite + Send + Unpin>;
11/// Boxed read half of one bidirectional stream.
12pub type BoxRecv = Box<dyn AsyncRead + Send + Unpin>;
13
14/// One authenticated peer connection (handshake already proved [`Self::peer_key`]).
15#[async_trait]
16pub trait SessionStreams: Send + Sync {
17    /// Ed25519 public key proven by the transport handshake.
18    fn peer_key(&self) -> [u8; 32];
19
20    /// Accept the next bidirectional stream.
21    async fn accept_bi(&self) -> io::Result<(BoxSend, BoxRecv)>;
22
23    /// Close the connection with an application code and reason.
24    fn close(&self, code: u32, reason: &[u8]);
25
26    /// Whether the peer has already closed.
27    fn is_closed(&self) -> bool {
28        false
29    }
30}
31
32/// The app's bus to the host for device-session capabilities.
33#[async_trait]
34pub trait HostBus: Send + Sync {
35    /// `core.device_session.verify` — full open with a credential.
36    async fn verify(&self, credential: &str, endpoint_id: &str) -> Result<Session, Refusal>;
37
38    /// `core.device_session.renew` — one-shot credential renewal, then close.
39    async fn renew(&self, endpoint_id: &str) -> Result<Session, Refusal>;
40
41    /// `core.device_session.admit_renewal` — open a renewal-only session.
42    async fn admit_renewal(&self, endpoint_id: &str) -> Result<Session, Refusal>;
43
44    /// Generic capability invoke for `device.call/1` (stamped payload already applied).
45    async fn invoke(
46        &self,
47        capability: &str,
48        payload: serde_json::Value,
49    ) -> Result<serde_json::Value, Refusal>;
50}
51
52/// Carrier-owned WebRTC signaling for `webrtc.signal/1`.
53///
54/// Prefer the shared [`crate::webrtc::WebRtcAnswerer`] (parameterised by
55/// [`crate::webrtc::WebRtcCarrier`]) unless the carrier needs a bespoke path.
56/// When [`None`] is passed to [`crate::serve`], the method is refused with
57/// `unsupported_transport`.
58#[async_trait]
59pub trait WebRtcSignaling: Send + Sync {
60    /// Handle one `webrtc.signal/1` stream on an opened session.
61    ///
62    /// `send`/`recv` already peeked the method prefix; the body (SDP / ICE
63    /// frames) starts at the next byte. Own the halves until the signaling
64    /// session ends.
65    async fn handle(&self, identity: &OpenOk, peer: [u8; 32], send: BoxSend, recv: BoxRecv);
66}