Skip to main content

clasp_router/
lib.rs

1//! # CLASP Router
2//!
3//! The router is the central message hub for CLASP (Creative Low-Latency Application Streaming Protocol).
4//!
5//! ## Core Responsibilities
6//!
7//! - **Session Management**: Track connected clients, handle authentication, manage session lifecycle
8//! - **Message Routing**: Route messages between clients based on address patterns
9//! - **State Management**: Maintain parameter state with revision tracking
10//! - **Subscription Handling**: Match published messages to subscriber patterns
11//! - **Protocol Bridging**: Interface with external protocols via bridges
12//!
13//! ## Architecture
14//!
15//! ```text
16//! ┌─────────────┐     ┌─────────────┐     ┌─────────────┐
17//! │   Client A  │     │   Client B  │     │   Client C  │
18//! └──────┬──────┘     └──────┬──────┘     └──────┬──────┘
19//!        │                   │                   │
20//!        └───────────────────┼───────────────────┘
21//!                            │
22//!                    ┌───────▼───────┐
23//!                    │    Router     │
24//!                    │  ┌─────────┐  │
25//!                    │  │  State  │  │  Parameter storage
26//!                    │  └─────────┘  │
27//!                    │  ┌─────────┐  │
28//!                    │  │Subscr.  │  │  Subscription matching
29//!                    │  └─────────┘  │
30//!                    │  ┌─────────┐  │
31//!                    │  │Sessions │  │  Client tracking
32//!                    │  └─────────┘  │
33//!                    └───────────────┘
34//! ```
35//!
36//! ## Transport Support
37//!
38//! The router is transport-agnostic and can accept connections from:
39//!
40//! - **WebSocket** (default): Universal, works in browsers and all platforms
41//! - **QUIC**: High-performance for native apps (requires UDP)
42//!
43//! ## Quick Start
44//!
45//! ```no_run
46//! use clasp_router::{Router, RouterConfig};
47//!
48//! #[tokio::main]
49//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
50//!     // Create router with default configuration
51//!     let router = Router::new(RouterConfig::default());
52//!
53//!     // Start WebSocket server on default port
54//!     router.serve_websocket("0.0.0.0:7330").await?;
55//!     Ok(())
56//! }
57//! ```
58//!
59//! ## Configuration
60//!
61//! ```no_run
62//! use clasp_router::{Router, RouterConfig};
63//!
64//! let config = RouterConfig {
65//!     max_sessions: 1000,
66//!     max_subscriptions_per_session: 1000,
67//!     session_timeout: 300,
68//!     ..Default::default()
69//! };
70//!
71//! let router = Router::new(config);
72//! ```
73//!
74//! ## Message Flow
75//!
76//! 1. Client connects and sends HELLO
77//! 2. Router responds with WELCOME (includes session ID, server time)
78//! 3. Client subscribes to patterns
79//! 4. Client sends SET/PUBLISH messages
80//! 5. Router stores state (for SET) and routes to matching subscribers
81//!
82//! ## Module Overview
83//!
84//! - [`router`] - Main Router struct and message handling
85//! - [`session`] - Client session management
86//! - [`state`] - Parameter state storage
87//! - [`subscription`] - Pattern-based subscription matching
88//! - [`p2p`] - Peer-to-peer mesh networking support
89//! - [`gesture`] - Gesture move coalescing for bandwidth optimization
90//! - [`error`] - Error types
91
92pub mod error;
93pub mod gesture;
94pub mod handlers;
95pub mod p2p;
96pub mod router;
97pub mod session;
98pub mod state;
99pub mod subscription;
100
101// Protocol adapters (feature-gated)
102#[cfg(any(feature = "mqtt-server", feature = "osc-server"))]
103pub mod adapters;
104
105pub use error::{Result, RouterError};
106pub use gesture::{GestureRegistry, GestureResult};
107pub use p2p::{analyze_address, P2PAddressType, P2PCapabilities};
108#[cfg(feature = "quic")]
109pub use router::QuicServerConfig;
110pub use router::{
111    MultiProtocolConfig, Router, RouterConfig, RouterConfigBuilder, SignalTransform, SnapshotFilter,
112    TransportConfig, WriteValidator,
113};
114pub use session::{Session, SessionId};
115pub use state::{RouterState, RouterStateConfig};
116pub use subscription::SubscriptionManager;
117
118#[cfg(feature = "rules")]
119pub use router::execute_rule_actions;
120
121// Re-export adapter configs
122#[cfg(feature = "mqtt-server")]
123pub use adapters::{MqttServerAdapter, MqttServerConfig};
124#[cfg(feature = "osc-server")]
125pub use adapters::{OscServerAdapter, OscServerConfig};