connexa 0.5.1

High level abstraction of rust-libp2p
Documentation
use std::{
    collections::VecDeque,
    task::{Context, Poll},
    time::Duration,
};

use crate::prelude::swarm::handler::ConnectionEvent;
use crate::prelude::swarm::{
    ConnectionHandler, ConnectionHandlerEvent, SubstreamProtocol, SupportedProtocols,
};
use crate::prelude::transport::upgrade::DeniedUpgrade;
use futures::FutureExt;
use futures_timer::Delay;
use libp2p::relay::HOP_PROTOCOL_NAME;

#[derive(Default, Debug)]
pub struct Handler {
    events: VecDeque<
        ConnectionHandlerEvent<
            <Self as ConnectionHandler>::OutboundProtocol,
            <Self as ConnectionHandler>::OutboundOpenInfo,
            <Self as ConnectionHandler>::ToBehaviour,
        >,
    >,

    supported: bool,

    supported_protocol: SupportedProtocols,

    blacklist_timer: Option<Delay>,
}

#[derive(Debug, Copy, Clone)]
pub enum In {
    Blacklist { duration: Duration },
}

#[derive(Debug, Copy, Clone)]
pub enum Out {
    Supported,
    Unsupported,
    BlacklistExpired,
}

#[allow(deprecated)]
impl ConnectionHandler for Handler {
    type FromBehaviour = In;
    type ToBehaviour = Out;
    type InboundProtocol = DeniedUpgrade;
    type OutboundProtocol = DeniedUpgrade;
    type InboundOpenInfo = ();
    type OutboundOpenInfo = ();

    fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
        SubstreamProtocol::new(DeniedUpgrade, ())
    }

    fn connection_keep_alive(&self) -> bool {
        false
    }

    fn on_behaviour_event(&mut self, event: Self::FromBehaviour) {
        match event {
            In::Blacklist { duration } => {
                self.blacklist_timer = Some(Delay::new(duration));
            }
        }
    }

    fn on_connection_event(
        &mut self,
        event: ConnectionEvent<
            Self::InboundProtocol,
            Self::OutboundProtocol,
            Self::InboundOpenInfo,
            Self::OutboundOpenInfo,
        >,
    ) {
        if let ConnectionEvent::RemoteProtocolsChange(protocol) = event {
            let change = self.supported_protocol.on_protocols_change(protocol);
            if change {
                let valid = self
                    .supported_protocol
                    .iter()
                    .any(|proto| HOP_PROTOCOL_NAME.eq(proto));

                match (valid, self.supported) {
                    (true, false) => {
                        self.supported = true;
                        self.events
                            .push_back(ConnectionHandlerEvent::NotifyBehaviour(Out::Supported));
                    }
                    (false, true) => {
                        self.supported = false;
                        self.blacklist_timer = None;
                        self.events
                            .push_back(ConnectionHandlerEvent::NotifyBehaviour(Out::Unsupported));
                    }
                    (true, true) => {}
                    _ => {}
                }
            }
        }
    }

    fn poll(
        &mut self,
        cx: &mut Context<'_>,
    ) -> Poll<
        ConnectionHandlerEvent<Self::OutboundProtocol, Self::OutboundOpenInfo, Self::ToBehaviour>,
    > {
        if let Some(event) = self.events.pop_front() {
            return Poll::Ready(event);
        }

        if let Some(timer) = self.blacklist_timer.as_mut()
            && timer.poll_unpin(cx).is_ready()
        {
            self.blacklist_timer = None;
            if self.supported {
                return Poll::Ready(ConnectionHandlerEvent::NotifyBehaviour(
                    Out::BlacklistExpired,
                ));
            }
        }

        Poll::Pending
    }
}