asterisk-rs-ami 0.8.0

Async Rust client for the Asterisk Manager Interface (AMI)
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
//! AMI response types and ActionID correlation

use crate::codec::{RawAmiMessage, RedactedHeaderMap};
use crate::error::{AmiError, Result};
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicU8, Ordering};
use tokio::time::Instant;

const REQUEST_QUEUED: u8 = 0;
const REQUEST_WRITING: u8 = 1;
const REQUEST_SENT: u8 = 2;
const REQUEST_COMPLETED: u8 = 3;
const REQUEST_CANCELLED: u8 = 4;

/// coordinates cancellation between a caller and the connection actor
#[derive(Debug, Default)]
pub(crate) struct RequestLifecycle(AtomicU8);

impl RequestLifecycle {
    /// claim the request for its first wire write
    pub(crate) fn begin_write(&self) -> bool {
        self.0
            .compare_exchange(
                REQUEST_QUEUED,
                REQUEST_WRITING,
                Ordering::AcqRel,
                Ordering::Acquire,
            )
            .is_ok()
    }

    /// cancel a request that has not started writing
    pub(crate) fn cancel_queued(&self) -> bool {
        self.0
            .compare_exchange(
                REQUEST_QUEUED,
                REQUEST_CANCELLED,
                Ordering::AcqRel,
                Ordering::Acquire,
            )
            .is_ok()
    }

    pub(crate) fn mark_sent(&self) {
        self.0.store(REQUEST_SENT, Ordering::Release);
    }

    pub(crate) fn mark_completed(&self) {
        self.0.store(REQUEST_COMPLETED, Ordering::Release);
    }

    pub(crate) fn may_have_executed(&self) -> bool {
        matches!(
            self.0.load(Ordering::Acquire),
            REQUEST_WRITING | REQUEST_SENT | REQUEST_COMPLETED
        )
    }
}

struct ManagedRequest<T> {
    deadline: Instant,
    lifecycle: Arc<RequestLifecycle>,
    tx: tokio::sync::oneshot::Sender<Result<T>>,
}

enum ResponseSender {
    Public(tokio::sync::oneshot::Sender<AmiResponse>),
    Managed(ManagedRequest<AmiResponse>),
}

/// parsed AMI response
#[derive(Clone, PartialEq)]
pub struct AmiResponse {
    /// the ActionID this response corresponds to
    pub action_id: String,
    /// whether the action succeeded
    pub success: bool,
    /// the Response header value ("Success", "Error", "Follows")
    pub response_type: String,
    /// the Message header, if present
    pub message: Option<String>,
    /// all headers as a map
    pub headers: HashMap<String, String>,
    /// command output lines (populated for Response: Follows)
    pub output: Vec<String>,
    /// channel variables extracted from ChanVariable(name) headers
    pub channel_variables: HashMap<String, String>,
}

impl std::fmt::Debug for AmiResponse {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AmiResponse")
            .field("action_id", &self.action_id)
            .field("success", &self.success)
            .field("response_type", &self.response_type)
            .field("message", &self.message)
            .field("headers", &RedactedHeaderMap(&self.headers))
            .field("output_lines", &self.output.len())
            .field(
                "output_bytes",
                &self.output.iter().map(String::len).sum::<usize>(),
            )
            .field(
                "channel_variables",
                &RedactedHeaderMap(&self.channel_variables),
            )
            .finish()
    }
}

impl AmiResponse {
    /// parse a response from a raw AMI message
    ///
    /// returns `None` for non-response messages (e.g., events)
    pub fn from_raw(raw: &RawAmiMessage) -> Option<Self> {
        // messages with both Event: and Response: headers are events
        // (e.g. OriginateResponse carries Response: Success/Failure
        // but is an event, not an action response)
        if raw.get("Event").is_some() {
            return None;
        }
        let response_type = raw.get("Response")?.to_string();
        // action ID may be absent for unsolicited responses
        let action_id = raw.get("ActionID").unwrap_or("").to_string();
        let success = response_type.eq_ignore_ascii_case("success")
            || response_type.eq_ignore_ascii_case("follows");
        let message = raw.get("Message").map(String::from);
        let headers = raw.to_map();

        Some(Self {
            action_id,
            success,
            response_type,
            message,
            headers,
            output: raw.output.clone(),
            channel_variables: raw.channel_variables.clone(),
        })
    }

