1use std::time::Duration;
5
6use smallvec::SmallVec;
8use tonic::Status;
9
10use slim_datapath::api::{
11 EncodedName, ProtoMessage as Message, ProtoName, ProtoSessionMessageType,
12};
13
14use crate::SessionError;
16
17pub const SESSION_RANGE: std::ops::Range<u32> = 0..(u32::MAX - 1000);
19
20pub const SESSION_UNSPECIFIED: u32 = u32::MAX;
22
23pub(crate) type AppChannelSender =
25 tokio::sync::mpsc::UnboundedSender<Result<Message, SessionError>>;
26pub type AppChannelReceiver = tokio::sync::mpsc::UnboundedReceiver<Result<Message, SessionError>>;
28pub type SlimChannelSender = tokio::sync::mpsc::Sender<Result<Message, Status>>;
30
31#[derive(Clone, PartialEq, Debug)]
33#[allow(dead_code)]
34pub enum State {
35 Active,
36 Inactive,
37}
38
39#[derive(Clone, Copy, PartialEq, Debug)]
40pub enum MessageDirection {
41 North,
42 South,
43}
44
45#[derive(Debug)]
47pub enum OutboundMessage {
48 ToSlim(Message),
50 ToApp(Result<Message, SessionError>),
52}
53
54#[derive(Debug, Default)]
58pub struct SessionOutput {
59 pub messages: SmallVec<[OutboundMessage; 2]>,
60}
61
62impl SessionOutput {
63 pub fn new() -> Self {
64 Self {
65 messages: SmallVec::new(),
66 }
67 }
68
69 pub fn to_slim(message: Message) -> Self {
71 let mut s = Self::new();
72 s.messages.push(OutboundMessage::ToSlim(message));
73 s
74 }
75
76 pub fn to_app(message: Result<Message, SessionError>) -> Self {
78 let mut s = Self::new();
79 s.messages.push(OutboundMessage::ToApp(message));
80 s
81 }
82
83 pub fn push_slim(&mut self, message: Message) {
85 self.messages.push(OutboundMessage::ToSlim(message));
86 }
87
88 pub fn push_app(&mut self, message: Result<Message, SessionError>) {
90 self.messages.push(OutboundMessage::ToApp(message));
91 }
92
93 pub fn is_empty(&self) -> bool {
94 self.messages.is_empty()
95 }
96
97 pub fn extend(&mut self, other: SessionOutput) {
99 self.messages.extend(other.messages);
100 }
101}
102
103#[allow(clippy::large_enum_variant)]
105#[derive(Debug)]
106pub enum SessionMessage {
107 OnMessage {
109 message: Message,
110 direction: MessageDirection,
111 ack_tx: Option<tokio::sync::oneshot::Sender<Result<(), SessionError>>>,
113 },
114 MessageError { error: SessionError },
116 TimerTimeout {
119 message_id: u32,
120 message_type: ProtoSessionMessageType,
121 name: Option<EncodedName>,
122 timeouts: u32,
123 },
124 TimerFailure {
127 message_id: u32,
128 message_type: ProtoSessionMessageType,
129 name: Option<EncodedName>,
130 timeouts: u32,
131 },
132 ParticipantDisconnected { name: Option<ProtoName> },
134 StartDrain { grace_period: Duration },
137 DeleteSession { session_id: u32 },
140 GetParticipantsList {
142 tx: tokio::sync::oneshot::Sender<Vec<ProtoName>>,
143 },
144 LeaveCleanup,
148}