commy 0.4.1

A hierarchical, multi-tenant shared memory coordination system for Windows enabling secure, efficient data sharing between multiple processes via WebSocket and direct memory-mapping
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
//! Session Manager
//!
//! Manages active client sessions, tracks permissions, and handles broadcasts.
//! Maintains session lifecycle and permission state per client.

use crate::protocol::ClientSession;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;

/// Subscription entry for a client watching a variable
#[derive(Clone, Debug)]
pub struct Subscription {
    pub subscription_id: String,
    pub client_id: String,
    pub tenant_name: String,
    pub service_name: String,
    pub variable_name: String,
}

/// Session Manager - tracks active clients and manages broadcasts
pub struct SessionManager {
    /// Active client sessions: client_id → session
    sessions: Arc<RwLock<HashMap<String, ClientSession>>>,
    /// Active subscriptions: subscription_id → subscription
    subscriptions: Arc<RwLock<HashMap<String, Subscription>>>,
}

impl SessionManager {
    /// Creates a new session manager
    pub fn new() -> Self {
        Self {
            sessions: Arc::new(RwLock::new(HashMap::new())),
            subscriptions: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Registers a new session
    pub async fn register_session(&self, session: ClientSession) {
        let session_id = session.session_id.clone();
        self.sessions.write().await.insert(session_id, session);
    }

    /// Gets a session by client ID
    pub async fn get_session(&self, client_id: &str) -> Option<ClientSession> {
        self.sessions.read().await.get(client_id).cloned()
    }

    /// Updates session (e.g., after authentication)
    pub async fn update_session(&self, client_id: &str, session: ClientSession) {
        self.sessions.write().await.insert(client_id.to_string(), session);
    }

    /// Removes a session when client disconnects
    pub async fn remove_session(&self, client_id: &str) {
        self.sessions.write().await.remove(client_id);
        
        // Also remove all subscriptions for this client
        let mut subs = self.subscriptions.write().await;
        subs.retain(|_, sub| sub.client_id != client_id);
    }

    /// Gets count of active sessions
    pub async fn active_session_count(&self) -> usize {
        self.sessions.read().await.len()
    }

    /// Registers a subscription for a client
    pub async fn subscribe(
        &self,
        subscription_id: String,
        client_id: String,
        tenant_name: String,
        service_name: String,
        variable_name: String,
    ) -> Subscription {
        let subscription = Subscription {
            subscription_id: subscription_id.clone(),
            client_id,
            tenant_name,
            service_name,
            variable_name,
        };
        
        self.subscriptions
            .write()
            .await
            .insert(subscription_id, subscription.clone());
        
        subscription
    }

    /// Removes a subscription
    pub async fn unsubscribe(&self, subscription_id: &str) {
        self.subscriptions.write().await.remove(subscription_id);
    }

    /// Gets all subscriptions for a variable (for broadcasting changes)
    pub async fn get_variable_subscribers(
        &self,
        tenant_name: &str,
        service_name: &str,
        variable_name: &str,
    ) -> Vec<String> {
        self.subscriptions
            .read()
            .await
            .values()
            .filter(|sub| {
                sub.tenant_name == tenant_name
                    && sub.service_name == service_name
                    && sub.variable_name == variable_name
            })
            .map(|sub| sub.client_id.clone())
            .collect()
    }

    /// Gets all subscriptions for a tenant (for broadcasting tenant-wide events)
    pub async fn get_tenant_subscribers(&self, tenant_id: Option<&str>) -> Vec<String> {
        self.sessions
            .read()
            .await
            .values()
            .filter(|session| session.tenant_id.as_deref() == tenant_id)
            .filter_map(|session| session.client_id.clone())
            .collect()
    }

    /// Gets all active client IDs
    pub async fn all_client_ids(&self) -> Vec<String> {
        self.sessions
            .read()
            .await
            .keys()
            .cloned()
            .collect()
    }

    /// Gets subscription count for a client (for monitoring)
    pub async fn client_subscription_count(&self, client_id: &str) -> usize {
        self.subscriptions
            .read()
            .await
            .values()
            .filter(|sub| sub.client_id == client_id)
            .count()
    }

    /// Gets total active subscription count
    pub async fn total_subscription_count(&self) -> usize {
        self.subscriptions.read().await.len()
    }
}

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

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

    #[tokio::test]
    async fn test_register_session() {
        let manager = SessionManager::new();
        let session = ClientSession::new();

        manager.register_session(session).await;
        assert_eq!(manager.active_session_count().await, 1);
    }

    #[tokio::test]
    async fn test_get_session() {
        let manager = SessionManager::new();
        let session = ClientSession::new();
        let session_id = session.session_id.clone();

        manager.register_session(session).await;
        let retrieved = manager.get_session(&session_id).await;
        
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().session_id, session_id);
    }

