rvoip-sip 0.2.3

SIP umbrella for RVoIP: api/* (UnifiedCoordinator, StreamPeer, CallbackPeer, Endpoint), server/* (B2BUA helpers), adapter/* (rvoip-core::ConnectionAdapter impl)
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
//! Session Registry for single session mapping in rvoip-sip
//!
//! This module provides simple mappings for the single session constraint.
//! Since only one session can exist at a time, the mappings are much simpler.
//!
//! Storage uses `arc_swap::ArcSwapOption` rather than `tokio::sync::RwLock`:
//! each field holds at most one optional value, never a collection, so the
//! single-writer-many-readers RwLock model adds overhead with no benefit.
//! ArcSwap reads are wait-free atomic loads; writes are a single
//! compare-and-swap. See `crates/sip/rvoip-sip/docs/PROFILING.md` Scenario 8
//! ("SessionRegistry contention") for the side-by-side benchmark this
//! choice is grounded in.

use arc_swap::ArcSwapOption;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;

use crate::types::{DialogId, IncomingCallInfo, MediaSessionId, SessionId};

/// Registry for single session mappings.
///
/// The `async fn` signatures are preserved across the RwLock → ArcSwap
/// migration so existing callers (which uniformly `.await` every method)
/// stay source-compatible. The await points are no-ops — each method
/// resolves synchronously — but the cost of an immediately-ready future
/// is negligible compared to the per-call `RwLock::read().await` that
/// these methods used to perform.
#[derive(Clone)]
pub struct SessionRegistry {
    /// Current session ID (if any).
    current_session: Arc<ArcSwapOption<SessionId>>,
    /// Current dialog ID (if any).
    current_dialog: Arc<ArcSwapOption<DialogId>>,
    /// Current media session ID (if any).
    current_media: Arc<ArcSwapOption<MediaSessionId>>,
    /// Temporary storage for pending incoming call.
    pending_incoming_call: Arc<ArcSwapOption<IncomingCallInfo>>,
    /// SIP_API_DESIGN_2 Phase A: parsed inbound INVITE request, retained
    /// while the call is in `Ringing` so `IncomingCall::raw_request()` can
    /// surface it.
    pending_incoming_request: Arc<ArcSwapOption<rvoip_sip_core::Request>>,
    /// Transport context for the pending inbound INVITE.
    pending_incoming_transport: Arc<ArcSwapOption<crate::auth::SipTransportSecurityContext>>,

    /// Diagnostic lifecycle counters for map/remove balance.
    dialog_mapped_total: Arc<AtomicU64>,
    media_mapped_total: Arc<AtomicU64>,
    removed_total: Arc<AtomicU64>,
    remove_missing_total: Arc<AtomicU64>,
}

impl SessionRegistry {
    /// Create a new session registry.
    pub fn new() -> Self {
        Self {
            current_session: Arc::new(ArcSwapOption::empty()),
            current_dialog: Arc::new(ArcSwapOption::empty()),
            current_media: Arc::new(ArcSwapOption::empty()),
            pending_incoming_call: Arc::new(ArcSwapOption::empty()),
            pending_incoming_request: Arc::new(ArcSwapOption::empty()),
            pending_incoming_transport: Arc::new(ArcSwapOption::empty()),
            dialog_mapped_total: Arc::new(AtomicU64::new(0)),
            media_mapped_total: Arc::new(AtomicU64::new(0)),
            removed_total: Arc::new(AtomicU64::new(0)),
            remove_missing_total: Arc::new(AtomicU64::new(0)),
        }
    }

    /// Map a dialog ID to a session ID (single session version).
    pub async fn map_dialog(&self, session_id: SessionId, dialog_id: DialogId) {
        let session_id = Arc::new(session_id);
        #[cfg(feature = "perf-infra-memory-diagnostics")]
        let previous_session = self.current_session.swap(Some(session_id.clone()));
        #[cfg(not(feature = "perf-infra-memory-diagnostics"))]
        let _previous_session = self.current_session.swap(Some(session_id.clone()));
        #[cfg(feature = "perf-infra-memory-diagnostics")]
        self.record_session_mapping_for_memory_diagnostics(
            previous_session.as_deref(),
            &session_id,
        );
        self.current_dialog.store(Some(Arc::new(dialog_id)));
        self.dialog_mapped_total.fetch_add(1, Ordering::Relaxed);
    }

    /// Map a media session ID to a session ID (single session version).
    pub async fn map_media(&self, session_id: SessionId, media_id: MediaSessionId) {
        let session_id = Arc::new(session_id);
        #[cfg(feature = "perf-infra-memory-diagnostics")]
        let previous_session = self.current_session.swap(Some(session_id.clone()));
        #[cfg(not(feature = "perf-infra-memory-diagnostics"))]
        let _previous_session = self.current_session.swap(Some(session_id.clone()));
        #[cfg(feature = "perf-infra-memory-diagnostics")]
        self.record_session_mapping_for_memory_diagnostics(
            previous_session.as_deref(),
            &session_id,
        );
        self.current_media.store(Some(Arc::new(media_id)));
        self.media_mapped_total.fetch_add(1, Ordering::Relaxed);
    }

    /// Get session ID by dialog ID (single session version).
    pub async fn get_session_by_dialog(&self, dialog_id: &DialogId) -> Option<SessionId> {
        if self.current_dialog.load().as_deref() == Some(dialog_id) {
            self.current_session.load().as_deref().cloned()
        } else {
            None
        }
    }

    /// Get session ID by media session ID (single session version).
    pub async fn get_session_by_media(&self, media_id: &MediaSessionId) -> Option<SessionId> {
        if self.current_media.load().as_deref() == Some(media_id) {
            self.current_session.load().as_deref().cloned()
        } else {
            None
        }
    }

    /// Get dialog ID by session ID (single session version).
    pub async fn get_dialog_by_session(&self, session_id: &SessionId) -> Option<DialogId> {
        if self.current_session.load().as_deref() == Some(session_id) {
            self.current_dialog.load().as_deref().cloned()
        } else {
            None
        }
    }

    /// Get media session ID by session ID (single session version).
    pub async fn get_media_by_session(&self, session_id: &SessionId) -> Option<MediaSessionId> {
        if self.current_session.load().as_deref() == Some(session_id) {
            self.current_media.load().as_deref().cloned()
        } else {
            None
        }
    }

    /// Remove all mappings for a session (single session version).
    pub async fn remove_session(&self, session_id: &SessionId) {
        loop {
            let current = self.current_session.load();
            if current.as_deref() != Some(session_id) {
                self.remove_missing_total.fetch_add(1, Ordering::Relaxed);
                return;
            }

            let previous = self.current_session.compare_and_swap(&*current, None);
            if Self::option_arc_ptr_eq(&current, &previous) {
                self.current_dialog.store(None);
                self.current_media.store(None);
                self.removed_total.fetch_add(1, Ordering::Relaxed);
                #[cfg(feature = "perf-infra-memory-diagnostics")]
                rvoip_infra_common::memory_diagnostics::record_dropped(
                    "sip.session_registry.current_session",
                    std::mem::size_of::<SessionId>(),
                );
                return;
            }
        }
    }

    /// Feature-gated lifecycle counters for perf leak investigations.
    #[cfg(feature = "perf-tests")]
    pub(crate) fn perf_lifecycle_counts(&self) -> serde_json::Value {
        serde_json::json!({
            "dialog_mapped_total": self.dialog_mapped_total.load(Ordering::Relaxed),
            "media_mapped_total": self.media_mapped_total.load(Ordering::Relaxed),
            "removed_total": self.removed_total.load(Ordering::Relaxed),
            "remove_missing_total": self.remove_missing_total.load(Ordering::Relaxed),
        })
    }

    /// Check if a session exists in the registry (single session version).
    pub async fn contains_session(&self, session_id: &SessionId) -> bool {
        self.current_session.load().as_deref() == Some(session_id)
    }

    /// Get the total number of sessions in the registry (0 or 1).
    pub async fn session_count(&self) -> usize {
        usize::from(self.current_session.load().is_some())
    }

    /// Clear all mappings (single session version).
    pub async fn clear(&self) {
        #[cfg(feature = "perf-infra-memory-diagnostics")]
        let previous_session = self.current_session.swap(None);
        #[cfg(not(feature = "perf-infra-memory-diagnostics"))]
        let _previous_session = self.current_session.swap(None);
        #[cfg(feature = "perf-infra-memory-diagnostics")]
        if previous_session.is_some() {
            rvoip_infra_common::memory_diagnostics::record_dropped(
                "sip.session_registry.current_session",
                std::mem::size_of::<SessionId>(),
            );
        }
        self.current_dialog.store(None);
        self.current_media.store(None);
        self.pending_incoming_call.store(None);
        self.pending_incoming_request.store(None);
        self.pending_incoming_transport.store(None);
    }

    #[cfg(feature = "perf-infra-memory-diagnostics")]
    fn record_session_mapping_for_memory_diagnostics(
        &self,
        previous: Option<&SessionId>,
        session_id: &SessionId,
    ) {
        if previous == Some(session_id) {
            return;
        }
        if previous.is_some() {
            rvoip_infra_common::memory_diagnostics::record_dropped(
                "sip.session_registry.current_session",
                std::mem::size_of::<SessionId>(),
            );
        }
        rvoip_infra_common::memory_diagnostics::record_created(
            "sip.session_registry.current_session",
            std::mem::size_of::<SessionId>(),
        );
    }

    fn option_arc_ptr_eq<T>(
        left: &Option<Arc<T>>,
        right: &arc_swap::Guard<Option<Arc<T>>>,
    ) -> bool {
        match (left.as_ref(), right.as_ref()) {
            (Some(left), Some(right)) => Arc::ptr_eq(left, right),
            (None, None) => true,
            _ => false,
        }
    }

    /// Store pending incoming call info (single session version).
    pub async fn store_pending_incoming_call(
        &self,
        _session_id: SessionId,
        info: IncomingCallInfo,
    ) {
        self.pending_incoming_call.store(Some(Arc::new(info)));
    }

    /// Get and remove pending incoming call info (single session version).
    pub async fn take_pending_incoming_call(
        &self,
        _session_id: &SessionId,
    ) -> Option<IncomingCallInfo> {
        self.pending_incoming_call
            .swap(None)
            .map(|arc| (*arc).clone())
    }

    /// SIP_API_DESIGN_2 Phase A: store the parsed inbound INVITE so
    /// `IncomingCall::raw_request()` can surface it. The companion
    /// take/peek accessors are used by the four API surfaces when
    /// constructing the user-facing `IncomingCall`.
    pub async fn store_pending_incoming_request(&self, request: Arc<rvoip_sip_core::Request>) {
        self.pending_incoming_request.store(Some(request));
    }

    /// Store the transport context for the pending inbound INVITE.
    pub async fn store_pending_incoming_transport(
        &self,
        transport: crate::auth::SipTransportSecurityContext,
    ) {
        self.pending_incoming_transport
            .store(Some(Arc::new(transport)));
    }

    /// Peek at the parsed inbound INVITE without consuming it. Used
    /// when multiple surfaces (StreamPeer, CallbackPeer event stream,
    /// Endpoint) may build their own `IncomingCall` view of the same
    /// inbound call.
    pub async fn peek_pending_incoming_request(&self) -> Option<Arc<rvoip_sip_core::Request>> {
        self.pending_incoming_request.load_full()
    }

    /// Peek at the transport context for the pending inbound INVITE.
    pub async fn peek_pending_incoming_transport(
        &self,
    ) -> Option<Arc<crate::auth::SipTransportSecurityContext>> {
        self.pending_incoming_transport.load_full()
    }

    /// Consume the parsed inbound INVITE once an
    /// `IncomingCall::accept()` / `reject()` / `defer()` resolves the
    /// call. Idempotent — repeated calls return `None`.
    pub async fn take_pending_incoming_request(&self) -> Option<Arc<rvoip_sip_core::Request>> {
        self.pending_incoming_request.swap(None)
    }

    /// Consume the pending inbound INVITE transport context.
    pub async fn take_pending_incoming_transport(
        &self,
    ) -> Option<Arc<crate::auth::SipTransportSecurityContext>> {
        self.pending_incoming_transport.swap(None)
    }
}

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

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

    #[tokio::test]
    async fn test_dialog_mapping() {
        let registry = SessionRegistry::new();
        let session_id = SessionId::new();
        let dialog_id = DialogId::new();

        registry
            .map_dialog(session_id.clone(), dialog_id.clone())
            .await;

        assert_eq!(
            registry.get_session_by_dialog(&dialog_id).await,
            Some(session_id.clone())
        );
        assert_eq!(
            registry.get_dialog_by_session(&session_id).await,
            Some(dialog_id)
        );
    }

    #[tokio::test]
    async fn test_media_mapping() {
        let registry = SessionRegistry::new();
        let session_id = SessionId::new();
        let media_id = MediaSessionId::new_v4();

        registry
            .map_media(session_id.clone(), media_id.clone())
            .await;

        assert_eq!(
            registry.get_session_by_media(&media_id).await,
            Some(session_id.clone())
        );
        assert_eq!(
            registry.get_media_by_session(&session_id).await,
            Some(media_id)
        );
    }

    #[tokio::test]
    async fn test_remove_session() {
        let registry = SessionRegistry::new();
        let session_id = SessionId::new();
        let dialog_id = DialogId::new();
        let media_id = MediaSessionId::new_v4();

        registry
            .map_dialog(session_id.clone(), dialog_id.clone())
            .await;
        registry
            .map_media(session_id.clone(), media_id.clone())
            .await;

        assert!(registry.contains_session(&session_id).await);

        registry.remove_session(&session_id).await;

        assert!(!registry.contains_session(&session_id).await);
        assert_eq!(registry.get_session_by_dialog(&dialog_id).await, None);
        assert_eq!(registry.get_session_by_media(&media_id).await, None);
    }

    #[tokio::test]
    async fn test_session_count() {
        let registry = SessionRegistry::new();

        assert_eq!(registry.session_count().await, 0);

        let session1 = SessionId::new();
        let session2 = SessionId::new();

        registry.map_dialog(session1.clone(), DialogId::new()).await;
        registry.map_dialog(session2.clone(), DialogId::new()).await;

        // Single-session registry: second map_dialog overwrites the first,
        // so count is 1, not 2.
        assert_eq!(registry.session_count().await, 1);
    }

    #[tokio::test]
    async fn test_clear() {
        let registry = SessionRegistry::new();
        let session_id = SessionId::new();

        registry
            .map_dialog(session_id.clone(), DialogId::new())
            .await;
        registry
            .map_media(session_id.clone(), MediaSessionId::new_v4())
            .await;

        registry.clear().await;

        assert_eq!(registry.session_count().await, 0);
        assert!(!registry.contains_session(&session_id).await);
    }
}