use std::{
fmt::{Debug, Display},
sync::{Arc, Mutex},
};
use crate::ds::DeliveryServiceError;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OutboundPacket {
pub payload: Vec<u8>,
pub subtopic: String,
pub conversation_id: String,
pub app_id: Vec<u8>,
}
impl OutboundPacket {
pub fn new(payload: Vec<u8>, subtopic: &str, conversation_id: &str, app_id: &[u8]) -> Self {
Self {
payload,
subtopic: subtopic.to_string(),
conversation_id: conversation_id.to_string(),
app_id: app_id.to_vec(),
}
}
pub fn delivery_address(&self) -> &str {
&self.conversation_id
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InboundPacket {
pub payload: Vec<u8>,
pub subtopic: String,
pub conversation_id: String,
pub app_id: Vec<u8>,
pub timestamp: i64,
}
impl InboundPacket {
pub fn new(
payload: Vec<u8>,
subtopic: &str,
conversation_id: &str,
app_id: Vec<u8>,
timestamp: i64,
) -> Self {
Self {
payload,
subtopic: subtopic.to_string(),
conversation_id: conversation_id.to_string(),
app_id,
timestamp,
}
}
}
pub trait DeliveryService: Debug + 'static {
type Error: Display + Debug + 'static;
fn publish(&mut self, packet: OutboundPacket) -> Result<(), Self::Error>;
fn subscribe(&mut self, delivery_address: &str) -> Result<(), Self::Error>;
}
pub type SharedDeliveryService =
Arc<Mutex<dyn DeliveryService<Error = DeliveryServiceError> + Send>>;