use std::collections::BTreeMap;
const MAX_CLIENTS: usize = 64;
const MAX_REQUESTS: usize = 256;
const REQUEST_TTL_MS: u64 = 300_000;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AccountAuthority {
pub account_id: String,
pub generation: String,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Phase {
Ringing,
Dialing,
Answering,
Connected,
Ending,
Uncertain,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Operation {
Dial,
Answer,
Reject,
Cancel,
Hangup,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CallView {
pub call_id: String,
pub revision: u64,
pub phase: Phase,
pub owned: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Snapshot {
pub busy: bool,
pub current: Option<CallView>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Error {
Invalid,
Capacity,
Busy,
NotConnected,
NotReady,
Stale,
NotOwner,
Conflict,
InvalidTransition,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CallIdentity {
attempt: u64,
call_id: String,
}
impl CallIdentity {
pub fn call_id(&self) -> &str {
&self.call_id
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Ticket {
attempt: u64,
authority: AccountAuthority,
call_id: String,
revision: u64,
operation: Operation,
}
impl Ticket {
pub fn identity(&self) -> CallIdentity {
CallIdentity {
attempt: self.attempt,
call_id: self.call_id.clone(),
}
}
pub fn authority(&self) -> &AccountAuthority {
&self.authority
}
pub fn call_id(&self) -> &str {
&self.call_id
}
pub fn operation(&self) -> Operation {
self.operation
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Decision {
Submit(Ticket),
Replay(Ticket),
}
#[derive(Clone)]
struct Client {
logical_id: u64,
authority: AccountAuthority,
principal: String,
ready: bool,
}
struct Session {
attempt: u64,
authority: AccountAuthority,
id: String,
revision: u64,
phase: Phase,
owner: Option<String>,
owner_principal: Option<String>,
owner_logical_id: Option<u64>,
owner_closed_at: Option<u64>,
offer_until: Option<u64>,
pending: Option<Ticket>,
invalidated: bool,
}
#[derive(Clone, PartialEq, Eq)]
struct Intent {
operation: Operation,
call_id: String,
expected_revision: Option<u64>,
parameters: String,
}
struct Receipt {
intent: Intent,
ticket: Ticket,
expires: u64,
}
#[derive(Default)]
pub struct Ownership {
next_attempt: u64,
next_client: u64,
clients: BTreeMap<String, Client>,
session: Option<Session>,
requests: BTreeMap<(u64, String), Receipt>,
}
fn valid_id(s: &str) -> bool {
!s.is_empty() && s.len() <= 200 && !s.chars().any(char::is_control)
}
impl Ownership {
pub fn connect(
&mut self,
client_id: &str,
authority: AccountAuthority,
principal: &str,
) -> Result<Snapshot, Error> {
if !valid_id(client_id)
|| !valid_id(&authority.account_id)
|| !valid_id(&authority.generation)
|| !valid_id(principal)
|| self.clients.contains_key(client_id)
{
return Err(Error::Invalid);
}
if self.clients.len() >= MAX_CLIENTS {
return Err(Error::Capacity);
}
let logical_id = self.next_client.checked_add(1).ok_or(Error::Capacity)?;
self.next_client = logical_id;
self.clients.insert(
client_id.into(),
Client {
logical_id,
authority,
principal: principal.into(),
ready: false,
},
);
self.snapshot(client_id)
}
pub fn ready(&mut self, client_id: &str, ready: bool) -> Result<(), Error> {
self.clients
.get_mut(client_id)
.ok_or(Error::NotConnected)?
.ready = ready;
Ok(())
}
pub fn snapshot(&self, client_id: &str) -> Result<Snapshot, Error> {
let client = self.clients.get(client_id).ok_or(Error::NotConnected)?;
let current = self.session.as_ref().and_then(|s| {
let owned = s.owner.as_deref() == Some(client_id)
&& s.owner_logical_id == Some(client.logical_id)
&& s.owner_principal.as_deref() == Some(client.principal.as_str());
(s.authority == client.authority
&& (owned || (s.phase == Phase::Ringing && s.owner.is_none())))
.then(|| CallView {
call_id: s.id.clone(),
revision: s.revision,
phase: s.phase,
owned,
})
});
Ok(Snapshot {
busy: self.session.is_some(),
current,
})
}
pub fn offer(
&mut self,
authority: AccountAuthority,
call_id: &str,
expires: u64,
now: u64,
) -> Result<CallIdentity, Error> {
if !valid_id(call_id)
|| !valid_id(&authority.account_id)
|| !valid_id(&authority.generation)
|| expires <= now
{
return Err(Error::Invalid);
}
if self.session.is_some() {
return Err(Error::Busy);
}
let attempt = self.next_attempt.checked_add(1).ok_or(Error::Capacity)?;
self.next_attempt = attempt;
self.session = Some(Session {
attempt,
authority,
id: call_id.into(),
revision: 1,
phase: Phase::Ringing,
owner: None,
owner_principal: None,
owner_logical_id: None,
owner_closed_at: None,
offer_until: Some(expires),
pending: None,
invalidated: false,
});
Ok(CallIdentity {
attempt,
call_id: call_id.into(),
})
}
pub fn offer_revision(
&mut self,
authority: &AccountAuthority,
identity: &CallIdentity,
revision: u64,
) -> Result<(), Error> {
let session = self.session.as_mut().ok_or(Error::Stale)?;
if &session.authority != authority
|| session.attempt != identity.attempt
|| session.id != identity.call_id
|| session.invalidated
|| session.owner.is_some()
|| session.phase != Phase::Ringing
|| revision <= session.revision
{
return Err(Error::Stale);
}
session.revision = revision;
Ok(())
}
pub fn pending_start(
&self,
authority: &AccountAuthority,
identity: &CallIdentity,
) -> Option<Operation> {
let session = self.session.as_ref()?;
if &session.authority != authority
|| session.attempt != identity.attempt
|| session.id != identity.call_id
|| session.invalidated
{
return None;
}
session
.pending
.as_ref()
.map(|ticket| ticket.operation)
.filter(|operation| matches!(operation, Operation::Dial | Operation::Answer))
}
pub fn pending_terminal_operation(
&self,
authority: &AccountAuthority,
identity: &CallIdentity,
) -> Option<Operation> {
let session = self.session.as_ref().filter(|session| {
&session.authority == authority
&& session.attempt == identity.attempt
&& session.id == identity.call_id
&& !session.invalidated
&& session.phase == Phase::Ending
})?;
session
.pending
.as_ref()
.map(|ticket| ticket.operation)
.filter(|operation| {
matches!(
operation,
Operation::Reject | Operation::Cancel | Operation::Hangup
)
})
}
pub fn pending_end(&self, authority: &AccountAuthority, identity: &CallIdentity) -> bool {
matches!(
self.pending_terminal_operation(authority, identity),
Some(Operation::Cancel | Operation::Hangup)
)
}
pub fn dial(
&mut self,
client_id: &str,
request_id: &str,
new_call_id: &str,
parameters: &str,
now: u64,
) -> Result<Decision, Error> {
self.command(
client_id,
request_id,
Intent {
operation: Operation::Dial,
call_id: new_call_id.into(),
expected_revision: None,
parameters: parameters.into(),
},
now,
)
}
pub fn act(
&mut self,
client_id: &str,
request_id: &str,
call_id: &str,
revision: u64,
operation: Operation,
now: u64,
) -> Result<Decision, Error> {
if operation == Operation::Dial {
return Err(Error::Invalid);
}
self.command(
client_id,
request_id,
Intent {
operation,
call_id: call_id.into(),
expected_revision: Some(revision),
parameters: String::new(),
},
now,
)
}
fn command(
&mut self,
client_id: &str,
request_id: &str,
intent: Intent,
now: u64,
) -> Result<Decision, Error> {
if !valid_id(request_id) || !valid_id(&intent.call_id) || intent.parameters.len() > 200 {
return Err(Error::Invalid);
}
let client = self
.clients
.get(client_id)
.ok_or(Error::NotConnected)?
.clone();
self.requests.retain(|_, r| {
now < r.expires
|| self
.session
.as_ref()
.is_some_and(|s| s.attempt == r.ticket.attempt)
});
let key = (client.logical_id, request_id.to_owned());
if let Some(prior) = self.requests.get(&key) {
return if prior.intent == intent && prior.ticket.authority == client.authority {
Ok(Decision::Replay(prior.ticket.clone()))
} else {
Err(Error::Conflict)
};
}
if self.requests.len() >= MAX_REQUESTS {
return Err(Error::Capacity);
}
if intent.operation == Operation::Dial {
if !client.ready {
return Err(Error::NotReady);
}
if self.session.is_some() {
return Err(Error::Busy);
}
let attempt = self.next_attempt.checked_add(1).ok_or(Error::Capacity)?;
self.next_attempt = attempt;
self.session = Some(Session {
attempt,
authority: client.authority.clone(),
id: intent.call_id.clone(),
revision: 1,
phase: Phase::Dialing,
owner: Some(client_id.into()),
owner_principal: Some(client.principal.clone()),
owner_logical_id: Some(client.logical_id),
owner_closed_at: None,
offer_until: None,
pending: None,
invalidated: false,
});
} else {
let s = self.session.as_mut().ok_or(Error::Stale)?;
if s.invalidated
|| s.authority != client.authority
|| s.id != intent.call_id
|| intent.expected_revision != Some(s.revision)
|| s.offer_until.is_some_and(|until| now >= until)
{
return Err(Error::Stale);
}
if let Some(owner) = &s.owner {
if owner != client_id
|| s.owner_logical_id != Some(client.logical_id)
|| s.owner_principal.as_deref() != Some(client.principal.as_str())
{
return Err(Error::NotOwner);
}
}
if s.pending.is_some() {
return Err(Error::InvalidTransition);
}
let next_phase = match intent.operation {
Operation::Answer if s.phase == Phase::Ringing && s.owner.is_none() => {
if !client.ready {
return Err(Error::NotReady);
}
Phase::Answering
}
Operation::Reject if s.phase == Phase::Ringing && s.owner.is_none() => {
Phase::Ending
}
Operation::Cancel if s.phase == Phase::Dialing => Phase::Ending,
Operation::Hangup if matches!(s.phase, Phase::Answering | Phase::Connected) => {
Phase::Ending
}
_ => return Err(Error::InvalidTransition),
};
let revision = s.revision.checked_add(1).ok_or(Error::Capacity)?;
s.owner = Some(client_id.into());
s.owner_principal = Some(client.principal.clone());
s.owner_logical_id = Some(client.logical_id);
s.phase = next_phase;
s.revision = revision;
s.offer_until = None;
}
let s = self.session.as_ref().expect("reserved session");
let ticket = Ticket {
attempt: s.attempt,
authority: s.authority.clone(),
call_id: s.id.clone(),
revision: s.revision,
operation: intent.operation,
};
self.session.as_mut().expect("reserved session").pending = Some(ticket.clone());
self.requests.insert(
key,
Receipt {
intent,
ticket: ticket.clone(),
expires: now.saturating_add(REQUEST_TTL_MS),
},
);
Ok(Decision::Submit(ticket))
}
pub fn result(&mut self, ticket: &Ticket, phase: Option<Phase>) -> Result<(), Error> {
let s = self.session.as_mut().ok_or(Error::Stale)?;
if s.attempt != ticket.attempt
|| s.authority != ticket.authority
|| s.id != ticket.call_id
|| s.pending.as_ref() != Some(ticket)
|| s.invalidated
{
return Err(Error::Stale);
}
if phase.is_some_and(|phase| !matches!(phase, Phase::Uncertain))
&& !s
.owner
.as_ref()
.and_then(|owner| self.clients.get(owner))
.is_some_and(|client| {
s.owner_logical_id == Some(client.logical_id)
&& client.authority == s.authority
&& s.owner_principal.as_deref() == Some(client.principal.as_str())
})
{
return Err(Error::Stale);
}
let next = match (ticket.operation, phase) {
(_, None) => None,
(_, Some(Phase::Uncertain)) => Some(Phase::Uncertain),
(Operation::Dial, Some(Phase::Dialing)) => Some(Phase::Dialing),
(Operation::Answer, Some(Phase::Answering)) => Some(Phase::Answering),
(Operation::Dial | Operation::Answer, Some(Phase::Connected)) => Some(Phase::Connected),
_ => return Err(Error::InvalidTransition),
};
if let Some(phase) = next {
s.revision = s.revision.checked_add(1).ok_or(Error::Capacity)?;
s.phase = phase;
s.pending = None;
} else {
self.session = None;
}
Ok(())
}
pub fn connected(&mut self, ticket: &Ticket) -> Result<(), Error> {
let s = self.session.as_mut().ok_or(Error::Stale)?;
if s.attempt != ticket.attempt
|| s.authority != ticket.authority
|| s.id != ticket.call_id
|| s.invalidated
|| s.pending.is_some()
|| !matches!(ticket.operation, Operation::Dial | Operation::Answer)
|| !matches!(s.phase, Phase::Dialing | Phase::Answering)
{
return Err(Error::Stale);
}
let revision = s.revision.checked_add(1).ok_or(Error::Capacity)?;
s.revision = revision;
s.phase = Phase::Connected;
Ok(())
}
pub fn uncertain(
&mut self,
authority: &AccountAuthority,
identity: &CallIdentity,
) -> Result<(), Error> {
let s = self.session.as_mut().ok_or(Error::Stale)?;
if &s.authority != authority || s.attempt != identity.attempt || s.id != identity.call_id {
return Err(Error::Stale);
}
let revision = s.revision.checked_add(1).ok_or(Error::Capacity)?;
s.revision = revision;
s.phase = Phase::Uncertain;
s.pending = None;
s.owner_closed_at = None;
Ok(())
}
pub fn pending_ticket_matches(&self, ticket: &Ticket) -> bool {
self.session.as_ref().is_some_and(|session| {
!session.invalidated
&& session.attempt == ticket.attempt
&& session.authority == ticket.authority
&& session.id == ticket.call_id
&& session.pending.as_ref() == Some(ticket)
})
}
pub fn terminal(
&mut self,
authority: &AccountAuthority,
identity: &CallIdentity,
) -> Result<(), Error> {
if !self.session.as_ref().is_some_and(|s| {
&s.authority == authority && s.id == identity.call_id && s.attempt == identity.attempt
}) {
return Err(Error::Stale);
}
self.session = None;
Ok(())
}
pub fn disconnect(&mut self, client_id: &str, now: u64) {
let Some(client) = self.clients.remove(client_id) else {
return;
};
if let Some(s) = self.session.as_mut().filter(|s| {
!s.invalidated
&& s.owner.as_deref() == Some(client_id)
&& s.owner_logical_id == Some(client.logical_id)
&& s.owner_principal.as_deref() == Some(client.principal.as_str())
}) {
s.owner_closed_at = Some(now);
}
}
pub fn expire(&mut self, now: u64) -> Option<(AccountAuthority, CallIdentity)> {
let s = self.session.as_mut()?;
if s.phase == Phase::Ringing && s.offer_until.is_some_and(|until| now >= until) {
self.session = None;
return None;
}
if s.owner_closed_at.is_some_and(|until| now >= until) {
s.owner_closed_at = None;
if !s.pending.as_ref().is_some_and(|ticket| {
matches!(ticket.operation, Operation::Dial | Operation::Answer)
}) {
s.pending = None;
}
s.phase = Phase::Uncertain;
s.revision = s.revision.saturating_add(1);
return Some((
s.authority.clone(),
CallIdentity {
attempt: s.attempt,
call_id: s.id.clone(),
},
));
}
None
}
pub fn invalidate(&mut self, authority: &AccountAuthority) -> Option<String> {
self.clients.retain(|_, c| &c.authority != authority);
if self
.session
.as_ref()
.is_some_and(|s| &s.authority == authority)
{
let s = self.session.as_mut().expect("matched session");
if s.invalidated {
return None;
}
s.invalidated = true;
s.phase = Phase::Uncertain;
s.revision = s.revision.saturating_add(1);
s.owner_closed_at = None;
return Some(s.id.clone());
}
None
}
}
#[cfg(test)]
#[path = "admission/tests.rs"]
mod tests;