1use std::{
4 fmt::{Debug, Display},
5 sync::{Arc, Mutex},
6};
7
8use crate::ds::DeliveryServiceError;
9
10#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct OutboundPacket {
13 pub payload: Vec<u8>,
14 pub subtopic: String,
15 pub conversation_id: String,
16 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 pub fn delivery_address(&self) -> &str {
32 &self.conversation_id
33 }
34}
35
36#[derive(Debug, Clone, PartialEq, Eq)]
38pub struct InboundPacket {
39 pub payload: Vec<u8>,
40 pub subtopic: String,
41 pub conversation_id: String,
42 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 fn publish(&mut self, packet: OutboundPacket) -> Result<(), Self::Error>;
70
71 fn subscribe(&mut self, delivery_address: &str) -> Result<(), Self::Error>;
73}
74
75pub type SharedDeliveryService =
79 Arc<Mutex<dyn DeliveryService<Error = DeliveryServiceError> + Send>>;