clasp-router 3.3.0

CLASP message router and server
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
//! Gesture Move Coalescing
//!
//! Per the CLASP protocol, routers MAY coalesce `move` phases to reduce bandwidth.
//! This module implements a gesture registry that:
//! - Buffers Move phases, only forwarding the most recent
//! - Immediately forwards Start, End, and Cancel phases
//! - Flushes buffered Moves when a non-Move phase arrives or after a timeout
//!
//! # Example
//!
//! ```ignore
//! let mut registry = GestureRegistry::new(Duration::from_millis(16));
//!
//! // Move phases get buffered
//! registry.process(gesture_move); // -> None (buffered)
//! registry.process(gesture_move); // -> None (replaces previous)
//!
//! // End phase flushes the buffer
//! registry.process(gesture_end);  // -> Some([buffered_move, end])
//! ```

use clasp_core::{GesturePhase, PublishMessage, SignalType};
use dashmap::DashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::mpsc;
use tracing::debug;

/// Key for tracking active gestures
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct GestureKey {
    /// The address this gesture is published to
    pub address: String,
    /// The gesture ID (for multi-touch)
    pub gesture_id: u32,
}

impl GestureKey {
    pub fn new(address: &str, gesture_id: u32) -> Self {
        Self {
            address: address.to_string(),
            gesture_id,
        }
    }
}

/// Buffered gesture state
#[derive(Debug, Clone)]
struct BufferedGesture {
    /// The most recent Move message (if any)
    pending_move: Option<PublishMessage>,
    /// When this gesture started
    started_at: Instant,
    /// When the last Move was buffered
    last_move_at: Option<Instant>,
}

impl BufferedGesture {
    fn new() -> Self {
        Self {
            pending_move: None,
            started_at: Instant::now(),
            last_move_at: None,
        }
    }
}

/// Result of processing a gesture message
#[derive(Debug)]
pub enum GestureResult {
    /// Forward these messages immediately
    Forward(Vec<PublishMessage>),
    /// Message was buffered, nothing to forward yet
    Buffered,
    /// Not a gesture message, pass through
    PassThrough,
}

/// Gesture registry for move coalescing
pub struct GestureRegistry {
    /// Active gestures indexed by (address, gesture_id)
    gestures: DashMap<GestureKey, BufferedGesture>,
    /// How long to buffer moves before flushing
    flush_interval: Duration,
}

impl GestureRegistry {
    /// Create a new gesture registry with the specified flush interval
    pub fn new(flush_interval: Duration) -> Self {
        Self {
            gestures: DashMap::new(),
            flush_interval,
        }
    }

    /// Create with default 16ms flush interval (60fps)
    pub fn default() -> Self {
        Self::new(Duration::from_millis(16))
    }

    /// Process a publish message, returning what should be forwarded
    pub fn process(&self, msg: &PublishMessage) -> GestureResult {
        // Only handle gesture messages
        if msg.signal != Some(SignalType::Gesture) {
            return GestureResult::PassThrough;
        }

        let phase = match msg.phase {
            Some(p) => p,
            None => return GestureResult::PassThrough,
        };

        let gesture_id = msg.id.unwrap_or(0);
        let key = GestureKey::new(&msg.address, gesture_id);

        match phase {
            GesturePhase::Start => {
                // Register new gesture and forward immediately
                self.gestures.insert(key, BufferedGesture::new());
                debug!("Gesture started: {}:{}", msg.address, gesture_id);
                GestureResult::Forward(vec![msg.clone()])
            }

            GesturePhase::Move => {
                // Buffer the move, replacing any previous
                if let Some(mut entry) = self.gestures.get_mut(&key) {
                    entry.pending_move = Some(msg.clone());
                    entry.last_move_at = Some(Instant::now());
                    GestureResult::Buffered
                } else {
                    // No active gesture - forward anyway (late join scenario)
                    GestureResult::Forward(vec![msg.clone()])
                }
            }

            GesturePhase::End | GesturePhase::Cancel => {
                // Flush any buffered move, then forward the end/cancel
                let mut to_forward = Vec::with_capacity(2);

                if let Some((_, buffered)) = self.gestures.remove(&key) {
                    if let Some(pending) = buffered.pending_move {
                        to_forward.push(pending);
                    }
                }

                to_forward.push(msg.clone());
                debug!("Gesture {:?}: {}:{}", phase, msg.address, gesture_id);
                GestureResult::Forward(to_forward)
            }
        }
    }

