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
//! Subscription management for WebSocket clients.

use crate::error::{Error, Result};
use crate::protocol::{EventType, SubscriptionFilter};
use dashmap::DashMap;
use std::collections::HashSet;
use std::ops::Range;
use std::sync::Arc;
use uuid::Uuid;

/// Subscription type.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SubscriptionType {
    /// Tile subscription with bbox and zoom range
    Tiles {
        /// Bounding box [min_x, min_y, max_x, max_y]
        bbox: [i64; 4], // Using i64 for exact comparison
        /// Zoom level range
        zoom_range: Range<u8>,
    },
    /// Feature subscription with filters
    Features {
        /// Layer name
        layer: Option<String>,
    },
    /// Event subscription
    Events {
        /// Event types
        event_types: HashSet<EventType>,
    },
}

/// A subscription to WebSocket updates.
#[derive(Debug, Clone)]
pub struct Subscription {
    /// Unique subscription ID
    pub id: String,
    /// Client ID that owns this subscription
    pub client_id: String,
    /// Subscription type
    pub subscription_type: SubscriptionType,
    /// Optional filter
    pub filter: Option<SubscriptionFilter>,
}

impl Subscription {
    /// Create a new subscription.
    pub fn new(
        client_id: String,
        subscription_type: SubscriptionType,
        filter: Option<SubscriptionFilter>,
    ) -> Self {
        Self {
            id: Uuid::new_v4().to_string(),
            client_id,
            subscription_type,
            filter,
        }
    }

    /// Create a tile subscription.
    pub fn tiles(
        client_id: String,
        bbox: [f64; 4],
        zoom_range: Range<u8>,
        filter: Option<SubscriptionFilter>,
    ) -> Self {
        // Convert f64 bbox to i64 for exact comparison (multiply by 1e6)
        let bbox_i64 = [
            (bbox[0] * 1_000_000.0) as i64,
            (bbox[1] * 1_000_000.0) as i64,
            (bbox[2] * 1_000_000.0) as i64,
            (bbox[3] * 1_000_000.0) as i64,
        ];

        Self::new(
            client_id,
            SubscriptionType::Tiles {
                bbox: bbox_i64,
                zoom_range,
            },
            filter,
        )
    }

    /// Create a feature subscription.
    pub fn features(
        client_id: String,
        layer: Option<String>,
        filter: Option<SubscriptionFilter>,
    ) -> Self {
        Self::new(client_id, SubscriptionType::Features { layer }, filter)
    }

    /// Create an event subscription.
    pub fn events(
        client_id: String,
        event_types: HashSet<EventType>,
        filter: Option<SubscriptionFilter>,
    ) -> Self {
        Self::new(client_id, SubscriptionType::Events { event_types }, filter)
    }

    /// Check if a tile matches this subscription.
    pub fn matches_tile(&self, tile_x: u32, tile_y: u32, zoom: u8) -> bool {
        match &self.subscription_type {
            SubscriptionType::Tiles { bbox, zoom_range } => {
                if !zoom_range.contains(&zoom) {
                    return false;
                }

                // Convert tile coordinates to bbox
                let n = 2_u32.pow(zoom.into());
                let tile_bbox = Self::tile_to_bbox(tile_x, tile_y, n);

                // Convert back to i64 for comparison
                let tile_bbox_i64 = [
                    (tile_bbox[0] * 1_000_000.0) as i64,
                    (tile_bbox[1] * 1_000_000.0) as i64,
                    (tile_bbox[2] * 1_000_000.0) as i64,
                    (tile_bbox[3] * 1_000_000.0) as i64,
                ];

                // Check if bboxes overlap
                Self::bboxes_overlap(bbox, &tile_bbox_i64)
            }
            _ => false,
        }
    }

    /// Convert tile coordinates to geographic bbox.
    fn tile_to_bbox(x: u32, y: u32, n: u32) -> [f64; 4] {
        let n_f64 = n as f64;
        let min_lon = (x as f64 / n_f64) * 360.0 - 180.0;
        let max_lon = ((x + 1) as f64 / n_f64) * 360.0 - 180.0;

        let lat_rad = |y_val: f64| -> f64 {
            let n_rad = std::f64::consts::PI - 2.0 * std::f64::consts::PI * y_val / n_f64;
            n_rad.sinh().atan().to_degrees()
        };

        let max_lat = lat_rad(y as f64);
        let min_lat = lat_rad((y + 1) as f64);

        [min_lon, min_lat, max_lon, max_lat]
    }