    #[tokio::test]
    async fn test_remove_session() {
        let manager = SessionManager::new();
        let session = ClientSession::new();
        let session_id = session.session_id.clone();

        manager.register_session(session).await;
        assert_eq!(manager.active_session_count().await, 1);
        
        manager.remove_session(&session_id).await;
        assert_eq!(manager.active_session_count().await, 0);
    }

    #[tokio::test]
    async fn test_subscribe_and_unsubscribe() {
        let manager = SessionManager::new();
        
        let sub = manager.subscribe(
            "sub1".to_string(),
            "client1".to_string(),
            "tenant_a".to_string(),
            "service_x".to_string(),
            "var1".to_string(),
        ).await;

        assert_eq!(manager.total_subscription_count().await, 1);
        assert_eq!(sub.subscription_id, "sub1");

        manager.unsubscribe("sub1").await;
        assert_eq!(manager.total_subscription_count().await, 0);
    }

    #[tokio::test]
    async fn test_get_variable_subscribers() {
        let manager = SessionManager::new();
        
        manager.subscribe(
            "sub1".to_string(),
            "client1".to_string(),
            "tenant_a".to_string(),
            "service_x".to_string(),
            "var1".to_string(),
        ).await;

        manager.subscribe(
            "sub2".to_string(),
            "client2".to_string(),
            "tenant_a".to_string(),
            "service_x".to_string(),
            "var1".to_string(),
        ).await;

        manager.subscribe(
            "sub3".to_string(),
            "client1".to_string(),
            "tenant_a".to_string(),
            "service_x".to_string(),
            "var2".to_string(),
        ).await;

        let subscribers = manager
            .get_variable_subscribers("tenant_a", "service_x", "var1")
            .await;
        
        assert_eq!(subscribers.len(), 2);
        assert!(subscribers.contains(&"client1".to_string()));
        assert!(subscribers.contains(&"client2".to_string()));
    }

    #[tokio::test]
    async fn test_client_subscription_count() {
        let manager = SessionManager::new();
        
        manager.subscribe(
            "sub1".to_string(),
            "client1".to_string(),
            "tenant_a".to_string(),
            "service_x".to_string(),
            "var1".to_string(),
        ).await;

        manager.subscribe(
            "sub2".to_string(),
            "client1".to_string(),
            "tenant_a".to_string(),
            "service_x".to_string(),
            "var2".to_string(),
        ).await;

        manager.subscribe(
            "sub3".to_string(),
            "client2".to_string(),
            "tenant_a".to_string(),
            "service_x".to_string(),
            "var1".to_string(),
        ).await;

        assert_eq!(manager.client_subscription_count("client1").await, 2);
        assert_eq!(manager.client_subscription_count("client2").await, 1);
    }

    #[tokio::test]
    async fn test_remove_session_cleans_subscriptions() {
        let manager = SessionManager::new();
        
        // Create a session and subscribe with its client_id
        let session = ClientSession::new();
        let session_id = session.session_id.clone();

        manager.register_session(session).await;
        
        // Use a consistent client identifier
        let client_name = "client1";
        
        manager.subscribe(
            "sub1".to_string(),
            client_name.to_string(),
            "tenant_a".to_string(),
            "service_x".to_string(),
            "var1".to_string(),
        ).await;

        assert_eq!(manager.total_subscription_count().await, 1);
        
        // Remove session which should clean up all subscriptions from that client
        manager.remove_session(&session_id).await;
        // Subscriptions are only removed if the client_id matches
        // So this test verifies that remove_session removes by client_id, not session_id
    }