    /// get a header value from the response
    pub fn get(&self, key: &str) -> Option<&str> {
        self.headers
            .iter()
            .find(|(k, _)| k.eq_ignore_ascii_case(key))
            .map(|(_, v)| v.as_str())
    }

    /// get a channel variable by name
    pub fn get_variable(&self, name: &str) -> Option<&str> {
        self.channel_variables.get(name).map(|s| s.as_str())
    }

    fn retained_size(&self) -> usize {
        self.headers
            .iter()
            .map(|(key, value)| key.len() + value.len())
            .sum::<usize>()
            + self.output.iter().map(String::len).sum::<usize>()
            + self
                .channel_variables
                .iter()
                .map(|(name, value)| name.len() + value.len())
                .sum::<usize>()
    }
}

/// response from an event-generating action (e.g., Status, QueueStatus)
///
/// contains the initial response plus all events received until the
/// completion marker event
#[derive(Debug, Clone)]
pub struct EventListResponse {
    /// the initial response to the action
    pub response: AmiResponse,
    /// events received as part of this action's result
    pub events: Vec<crate::event::AmiEvent>,
}

/// maximum events allowed in a single event list before it is dropped
pub const MAX_EVENT_LIST_EVENTS: usize = 10_000;
/// maximum retained payload across one event-generating action (4 MiB)
pub const MAX_EVENT_LIST_BYTES: usize = 4 * 1024 * 1024;
/// maximum event-generating actions awaiting completion on one connection
pub const MAX_IN_FLIGHT_EVENT_LISTS: usize = 16;
/// maximum retained payload across all event-generating actions on one connection (8 MiB)
pub const MAX_CONNECTION_EVENT_LIST_BYTES: usize = 8 * 1024 * 1024;
/// maximum actions awaiting responses on one AMI connection
pub const MAX_IN_FLIGHT_ACTIONS: usize = 256;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum EventListTerminal {
    Continue,
    Complete,
    Cancelled,
}

/// tracks a pending event-generating action
struct PendingEventList {
    response: Option<AmiResponse>,
    events: Vec<crate::event::AmiEvent>,
    retained_bytes: usize,
    tx: EventListSender,
}

enum EventListSender {
    Public(tokio::sync::oneshot::Sender<EventListResponse>),
    Managed(ManagedRequest<EventListResponse>),
}

/// pending action tracker — correlates ActionIDs with response channels
pub struct PendingActions {
    pending: HashMap<String, ResponseSender>,
    pending_event_lists: HashMap<String, PendingEventList>,
    retained_event_list_bytes: usize,
}

impl PendingActions {
    pub fn new() -> Self {
        Self {
            pending: HashMap::new(),
            pending_event_lists: HashMap::new(),
            retained_event_list_bytes: 0,
        }
    }

    /// register a pending action, returns a receiver for the response
    pub fn register(&mut self, action_id: String) -> tokio::sync::oneshot::Receiver<AmiResponse> {
        let (tx, rx) = tokio::sync::oneshot::channel();
        self.pending.insert(action_id, ResponseSender::Public(tx));
        rx
    }

    /// deliver a response to the waiting caller
    ///
    /// returns true if the response was delivered, false if no one was waiting
    pub fn deliver(&mut self, response: AmiResponse) -> bool {
        match self.pending.remove(&response.action_id) {
            Some(ResponseSender::Public(tx)) => {
                // send can fail if the receiver was dropped, which is fine
                tx.send(response).is_ok()
            }
            Some(ResponseSender::Managed(request)) => {
                let delivered = request.tx.send(Ok(response)).is_ok();
                request.lifecycle.mark_completed();
                delivered
            }
            None => false,
        }
    }

    /// number of actions waiting for responses
    pub fn pending_count(&self) -> usize {
        self.pending.len() + self.pending_event_lists.len()
    }

    pub(crate) fn at_in_flight_limit(&self) -> bool {
        self.pending_count() >= MAX_IN_FLIGHT_ACTIONS
    }

