mod analyze;
mod cem;
pub mod core;
mod rm;
mod set;
use alloc::string::String;
pub use analyze::{Analyzer, Observed};
pub use cem::{CemConfig, CemEvent, CemSession};
pub use core::{
AckLedger, AckOutcome, Backoff, CloseReason, MessageHandle, Negotiation, OutOfOrderPolicy,
Outgoing, Registry, SendError, SessionCore, SessionState, Stats,
};
pub use rm::{Instructed, RmConfig, RmEvent, RmSession};
pub use set::SessionSet;
use crate::message::Message;
use crate::message::MessageKind;
use crate::types::common::ReceptionStatusValues;
use crate::types::{ProtocolVersion, Timestamp};
use crate::validate::Report;
pub trait Session {
type Event;
fn open(&mut self, now: Timestamp);
fn handle_text(&mut self, text: &str, now: Timestamp) -> Inbound;
fn handle_message(&mut self, message: Message, now: Timestamp) -> Inbound;
fn handle_timeout(&mut self, now: Timestamp);
fn transport_closed(&mut self, now: Timestamp);
fn poll_transmit(&mut self) -> Option<Outgoing>;
fn poll_event(&mut self) -> Option<Self::Event>;
fn poll_timeout(&self) -> Option<Timestamp>;
fn state(&self) -> &SessionState;
fn negotiated_version(&self) -> Option<&ProtocolVersion>;
fn registry(&self) -> &Registry;
fn stats(&self) -> Stats;
fn close(&mut self);
}
macro_rules! impl_session {
($ty:ty, $event:ty) => {
impl Session for $ty {
type Event = $event;
fn open(&mut self, now: Timestamp) {
<$ty>::open(self, now);
}
fn handle_text(&mut self, text: &str, now: Timestamp) -> Inbound {
<$ty>::handle_text(self, text, now)
}
fn handle_message(&mut self, message: Message, now: Timestamp) -> Inbound {
<$ty>::handle_message(self, message, now)
}
fn handle_timeout(&mut self, now: Timestamp) {
<$ty>::handle_timeout(self, now);
}
fn transport_closed(&mut self, now: Timestamp) {
<$ty>::transport_closed(self, now);
}
fn poll_transmit(&mut self) -> Option<Outgoing> {
<$ty>::poll_transmit(self)
}
fn poll_event(&mut self) -> Option<$event> {
<$ty>::poll_event(self)
}
fn poll_timeout(&self) -> Option<Timestamp> {
<$ty>::poll_timeout(self)
}
fn state(&self) -> &SessionState {
<$ty>::state(self)
}
fn negotiated_version(&self) -> Option<&ProtocolVersion> {
<$ty>::negotiated_version(self)
}
fn registry(&self) -> &Registry {
<$ty>::registry(self)
}
fn stats(&self) -> Stats {
<$ty>::stats(self)
}
fn close(&mut self) {
<$ty>::close(self);
}
}
};
}
impl_session!(RmSession, RmEvent);
impl_session!(CemSession, CemEvent);
#[derive(Debug, Clone, PartialEq)]
pub struct Inbound {
pub kind: Option<MessageKind>,
pub message_id: Option<crate::types::Id>,
pub status: ReceptionStatusValues,
pub report: Report,
}
impl Inbound {
pub(crate) fn ignored(kind: MessageKind, message_id: Option<crate::types::Id>) -> Self {
Self {
kind: Some(kind),
message_id,
status: ReceptionStatusValues::Ok,
report: Report::new(),
}
}
#[must_use]
pub fn accepted(&self) -> bool {
self.status == ReceptionStatusValues::Ok
}
}
pub trait InboundPolicy {
fn review(&mut self, message: &crate::Message) -> Option<(ReceptionStatusValues, String)>;
}
impl<F> InboundPolicy for F
where
F: FnMut(&crate::Message) -> Option<(ReceptionStatusValues, String)>,
{
fn review(&mut self, message: &crate::Message) -> Option<(ReceptionStatusValues, String)> {
self(message)
}
}