signaling 0.5.0

Sans-IO Signaling in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use crate::client::ClientId;
use crate::messages::Message;
use crate::room::{Room, RoomId};
use sansio::Protocol;
use std::collections::HashMap;
use std::convert::Infallible;
use std::time::{Duration, Instant};

/// A map of signaling rooms.
///
/// The Go implementation protects this map with a mutex. The signaling crate is
/// Sans-IO and is driven through exclusive `&mut self` access, so synchronization
/// belongs to the caller and no lock is needed here.
pub struct RoomTable {
    rooms: HashMap<RoomId, Room>,
    register_timeout: Duration,
}

impl RoomTable {
    pub fn new(register_timeout: Duration) -> Self {
        Self {
            rooms: HashMap::new(),
            register_timeout,
        }
    }

    /// Return the room specified by `id`, creating it when it does not exist.
    pub fn room(&mut self, id: &RoomId) -> &mut Room {
        self.rooms
            .entry(id.clone())
            .or_insert_with(|| Room::new(id.clone(), self.register_timeout))
    }

    /// Remove a client and remove its room as well when it becomes empty.
    pub fn remove(&mut self, room_id: &RoomId, client_id: &ClientId) {
        let remove_room = if let Some(room) = self.rooms.get_mut(room_id) {
            room.remove(client_id);
            room.empty()
        } else {
            false
        };

        if remove_room {
            self.rooms.remove(room_id);
        }
    }

    /// Forward a message to a room, creating that room when necessary.
    pub fn send(
        &mut self,
        now: Instant,
        room_id: &RoomId,
        source_id: &ClientId,
        msg: String,
    ) -> Result<(), String> {
        self.room(room_id).send(now, source_id, msg)
    }

    /// Store or relay an outbound client message.
    ///
    /// This is the in-process replacement for the old room-server-to-Collider
    /// HTTP bridge. `Room::send` queues when the peer is absent and relays when
    /// the peer is present.
    pub fn save_or_send(
        &mut self,
        now: Instant,
        room_id: &RoomId,
        source_id: &ClientId,
        msg: String,
    ) -> Result<(), String> {
        self.send(now, room_id, source_id, msg)
    }

    /// Allocate a client for a `/join` request and return its initiator flag plus
    /// messages queued by the other client.
    pub fn join(
        &mut self,
        now: Instant,
        room_id: &RoomId,
        client_id: &ClientId,
        is_loopback: bool,
    ) -> Result<(bool, Vec<String>), String> {
        self.room(room_id).add_client(now, client_id, is_loopback)
    }

    /// Remove a client for a browser `/leave` or an internal removal.
    pub fn leave(&mut self, room_id: &RoomId, client_id: &ClientId) {
        self.remove(room_id, client_id);
    }

    /// Return the number of clients in a room, or zero when the room is absent.
    pub fn occupancy(&self, room_id: &RoomId) -> usize {
        self.rooms.get(room_id).map_or(0, Room::occupancy)
    }

    /// Register a client's connection, creating the room or client if needed.
    pub fn register(
        &mut self,
        now: Instant,
        room_id: &RoomId,
        client_id: &ClientId,
    ) -> Result<(), String> {
        self.room(room_id).register(now, client_id)
    }

    /// Clear a client's connection registration and arm its reconnect timeout.
    /// The client remains in the room so it can reconnect while roaming networks.
    pub fn deregister(&mut self, now: Instant, room_id: &RoomId, client_id: &ClientId) {
        if let Some(room) = self.rooms.get_mut(room_id) {
            room.deregister(now, client_id);
        }
    }

    /// Remove a client only if it is still unregistered, then reap an empty room.
    ///
    /// In the Sans-IO implementation the client's deadline identifies the client
    /// incarnation: registration cancels that deadline, so a stale timeout cannot
    /// remove a client that has re-registered.
    pub fn remove_if_unregistered(&mut self, room_id: &RoomId, client_id: &ClientId) {
        let remove_room = self
            .rooms
            .get_mut(room_id)
            .is_some_and(|room| room.remove_if_unregistered(client_id) && room.empty());

        if remove_room {
            self.rooms.remove(room_id);
        }
    }

    /// Return the number of currently registered client connections.
    pub fn ws_count(&self) -> usize {
        self.rooms.values().map(Room::ws_count).sum()
    }

    pub fn room_count(&self) -> usize {
        self.rooms.len()
    }

    pub fn client_count(&self) -> usize {
        self.rooms.values().map(Room::occupancy).sum()
    }
}