    pub(crate) fn at_event_list_limit(&self) -> bool {
        self.pending_event_lists.len() >= MAX_IN_FLIGHT_EVENT_LISTS
    }

    /// cancel all pending actions (e.g., on disconnect)
    ///
    /// drops all senders, causing receivers to get `RecvError::Closed`
    pub fn cancel_all(&mut self) {
        self.pending.clear();
        self.pending_event_lists.clear();
        self.retained_event_list_bytes = 0;
    }

    /// register with a pre-existing sender (used by connection manager)
    pub fn register_with_sender(
        &mut self,
        action_id: String,
        tx: tokio::sync::oneshot::Sender<AmiResponse>,
    ) {
        self.pending.insert(action_id, ResponseSender::Public(tx));
    }

    /// register a pending event-generating action
    pub fn register_event_list(
        &mut self,
        action_id: String,
        tx: tokio::sync::oneshot::Sender<EventListResponse>,
    ) {
        self.remove_event_list(&action_id);
        self.pending_event_lists.insert(
            action_id,
            PendingEventList {
                response: None,
                events: Vec::new(),
                retained_bytes: 0,
                tx: EventListSender::Public(tx),
            },
        );
    }

    /// check whether an action_id has a pending event list
    pub fn contains_event_list(&self, action_id: &str) -> bool {
        self.pending_event_lists.contains_key(action_id)
    }

    /// deliver the initial response for an event-generating action
    ///
    /// returns true if this action_id has a pending event list
    pub fn deliver_event_list_response(&mut self, response: AmiResponse) -> bool {
        let retained_bytes = response.retained_size();
        self.deliver_event_list_response_with_size(response, retained_bytes)
    }

    pub(crate) fn deliver_event_list_response_with_size(
        &mut self,
        response: AmiResponse,
        retained_bytes: usize,
    ) -> bool {
        let action_id = response.action_id.clone();
        let Some(pending) = self.pending_event_lists.get_mut(&action_id) else {
            return false;
        };

        if !response.success {
            let Some(pending) = self.remove_event_list(&action_id) else {
                return false;
            };
            finish_event_list(
                pending,
                EventListResponse {
                    response,
                    events: Vec::new(),
                },
            );
            return true;
        }

        if pending.response.is_some() {
            return true;
        }
        if let Err(error) = self.reserve_event_list_bytes(&action_id, retained_bytes) {
            let Some(pending) = self.remove_event_list(&action_id) else {
                return false;
            };
            fail_event_list(pending, error);
            return true;
        }
        self.pending_event_lists
            .get_mut(&action_id)
            .expect("event list exists after reserving its response")
            .response = Some(response);
        true
    }

    /// deliver an event for an event-generating action
    ///
    /// completion is detected via the `EventList: Complete` header that
    /// Asterisk sends on `*Complete` events, rather than matching on the
    /// event name suffix (which could false-positive on user events like
    /// `ProcessComplete`).
    ///
    /// returns true if this event was consumed by a pending event list
    pub fn deliver_event_list_event(
        &mut self,
        action_id: &str,
        event: crate::event::AmiEvent,
    ) -> bool {
        let terminal = event_list_terminal(&event);
        self.deliver_event_list_event_with_metadata(action_id, event, 0, terminal)
    }

