oxigdal-ws 0.1.5

WebSocket streaming support for OxiGDAL - real-time geospatial data delivery
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
//! WebSocket server implementation.

use crate::error::{Error, Result};
use crate::protocol::{Compression, Message, MessageFormat};
use crate::subscription::{Subscription, SubscriptionManager};
use axum::{
    Router,
    extract::{
        State,
        ws::{WebSocket, WebSocketUpgrade},
    },
    response::IntoResponse,
    routing::get,
};
use dashmap::DashMap;
use futures::{SinkExt, StreamExt};
use std::net::SocketAddr;
use std::sync::Arc;
use tokio::sync::mpsc;
use tower_http::cors::CorsLayer;
use tracing::{debug, error, info, warn};
use uuid::Uuid;

/// WebSocket server configuration.
#[derive(Debug, Clone)]
pub struct ServerConfig {
    /// Bind address
    pub bind_addr: SocketAddr,
    /// Maximum connections
    pub max_connections: usize,
    /// Message buffer size per client
    pub message_buffer_size: usize,
    /// Default message format
    pub default_format: MessageFormat,
    /// Default compression
    pub default_compression: Compression,
    /// Enable CORS
    pub enable_cors: bool,
}

impl Default for ServerConfig {
    fn default() -> Self {
        Self {
            bind_addr: SocketAddr::from(([0, 0, 0, 0], 9001)),
            max_connections: 10000,
            message_buffer_size: 1000,
            default_format: MessageFormat::MessagePack,
            default_compression: Compression::Zstd,
            enable_cors: true,
        }
    }
}

/// Client connection state.
struct ClientState {
    /// Client ID
    id: String,
    /// Message sender
    tx: mpsc::UnboundedSender<Message>,
    /// Message format preference
    format: MessageFormat,
    /// Compression preference
    compression: Compression,
}

impl ClientState {
    /// Send a message to the client.
    fn send(&self, message: Message) -> Result<()> {
        self.tx
            .send(message)
            .map_err(|_| Error::Send("Client disconnected".to_string()))
    }
}

/// Shared server state.
#[derive(Clone)]
struct AppState {
    /// Active clients
    clients: Arc<DashMap<String, ClientState>>,
    /// Subscription manager
    subscriptions: Arc<SubscriptionManager>,
    /// Server configuration
    config: Arc<ServerConfig>,
}

impl AppState {
    fn new(config: ServerConfig) -> Self {
        Self {
            clients: Arc::new(DashMap::new()),
            subscriptions: Arc::new(SubscriptionManager::new()),
            config: Arc::new(config),
        }
    }

    /// Broadcast message to all clients.
    fn broadcast(&self, message: Message) {
        for client in self.clients.iter() {
            if let Err(e) = client.send(message.clone()) {
                warn!("Failed to send to client {}: {}", client.id, e);
            }
        }
    }

    /// Send message to specific client.
    fn send_to_client(&self, client_id: &str, message: Message) -> Result<()> {
        if let Some(client) = self.clients.get(client_id) {
            client.send(message)
        } else {
            Err(Error::NotFound(format!("Client not found: {}", client_id)))
        }
    }

    /// Send message to all subscribers of a subscription type.
    #[allow(dead_code)]
    fn send_to_subscribers(&self, subscription_id: &str, message: Message) {
        if let Some(sub) = self.subscriptions.get(subscription_id) {
            if let Err(e) = self.send_to_client(&sub.client_id, message) {
                warn!("Failed to send to subscriber {}: {}", sub.client_id, e);
            }
        }
    }
}

/// WebSocket server.
pub struct WebSocketServer {
    state: AppState,
}

impl WebSocketServer {
    /// Create a new WebSocket server with default configuration.
    pub fn new() -> Self {
        Self::with_config(ServerConfig::default())
    }

    /// Create a new WebSocket server with custom configuration.
    pub fn with_config(config: ServerConfig) -> Self {
        Self {
            state: AppState::new(config),
        }
    }

    /// Create a builder for the server.
    pub fn builder() -> ServerBuilder {
        ServerBuilder::new()
    }

    /// Run the WebSocket server.
    pub async fn run(self) -> Result<()> {
        let bind_addr = self.state.config.bind_addr;

        let mut app = Router::new()
            .route("/ws", get(ws_handler))
            .route("/health", get(health_handler))
            .with_state(self.state.clone());

        if self.state.config.enable_cors {
            app = app.layer(CorsLayer::permissive());
        }

        info!("WebSocket server listening on {}", bind_addr);

        let listener = tokio::net::TcpListener::bind(bind_addr)
            .await
            .map_err(|e| Error::Server(format!("Failed to bind: {}", e)))?;

        axum::serve(listener, app)
            .await
            .map_err(|e| Error::Server(format!("Server error: {}", e)))?;

        Ok(())
    }

