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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
//! Main router implementation
//!
//! The router is transport-agnostic - it can accept connections from any transport
//! that implements the `TransportServer` trait (WebSocket, QUIC, TCP, etc.).
//!
//! # Transport Support
//!
//! - **WebSocket** (default): Works everywhere, including browsers and DO App Platform
//! - **QUIC**: High-performance for native apps. Requires UDP - NOT supported on DO App Platform
//! - **TCP**: Simple fallback, works everywhere
//!
//! # Example
//!
//! ```no_run
//! use clasp_router::{Router, RouterConfig};
//!
//! #[tokio::main]
//! async fn main() {
//! let router = Router::new(RouterConfig::default());
//!
//! // WebSocket (most common)
//! router.serve_websocket("0.0.0.0:7330").await.unwrap();
//!
//! // Or use any TransportServer implementation
//! // router.serve_on(my_custom_server).await.unwrap();
//! }
//! ```
use bytes::Bytes;
use clasp_core::{
codec, Action, AckMessage, CpskValidator, ErrorMessage, Frame, Message, SecurityMode,
SignalType, TokenValidator, ValidationResult, PROTOCOL_VERSION,
};
use clasp_transport::{TransportEvent, TransportReceiver, TransportSender, TransportServer};
use dashmap::DashMap;
use parking_lot::RwLock;
use std::net::SocketAddr;
use std::sync::Arc;
use tracing::{debug, error, info, warn};
#[cfg(feature = "websocket")]
use clasp_transport::WebSocketServer;
#[cfg(feature = "quic")]
use clasp_transport::{QuicConfig, QuicTransport};
use crate::{
error::{Result, RouterError},
session::{Session, SessionId},
state::RouterState,
subscription::{Subscription, SubscriptionManager},
};
/// Transport configuration for multi-transport serving.
///
/// Use with `Router::serve_multi()` to run multiple transports simultaneously.
#[derive(Debug, Clone)]
pub enum TransportConfig {
/// WebSocket transport (default, works everywhere)
#[cfg(feature = "websocket")]
WebSocket {
/// Listen address, e.g., "0.0.0.0:7330"
addr: String,
},
/// QUIC transport (high-performance, requires UDP)
///
/// **WARNING**: Not supported on DigitalOcean App Platform or most PaaS.
/// Use a VPS/Droplet for QUIC support.
#[cfg(feature = "quic")]
Quic {
/// Listen address
addr: SocketAddr,
/// TLS certificate (DER format)
cert: Vec<u8>,
/// TLS private key (DER format)
key: Vec<u8>,
},
}
/// Router configuration
#[derive(Debug, Clone)]
pub struct RouterConfig {
/// Server name
pub name: String,
/// Supported features
pub features: Vec<String>,
/// Maximum sessions
pub max_sessions: usize,
/// Session timeout (seconds)
pub session_timeout: u64,
/// Security mode (Open or Authenticated)
pub security_mode: SecurityMode,
}
impl Default for RouterConfig {
fn default() -> Self {
Self {
name: "Clasp Router".to_string(),
features: vec![
"param".to_string(),
"event".to_string(),
"stream".to_string(),
"timeline".to_string(),
],
max_sessions: 100,
session_timeout: 300,
security_mode: SecurityMode::Open,
}
}
}
/// Clasp router
pub struct Router {
config: RouterConfig,
/// Active sessions
sessions: Arc<DashMap<SessionId, Arc<Session>>>,
/// Subscription manager
subscriptions: Arc<SubscriptionManager>,
/// Global state
state: Arc<RouterState>,
/// Running flag
running: Arc<RwLock<bool>>,
/// Token validator (None = always reject in authenticated mode)
token_validator: Option<Arc<dyn TokenValidator>>,
}
impl Router {
/// Create a new router with the given configuration
pub fn new(config: RouterConfig) -> Self {
Self {
config,
sessions: Arc::new(DashMap::new()),
subscriptions: Arc::new(SubscriptionManager::new()),
state: Arc::new(RouterState::new()),
running: Arc::new(RwLock::new(false)),
token_validator: None,
}
}
/// Create a router with a token validator for authenticated mode
pub fn with_validator<V: TokenValidator + 'static>(mut self, validator: V) -> Self {
self.token_validator = Some(Arc::new(validator));
self
}
/// Set the token validator
pub fn set_validator<V: TokenValidator + 'static>(&mut self, validator: V) {
self.token_validator = Some(Arc::new(validator));
}
/// Get a reference to the CPSK validator if one is configured
/// This allows adding tokens at runtime
pub fn cpsk_validator(&self) -> Option<&CpskValidator> {
self.token_validator
.as_ref()
.and_then(|v| v.as_any().downcast_ref::<CpskValidator>())
}
/// Get the security mode
pub fn security_mode(&self) -> SecurityMode {
self.config.security_mode
}
// =========================================================================
// Transport-Agnostic Methods
// =========================================================================
/// Serve using any TransportServer implementation.
///
/// This is the core method that all transport-specific methods use internally.
/// Use this when you have a custom transport or want full control.
///
/// # Example
///
/// ```no_run
/// use clasp_router::Router;
/// use clasp_transport::WebSocketServer;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let router = Router::default();
/// let server = WebSocketServer::bind("0.0.0.0:7330").await?;
/// router.serve_on(server).await?;
/// # Ok(())
/// # }
/// ```
pub async fn serve_on<S>(&self, mut server: S) -> Result<()>
where
S: TransportServer + 'static,
S::Sender: 'static,
S::Receiver: 'static,
{
info!("Router accepting connections");
*self.running.write() = true;
while *self.running.read() {
match server.accept().await {
Ok((sender, receiver, addr)) => {
info!("New connection from {}", addr);
self.handle_connection(Arc::new(sender), receiver, addr);
}
Err(e) => {
error!("Accept error: {}", e);
}
}
}
Ok(())
}
// =========================================================================
// WebSocket Transport
// =========================================================================
/// Start the router on WebSocket (default, recommended).
///
/// WebSocket is the universal baseline transport:
/// - Works in browsers
/// - Works on all hosting platforms (including DO App Platform)
/// - Easy firewall/proxy traversal
///
/// Default port: 7330
#[cfg(feature = "websocket")]
pub async fn serve_websocket(&self, addr: &str) -> Result<()> {
let server = WebSocketServer::bind(addr).await?;
info!("WebSocket server listening on {}", addr);
self.serve_on(server).await
}
/// Backward-compatible alias for `serve_websocket`.
#[cfg(feature = "websocket")]
pub async fn serve(&self, addr: &str) -> Result<()> {
self.serve_websocket(addr).await
}
// =========================================================================
// QUIC Transport (feature-gated)
// =========================================================================
/// Start the router on QUIC.
///
/// QUIC is ideal for native applications:
/// - 0-RTT connection establishment
/// - Connection migration (mobile networks)
/// - Built-in encryption (TLS 1.3)
/// - Lower latency than WebSocket
///
/// **WARNING**: QUIC requires UDP, which is NOT supported on:
/// - DigitalOcean App Platform
/// - Many PaaS providers
/// - Some corporate firewalls
///
/// Use a VPS/Droplet for QUIC support.
///
/// Default port: 7331 (to avoid conflict with WebSocket on 7330)
#[cfg(feature = "quic")]
pub async fn serve_quic(
&self,
addr: SocketAddr,
cert_der: Vec<u8>,
key_der: Vec<u8>,
) -> Result<()> {
let server = QuicTransport::new_server(addr, cert_der, key_der)
.map_err(|e| RouterError::Transport(e))?;
info!("QUIC server listening on {}", addr);
self.serve_quic_transport(server).await
}
/// Internal: Serve using a QuicTransport server.
///
/// QUIC has a different accept pattern (connection then stream),
/// so we need special handling.
#[cfg(feature = "quic")]
async fn serve_quic_transport(&self, server: QuicTransport) -> Result<()> {
*self.running.write() = true;
while *self.running.read() {
match server.accept().await {
Ok(connection) => {
let addr = connection.remote_address();
info!("QUIC connection from {}", addr);
// Accept bidirectional stream for CLASP protocol
match connection.accept_bi().await {
Ok((sender, receiver)) => {
self.handle_connection(Arc::new(sender), receiver, addr);
}
Err(e) => {
error!("QUIC stream accept error: {}", e);
}
}
}
Err(e) => {
error!("QUIC accept error: {}", e);
}
}
}
Ok(())
}
// =========================================================================
// Multi-Transport Support
// =========================================================================
/// Serve on multiple transports simultaneously.
///
/// All transports share the same router state, so a client connected via
/// WebSocket can communicate with a client connected via QUIC.
///
/// # Example
///
/// ```no_run
/// use clasp_router::{Router, TransportConfig};
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let router = Router::default();
/// router.serve_multi(vec![
/// TransportConfig::WebSocket { addr: "0.0.0.0:7330".into() },
/// // QUIC requires feature and UDP support
/// // TransportConfig::Quic { addr: "0.0.0.0:7331".parse()?, cert, key },
/// ]).await?;
/// # Ok(())
/// # }
/// ```
pub async fn serve_multi(&self, transports: Vec<TransportConfig>) -> Result<()> {
use futures::future::try_join_all;
if transports.is_empty() {
return Err(RouterError::Config("No transports configured".into()));
}
let mut handles = vec![];
for config in transports {
let router = self.clone_internal();
let handle = tokio::spawn(async move {
match config {
#[cfg(feature = "websocket")]
TransportConfig::WebSocket { addr } => router.serve_websocket(&addr).await,
#[cfg(feature = "quic")]
TransportConfig::Quic { addr, cert, key } => {
router.serve_quic(addr, cert, key).await
}
#[allow(unreachable_patterns)]
_ => Err(RouterError::Config(
"Transport not enabled at compile time".into(),
)),
}
});
handles.push(handle);
}
// Wait for all transports (or first error)
let results = try_join_all(handles)
.await
.map_err(|e| RouterError::Config(format!("Transport task failed: {}", e)))?;
// Check for errors from any transport
for result in results {
result?;
}
Ok(())
}
/// Internal clone for spawning transport tasks.
/// Shares all Arc state with the original.
fn clone_internal(&self) -> Self {
Self {
config: self.config.clone(),
sessions: Arc::clone(&self.sessions),
subscriptions: Arc::clone(&self.subscriptions),
state: Arc::clone(&self.state),
running: Arc::clone(&self.running),
token_validator: self.token_validator.clone(),
}
}
/// Handle a new connection
fn handle_connection(
&self,
sender: Arc<dyn TransportSender>,
mut receiver: impl TransportReceiver + 'static,
addr: SocketAddr,
) {
let sessions = Arc::clone(&self.sessions);
let subscriptions = Arc::clone(&self.subscriptions);
let state = Arc::clone(&self.state);
let config = self.config.clone();
let running = Arc::clone(&self.running);
let token_validator = self.token_validator.clone();
let security_mode = self.config.security_mode;
tokio::spawn(async move {
let mut session: Option<Arc<Session>> = None;
while *running.read() {
match receiver.recv().await {
Some(TransportEvent::Data(data)) => {
// Decode message
match codec::decode(&data) {
Ok((msg, frame)) => {
// Handle message
if let Some(response) = handle_message(
&msg,
&frame,
&session,
&sender,
&sessions,
&subscriptions,
&state,
&config,
security_mode,
&token_validator,
)
.await
{
match response {
MessageResult::NewSession(s) => {
session = Some(s);
}
MessageResult::Send(bytes) => {
if let Err(e) = sender.send(bytes).await {
error!("Send error: {}", e);
break;
}
}
MessageResult::Broadcast(bytes, exclude) => {
broadcast_to_subscribers(&bytes, &sessions, &exclude)
.await;
}
MessageResult::Disconnect => {
info!("Disconnecting client {} due to auth failure", addr);
break;
}
MessageResult::None => {}
}
}
}
Err(e) => {
warn!("Decode error from {}: {}", addr, e);
}
}
}
Some(TransportEvent::Disconnected { reason }) => {
info!("Client {} disconnected: {:?}", addr, reason);
break;
}
Some(TransportEvent::Error(e)) => {
error!("Transport error from {}: {}", addr, e);
break;
}
None => {
break;
}
_ => {}
}
}
// Cleanup session
if let Some(s) = session {
info!("Removing session {}", s.id);
sessions.remove(&s.id);
subscriptions.remove_session(&s.id);
}
});
}
/// Stop the router
pub fn stop(&self) {
*self.running.write() = false;
}
/// Get session count
pub fn session_count(&self) -> usize {
self.sessions.len()
}
/// Get state
pub fn state(&self) -> &RouterState {
&self.state
}
/// Get subscription count
pub fn subscription_count(&self) -> usize {
self.subscriptions.len()
}
}
impl Default for Router {
fn default() -> Self {
Self::new(RouterConfig::default())
}
}
/// Result of handling a message
enum MessageResult {
NewSession(Arc<Session>),
Send(Bytes),
Broadcast(Bytes, SessionId),
Disconnect,
None,
}
/// Handle an incoming message
async fn handle_message(
msg: &Message,
_frame: &Frame,
session: &Option<Arc<Session>>,
sender: &Arc<dyn TransportSender>,
sessions: &Arc<DashMap<SessionId, Arc<Session>>>,
subscriptions: &Arc<SubscriptionManager>,
state: &Arc<RouterState>,
config: &RouterConfig,
security_mode: SecurityMode,
token_validator: &Option<Arc<dyn TokenValidator>>,
) -> Option<MessageResult> {
match msg {
Message::Hello(hello) => {
// In authenticated mode, validate the token
let (authenticated, subject, scopes) = match security_mode {
SecurityMode::Open => {
// Open mode: no authentication required
(false, None, Vec::new())
}
SecurityMode::Authenticated => {
// Authenticated mode: require valid token
let token = match &hello.token {
Some(t) => t,
None => {
warn!("Connection rejected: no token provided in authenticated mode");
let error = Message::Error(ErrorMessage {
code: 300, // Unauthorized
message: "Authentication required".to_string(),
address: None,
correlation_id: None,
});
let bytes = codec::encode(&error).ok()?;
let _ = sender.send(bytes).await;
return Some(MessageResult::Disconnect);
}
};
// Validate token
let validator = match token_validator {
Some(v) => v,
None => {
error!("Authenticated mode but no token validator configured");
let error = Message::Error(ErrorMessage {
code: 500, // Internal error
message: "Server misconfiguration".to_string(),
address: None,
correlation_id: None,
});
let bytes = codec::encode(&error).ok()?;
let _ = sender.send(bytes).await;
return Some(MessageResult::Disconnect);
}
};
match validator.validate(token) {
ValidationResult::Valid(info) => {
info!(
"Token validated for subject: {:?}, scopes: {}",
info.subject,
info.scopes.len()
);
(true, info.subject, info.scopes)
}
ValidationResult::Expired => {
warn!("Connection rejected: token expired");
let error = Message::Error(ErrorMessage {
code: 302, // TokenExpired
message: "Token has expired".to_string(),
address: None,
correlation_id: None,
});
let bytes = codec::encode(&error).ok()?;
let _ = sender.send(bytes).await;
return Some(MessageResult::Disconnect);
}
ValidationResult::Invalid(reason) => {
warn!("Connection rejected: invalid token - {}", reason);
let error = Message::Error(ErrorMessage {
code: 300, // Unauthorized
message: format!("Invalid token: {}", reason),
address: None,
correlation_id: None,
});
let bytes = codec::encode(&error).ok()?;
let _ = sender.send(bytes).await;
return Some(MessageResult::Disconnect);
}
ValidationResult::NotMyToken => {
warn!("Connection rejected: unrecognized token format");
let error = Message::Error(ErrorMessage {
code: 300, // Unauthorized
message: "Unrecognized token format".to_string(),
address: None,
correlation_id: None,
});
let bytes = codec::encode(&error).ok()?;
let _ = sender.send(bytes).await;
return Some(MessageResult::Disconnect);
}
}
}
};
// Create new session
let mut new_session = Session::new(
sender.clone(),
hello.name.clone(),
hello.features.clone(),
);
// Set authentication state
if authenticated {
new_session.set_authenticated(
hello.token.clone().unwrap_or_default(),
subject,
scopes,
);
}
let new_session = Arc::new(new_session);
let session_id = new_session.id.clone();
sessions.insert(session_id.clone(), new_session.clone());
info!(
"Session created: {} ({}) authenticated={}",
hello.name, session_id, new_session.authenticated
);
// Send welcome
let welcome = new_session.welcome_message(&config.name, &config.features);
let response = codec::encode(&welcome).ok()?;
// Send welcome first
let _ = sender.send(response).await;
// Send initial snapshot
let snapshot = Message::Snapshot(state.full_snapshot());
let snapshot_bytes = codec::encode(&snapshot).ok()?;
let _ = sender.send(snapshot_bytes).await;
Some(MessageResult::NewSession(new_session))
}
Message::Subscribe(sub) => {
let session = session.as_ref()?;
// Check scope for read access (in authenticated mode)
if security_mode == SecurityMode::Authenticated && !session.has_scope(Action::Read, &sub.pattern) {
warn!(
"Session {} denied SUBSCRIBE to {} - insufficient scope",
session.id, sub.pattern
);
let error = Message::Error(ErrorMessage {
code: 301, // Forbidden
message: "Insufficient scope for subscription".to_string(),
address: Some(sub.pattern.clone()),
correlation_id: None,
});
let bytes = codec::encode(&error).ok()?;
return Some(MessageResult::Send(bytes));
}
// Create subscription
match Subscription::new(
sub.id,
session.id.clone(),
&sub.pattern,
sub.types.clone(),
sub.options.clone().unwrap_or_default(),
) {
Ok(subscription) => {
subscriptions.add(subscription);
session.add_subscription(sub.id);
debug!("Session {} subscribed to {}", session.id, sub.pattern);
// Send matching current values
let snapshot = state.snapshot(&sub.pattern);
if !snapshot.params.is_empty() {
let msg = Message::Snapshot(snapshot);
let bytes = codec::encode(&msg).ok()?;
return Some(MessageResult::Send(bytes));
}
}
Err(e) => {
warn!("Invalid subscription pattern: {}", e);
let error = Message::Error(ErrorMessage {
code: 202,
message: e.to_string(),
address: Some(sub.pattern.clone()),
correlation_id: None,
});
let bytes = codec::encode(&error).ok()?;
return Some(MessageResult::Send(bytes));
}
}
Some(MessageResult::None)
}
Message::Unsubscribe(unsub) => {
let session = session.as_ref()?;
subscriptions.remove(&session.id, unsub.id);
session.remove_subscription(unsub.id);
Some(MessageResult::None)
}
Message::Set(set) => {
let session = session.as_ref()?;
// Check scope for write access (in authenticated mode)
if security_mode == SecurityMode::Authenticated && !session.has_scope(Action::Write, &set.address) {
warn!(
"Session {} denied SET to {} - insufficient scope",
session.id, set.address
);
let error = Message::Error(ErrorMessage {
code: 301, // Forbidden
message: "Insufficient scope for write operation".to_string(),
address: Some(set.address.clone()),
correlation_id: None,
});
let bytes = codec::encode(&error).ok()?;
return Some(MessageResult::Send(bytes));
}
// Apply to state
match state.apply_set(set, &session.id) {
Ok(revision) => {
// Broadcast to subscribers
let subscribers =
subscriptions.find_subscribers(&set.address, Some(SignalType::Param));
// Create updated SET message with revision
let mut updated_set = set.clone();
updated_set.revision = Some(revision);
let broadcast_msg = Message::Set(updated_set);
if let Ok(bytes) = codec::encode(&broadcast_msg) {
// Send to all subscribers (including sender for confirmation)
for sub_session_id in subscribers {
if let Some(sub_session) = sessions.get(&sub_session_id) {
let _ = sub_session.send(bytes.clone()).await;
}
}
}
// Send ACK to sender
let ack = Message::Ack(AckMessage {
address: Some(set.address.clone()),
revision: Some(revision),
locked: None,
holder: None,
correlation_id: None,
});
let ack_bytes = codec::encode(&ack).ok()?;
return Some(MessageResult::Send(ack_bytes));
}
Err(e) => {
let error = Message::Error(ErrorMessage {
code: 400,
message: format!("{:?}", e),
address: Some(set.address.clone()),
correlation_id: None,
});
let bytes = codec::encode(&error).ok()?;
return Some(MessageResult::Send(bytes));
}
}
}
Message::Get(get) => {
let session = session.as_ref()?;
// Check scope for read access (in authenticated mode)
if security_mode == SecurityMode::Authenticated && !session.has_scope(Action::Read, &get.address) {
warn!(
"Session {} denied GET to {} - insufficient scope",
session.id, get.address
);
let error = Message::Error(ErrorMessage {
code: 301, // Forbidden
message: "Insufficient scope for read operation".to_string(),
address: Some(get.address.clone()),
correlation_id: None,
});
let bytes = codec::encode(&error).ok()?;
return Some(MessageResult::Send(bytes));
}
if let Some(param_state) = state.get_state(&get.address) {
let snapshot = Message::Snapshot(clasp_core::SnapshotMessage {
params: vec![clasp_core::ParamValue {
address: get.address.clone(),
value: param_state.value,
revision: param_state.revision,
writer: Some(param_state.writer),
timestamp: Some(param_state.timestamp),
}],
});
let bytes = codec::encode(&snapshot).ok()?;
return Some(MessageResult::Send(bytes));
}
Some(MessageResult::None)
}
Message::Publish(pub_msg) => {
let session = session.as_ref()?;
// Check scope for write access (in authenticated mode)
if security_mode == SecurityMode::Authenticated && !session.has_scope(Action::Write, &pub_msg.address) {
warn!(
"Session {} denied PUBLISH to {} - insufficient scope",
session.id, pub_msg.address
);
let error = Message::Error(ErrorMessage {
code: 301, // Forbidden
message: "Insufficient scope for publish operation".to_string(),
address: Some(pub_msg.address.clone()),
correlation_id: None,
});
let bytes = codec::encode(&error).ok()?;
return Some(MessageResult::Send(bytes));
}
// Determine signal type
let signal_type = pub_msg.signal;
// Find subscribers
let subscribers = subscriptions.find_subscribers(&pub_msg.address, signal_type);
// Broadcast
if let Ok(bytes) = codec::encode(msg) {
for sub_session_id in subscribers {
if sub_session_id != session.id {
if let Some(sub_session) = sessions.get(&sub_session_id) {
let _ = sub_session.send(bytes.clone()).await;
}
}
}
}
Some(MessageResult::None)
}
Message::Ping => {
let pong = Message::Pong;
let bytes = codec::encode(&pong).ok()?;
Some(MessageResult::Send(bytes))
}
Message::Query(_query) => {
// Return signal definitions (simplified - would need schema registry)
let result = Message::Result(clasp_core::ResultMessage { signals: vec![] });
let bytes = codec::encode(&result).ok()?;
Some(MessageResult::Send(bytes))
}
_ => Some(MessageResult::None),
}
}
/// Broadcast to all sessions except one
async fn broadcast_to_subscribers(
data: &Bytes,
sessions: &Arc<DashMap<SessionId, Arc<Session>>>,
exclude: &SessionId,
) {
for entry in sessions.iter() {
if entry.key() != exclude {
let _ = entry.value().send(data.clone()).await;
}
}
}