stochastic-routing-extended 1.0.2

SRX (Stochastic Routing eXtended) — a next-generation VPN protocol with stochastic routing, DPI evasion, post-quantum cryptography, and multi-transport channel splitting
Documentation
//! Automatic fallback: seamlessly switch transports when one is blocked.

use crate::transport::TransportKind;

/// Controls automatic fallback between transports.
pub struct FallbackController {
    /// Priority-ordered list of transports to try.
    priority: Vec<TransportKind>,
    /// Currently active transport.
    current: Option<TransportKind>,
}

impl FallbackController {
    pub fn new(priority: Vec<TransportKind>) -> Self {
        let current = priority.first().copied();
        Self { priority, current }
    }

    /// Trigger a fallback to the next available transport.
    pub fn fallback(&mut self) -> Option<TransportKind> {
        let current_idx = self
            .current
            .and_then(|c| self.priority.iter().position(|&p| p == c))?;
        let next = self.priority.get(current_idx + 1).copied();
        self.current = next;
        next
    }

    /// Get the currently active transport.
    pub fn current(&self) -> Option<TransportKind> {
        self.current
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn steps_through_priority() {
        let mut f = FallbackController::new(vec![TransportKind::Tcp, TransportKind::Udp]);
        assert_eq!(f.current(), Some(TransportKind::Tcp));
        assert_eq!(f.fallback(), Some(TransportKind::Udp));
        assert_eq!(f.current(), Some(TransportKind::Udp));
        assert!(f.fallback().is_none());
        assert!(f.current().is_none());
    }
}