    /// Check if two bboxes overlap.
    fn bboxes_overlap(bbox1: &[i64; 4], bbox2: &[i64; 4]) -> bool {
        bbox1[0] <= bbox2[2] && bbox1[2] >= bbox2[0] && bbox1[1] <= bbox2[3] && bbox1[3] >= bbox2[1]
    }

    /// Check if a feature matches this subscription.
    pub fn matches_feature(&self, layer: Option<&str>) -> bool {
        match &self.subscription_type {
            SubscriptionType::Features { layer: sub_layer } => {
                if let Some(sub_layer) = sub_layer {
                    layer == Some(sub_layer.as_str())
                } else {
                    true // Match all layers
                }
            }
            _ => false,
        }
    }

    /// Check if an event matches this subscription.
    pub fn matches_event(&self, event_type: EventType) -> bool {
        match &self.subscription_type {
            SubscriptionType::Events { event_types } => event_types.contains(&event_type),
            _ => false,
        }
    }
}

/// Manager for all subscriptions.
pub struct SubscriptionManager {
    /// All subscriptions by ID
    subscriptions: Arc<DashMap<String, Subscription>>,
    /// Subscriptions by client ID
    client_subscriptions: Arc<DashMap<String, HashSet<String>>>,
}

impl SubscriptionManager {
    /// Create a new subscription manager.
    pub fn new() -> Self {
        Self {
            subscriptions: Arc::new(DashMap::new()),
            client_subscriptions: Arc::new(DashMap::new()),
        }
    }

    /// Add a subscription.
    pub fn add(&self, subscription: Subscription) -> Result<String> {
        let sub_id = subscription.id.clone();
        let client_id = subscription.client_id.clone();

        self.subscriptions.insert(sub_id.clone(), subscription);

        self.client_subscriptions
            .entry(client_id)
            .or_default()
            .insert(sub_id.clone());

        Ok(sub_id)
    }

    /// Remove a subscription.
    pub fn remove(&self, subscription_id: &str) -> Result<()> {
        if let Some((_, subscription)) = self.subscriptions.remove(subscription_id) {
            if let Some(mut client_subs) =
                self.client_subscriptions.get_mut(&subscription.client_id)
            {
                client_subs.remove(subscription_id);
            }
            Ok(())
        } else {
            Err(Error::NotFound(format!(
                "Subscription not found: {}",
                subscription_id
            )))
        }
    }

    /// Remove all subscriptions for a client.
    pub fn remove_client(&self, client_id: &str) -> Result<()> {
        if let Some((_, sub_ids)) = self.client_subscriptions.remove(client_id) {
            for sub_id in sub_ids {
                self.subscriptions.remove(&sub_id);
            }
        }
        Ok(())
    }

    /// Get a subscription by ID.
    pub fn get(&self, subscription_id: &str) -> Option<Subscription> {
        self.subscriptions.get(subscription_id).map(|s| s.clone())
    }

    /// Get all subscriptions for a client.
    pub fn get_client_subscriptions(&self, client_id: &str) -> Vec<Subscription> {
        if let Some(sub_ids) = self.client_subscriptions.get(client_id) {
            sub_ids.iter().filter_map(|id| self.get(id)).collect()
        } else {
            Vec::new()
        }
    }

    /// Find subscriptions matching a tile.
    pub fn find_tile_subscriptions(&self, tile_x: u32, tile_y: u32, zoom: u8) -> Vec<Subscription> {
        self.subscriptions
            .iter()
            .filter(|entry| entry.value().matches_tile(tile_x, tile_y, zoom))
            .map(|entry| entry.value().clone())
            .collect()
    }

    /// Find subscriptions matching a feature.
    pub fn find_feature_subscriptions(&self, layer: Option<&str>) -> Vec<Subscription> {
        self.subscriptions
            .iter()
            .filter(|entry| entry.value().matches_feature(layer))
            .map(|entry| entry.value().clone())
            .collect()
    }

    /// Find subscriptions matching an event.
    pub fn find_event_subscriptions(&self, event_type: EventType) -> Vec<Subscription> {
        self.subscriptions
            .iter()
            .filter(|entry| entry.value().matches_event(event_type))
            .map(|entry| entry.value().clone())
            .collect()
    }

