bornera_core/connection/
machine.rs1use 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#[derive(Clone, Copy, Debug, Eq, PartialEq)]
12pub enum ConnectionPhase {
13 Live,
15 Closing,
17 Closed,
19}
20
21#[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 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 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 pub const fn epoch(&self) -> ConnectionEpoch {
80 self.epoch
81 }
82
83 pub const fn matching(&self) -> &OrderedVerified {
85 &self.matching
86 }
87}