Skip to main content

antenna_protocol/handshake/
mod.rs

1mod host;
2mod input;
3mod joiner;
4mod output;
5mod signaling;
6mod state;
7mod strategy;
8mod test;
9
10use anyhow::Result;
11use serde::{Deserialize, Serialize};
12
13pub use host::Host;
14pub use input::HandshakeInput;
15pub use joiner::Joiner;
16pub use output::HandshakeOutput;
17pub use signaling::SignalingPayload;
18pub use state::HandshakeState;
19pub use strategy::{HandshakeStrategy, StrategyFSM};
20
21use crate::PeerID;
22
23/// Per-peer handshake state machine.
24///
25/// One [`HandshakeFSM`] exists per remote peer in [`crate::MeshNodeFSM`].
26/// Dispatches to a [`Host`] or [`Joiner`] strategy depending on which side
27/// initiates the SDP exchange.
28pub struct HandshakeFSM {
29    strategy: HandshakeStrategy,
30    fsm: StrategyFSM,
31}
32
33impl HandshakeFSM {
34    pub fn new(strategy: HandshakeStrategy) -> Self {
35        match strategy {
36            HandshakeStrategy::Host => Self::host(),
37            HandshakeStrategy::Joiner => Self::joiner(),
38        }
39    }
40
41    /// Host created in relay flow, knows its joiner from the initiation
42    pub fn host() -> Self {
43        Self {
44            strategy: HandshakeStrategy::Host,
45            fsm: StrategyFSM::Host(Host::new()),
46        }
47    }
48
49    pub fn joiner() -> Self {
50        Self {
51            strategy: HandshakeStrategy::Joiner,
52            fsm: StrategyFSM::Joiner(Joiner::new()),
53        }
54    }
55
56    /// Get current handshake stat
57    pub fn state(&self) -> &HandshakeState {
58        self.fsm.state()
59    }
60
61    pub fn strategy(&self) -> &HandshakeStrategy {
62        &self.strategy
63    }
64
65    /// Process handshake input and generate handshake output
66    pub fn process(&mut self, input: HandshakeInput) -> Result<Option<HandshakeOutput>> {
67        self.fsm.process(input)
68    }
69}
70
71/// How the SDP offer/answer pair is exchanged between two peers.
72#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
73pub enum HandshakeMode {
74    /// First connection between two strangers — signaling goes through an
75    /// external transport (the bundled signaling server, copy-paste, QR, etc.).
76    Bootstrap,
77
78    /// Newcomer joining an existing mesh — signaling is relayed over the data
79    /// channel of the given intermediary peer; no external transport needed.
80    Relay(PeerID),
81}