use std::{cell::RefCell, rc::Rc};
use crate::{
AdmissionGate, ConnectionEpoch, ConnectionId, ConnectionLimits, EndpointId, IdentitySeeds,
LaneId, OrderedVerified, admission::ReservationLedger, identity::IdentityGenerator,
};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ConnectionPhase {
Live,
Closing,
Closed,
}
#[derive(Debug)]
pub struct ConnectionMachine {
pub(super) endpoint: EndpointId,
pub(super) lane: LaneId,
pub(super) connection: ConnectionId,
pub(super) epoch: ConnectionEpoch,
pub(super) phase: ConnectionPhase,
pub(super) gate: AdmissionGate,
pub(super) close_reason: Option<crate::CloseReason>,
pub(super) identities: IdentityGenerator,
pub(super) ledger: Rc<RefCell<ReservationLedger>>,
pub(super) matching: OrderedVerified,
}
impl ConnectionMachine {
pub(crate) fn new(
endpoint: EndpointId,
lane: LaneId,
connection: ConnectionId,
epoch: ConnectionEpoch,
limits: ConnectionLimits,
) -> Self {
Self::with_identity_seeds(
endpoint,
lane,
connection,
epoch,
limits,
IdentitySeeds::ZERO,
)
}
pub(crate) fn with_identity_seeds(
endpoint: EndpointId,
lane: LaneId,
connection: ConnectionId,
epoch: ConnectionEpoch,
limits: ConnectionLimits,
seeds: IdentitySeeds,
) -> Self {
Self {
endpoint,
lane,
connection,
epoch,
phase: ConnectionPhase::Live,
gate: AdmissionGate::SessionOnly,
close_reason: None,
identities: IdentityGenerator::new(seeds),
ledger: Rc::new(RefCell::new(ReservationLedger::new(limits))),
matching: OrderedVerified::new(limits.match_keys(), limits.max_operations()),
}
}
pub const fn epoch(&self) -> ConnectionEpoch {
self.epoch
}
pub const fn matching(&self) -> &OrderedVerified {
&self.matching
}
}