sonos-sdk-stream 0.5.2

Internal event streaming and subscription management for sonos-sdk
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
//! Speaker and service registration with duplicate protection
//!
//! This module provides thread-safe registration and management of speaker/service
//! pairs, ensuring that duplicate registrations are prevented and providing
//! efficient lookup capabilities.

use std::collections::HashMap;
use std::net::IpAddr;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use tokio::sync::RwLock;

use crate::error::{RegistryError, RegistryResult};

/// Unique identifier for a speaker/service registration
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct RegistrationId(u64);

impl RegistrationId {
    /// Create a new RegistrationId with the given value
    pub fn new(id: u64) -> Self {
        Self(id)
    }

    /// Get the raw ID value
    pub fn as_u64(&self) -> u64 {
        self.0
    }
}

impl std::fmt::Display for RegistrationId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "reg-{}", self.0)
    }
}

/// A speaker and service type pair
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct SpeakerServicePair {
    /// IP address of the speaker
    pub speaker_ip: IpAddr,
    /// UPnP service type
    pub service: sonos_api::Service,
}

impl SpeakerServicePair {
    /// Create a new SpeakerServicePair
    pub fn new(speaker_ip: IpAddr, service: sonos_api::Service) -> Self {
        Self {
            speaker_ip,
            service,
        }
    }
}

impl std::fmt::Display for SpeakerServicePair {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}:{:?}", self.speaker_ip, self.service)
    }
}

/// Thread-safe registry for speaker/service pairs with duplicate protection
///
/// This registry maintains bidirectional mappings between registration IDs and
/// speaker/service pairs, allowing for efficient lookups in both directions
/// while preventing duplicate registrations.
pub struct SpeakerServiceRegistry {
    /// Mapping from registration ID to speaker/service pair
    registrations: Arc<RwLock<HashMap<RegistrationId, SpeakerServicePair>>>,

    /// Reverse mapping from speaker/service pair to registration ID for duplicate detection
    pair_to_registration: Arc<RwLock<HashMap<SpeakerServicePair, RegistrationId>>>,

    /// Atomic counter for generating unique registration IDs
    next_id: Arc<AtomicU64>,

    /// Maximum number of registrations allowed
    max_registrations: usize,
}

impl SpeakerServiceRegistry {
    /// Create a new registry with the specified maximum registrations
    pub fn new(max_registrations: usize) -> Self {
        Self {
            registrations: Arc::new(RwLock::new(HashMap::new())),
            pair_to_registration: Arc::new(RwLock::new(HashMap::new())),
            next_id: Arc::new(AtomicU64::new(1)),
            max_registrations,
        }
    }

    /// Register a speaker/service pair, returning either a new or existing registration ID
    ///
    /// If the pair is already registered, returns the existing registration ID.
    /// If the pair is new, creates a new registration ID.
    ///
    /// # Arguments
    /// * `speaker_ip` - IP address of the speaker
    /// * `service` - UPnP service type
    ///
    /// # Returns
    /// * `Ok(RegistrationId)` - The registration ID (new or existing)
    /// * `Err(RegistryError)` - If registration fails
    pub async fn register(
        &self,
        speaker_ip: IpAddr,
        service: sonos_api::Service,
    ) -> RegistryResult<RegistrationId> {
        let pair = SpeakerServicePair::new(speaker_ip, service);

        // First check if this pair is already registered (read lock)
        {
            let pair_lookup = self.pair_to_registration.read().await;
            if let Some(existing_id) = pair_lookup.get(&pair) {
                return Ok(*existing_id);
            }
        }

        // Need to register new pair (write locks)
        let mut registrations = self.registrations.write().await;
        let mut pair_lookup = self.pair_to_registration.write().await;

        // Double-check in case another task registered it between locks
        if let Some(existing_id) = pair_lookup.get(&pair) {
            return Ok(*existing_id);
        }

        // Check registration limit
        if registrations.len() >= self.max_registrations {
            return Err(RegistryError::RegistryFull {
                max_registrations: self.max_registrations,
            });
        }

        // Generate new registration ID
        let registration_id = RegistrationId::new(self.next_id.fetch_add(1, Ordering::Relaxed));

        // Insert into both mappings
        registrations.insert(registration_id, pair.clone());
        pair_lookup.insert(pair, registration_id);

        Ok(registration_id)
    }

    /// Unregister a registration ID and return the associated pair
    ///
    /// # Arguments
    /// * `registration_id` - The registration ID to unregister
    ///
    /// # Returns
    /// * `Ok(SpeakerServicePair)` - The pair that was unregistered
    /// * `Err(RegistryError::NotFound)` - If the registration ID is not found
    pub async fn unregister(
        &self,
        registration_id: RegistrationId,
    ) -> RegistryResult<SpeakerServicePair> {
        let mut registrations = self.registrations.write().await;
        let mut pair_lookup = self.pair_to_registration.write().await;

        // Remove from primary mapping
        let pair = registrations
            .remove(&registration_id)
            .ok_or(RegistryError::NotFound(registration_id))?;

        // Remove from reverse mapping
        pair_lookup.remove(&pair);

        Ok(pair)
    }