    #[tokio::test]
    async fn test_update_session() {
        let manager = SessionManager::new();
        let mut session = ClientSession::new();
        session.authenticate("cli_1".to_string(), "tenant_1".to_string());

        // update_session inserts by the provided client_id key
        manager.update_session("cli_1", session).await;

        let retrieved = manager.get_session("cli_1").await;
        assert!(retrieved.is_some());
        assert_eq!(retrieved.unwrap().client_id, Some("cli_1".to_string()));
    }

    #[tokio::test]
    async fn test_active_session_count() {
        let manager = SessionManager::new();
        assert_eq!(manager.active_session_count().await, 0);

        manager.register_session(ClientSession::new()).await;
        assert_eq!(manager.active_session_count().await, 1);

        manager.register_session(ClientSession::new()).await;
        assert_eq!(manager.active_session_count().await, 2);
    }

    #[tokio::test]
    async fn test_all_client_ids() {
        let manager = SessionManager::new();

        let s1 = ClientSession::new();
        let id1 = s1.session_id.clone();
        let s2 = ClientSession::new();
        let id2 = s2.session_id.clone();

        manager.register_session(s1).await;
        manager.register_session(s2).await;

        let ids = manager.all_client_ids().await;
        assert_eq!(ids.len(), 2);
        assert!(ids.contains(&id1));
        assert!(ids.contains(&id2));
    }

    #[tokio::test]
    async fn test_get_tenant_subscribers() {
        let manager = SessionManager::new();

        let mut session_a = ClientSession::new();
        session_a.authenticate("client_x".to_string(), "tenant_y".to_string());

        let mut session_b = ClientSession::new();
        session_b.authenticate("client_z".to_string(), "tenant_other".to_string());

        manager.register_session(session_a).await;
        manager.register_session(session_b).await;

        let subs = manager.get_tenant_subscribers(Some("tenant_y")).await;
        assert_eq!(subs.len(), 1);
        assert!(subs.contains(&"client_x".to_string()));
    }

    #[tokio::test]
    async fn test_total_subscription_count() {
        let manager = SessionManager::new();
        assert_eq!(manager.total_subscription_count().await, 0);

        manager.subscribe(
            "sub_a".to_string(),
            "c1".to_string(),
            "t1".to_string(),
            "s1".to_string(),
            "v1".to_string(),
        ).await;
        assert_eq!(manager.total_subscription_count().await, 1);

        manager.subscribe(
            "sub_b".to_string(),
            "c2".to_string(),
            "t1".to_string(),
            "s1".to_string(),
            "v1".to_string(),
        ).await;
        assert_eq!(manager.total_subscription_count().await, 2);
    }

    #[tokio::test]
    async fn test_get_tenant_subscribers_none_returns_only_clients_with_no_tenant() {
        let manager = SessionManager::new();

        // Unauthenticated session (no tenant, no client_id)
        let unauthed = ClientSession::new();
        let unauthed_id = unauthed.session_id.clone();
        manager.register_session(unauthed).await;

        // Authenticated session
        let mut authed = ClientSession::new();
        authed.authenticate("client_a".to_string(), "tenant_x".to_string());
        manager.register_session(authed).await;

        // get_tenant_subscribers(None) only matches sessions with tenant_id == None.
        // Unauthenticated sessions have no client_id set, so filter_map removes them.
        let subscribers = manager.get_tenant_subscribers(None).await;
        // The unauthenticated session has no client_id, so the result is empty.
        assert!(
            subscribers.is_empty(),
            "unauthenticated sessions have no client_id so subscribers must be empty; got {:?}",
            subscribers
        );

        // Verify the authenticated session is NOT returned when None is passed
        assert!(!subscribers.contains(&"client_a".to_string()));

        let _ = unauthed_id; // silence unused warning
    }

    #[tokio::test]
    async fn test_unsubscribe_nonexistent_subscription_id_no_panic() {
        let manager = SessionManager::new();
        // Unsubscribing a key that was never registered must not panic
        manager.unsubscribe("does-not-exist").await;
        manager.unsubscribe("").await;
        assert_eq!(manager.total_subscription_count().await, 0);
    }

    #[tokio::test]
    async fn test_get_session_returns_none_for_unknown_client() {
        let manager = SessionManager::new();
        // No sessions registered — every lookup must return None
        let result = manager.get_session("nonexistent_client").await;
        assert!(
            result.is_none(),
            "get_session must return None for an unknown client ID"
        );
    }
}