use std::time::Duration;
use smallvec::SmallVec;
use tonic::Status;
use slim_datapath::api::{
EncodedName, ProtoMessage as Message, ProtoName, ProtoSessionMessageType,
};
use crate::SessionError;
pub const SESSION_RANGE: std::ops::Range<u32> = 0..(u32::MAX - 1000);
pub const SESSION_UNSPECIFIED: u32 = u32::MAX;
pub(crate) type AppChannelSender =
tokio::sync::mpsc::UnboundedSender<Result<Message, SessionError>>;
pub type AppChannelReceiver = tokio::sync::mpsc::UnboundedReceiver<Result<Message, SessionError>>;
pub type SlimChannelSender = tokio::sync::mpsc::Sender<Result<Message, Status>>;
#[derive(Clone, PartialEq, Debug)]
#[allow(dead_code)]
pub enum State {
Active,
Inactive,
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum MessageDirection {
North,
South,
}
#[derive(Debug)]
pub enum OutboundMessage {
ToSlim(Message),
ToApp(Result<Message, SessionError>),
}
#[derive(Debug, Default)]
pub struct SessionOutput {
pub messages: SmallVec<[OutboundMessage; 2]>,
}
impl SessionOutput {
pub fn new() -> Self {
Self {
messages: SmallVec::new(),
}
}
pub fn to_slim(message: Message) -> Self {
let mut s = Self::new();
s.messages.push(OutboundMessage::ToSlim(message));
s
}
pub fn to_app(message: Result<Message, SessionError>) -> Self {
let mut s = Self::new();
s.messages.push(OutboundMessage::ToApp(message));
s
}
pub fn push_slim(&mut self, message: Message) {
self.messages.push(OutboundMessage::ToSlim(message));
}
pub fn push_app(&mut self, message: Result<Message, SessionError>) {
self.messages.push(OutboundMessage::ToApp(message));
}
pub fn is_empty(&self) -> bool {
self.messages.is_empty()
}
pub fn extend(&mut self, other: SessionOutput) {
self.messages.extend(other.messages);
}
}
#[allow(clippy::large_enum_variant)]
#[derive(Debug)]
pub enum SessionMessage {
OnMessage {
message: Message,
direction: MessageDirection,
ack_tx: Option<tokio::sync::oneshot::Sender<Result<(), SessionError>>>,
},
MessageError { error: SessionError },
TimerTimeout {
message_id: u32,
message_type: ProtoSessionMessageType,
name: Option<EncodedName>,
timeouts: u32,
},
TimerFailure {
message_id: u32,
message_type: ProtoSessionMessageType,
name: Option<EncodedName>,
timeouts: u32,
},
ParticipantDisconnected { name: Option<ProtoName> },
StartDrain { grace_period: Duration },
DeleteSession { session_id: u32 },
GetParticipantsList {
tx: tokio::sync::oneshot::Sender<Vec<ProtoName>>,
},
LeaveCleanup,
}