    /// Check if a speaker/service pair is already registered
    ///
    /// # Arguments
    /// * `speaker_ip` - IP address of the speaker
    /// * `service` - UPnP service type
    ///
    /// # Returns
    /// * `true` if the pair is registered, `false` otherwise
    pub async fn is_registered(&self, speaker_ip: IpAddr, service: sonos_api::Service) -> bool {
        let pair = SpeakerServicePair::new(speaker_ip, service);
        let pair_lookup = self.pair_to_registration.read().await;
        pair_lookup.contains_key(&pair)
    }

    /// Get the registration ID for a speaker/service pair
    ///
    /// # Arguments
    /// * `speaker_ip` - IP address of the speaker
    /// * `service` - UPnP service type
    ///
    /// # Returns
    /// * `Some(RegistrationId)` if the pair is registered
    /// * `None` if the pair is not registered
    pub async fn get_registration_id(
        &self,
        speaker_ip: IpAddr,
        service: sonos_api::Service,
    ) -> Option<RegistrationId> {
        let pair = SpeakerServicePair::new(speaker_ip, service);
        let pair_lookup = self.pair_to_registration.read().await;
        pair_lookup.get(&pair).copied()
    }

    /// Get the speaker/service pair for a registration ID
    ///
    /// # Arguments
    /// * `registration_id` - The registration ID to look up
    ///
    /// # Returns
    /// * `Some(SpeakerServicePair)` if the registration ID is found
    /// * `None` if the registration ID is not found
    pub async fn get_pair(&self, registration_id: RegistrationId) -> Option<SpeakerServicePair> {
        let registrations = self.registrations.read().await;
        registrations.get(&registration_id).cloned()
    }

    /// List all current registrations
    ///
    /// # Returns
    /// A vector of (RegistrationId, SpeakerServicePair) tuples
    pub async fn list_registrations(&self) -> Vec<(RegistrationId, SpeakerServicePair)> {
        let registrations = self.registrations.read().await;
        registrations
            .iter()
            .map(|(id, pair)| (*id, pair.clone()))
            .collect()
    }

    /// Get the number of current registrations
    pub async fn count(&self) -> usize {
        let registrations = self.registrations.read().await;
        registrations.len()
    }

    /// Get the maximum number of registrations allowed
    pub fn max_registrations(&self) -> usize {
        self.max_registrations
    }

    /// Clear all registrations (useful for testing or shutdown)
    pub async fn clear(&self) {
        let mut registrations = self.registrations.write().await;
        let mut pair_lookup = self.pair_to_registration.write().await;
        registrations.clear();
        pair_lookup.clear();
    }

    /// Get statistics about the registry
    pub async fn stats(&self) -> RegistryStats {
        let registrations = self.registrations.read().await;
        let count = registrations.len();

        // Count services
        let mut service_counts = HashMap::new();
        for pair in registrations.values() {
            *service_counts.entry(pair.service).or_insert(0) += 1;
        }

        RegistryStats {
            total_registrations: count,
            max_registrations: self.max_registrations,
            service_breakdown: service_counts,
        }
    }
}

/// Statistics about the registry state
#[derive(Debug, Clone)]
pub struct RegistryStats {
    pub total_registrations: usize,
    pub max_registrations: usize,
    pub service_breakdown: HashMap<sonos_api::Service, usize>,
}

