use crate::protocol::{RemoteInfo, IdentifyProtocolConfig, ReplySubstream};
use futures::prelude::*;
use libp2p_core::upgrade::{
InboundUpgrade,
OutboundUpgrade,
Negotiated
};
use libp2p_swarm::{
KeepAlive,
SubstreamProtocol,
ProtocolsHandler,
ProtocolsHandlerEvent,
ProtocolsHandlerUpgrErr
};
use smallvec::SmallVec;
use std::{io, marker::PhantomData, time::Duration};
use tokio_io::{AsyncRead, AsyncWrite};
use wasm_timer::{Delay, Instant};
const DELAY_TO_FIRST_ID: Duration = Duration::from_millis(500);
const DELAY_TO_NEXT_ID: Duration = Duration::from_secs(5 * 60);
const TRY_AGAIN_ON_ERR: Duration = Duration::from_secs(60 * 60);
pub struct IdentifyHandler<TSubstream> {
config: IdentifyProtocolConfig,
events: SmallVec<[IdentifyHandlerEvent<TSubstream>; 4]>,
next_id: Delay,
keep_alive: KeepAlive,
marker: PhantomData<TSubstream>,
}
#[derive(Debug)]
pub enum IdentifyHandlerEvent<TSubstream> {
Identified(RemoteInfo),
Identify(ReplySubstream<Negotiated<TSubstream>>),
IdentificationError(ProtocolsHandlerUpgrErr<io::Error>),
}
impl<TSubstream> IdentifyHandler<TSubstream> {
pub fn new() -> Self {
IdentifyHandler {
config: IdentifyProtocolConfig,
events: SmallVec::new(),
next_id: Delay::new(Instant::now() + DELAY_TO_FIRST_ID),
keep_alive: KeepAlive::Yes,
marker: PhantomData,
}
}
}
impl<TSubstream> ProtocolsHandler for IdentifyHandler<TSubstream>
where
TSubstream: AsyncRead + AsyncWrite,
{
type InEvent = ();
type OutEvent = IdentifyHandlerEvent<TSubstream>;
type Error = wasm_timer::Error;
type Substream = TSubstream;
type InboundProtocol = IdentifyProtocolConfig;
type OutboundProtocol = IdentifyProtocolConfig;
type OutboundOpenInfo = ();
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol> {
SubstreamProtocol::new(self.config.clone())
}
fn inject_fully_negotiated_inbound(
&mut self,
protocol: <Self::InboundProtocol as InboundUpgrade<TSubstream>>::Output
) {
self.events.push(IdentifyHandlerEvent::Identify(protocol))
}
fn inject_fully_negotiated_outbound(
&mut self,
protocol: <Self::OutboundProtocol as OutboundUpgrade<TSubstream>>::Output,
_info: Self::OutboundOpenInfo,
) {
self.events.push(IdentifyHandlerEvent::Identified(protocol));
self.keep_alive = KeepAlive::No;
}
fn inject_event(&mut self, _: Self::InEvent) {}
fn inject_dial_upgrade_error(
&mut self,
_info: Self::OutboundOpenInfo,
err: ProtocolsHandlerUpgrErr<
<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error
>
) {
self.events.push(IdentifyHandlerEvent::IdentificationError(err));
self.keep_alive = KeepAlive::No;
self.next_id.reset(Instant::now() + TRY_AGAIN_ON_ERR);
}
fn connection_keep_alive(&self) -> KeepAlive {
self.keep_alive
}
fn poll(&mut self) -> Poll<
ProtocolsHandlerEvent<
Self::OutboundProtocol,
Self::OutboundOpenInfo,
IdentifyHandlerEvent<TSubstream>,
>,
Self::Error,
> {
if !self.events.is_empty() {
return Ok(Async::Ready(ProtocolsHandlerEvent::Custom(
self.events.remove(0),
)));
}
match self.next_id.poll()? {
Async::NotReady => Ok(Async::NotReady),
Async::Ready(()) => {
self.next_id.reset(Instant::now() + DELAY_TO_NEXT_ID);
let ev = ProtocolsHandlerEvent::OutboundSubstreamRequest {
protocol: SubstreamProtocol::new(self.config.clone()),
info: (),
};
Ok(Async::Ready(ev))
}
}
}
}