impl Protocol<Message, Message, Infallible> for RoomTable {
    /// A client message surfaced by a room, tagged with its room and client IDs.
    type Rout = Message;
    /// A server message ready for a client, tagged with its room and client IDs.
    type Wout = Message;
    type Eout = Infallible;
    type Error = String;
    type Time = Instant;

    /// Deliver a message read from a client's connection to its room. Messages for
    /// rooms that have already been removed are dropped.
    fn handle_read(&mut self, msg: Message) -> Result<(), Self::Error> {
        if let Some(room) = self.rooms.get_mut(&msg.roomid) {
            room.handle_read(msg)?;
        }
        Ok(())
    }

    /// Drain the next client message from any room and attach its room ID.
    fn poll_read(&mut self) -> Option<Self::Rout> {
        for room in self.rooms.values_mut() {
            if let Some(msg) = room.poll_read() {
                return Some(msg);
            }
        }
        None
    }

    /// Deliver an outbound server message to its room and client.
    /// Messages for rooms that have already been removed are dropped.
    fn handle_write(&mut self, msg: Message) -> Result<(), Self::Error> {
        if let Some(room) = self.rooms.get_mut(&msg.roomid) {
            room.handle_write(msg)?;
        }
        Ok(())
    }

    /// Drain the next client-bound message from any room and attach its room ID.
    fn poll_write(&mut self) -> Option<Self::Wout> {
        for room in self.rooms.values_mut() {
            if let Some(msg) = room.poll_write() {
                return Some(msg);
            }
        }
        None
    }

    fn handle_event(&mut self, evt: Infallible) -> Result<(), Self::Error> {
        match evt {}
    }

    /// Drive every room's client registration deadlines and reap rooms whose last
    /// client expired.
    fn handle_timeout(&mut self, now: Self::Time) -> Result<(), Self::Error> {
        for room in self.rooms.values_mut() {
            room.handle_timeout(now)?;
        }
        self.rooms.retain(|_, room| !room.empty());
        Ok(())
    }

    /// Return the earliest client registration deadline across all rooms.
    fn poll_timeout(&mut self) -> Option<Self::Time> {
        self.rooms.values_mut().filter_map(Room::poll_timeout).min()
    }