    /// Get total subscription count.
    pub fn count(&self) -> usize {
        self.subscriptions.len()
    }

    /// Get client count.
    pub fn client_count(&self) -> usize {
        self.client_subscriptions.len()
    }
}

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

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

    #[test]
    fn test_subscription_creation() {
        let sub = Subscription::tiles(
            "client-1".to_string(),
            [-180.0, -90.0, 180.0, 90.0],
            0..14,
            None,
        );

        assert_eq!(sub.client_id, "client-1");
        assert!(!sub.id.is_empty());
    }

    #[test]
    fn test_tile_matching() {
        let sub = Subscription::tiles(
            "client-1".to_string(),
            [-180.0, -90.0, 0.0, 0.0],
            5..10,
            None,
        );

        // Should match tiles in the southwest quadrant at zoom 5-9
        // At zoom 5 (n=32), tile (0,0) is northwest, tile (0,31) is southwest
        // At zoom 5, southwest quadrant uses x=[0,15], y=[16,31]
        assert!(sub.matches_tile(0, 31, 5)); // Southwest corner tile
        assert!(sub.matches_tile(15, 16, 5)); // Near equator/prime meridian boundary
        // At zoom 8 (n=256), southwest quadrant uses x=[0,127], y=[128,255]
        assert!(sub.matches_tile(100, 200, 8)); // Deep in southwest quadrant

        // Should not match tiles outside zoom range
        assert!(!sub.matches_tile(0, 31, 4)); // Correct tile but wrong zoom
        assert!(!sub.matches_tile(0, 31, 10)); // Correct tile but wrong zoom

        // Should not match tiles outside bbox
        assert!(!sub.matches_tile(0, 0, 5)); // Northwest, not southwest
        assert!(!sub.matches_tile(16, 16, 8)); // Northeast, not southwest
    }

    #[test]
    fn test_subscription_manager() {
        let manager = SubscriptionManager::new();

        let sub1 = Subscription::tiles(
            "client-1".to_string(),
            [-180.0, -90.0, 0.0, 0.0],
            0..14,
            None,
        );
        let sub_id1 = sub1.id.clone();

        assert!(manager.add(sub1).is_ok());

        assert_eq!(manager.count(), 1);
        assert_eq!(manager.client_count(), 1);

        let retrieved = manager.get(&sub_id1);
        assert!(retrieved.is_some());
        if let Some(ref sub) = retrieved {
            assert_eq!(sub.id, sub_id1);
        }

        assert!(manager.remove(&sub_id1).is_ok());
        assert_eq!(manager.count(), 0);
    }

    #[test]
    fn test_find_tile_subscriptions() {
        let manager = SubscriptionManager::new();

        // client-1: southwest quadrant
        let sub1 = Subscription::tiles(
            "client-1".to_string(),
            [-180.0, -90.0, 0.0, 0.0],
            5..10,
            None,
        );

        // client-2: northeast quadrant
        let sub2 =
            Subscription::tiles("client-2".to_string(), [0.0, 0.0, 180.0, 90.0], 5..10, None);

        assert!(manager.add(sub1).is_ok());
        assert!(manager.add(sub2).is_ok());

        // Find subscriptions for a tile in the southwest quadrant
        // At zoom 5, tile (0,31) is in the southwest corner
        let matches = manager.find_tile_subscriptions(0, 31, 5);
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].client_id, "client-1");

        // Find subscriptions for a tile in the northeast quadrant
        // At zoom 5, tile (31,0) is in the northeast corner
        let matches2 = manager.find_tile_subscriptions(31, 0, 5);
        assert_eq!(matches2.len(), 1);
        assert_eq!(matches2[0].client_id, "client-2");
    }

    #[test]
    fn test_remove_client() {
        let manager = SubscriptionManager::new();

        let sub1 = Subscription::tiles(
            "client-1".to_string(),
            [-180.0, -90.0, 0.0, 0.0],
            0..14,
            None,
        );

        let sub2 = Subscription::features("client-1".to_string(), Some("layer1".to_string()), None);

        assert!(manager.add(sub1).is_ok());
        assert!(manager.add(sub2).is_ok());

        assert_eq!(manager.count(), 2);

        assert!(manager.remove_client("client-1").is_ok());
        assert_eq!(manager.count(), 0);
    }
}