impl std::fmt::Display for RegistryStats {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Registry Stats:")?;
        writeln!(
            f,
            "  Total: {}/{}",
            self.total_registrations, self.max_registrations
        )?;
        writeln!(f, "  Service breakdown:")?;
        for (service, count) in &self.service_breakdown {
            writeln!(f, "    {service:?}: {count}")?;
        }
        Ok(())
    }
}

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

    #[tokio::test]
    async fn test_registration_basic() {
        let registry = SpeakerServiceRegistry::new(100);
        let ip: IpAddr = "192.168.1.100".parse().unwrap();
        let service = sonos_api::Service::AVTransport;

        // First registration should succeed
        let reg_id = registry.register(ip, service).await.unwrap();
        assert!(registry.is_registered(ip, service).await);
        assert_eq!(registry.count().await, 1);

        // Same pair should return same ID
        let reg_id2 = registry.register(ip, service).await.unwrap();
        assert_eq!(reg_id, reg_id2);
        assert_eq!(registry.count().await, 1); // Should not increase
    }

    #[tokio::test]
    async fn test_duplicate_detection() {
        let registry = SpeakerServiceRegistry::new(100);
        let ip: IpAddr = "192.168.1.100".parse().unwrap();
        let service = sonos_api::Service::AVTransport;

        let reg_id1 = registry.register(ip, service).await.unwrap();
        let reg_id2 = registry.register(ip, service).await.unwrap();

        assert_eq!(reg_id1, reg_id2);
        assert_eq!(registry.count().await, 1);
    }

    #[tokio::test]
    async fn test_different_services() {
        let registry = SpeakerServiceRegistry::new(100);
        let ip: IpAddr = "192.168.1.100".parse().unwrap();

        let av_reg = registry
            .register(ip, sonos_api::Service::AVTransport)
            .await
            .unwrap();
        let rc_reg = registry
            .register(ip, sonos_api::Service::RenderingControl)
            .await
            .unwrap();

        assert_ne!(av_reg, rc_reg);
        assert_eq!(registry.count().await, 2);
    }

    #[tokio::test]
    async fn test_unregistration() {
        let registry = SpeakerServiceRegistry::new(100);
        let ip: IpAddr = "192.168.1.100".parse().unwrap();
        let service = sonos_api::Service::AVTransport;

        let reg_id = registry.register(ip, service).await.unwrap();
        assert_eq!(registry.count().await, 1);

        let pair = registry.unregister(reg_id).await.unwrap();
        assert_eq!(pair.speaker_ip, ip);
        assert_eq!(pair.service, service);
        assert_eq!(registry.count().await, 0);
        assert!(!registry.is_registered(ip, service).await);
    }

    #[tokio::test]
    async fn test_registration_limit() {
        let registry = SpeakerServiceRegistry::new(2);
        let ip1: IpAddr = "192.168.1.100".parse().unwrap();
        let ip2: IpAddr = "192.168.1.101".parse().unwrap();
        let ip3: IpAddr = "192.168.1.102".parse().unwrap();
        let service = sonos_api::Service::AVTransport;

        // First two should succeed
        assert!(registry.register(ip1, service).await.is_ok());
        assert!(registry.register(ip2, service).await.is_ok());

        // Third should fail
        let result = registry.register(ip3, service).await;
        assert!(matches!(result, Err(RegistryError::RegistryFull { .. })));
    }

    #[tokio::test]
    async fn test_lookups() {
        let registry = SpeakerServiceRegistry::new(100);
        let ip: IpAddr = "192.168.1.100".parse().unwrap();
        let service = sonos_api::Service::AVTransport;

        let reg_id = registry.register(ip, service).await.unwrap();

        // Test bidirectional lookups
        assert_eq!(
            registry.get_registration_id(ip, service).await,
            Some(reg_id)
        );
        assert_eq!(
            registry.get_pair(reg_id).await,
            Some(SpeakerServicePair::new(ip, service))
        );

        // Test non-existent lookups
        assert_eq!(
            registry
                .get_registration_id(ip, sonos_api::Service::RenderingControl)
                .await,
            None
        );
        assert_eq!(registry.get_pair(RegistrationId::new(999)).await, None);
    }

    #[tokio::test]
    async fn test_list_and_stats() {
        let registry = SpeakerServiceRegistry::new(100);
        let ip: IpAddr = "192.168.1.100".parse().unwrap();

        let _av_reg = registry
            .register(ip, sonos_api::Service::AVTransport)
            .await
            .unwrap();
        let _rc_reg = registry
            .register(ip, sonos_api::Service::RenderingControl)
            .await
            .unwrap();

        let registrations = registry.list_registrations().await;
        assert_eq!(registrations.len(), 2);

        let stats = registry.stats().await;
        assert_eq!(stats.total_registrations, 2);
        assert_eq!(
            stats
                .service_breakdown
                .get(&sonos_api::Service::AVTransport),
            Some(&1)
        );
        assert_eq!(
            stats
                .service_breakdown
                .get(&sonos_api::Service::RenderingControl),
            Some(&1)
        );
    }

    #[tokio::test]
    async fn test_concurrent_access() {
        let registry = Arc::new(SpeakerServiceRegistry::new(100));
        let ip: IpAddr = "192.168.1.100".parse().unwrap();
        let service = sonos_api::Service::AVTransport;

        // Simulate concurrent registration attempts
        let handles: Vec<_> = (0..10)
            .map(|_| {
                let registry = Arc::clone(&registry);
                tokio::spawn(async move { registry.register(ip, service).await })
            })
            .collect();

        let results: Vec<_> = futures::future::join_all(handles)
            .await
            .into_iter()
            .collect::<Result<Vec<_>, _>>()
            .unwrap()
            .into_iter()
            .collect::<Result<Vec<_>, _>>()
            .unwrap();

        // All should return the same registration ID
        let first_id = results[0];
        for result in &results {
            assert_eq!(*result, first_id);
        }

        // Should only have one registration
        assert_eq!(registry.count().await, 1);
    }
}