    fn close(&mut self) -> Result<(), Self::Error> {
        for room in self.rooms.values_mut() {
            room.close()?;
        }
        self.rooms.clear();
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn ids() -> (RoomId, ClientId, ClientId) {
        ("room".to_string(), "1".to_string(), "2".to_string())
    }

    fn message(room_id: &str, client_id: &str, msg: &str) -> Message {
        Message {
            roomid: room_id.to_string(),
            clientid: client_id.to_string(),
            msg: msg.to_string(),
        }
    }

    fn assert_message(actual: Option<Message>, room_id: &str, client_id: &str, msg: &str) {
        let actual = actual.expect("expected a message");
        assert_eq!(actual.roomid, room_id);
        assert_eq!(actual.clientid, client_id);
        assert_eq!(actual.msg, msg);
    }

    #[test]
    fn room_gets_or_creates() {
        let mut table = RoomTable::new(Duration::from_secs(10));
        let room_id = "room".to_string();

        assert_eq!(table.room(&room_id).id(), &room_id);
        assert_eq!(table.room_count(), 1);
        assert_eq!(table.room(&room_id).id(), &room_id);
        assert_eq!(table.room_count(), 1);
    }

    #[test]
    fn remove_reaps_an_empty_room_and_promotes_a_survivor() {
        let now = Instant::now();
        let mut table = RoomTable::new(Duration::from_secs(10));
        let (room_id, client1, client2) = ids();

        assert!(table.join(now, &room_id, &client1, false).unwrap().0);
        assert!(!table.join(now, &room_id, &client2, false).unwrap().0);
        table.remove(&room_id, &client1);

        assert_eq!(table.occupancy(&room_id), 1);
        assert!(table.room(&room_id).client_is_initiator(&client2));

        table.remove(&room_id, &client2);
        assert_eq!(table.occupancy(&room_id), 0);
        assert_eq!(table.room_count(), 0);
    }

    #[test]
    fn send_and_save_or_send_create_rooms_and_queue_messages() {
        let now = Instant::now();
        let mut table = RoomTable::new(Duration::from_secs(10));
        let (room_id, client1, _) = ids();

        table
            .send(now, &room_id, &client1, "offer".to_string())
            .unwrap();
        table
            .save_or_send(now, &room_id, &client1, "candidate".to_string())
            .unwrap();

        let (_, messages) = table
            .join(now, &room_id, &"new-client".to_string(), false)
            .unwrap();
        assert_eq!(messages, ["offer", "candidate"]);
    }

    #[test]
    fn join_leave_and_occupancy_match_room_server_operations() {
        let now = Instant::now();
        let mut table = RoomTable::new(Duration::from_secs(10));
        let (room_id, client1, client2) = ids();

        let (initiator, messages) = table.join(now, &room_id, &client1, false).unwrap();
        assert!(initiator);
        assert!(messages.is_empty());
        assert_eq!(table.occupancy(&room_id), 1);

        assert!(!table.join(now, &room_id, &client2, false).unwrap().0);
        assert_eq!(table.occupancy(&room_id), 2);

        table.leave(&room_id, &client1);
        table.leave(&room_id, &client2);
        assert_eq!(table.occupancy(&room_id), 0);
    }

    #[test]
    fn register_deregister_and_remove_if_unregistered() {
        let now = Instant::now();
        let timeout = Duration::from_secs(10);
        let mut table = RoomTable::new(timeout);
        let (room_id, client1, _) = ids();

        table.register(now, &room_id, &client1).unwrap();
        assert_eq!(table.ws_count(), 1);

        table.deregister(now, &room_id, &client1);
        assert_eq!(table.ws_count(), 0);
        assert_eq!(table.occupancy(&room_id), 1);

        table.remove_if_unregistered(&room_id, &client1);
        assert_eq!(table.room_count(), 0);
    }

    #[test]
    fn remove_if_unregistered_preserves_registered_client() {
        let now = Instant::now();
        let mut table = RoomTable::new(Duration::from_secs(10));
        let (room_id, client1, _) = ids();

        table.register(now, &room_id, &client1).unwrap();
        table.remove_if_unregistered(&room_id, &client1);

        assert_eq!(table.occupancy(&room_id), 1);
        assert_eq!(table.ws_count(), 1);
    }

    #[test]
    fn protocol_read_plane_preserves_room_and_client_address() {
        let now = Instant::now();
        let mut table = RoomTable::new(Duration::from_secs(10));
        let (room_id, client1, _) = ids();
        table.join(now, &room_id, &client1, false).unwrap();

        table
            .handle_read(message(&room_id, &client1, "offer"))
            .unwrap();

        assert_message(table.poll_read(), &room_id, &client1, "offer");
        assert!(table.poll_read().is_none());
    }

    #[test]
    fn protocol_write_plane_preserves_room_and_client_address() {
        let now = Instant::now();
        let mut table = RoomTable::new(Duration::from_secs(10));
        let (room_id, client1, _) = ids();
        table.register(now, &room_id, &client1).unwrap();

        table
            .handle_write(message(&room_id, &client1, "answer"))
            .unwrap();

        assert_message(table.poll_write(), &room_id, &client1, "answer");
        assert!(table.poll_write().is_none());
    }

    #[test]
    fn protocol_drops_messages_for_unknown_rooms() {
        let mut table = RoomTable::new(Duration::from_secs(10));
        let (room_id, client1, _) = ids();

        table
            .handle_read(message(&room_id, &client1, "read"))
            .unwrap();
        table
            .handle_write(message(&room_id, &client1, "write"))
            .unwrap();

        assert_eq!(table.room_count(), 0);
        assert!(table.poll_read().is_none());
        assert!(table.poll_write().is_none());
    }

    #[test]
    fn protocol_timeout_reaps_expired_clients_and_empty_rooms() {
        let now = Instant::now();
        let timeout = Duration::from_secs(10);
        let mut table = RoomTable::new(timeout);
        let (room_id, client1, _) = ids();
        table.join(now, &room_id, &client1, false).unwrap();

        assert_eq!(table.poll_timeout(), Some(now + timeout));
        table.handle_timeout(now + timeout).unwrap();

        assert_eq!(table.room_count(), 0);
        assert!(table.poll_timeout().is_none());
    }

    #[test]
    fn protocol_timeout_uses_the_earliest_room_deadline() {
        let now = Instant::now();
        let timeout = Duration::from_secs(10);
        let mut table = RoomTable::new(timeout);
        let client = "client".to_string();
        let room1 = "room-1".to_string();
        let room2 = "room-2".to_string();

        table.join(now, &room1, &client, false).unwrap();
        table
            .join(now + Duration::from_secs(2), &room2, &client, false)
            .unwrap();

        assert_eq!(table.poll_timeout(), Some(now + timeout));
    }

    #[test]
    fn protocol_close_clears_all_rooms() {
        let now = Instant::now();
        let mut table = RoomTable::new(Duration::from_secs(10));
        let (room_id, client1, _) = ids();
        table.register(now, &room_id, &client1).unwrap();

        table.close().unwrap();

        assert_eq!(table.room_count(), 0);
        assert_eq!(table.ws_count(), 0);
    }
}