use std::time::Duration;
use super::Connection;
pub use super::error::{
ConnectionMatrixAddError, ConnectionMatrixRecvError, ConnectionMatrixRecvTimeoutError,
ConnectionMatrixRemoveError, ConnectionMatrixSendError,
};
#[derive(Debug, Default, PartialEq)]
pub struct ConnectionMatrixEnvelope {
id: String,
payload: Vec<u8>,
}
impl ConnectionMatrixEnvelope {
pub fn new(id: String, payload: Vec<u8>) -> Self {
ConnectionMatrixEnvelope { id, payload }
}
pub fn id(&self) -> &str {
&self.id
}
pub fn payload(&self) -> &[u8] {
&self.payload
}
#[deprecated(since = "0.3.19", note = "Please use into_inner() instead")]
pub fn take_payload(self) -> Vec<u8> {
self.payload
}
pub fn into_inner(self) -> Vec<u8> {
self.payload
}
}
impl From<ConnectionMatrixEnvelope> for Vec<u8> {
fn from(envelope: ConnectionMatrixEnvelope) -> Self {
envelope.payload.to_vec()
}
}
pub trait ConnectionMatrixLifeCycle: Clone + Send {
fn add(
&self,
connection: Box<dyn Connection>,
id: String,
) -> Result<usize, ConnectionMatrixAddError>;
fn remove(&self, id: &str) -> Result<Box<dyn Connection>, ConnectionMatrixRemoveError>;
}
pub trait ConnectionMatrixSender: Clone + Send {
fn send(&self, id: String, message: Vec<u8>) -> Result<(), ConnectionMatrixSendError>;
}
pub trait ConnectionMatrixReceiver: Clone + Send {
fn recv(&self) -> Result<ConnectionMatrixEnvelope, ConnectionMatrixRecvError>;
fn recv_timeout(
&self,
timeout: Duration,
) -> Result<ConnectionMatrixEnvelope, ConnectionMatrixRecvTimeoutError>;
}