node_device_session/
registry.rs1use crate::webrtc::WebRtcHandle;
10use parking_lot::Mutex;
11use std::collections::HashMap;
12use std::sync::atomic::{AtomicU64, Ordering};
13use std::sync::Arc;
14
15pub const SUPERSEDED_CLOSE_CODE: u32 = 3;
17pub const REVOKED_CLOSE_CODE: u32 = 4;
19
20pub type SessionClose = Arc<dyn Fn(u32, &[u8]) + Send + Sync>;
22
23struct Entry {
24 session_id: u64,
25 credential: String,
26 endpoint_hex: String,
27 close: SessionClose,
28 webrtc: Option<WebRtcHandle>,
29 carrier_open: bool,
34}
35
36impl Entry {
37 fn webrtc_live(&self) -> bool {
38 self.webrtc
39 .as_ref()
40 .is_some_and(|handle| !handle.is_closed())
41 }
42}
43
44fn prune(entries: &mut HashMap<String, Entry>) {
46 entries.retain(|_, entry| entry.carrier_open || entry.webrtc_live());
47}
48
49impl Drop for Entry {
50 fn drop(&mut self) {
51 if let Some(handle) = &self.webrtc {
52 handle.close();
53 }
54 }
55}
56
57struct Inner {
58 next_id: AtomicU64,
59 entries: Mutex<HashMap<String, Entry>>,
60}
61
62#[derive(Clone)]
66pub struct SessionRegistry {
67 inner: Arc<Inner>,
68}
69
70impl Default for SessionRegistry {
71 fn default() -> Self {
72 Self::new()
73 }
74}
75
76impl SessionRegistry {
77 pub fn new() -> Self {
79 Self {
80 inner: Arc::new(Inner {
81 next_id: AtomicU64::new(1),
82 entries: Mutex::new(HashMap::new()),
83 }),
84 }
85 }
86
87 pub fn len(&self) -> usize {
89 self.inner
90 .entries
91 .lock()
92 .values()
93 .filter(|entry| entry.carrier_open)
94 .count()
95 }
96
97 pub fn is_empty(&self) -> bool {
99 self.len() == 0
100 }
101
102 pub fn contains(&self, device_id: &str) -> bool {
104 self.inner
105 .entries
106 .lock()
107 .get(device_id)
108 .is_some_and(|entry| entry.carrier_open)
109 }
110
111 pub fn insert(
118 &self,
119 device_id: String,
120 credential: String,
121 endpoint_hex: String,
122 close: SessionClose,
123 ) -> u64 {
124 let session_id = self.inner.next_id.fetch_add(1, Ordering::Relaxed);
125 let previous = {
126 let mut entries = self.inner.entries.lock();
127 prune(&mut entries);
128 let mut previous = entries.remove(&device_id);
129 let webrtc = previous
130 .as_mut()
131 .and_then(|old| old.webrtc.take())
132 .filter(|handle| !handle.is_closed());
133 entries.insert(
134 device_id,
135 Entry {
136 session_id,
137 credential,
138 endpoint_hex,
139 close,
140 webrtc,
141 carrier_open: true,
142 },
143 );
144 previous
145 };
146 if let Some(old) = previous {
147 if old.carrier_open {
148 (old.close)(SUPERSEDED_CLOSE_CODE, b"superseded");
149 }
150 }
151 session_id
152 }
153
154 pub fn remove_if(&self, device_id: &str, session_id: u64) -> bool {
160 let mut entries = self.inner.entries.lock();
161 match entries.get_mut(device_id) {
162 Some(entry) if entry.session_id == session_id => {
163 if entry.webrtc_live() {
164 entry.carrier_open = false;
165 } else {
166 entries.remove(device_id);
167 }
168 true
169 }
170 _ => false,
171 }
172 }
173
174 pub fn close(&self, device_id: &str, session_id: u64, reason: &'static [u8]) -> bool {
176 let removed = {
177 let mut entries = self.inner.entries.lock();
178 match entries.get(device_id) {
179 Some(entry) if entry.session_id == session_id => entries.remove(device_id),
180 _ => None,
181 }
182 };
183 match removed {
184 Some(entry) => {
185 (entry.close)(REVOKED_CLOSE_CODE, reason);
186 true
187 }
188 None => false,
189 }
190 }
191
192 pub fn bind_webrtc(&self, device_id: &str, handle: WebRtcHandle) -> bool {
197 let mut entries = self.inner.entries.lock();
198 let Some(entry) = entries.get_mut(device_id) else {
199 handle.close();
200 return false;
201 };
202 if let Some(old) = entry.webrtc.replace(handle) {
203 old.close();
204 }
205 true
206 }
207
208 pub(crate) fn credentials(&self) -> Vec<(String, u64, String, String)> {
210 let mut entries = self.inner.entries.lock();
211 prune(&mut entries);
212 entries
213 .iter()
214 .map(|(device_id, e)| {
215 (
216 device_id.clone(),
217 e.session_id,
218 e.credential.clone(),
219 e.endpoint_hex.clone(),
220 )
221 })
222 .collect()
223 }
224}