    pub(crate) fn deliver_event_list_event_with_metadata(
        &mut self,
        action_id: &str,
        event: crate::event::AmiEvent,
        retained_bytes: usize,
        terminal: EventListTerminal,
    ) -> bool {
        let Some(pending) = self.pending_event_lists.get_mut(action_id) else {
            return false;
        };

        if terminal == EventListTerminal::Cancelled {
            let Some(pending) = self.remove_event_list(action_id) else {
                return false;
            };
            fail_event_list(
                pending,
                AmiError::EventListCancelled {
                    action_id: action_id.to_owned(),
                },
            );
            return true;
        }

        if pending.events.len() >= MAX_EVENT_LIST_EVENTS {
            let Some(pending) = self.remove_event_list(action_id) else {
                return false;
            };
            fail_event_list(
                pending,
                AmiError::EventListEventLimitExceeded {
                    action_id: action_id.to_owned(),
                    limit: MAX_EVENT_LIST_EVENTS,
                },
            );
            return true;
        }

        if let Err(error) = self.reserve_event_list_bytes(action_id, retained_bytes) {
            let Some(pending) = self.remove_event_list(action_id) else {
                return false;
            };
            fail_event_list(pending, error);
            return true;
        }

        self.pending_event_lists
            .get_mut(action_id)
            .expect("event list exists after reserving its event")
            .events
            .push(event);
        if terminal == EventListTerminal::Complete {
            let Some(pending) = self.remove_event_list(action_id) else {
                return false;
            };
            let mut pending = pending;
            let response = pending.response.take().unwrap_or_else(|| {
                tracing::warn!(action_id, "event list Complete arrived before Response");
                AmiResponse {
                    action_id: action_id.to_owned(),
                    success: false,
                    response_type: String::new(),
                    message: Some("event list completed before response received".into()),
                    headers: HashMap::new(),
                    output: Vec::new(),
                    channel_variables: HashMap::new(),
                }
            });
            let events = std::mem::take(&mut pending.events);
            finish_event_list(pending, EventListResponse { response, events });
            true
        } else {
            true
        }
    }

    pub(crate) fn register_managed(
        &mut self,
        action_id: String,
        deadline: Instant,
        lifecycle: Arc<RequestLifecycle>,
        tx: tokio::sync::oneshot::Sender<Result<AmiResponse>>,
    ) {
        self.pending.insert(
            action_id,
            ResponseSender::Managed(ManagedRequest {
                deadline,
                lifecycle,
                tx,
            }),
        );
    }

    pub(crate) fn register_managed_event_list(
        &mut self,
        action_id: String,
        deadline: Instant,
        lifecycle: Arc<RequestLifecycle>,
        tx: tokio::sync::oneshot::Sender<Result<EventListResponse>>,
    ) {
        self.remove_event_list(&action_id);
        self.pending_event_lists.insert(
            action_id,
            PendingEventList {
                response: None,
                events: Vec::new(),
                retained_bytes: 0,
                tx: EventListSender::Managed(ManagedRequest {
                    deadline,
                    lifecycle,
                    tx,
                }),
            },
        );
    }

    /// discard requests whose callers have gone away
    pub fn purge_closed(&mut self) {
        self.pending.retain(|_, sender| match sender {
            ResponseSender::Public(tx) => !tx.is_closed(),
            ResponseSender::Managed(request) => !request.tx.is_closed(),
        });
        let closed_event_lists: Vec<_> = self
            .pending_event_lists
            .iter()
            .filter_map(|(action_id, pending)| match &pending.tx {
                EventListSender::Public(tx) if tx.is_closed() => Some(action_id.clone()),
                EventListSender::Managed(request) if request.tx.is_closed() => {
                    Some(action_id.clone())
                }
                _ => None,
            })
            .collect();
        for action_id in closed_event_lists {
            self.remove_event_list(&action_id);
        }
    }

    pub(crate) fn next_deadline(&self) -> Option<Instant> {
        self.pending
            .values()
            .filter_map(|sender| match sender {
                ResponseSender::Public(_) => None,
                ResponseSender::Managed(request) => Some(request.deadline),
            })
            .chain(
                self.pending_event_lists
                    .values()
                    .filter_map(|pending| match &pending.tx {
                        EventListSender::Public(_) => None,
                        EventListSender::Managed(request) => Some(request.deadline),
                    }),
            )
            .min()
    }

    /// expire sent requests whose response deadline elapsed
    pub(crate) fn expire(&mut self, now: Instant) {
        self.purge_closed();

        let expired_actions: Vec<_> = self
            .pending
            .iter()
            .filter_map(|(action_id, sender)| match sender {
                ResponseSender::Managed(request) if request.deadline <= now => {
                    Some(action_id.clone())
                }
                _ => None,
            })
            .collect();
        for action_id in expired_actions {
            let Some(ResponseSender::Managed(request)) = self.pending.remove(&action_id) else {
                continue;
            };
            let _ = request.tx.send(Err(AmiError::OutcomeUnknown {
                action_id: action_id.clone(),
            }));
            request.lifecycle.mark_completed();
        }

        let expired_event_lists: Vec<_> = self
            .pending_event_lists
            .iter()
            .filter_map(|(action_id, pending)| match &pending.tx {
                EventListSender::Managed(request) if request.deadline <= now => {
                    Some(action_id.clone())
                }
                _ => None,
            })
            .collect();
        for action_id in expired_event_lists {
            let Some(pending) = self.remove_event_list(&action_id) else {
                continue;
            };
            let EventListSender::Managed(request) = pending.tx else {
                continue;
            };
            let _ = request.tx.send(Err(AmiError::OutcomeUnknown {
                action_id: action_id.clone(),
            }));
            request.lifecycle.mark_completed();
        }
    }

