Skip to main content

bornera_core/connection/
machine.rs

1//! Fixed-epoch owner state and construction.
2
3use std::{cell::RefCell, rc::Rc};
4
5use crate::{
6    AdmissionGate, ConnectionEpoch, ConnectionId, ConnectionLimits, EndpointId, IdentitySeeds,
7    LaneId, OrderedVerified, admission::ReservationLedger, identity::IdentityGenerator,
8};
9
10/// Physical lifecycle of one fixed connection epoch.
11#[derive(Clone, Copy, Debug, Eq, PartialEq)]
12pub enum ConnectionPhase {
13    /// The fixed-epoch policy owner exists; transport establishment may still be pending.
14    Live,
15    /// Closure was requested and awaits capability confirmation.
16    Closing,
17    /// The physical capability confirmed closure.
18    Closed,
19}
20
21/// Deterministic single-owner policy for one exact connection epoch.
22#[derive(Debug)]
23pub struct ConnectionMachine {
24    pub(super) endpoint: EndpointId,
25    pub(super) lane: LaneId,
26    pub(super) connection: ConnectionId,
27    pub(super) epoch: ConnectionEpoch,
28    pub(super) phase: ConnectionPhase,
29    pub(super) gate: AdmissionGate,
30    pub(super) close_reason: Option<crate::CloseReason>,
31    pub(super) identities: IdentityGenerator,
32    pub(super) ledger: Rc<RefCell<ReservationLedger>>,
33    pub(super) matching: OrderedVerified,
34}
35
36impl ConnectionMachine {
37    /// Creates an active epoch with session-only admission and zero-based identities.
38    pub(crate) fn new(
39        endpoint: EndpointId,
40        lane: LaneId,
41        connection: ConnectionId,
42        epoch: ConnectionEpoch,
43        limits: ConnectionLimits,
44    ) -> Self {
45        Self::with_identity_seeds(
46            endpoint,
47            lane,
48            connection,
49            epoch,
50            limits,
51            IdentitySeeds::ZERO,
52        )
53    }
54
55    /// Creates an epoch with deterministic seeds for replay and exhaustion proofs.
56    pub(crate) fn with_identity_seeds(
57        endpoint: EndpointId,
58        lane: LaneId,
59        connection: ConnectionId,
60        epoch: ConnectionEpoch,
61        limits: ConnectionLimits,
62        seeds: IdentitySeeds,
63    ) -> Self {
64        Self {
65            endpoint,
66            lane,
67            connection,
68            epoch,
69            phase: ConnectionPhase::Live,
70            gate: AdmissionGate::SessionOnly,
71            close_reason: None,
72            identities: IdentityGenerator::new(seeds),
73            ledger: Rc::new(RefCell::new(ReservationLedger::new(limits))),
74            matching: OrderedVerified::new(limits.match_keys(), limits.max_operations()),
75        }
76    }
77
78    /// Returns the exact epoch owned by this machine.
79    pub const fn epoch(&self) -> ConnectionEpoch {
80        self.epoch
81    }
82
83    /// Returns immutable state for the configured matching discipline.
84    pub const fn matching(&self) -> &OrderedVerified {
85        &self.matching
86    }
87}