    /// Flush all gestures that have pending moves older than the flush interval
    /// Returns messages that should be forwarded
    pub fn flush_stale(&self) -> Vec<PublishMessage> {
        let now = Instant::now();
        let mut to_forward = Vec::new();

        for mut entry in self.gestures.iter_mut() {
            if let Some(last_move) = entry.last_move_at {
                if now.duration_since(last_move) >= self.flush_interval {
                    if let Some(pending) = entry.pending_move.take() {
                        to_forward.push(pending);
                    }
                    entry.last_move_at = None;
                }
            }
        }

        to_forward
    }

    /// Get count of active gestures
    pub fn active_count(&self) -> usize {
        self.gestures.len()
    }

    /// Remove stale gestures (no activity for extended period)
    /// This prevents memory leaks from abandoned gestures
    pub fn cleanup_stale(&self, max_age: Duration) {
        let now = Instant::now();
        self.gestures
            .retain(|_, v| now.duration_since(v.started_at) < max_age);
    }
}

/// Background task that periodically flushes stale gestures
pub fn spawn_flush_task(
    registry: Arc<GestureRegistry>,
    flush_tx: mpsc::UnboundedSender<Vec<PublishMessage>>,
    interval: Duration,
) -> tokio::task::JoinHandle<()> {
    tokio::spawn(async move {
        let mut ticker = tokio::time::interval(interval);

        loop {
            ticker.tick().await;

            let to_flush = registry.flush_stale();
            if !to_flush.is_empty() {
                if flush_tx.send(to_flush).is_err() {
                    break; // Channel closed
                }
            }

            // Cleanup very old gestures (> 5 minutes with no end)
            registry.cleanup_stale(Duration::from_secs(300));
        }
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use clasp_core::Value;
    use std::collections::HashMap;

    fn make_gesture(address: &str, id: u32, phase: GesturePhase) -> PublishMessage {
        PublishMessage {
            address: address.to_string(),
            signal: Some(SignalType::Gesture),
            phase: Some(phase),
            id: Some(id),
            value: None,
            payload: Some(Value::Map(Default::default())),
            samples: None,
            rate: None,
            timestamp: None,
            timeline: None,
        }
    }

    fn make_gesture_with_payload(
        address: &str,
        id: u32,
        phase: GesturePhase,
        payload: Value,
    ) -> PublishMessage {
        PublishMessage {
            address: address.to_string(),
            signal: Some(SignalType::Gesture),
            phase: Some(phase),
            id: Some(id),
            value: None,
            payload: Some(payload),
            samples: None,
            rate: None,
            timestamp: None,
            timeline: None,
        }
    }

    #[test]
    fn test_start_forwards_immediately() {
        let registry = GestureRegistry::default();
        let msg = make_gesture("/touch", 1, GesturePhase::Start);

        match registry.process(&msg) {
            GestureResult::Forward(msgs) => {
                assert_eq!(msgs.len(), 1);
                assert_eq!(msgs[0].phase, Some(GesturePhase::Start));
                assert_eq!(msgs[0].id, Some(1));
            }
            _ => panic!("Expected Forward"),
        }
    }

    #[test]
    fn test_move_gets_buffered() {
        let registry = GestureRegistry::default();

        // Start gesture
        let start = make_gesture("/touch", 1, GesturePhase::Start);
        registry.process(&start);

        // Move should be buffered
        let move1 = make_gesture("/touch", 1, GesturePhase::Move);
        match registry.process(&move1) {
            GestureResult::Buffered => {}
            _ => panic!("Expected Buffered"),
        }

        // Registry should have active gesture
        assert_eq!(registry.active_count(), 1);
    }

    #[test]
    fn test_move_replaces_previous_move() {
        let registry = GestureRegistry::default();

        // Start gesture
        registry.process(&make_gesture("/touch", 1, GesturePhase::Start));

        // First move
        let move1 = make_gesture_with_payload("/touch", 1, GesturePhase::Move, Value::Int(1));
        registry.process(&move1);

        // Second move (should replace first)
        let move2 = make_gesture_with_payload("/touch", 1, GesturePhase::Move, Value::Int(2));
        registry.process(&move2);

        // End should flush only the second move
        let end = make_gesture("/touch", 1, GesturePhase::End);
        match registry.process(&end) {
            GestureResult::Forward(msgs) => {
                assert_eq!(msgs.len(), 2);
                // First message should be the last move (value 2)
                if let Some(Value::Int(v)) = msgs[0].payload.as_ref().and_then(|p| match p {
                    Value::Int(i) => Some(Value::Int(*i)),
                    _ => None,
                }) {
                    assert_eq!(v, 2);
                } else {
                    panic!("Expected last move to have value 2");
                }
                assert_eq!(msgs[1].phase, Some(GesturePhase::End));
            }
            _ => panic!("Expected Forward with 2 messages"),
        }
    }

    #[test]
    fn test_end_flushes_buffered_move() {
        let registry = GestureRegistry::default();

        // Start gesture
        let start = make_gesture("/touch", 1, GesturePhase::Start);
        registry.process(&start);

        // Buffer some moves
        registry.process(&make_gesture("/touch", 1, GesturePhase::Move));
        registry.process(&make_gesture("/touch", 1, GesturePhase::Move));

        // End should flush the last move + end
        let end = make_gesture("/touch", 1, GesturePhase::End);
        match registry.process(&end) {
            GestureResult::Forward(msgs) => {
                assert_eq!(msgs.len(), 2);
                assert_eq!(msgs[0].phase, Some(GesturePhase::Move));
                assert_eq!(msgs[1].phase, Some(GesturePhase::End));
            }
            _ => panic!("Expected Forward with 2 messages"),
        }

        // Gesture should be removed
        assert_eq!(registry.active_count(), 0);
    }

    #[test]
    fn test_end_without_move() {
        let registry = GestureRegistry::default();

        // Start gesture
        registry.process(&make_gesture("/touch", 1, GesturePhase::Start));

        // End without any moves
        let end = make_gesture("/touch", 1, GesturePhase::End);
        match registry.process(&end) {
            GestureResult::Forward(msgs) => {
                assert_eq!(msgs.len(), 1);
                assert_eq!(msgs[0].phase, Some(GesturePhase::End));
            }
            _ => panic!("Expected Forward with 1 message"),
        }
    }

    #[test]
    fn test_cancel_flushes_buffered_move() {
        let registry = GestureRegistry::default();

        let start = make_gesture("/touch", 1, GesturePhase::Start);
        registry.process(&start);

        registry.process(&make_gesture("/touch", 1, GesturePhase::Move));

        let cancel = make_gesture("/touch", 1, GesturePhase::Cancel);
        match registry.process(&cancel) {
            GestureResult::Forward(msgs) => {
                assert_eq!(msgs.len(), 2);
                assert_eq!(msgs[0].phase, Some(GesturePhase::Move));
                assert_eq!(msgs[1].phase, Some(GesturePhase::Cancel));
            }
            _ => panic!("Expected Forward with 2 messages"),
        }
    }

    #[test]
    fn test_multiple_gestures_independent() {
        let registry = GestureRegistry::default();

        // Start two gestures
        registry.process(&make_gesture("/touch", 1, GesturePhase::Start));
        registry.process(&make_gesture("/touch", 2, GesturePhase::Start));

        // Buffer moves for both
        registry.process(&make_gesture("/touch", 1, GesturePhase::Move));
        registry.process(&make_gesture("/touch", 2, GesturePhase::Move));

        // End gesture 1 - should only flush gesture 1's move
        match registry.process(&make_gesture("/touch", 1, GesturePhase::End)) {
            GestureResult::Forward(msgs) => {
                assert_eq!(msgs.len(), 2);
                assert_eq!(msgs[0].id, Some(1));
                assert_eq!(msgs[1].id, Some(1));
            }
            _ => panic!("Expected Forward"),
        }

        // Gesture 2 should still be active
        assert_eq!(registry.active_count(), 1);
    }

    #[test]
    fn test_different_addresses_independent() {
        let registry = GestureRegistry::default();

        // Start gestures on different addresses
        registry.process(&make_gesture("/touch1", 1, GesturePhase::Start));
        registry.process(&make_gesture("/touch2", 1, GesturePhase::Start));

        // Buffer moves
        registry.process(&make_gesture("/touch1", 1, GesturePhase::Move));
        registry.process(&make_gesture("/touch2", 1, GesturePhase::Move));

        // End one
        match registry.process(&make_gesture("/touch1", 1, GesturePhase::End)) {
            GestureResult::Forward(msgs) => {
                assert_eq!(msgs.len(), 2);
                assert_eq!(msgs[0].address, "/touch1");
                assert_eq!(msgs[1].address, "/touch1");
            }
            _ => panic!("Expected Forward"),
        }

        // Other should still be active
        assert_eq!(registry.active_count(), 1);
    }

    #[test]
    fn test_move_without_start() {
        let registry = GestureRegistry::default();

        // Move without start (late join scenario)
        let move_msg = make_gesture("/touch", 1, GesturePhase::Move);
        match registry.process(&move_msg) {
            GestureResult::Forward(msgs) => {
                // Should forward anyway
                assert_eq!(msgs.len(), 1);
                assert_eq!(msgs[0].phase, Some(GesturePhase::Move));
            }
            _ => panic!("Expected Forward for late join"),
        }
    }

    #[test]
    fn test_rapid_start_end() {
        let registry = GestureRegistry::default();

        // Rapid start/end without moves
        registry.process(&make_gesture("/touch", 1, GesturePhase::Start));
        let end = make_gesture("/touch", 1, GesturePhase::End);

        match registry.process(&end) {
            GestureResult::Forward(msgs) => {
                assert_eq!(msgs.len(), 1);
                assert_eq!(msgs[0].phase, Some(GesturePhase::End));
            }
            _ => panic!("Expected Forward"),
        }
    }

    #[test]
    fn test_concurrent_gestures_same_address() {
        let registry = GestureRegistry::default();

        // Multiple concurrent gestures on same address (multitouch)
        for id in 1..=5 {
            registry.process(&make_gesture("/multitouch", id, GesturePhase::Start));
            registry.process(&make_gesture("/multitouch", id, GesturePhase::Move));
        }

        assert_eq!(registry.active_count(), 5);

        // End all
        for id in 1..=5 {
            let end = make_gesture("/multitouch", id, GesturePhase::End);
            match registry.process(&end) {
                GestureResult::Forward(msgs) => {
                    assert_eq!(msgs.len(), 2); // Move + End
                    assert_eq!(msgs[0].id, Some(id));
                    assert_eq!(msgs[1].id, Some(id));
                }
                _ => panic!("Expected Forward"),
            }
        }

        assert_eq!(registry.active_count(), 0);
    }

    #[test]
    fn test_flush_stale() {
        let registry = GestureRegistry::new(Duration::from_millis(1));

        // Start and buffer a move
        registry.process(&make_gesture("/touch", 1, GesturePhase::Start));
        registry.process(&make_gesture("/touch", 1, GesturePhase::Move));

        // Wait for flush interval
        std::thread::sleep(Duration::from_millis(5));

        let flushed = registry.flush_stale();
        assert_eq!(flushed.len(), 1);
        assert_eq!(flushed[0].phase, Some(GesturePhase::Move));

        // Second flush should be empty (no pending moves)
        let flushed2 = registry.flush_stale();
        assert!(flushed2.is_empty());
    }

    #[test]
    fn test_flush_stale_multiple_gestures() {
        let registry = GestureRegistry::new(Duration::from_millis(1));

        // Start multiple gestures
        for id in 1..=3 {
            registry.process(&make_gesture("/touch", id, GesturePhase::Start));
            registry.process(&make_gesture("/touch", id, GesturePhase::Move));
        }

        std::thread::sleep(Duration::from_millis(5));

        let flushed = registry.flush_stale();
        assert_eq!(flushed.len(), 3);

        // All should still be active
        assert_eq!(registry.active_count(), 3);
    }

    #[test]
    fn test_cleanup_stale() {
        let registry = GestureRegistry::default();

        // Start a gesture
        registry.process(&make_gesture("/touch", 1, GesturePhase::Start));
        assert_eq!(registry.active_count(), 1);

        // Cleanup shouldn't remove recent gesture
        registry.cleanup_stale(Duration::from_secs(300));
        assert_eq!(registry.active_count(), 1);

        // But should remove very old ones (simulated by setting started_at in past)
        // This is harder to test without exposing internals, so we test the behavior
        // by ensuring cleanup doesn't break active gestures
    }

    #[test]
    fn test_non_gesture_passes_through() {
        let registry = GestureRegistry::default();

        let msg = PublishMessage {
            address: "/test".to_string(),
            signal: Some(SignalType::Event),
            phase: None,
            id: None,
            value: Some(Value::Bool(true)),
            payload: None,
            samples: None,
            rate: None,
            timestamp: None,
            timeline: None,
        };

        match registry.process(&msg) {
            GestureResult::PassThrough => {}
            _ => panic!("Expected PassThrough"),
        }
    }

    #[test]
    fn test_gesture_without_phase() {
        let registry = GestureRegistry::default();

        let msg = PublishMessage {
            address: "/test".to_string(),
            signal: Some(SignalType::Gesture),
            phase: None, // Missing phase
            id: Some(1),
            value: None,
            payload: None,
            samples: None,
            rate: None,
            timestamp: None,
            timeline: None,
        };

        match registry.process(&msg) {
            GestureResult::PassThrough => {}
            _ => panic!("Expected PassThrough for gesture without phase"),
        }
    }

    #[test]
    fn test_gesture_without_id() {
        let registry = GestureRegistry::default();

        // Gesture without ID should use 0
        let msg = PublishMessage {
            address: "/test".to_string(),
            signal: Some(SignalType::Gesture),
            phase: Some(GesturePhase::Start),
            id: None, // No ID
            value: None,
            payload: None,
            samples: None,
            rate: None,
            timestamp: None,
            timeline: None,
        };

        match registry.process(&msg) {
            GestureResult::Forward(msgs) => {
                assert_eq!(msgs.len(), 1);
                assert_eq!(msgs[0].id, None); // ID is preserved as None
            }
            _ => panic!("Expected Forward"),
        }
    }

    #[test]
    fn test_stress_many_gestures() {
        let registry = GestureRegistry::default();

        // Create 100 concurrent gestures
        for id in 0..100 {
            registry.process(&make_gesture("/stress", id, GesturePhase::Start));
            registry.process(&make_gesture("/stress", id, GesturePhase::Move));
        }

        assert_eq!(registry.active_count(), 100);

        // End all
        for id in 0..100 {
            registry.process(&make_gesture("/stress", id, GesturePhase::End));
        }

        assert_eq!(registry.active_count(), 0);
    }

    #[test]
    fn test_rapid_move_updates() {
        let registry = GestureRegistry::default();

        registry.process(&make_gesture("/rapid", 1, GesturePhase::Start));

        // Send 1000 rapid moves
        for i in 0..1000 {
            let payload = Value::Map({
                let mut m = HashMap::new();
                m.insert("index".to_string(), Value::Int(i));
                m
            });
            registry.process(&make_gesture_with_payload(
                "/rapid",
                1,
                GesturePhase::Move,
                payload,
            ));
        }

        // Only last move should be buffered
        let end = make_gesture("/rapid", 1, GesturePhase::End);
        match registry.process(&end) {
            GestureResult::Forward(msgs) => {
                assert_eq!(msgs.len(), 2);
                // Last move should have index 999
                if let Some(Value::Map(map)) = msgs[0].payload.as_ref() {
                    if let Some(Value::Int(idx)) = map.get("index") {
                        assert_eq!(*idx, 999);
                    } else {
                        panic!("Expected index in payload");
                    }
                } else {
                    panic!("Expected Map payload");
                }
            }
            _ => panic!("Expected Forward"),
        }
    }
}