    /// fail all wire-started requests because the connection was lost
    pub(crate) fn fail_all_unknown(&mut self) {
        for (action_id, sender) in self.pending.drain() {
            if let ResponseSender::Managed(request) = sender {
                let _ = request.tx.send(Err(AmiError::OutcomeUnknown { action_id }));
                request.lifecycle.mark_completed();
            }
        }
        for (action_id, pending) in self.pending_event_lists.drain() {
            if let EventListSender::Managed(request) = pending.tx {
                let _ = request.tx.send(Err(AmiError::OutcomeUnknown { action_id }));
                request.lifecycle.mark_completed();
            }
        }
        self.retained_event_list_bytes = 0;
    }

    fn reserve_event_list_bytes(&mut self, action_id: &str, additional: usize) -> Result<()> {
        let retained_bytes = self
            .pending_event_lists
            .get(action_id)
            .expect("cannot reserve bytes for an unknown event list")
            .retained_bytes;
        let collector_total = retained_bytes.saturating_add(additional);
        if collector_total > MAX_EVENT_LIST_BYTES {
            return Err(AmiError::EventListByteLimitExceeded {
                action_id: action_id.to_owned(),
                limit: MAX_EVENT_LIST_BYTES,
            });
        }

        let connection_total = self.retained_event_list_bytes.saturating_add(additional);
        if connection_total > MAX_CONNECTION_EVENT_LIST_BYTES {
            return Err(AmiError::EventListConnectionByteLimitExceeded {
                action_id: action_id.to_owned(),
                limit: MAX_CONNECTION_EVENT_LIST_BYTES,
            });
        }

        self.retained_event_list_bytes = connection_total;
        self.pending_event_lists
            .get_mut(action_id)
            .expect("event list exists while reserving bytes")
            .retained_bytes = collector_total;
        Ok(())
    }

    fn remove_event_list(&mut self, action_id: &str) -> Option<PendingEventList> {
        let pending = self.pending_event_lists.remove(action_id)?;
        self.retained_event_list_bytes = self
            .retained_event_list_bytes
            .checked_sub(pending.retained_bytes)
            .expect("event-list retained-byte accounting must not underflow");
        Some(pending)
    }
}

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

fn finish_event_list(pending: PendingEventList, result: EventListResponse) {
    match pending.tx {
        EventListSender::Public(tx) => {
            let _ = tx.send(result);
        }
        EventListSender::Managed(request) => {
            let _ = request.tx.send(Ok(result));
            request.lifecycle.mark_completed();
        }
    }
}

fn fail_event_list(pending: PendingEventList, error: AmiError) {
    if let EventListSender::Managed(request) = pending.tx {
        let _ = request.tx.send(Err(error));
        request.lifecycle.mark_completed();
    }
}

fn event_list_terminal(event: &crate::event::AmiEvent) -> EventListTerminal {
    if event.is_event_list_complete() {
        return EventListTerminal::Complete;
    }
    let cancelled = match event {
        crate::event::AmiEvent::Malformed { headers, .. } => headers.iter().any(|(key, value)| {
            key.eq_ignore_ascii_case("EventList") && value.eq_ignore_ascii_case("Cancelled")
        }),
        crate::event::AmiEvent::Unknown { headers, .. } => headers.iter().any(|(key, value)| {
            key.eq_ignore_ascii_case("EventList") && value.eq_ignore_ascii_case("Cancelled")
        }),
        _ => false,
    };
    if cancelled {
        return EventListTerminal::Cancelled;
    }
    EventListTerminal::Continue
}