Skip to main content

de_mls/ds/
transport.rs

1//! Transport-agnostic envelopes and the [`DeliveryService`] trait.
2
3use std::{
4    fmt::{Debug, Display},
5    sync::{Arc, Mutex},
6};
7
8use crate::ds::DeliveryServiceError;
9
10/// A transport-agnostic packet that should be sent to the network.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct OutboundPacket {
13    pub payload: Vec<u8>,
14    pub subtopic: String,
15    pub conversation_id: String,
16    /// Application instance identifier. Used for self-message filtering.
17    pub app_id: Vec<u8>,
18}
19
20impl OutboundPacket {
21    pub fn new(payload: Vec<u8>, subtopic: &str, conversation_id: &str, app_id: &[u8]) -> Self {
22        Self {
23            payload,
24            subtopic: subtopic.to_string(),
25            conversation_id: conversation_id.to_string(),
26            app_id: app_id.to_vec(),
27        }
28    }
29
30    /// Address this packet is delivered to.
31    pub fn delivery_address(&self) -> &str {
32        &self.conversation_id
33    }
34}
35
36/// A transport-agnostic packet delivered from the network into the application.
37#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct InboundPacket {
39    pub payload: Vec<u8>,
40    pub subtopic: String,
41    pub conversation_id: String,
42    /// Sender's application instance id.
43    pub app_id: Vec<u8>,
44    pub timestamp: i64,
45}
46
47impl InboundPacket {
48    pub fn new(
49        payload: Vec<u8>,
50        subtopic: &str,
51        conversation_id: &str,
52        app_id: Vec<u8>,
53        timestamp: i64,
54    ) -> Self {
55        Self {
56            payload,
57            subtopic: subtopic.to_string(),
58            conversation_id: conversation_id.to_string(),
59            app_id,
60            timestamp,
61        }
62    }
63}
64
65pub trait DeliveryService: Debug + 'static {
66    type Error: Display + Debug + 'static;
67
68    /// Publish a packet to the network.
69    fn publish(&mut self, packet: OutboundPacket) -> Result<(), Self::Error>;
70
71    /// Register interest in a delivery address.
72    fn subscribe(&mut self, delivery_address: &str) -> Result<(), Self::Error>;
73}
74
75/// Trait-object shape used internally — pinned `Error` so dyn-dispatch
76/// is object-safe and `UserError::Transport(#[from] DeliveryServiceError)`
77/// resolves.
78pub type SharedDeliveryService =
79    Arc<Mutex<dyn DeliveryService<Error = DeliveryServiceError> + Send>>;