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
23pub struct HandshakeFSM {
24    strategy: HandshakeStrategy,
25    fsm: StrategyFSM,
26}
27
28impl HandshakeFSM {
29    pub fn new(strategy: HandshakeStrategy) -> Self {
30        match strategy {
31            HandshakeStrategy::Host => Self::host(),
32            HandshakeStrategy::Joiner => Self::joiner(),
33        }
34    }
35
36    /// Host created in relay flow, knows its joiner from the initiation
37    pub fn host() -> Self {
38        Self {
39            strategy: HandshakeStrategy::Host,
40            fsm: StrategyFSM::Host(Host::new()),
41        }
42    }
43
44    pub fn joiner() -> Self {
45        Self {
46            strategy: HandshakeStrategy::Joiner,
47            fsm: StrategyFSM::Joiner(Joiner::new()),
48        }
49    }
50
51    /// Get current handshake stat
52    pub fn state(&self) -> &HandshakeState {
53        self.fsm.state()
54    }
55
56    pub fn strategy(&self) -> &HandshakeStrategy {
57        &self.strategy
58    }
59
60    /// Process handshake input and generate handshake output
61    pub fn process(&mut self, input: HandshakeInput) -> Result<Option<HandshakeOutput>> {
62        self.fsm.process(input)
63    }
64}
65
66#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
67pub enum HandshakeMode {
68    /// Initial handshake with completely new peer, establishing first datachannel with that peer to our mesh
69    Bootstrap,
70
71    /// Handshake of newly connected peer with all others through "inviter" data channel.
72    Relay(PeerID),
73}