    /// Get server statistics.
    pub fn stats(&self) -> ServerStats {
        ServerStats {
            active_connections: self.state.clients.len(),
            total_subscriptions: self.state.subscriptions.count(),
            unique_clients: self.state.subscriptions.client_count(),
        }
    }

    /// Broadcast a message to all connected clients.
    pub fn broadcast(&self, message: Message) {
        self.state.broadcast(message);
    }

    /// Send a message to a specific client.
    pub fn send_to_client(&self, client_id: &str, message: Message) -> Result<()> {
        self.state.send_to_client(client_id, message)
    }

    /// Get subscription manager.
    pub fn subscriptions(&self) -> &SubscriptionManager {
        &self.state.subscriptions
    }
}

impl Default for WebSocketServer {
    fn default() -> Self {
        Self::new()
    }
}

/// Server statistics.
#[derive(Debug, Clone)]
pub struct ServerStats {
    /// Number of active WebSocket connections
    pub active_connections: usize,
    /// Total number of subscriptions
    pub total_subscriptions: usize,
    /// Number of unique clients with subscriptions
    pub unique_clients: usize,
}

/// Builder for WebSocket server.
pub struct ServerBuilder {
    config: ServerConfig,
}

impl ServerBuilder {
    /// Create a new server builder.
    pub fn new() -> Self {
        Self {
            config: ServerConfig::default(),
        }
    }

    /// Set bind address.
    pub fn bind(mut self, addr: &str) -> Result<Self> {
        self.config.bind_addr = addr
            .parse()
            .map_err(|e| Error::InvalidParameter(format!("Invalid address: {}", e)))?;
        Ok(self)
    }

    /// Set maximum connections.
    pub fn max_connections(mut self, max: usize) -> Self {
        self.config.max_connections = max;
        self
    }

    /// Set message buffer size.
    pub fn message_buffer_size(mut self, size: usize) -> Self {
        self.config.message_buffer_size = size;
        self
    }

    /// Set default message format.
    pub fn default_format(mut self, format: MessageFormat) -> Self {
        self.config.default_format = format;
        self
    }

    /// Set default compression.
    pub fn default_compression(mut self, compression: Compression) -> Self {
        self.config.default_compression = compression;
        self
    }

    /// Enable CORS.
    pub fn enable_cors(mut self, enable: bool) -> Self {
        self.config.enable_cors = enable;
        self
    }

    /// Build the server.
    pub fn build(self) -> WebSocketServer {
        WebSocketServer::with_config(self.config)
    }
}

impl Default for ServerBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Health check handler.
async fn health_handler() -> &'static str {
    "OK"
}

/// WebSocket upgrade handler.
async fn ws_handler(ws: WebSocketUpgrade, State(state): State<AppState>) -> impl IntoResponse {
    ws.on_upgrade(|socket| handle_socket(socket, state))
}

/// Handle WebSocket connection.
async fn handle_socket(socket: WebSocket, state: AppState) {
    let client_id = Uuid::new_v4().to_string();
    info!("New WebSocket connection: {}", client_id);

    let (mut sender, mut receiver) = socket.split();
    let (tx, mut rx) = mpsc::unbounded_channel();

    // Default protocol settings
    let mut format = state.config.default_format;
    let mut compression = state.config.default_compression;

    // Add client to state
    let client_state = ClientState {
        id: client_id.clone(),
        tx: tx.clone(),
        format,
        compression,
    };
    state.clients.insert(client_id.clone(), client_state);

    // Spawn task to send messages to client
    let client_id_clone = client_id.clone();
    tokio::spawn(async move {
        while let Some(message) = rx.recv().await {
            // Encode message
            let data = match message.encode(format, compression) {
                Ok(data) => data,
                Err(e) => {
                    error!("Failed to encode message: {}", e);
                    continue;
                }
            };

            // Send as binary message
            if let Err(e) = sender
                .send(axum::extract::ws::Message::Binary(data.into()))
                .await
            {
                error!("Failed to send message to {}: {}", client_id_clone, e);
                break;
            }
        }
    });

    // Handle incoming messages
    while let Some(msg) = receiver.next().await {
        let msg = match msg {
            Ok(msg) => msg,
            Err(e) => {
                error!("WebSocket error for {}: {}", client_id, e);
                break;
            }
        };

        let data = match msg {
            axum::extract::ws::Message::Binary(data) => data.to_vec(),
            axum::extract::ws::Message::Text(text) => text.as_bytes().to_vec(),
            axum::extract::ws::Message::Close(_) => {
                info!("Client {} disconnected", client_id);
                break;
            }
            axum::extract::ws::Message::Ping(_) | axum::extract::ws::Message::Pong(_) => {
                continue;
            }
        };

        // Decode message
        let message = match Message::decode(&data, format, compression) {
            Ok(msg) => msg,
            Err(e) => {
                error!("Failed to decode message from {}: {}", client_id, e);
                continue;
            }
        };

        // Handle message
        if let Err(e) =
            handle_message(message, &client_id, &state, &mut format, &mut compression).await
        {
            error!("Error handling message from {}: {}", client_id, e);
        }
    }

    // Cleanup on disconnect
    info!("Cleaning up client {}", client_id);
    state.clients.remove(&client_id);
    if let Err(e) = state.subscriptions.remove_client(&client_id) {
        error!("Failed to remove client subscriptions: {}", e);
    }
}

/// Handle a received message.
async fn handle_message(
    message: Message,
    client_id: &str,
    state: &AppState,
    format: &mut MessageFormat,
    compression: &mut Compression,
) -> Result<()> {
    match message {
        Message::Handshake {
            version,
            format: client_format,
            compression: client_compression,
        } => {
            debug!("Handshake from {}: v{}", client_id, version);

            // Negotiate protocol
            *format = client_format;
            *compression = client_compression;

            // Update client state
            if let Some(mut client) = state.clients.get_mut(client_id) {
                client.format = *format;
                client.compression = *compression;
            }

            // Send acknowledgement
            state.send_to_client(
                client_id,
                Message::HandshakeAck {
                    version,
                    format: *format,
                    compression: *compression,
                },
            )?;
        }

        Message::SubscribeTiles {
            subscription_id,
            bbox,
            zoom_range,
            ..
        } => {
            debug!("Subscribe tiles from {}: {}", client_id, subscription_id);

            let sub = Subscription::tiles(client_id.to_string(), bbox, zoom_range, None);
            state.subscriptions.add(sub)?;

            state.send_to_client(
                client_id,
                Message::Ack {
                    request_id: subscription_id,
                    success: true,
                    message: Some("Subscribed to tiles".to_string()),
                },
            )?;
        }

        Message::SubscribeFeatures {
            subscription_id,
            layer,
            ..
        } => {
            debug!("Subscribe features from {}: {}", client_id, subscription_id);

            let sub = Subscription::features(client_id.to_string(), layer, None);
            state.subscriptions.add(sub)?;

            state.send_to_client(
                client_id,
                Message::Ack {
                    request_id: subscription_id,
                    success: true,
                    message: Some("Subscribed to features".to_string()),
                },
            )?;
        }

        Message::SubscribeEvents {
            subscription_id,
            event_types,
        } => {
            debug!("Subscribe events from {}: {}", client_id, subscription_id);

            let event_types_set = event_types.into_iter().collect();
            let sub = Subscription::events(client_id.to_string(), event_types_set, None);
            state.subscriptions.add(sub)?;

            state.send_to_client(
                client_id,
                Message::Ack {
                    request_id: subscription_id,
                    success: true,
                    message: Some("Subscribed to events".to_string()),
                },
            )?;
        }

        Message::Unsubscribe { subscription_id } => {
            debug!("Unsubscribe from {}: {}", client_id, subscription_id);

            state.subscriptions.remove(&subscription_id)?;

            state.send_to_client(
                client_id,
                Message::Ack {
                    request_id: subscription_id,
                    success: true,
                    message: Some("Unsubscribed".to_string()),
                },
            )?;
        }

        Message::Ping { id } => {
            state.send_to_client(client_id, Message::Pong { id })?;
        }

        _ => {
            warn!("Unexpected message type from {}", client_id);
        }
    }

    Ok(())
}

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

    #[test]
    fn test_server_config_default() {
        let config = ServerConfig::default();
        assert_eq!(config.max_connections, 10000);
        assert_eq!(config.message_buffer_size, 1000);
        assert!(config.enable_cors);
    }

    #[test]
    fn test_server_builder() {
        let result = ServerBuilder::new().bind("127.0.0.1:8080");
        assert!(result.is_ok());
        if let Ok(builder) = result {
            let server = builder
                .max_connections(5000)
                .message_buffer_size(500)
                .default_format(MessageFormat::Json)
                .enable_cors(false)
                .build();

            assert_eq!(server.state.config.bind_addr.to_string(), "127.0.0.1:8080");
            assert_eq!(server.state.config.max_connections, 5000);
            assert_eq!(server.state.config.message_buffer_size, 500);
            assert_eq!(server.state.config.default_format, MessageFormat::Json);
            assert!(!server.state.config.enable_cors);
        }
    }

    #[test]
    fn test_app_state() {
        let state = AppState::new(ServerConfig::default());

        assert_eq!(state.clients.len(), 0);
        assert_eq!(state.subscriptions.count(), 0);
    }
}