2018-11-02 10:06:59 +01:00
|
|
|
// Copyright 2018 Parity Technologies (UK) Ltd.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2018-12-05 17:04:25 +01:00
|
|
|
use crate::protocol::{RemoteInfo, IdentifyProtocolConfig};
|
2018-11-02 10:06:59 +01:00
|
|
|
use futures::prelude::*;
|
2018-11-15 17:41:11 +01:00
|
|
|
use libp2p_core::{
|
2018-12-18 11:23:13 +01:00
|
|
|
protocols_handler::{ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr},
|
2018-11-20 15:09:59 +01:00
|
|
|
upgrade::{DeniedUpgrade, OutboundUpgrade}
|
2018-11-15 17:41:11 +01:00
|
|
|
};
|
|
|
|
use std::{io, marker::PhantomData, time::{Duration, Instant}};
|
2018-11-02 10:06:59 +01:00
|
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
|
|
|
use tokio_timer::Delay;
|
2018-11-15 17:41:11 +01:00
|
|
|
use void::{Void, unreachable};
|
2018-11-02 10:06:59 +01:00
|
|
|
|
|
|
|
/// Delay between the moment we connect and the first time we identify.
|
|
|
|
const DELAY_TO_FIRST_ID: Duration = Duration::from_millis(500);
|
|
|
|
/// After an identification succeeded, wait this long before the next time.
|
|
|
|
const DELAY_TO_NEXT_ID: Duration = Duration::from_secs(5 * 60);
|
|
|
|
/// After we failed to identify the remote, try again after the given delay.
|
|
|
|
const TRY_AGAIN_ON_ERR: Duration = Duration::from_secs(60 * 60);
|
|
|
|
|
|
|
|
/// Protocol handler that identifies the remote at a regular period.
|
2018-12-07 19:21:02 +01:00
|
|
|
pub struct PeriodicIdHandler<TSubstream> {
|
2018-11-02 10:06:59 +01:00
|
|
|
/// Configuration for the protocol.
|
2018-11-20 15:09:59 +01:00
|
|
|
config: IdentifyProtocolConfig,
|
2018-11-02 10:06:59 +01:00
|
|
|
|
2018-12-07 19:21:02 +01:00
|
|
|
/// If `Some`, we successfully generated an `PeriodicIdHandlerEvent` and we will produce
|
2018-11-02 10:06:59 +01:00
|
|
|
/// it the next time `poll()` is invoked.
|
2018-12-07 19:21:02 +01:00
|
|
|
pending_result: Option<PeriodicIdHandlerEvent>,
|
2018-11-02 10:06:59 +01:00
|
|
|
|
|
|
|
/// Future that fires when we need to identify the node again. If `None`, means that we should
|
|
|
|
/// shut down.
|
|
|
|
next_id: Option<Delay>,
|
|
|
|
|
|
|
|
/// Marker for strong typing.
|
|
|
|
marker: PhantomData<TSubstream>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Event produced by the periodic identifier.
|
|
|
|
#[derive(Debug)]
|
2018-12-07 19:21:02 +01:00
|
|
|
pub enum PeriodicIdHandlerEvent {
|
2018-11-02 10:06:59 +01:00
|
|
|
/// We obtained identification information from the remote
|
2018-11-15 17:41:11 +01:00
|
|
|
Identified(RemoteInfo),
|
2018-11-02 10:06:59 +01:00
|
|
|
/// Failed to identify the remote.
|
2018-12-18 11:23:13 +01:00
|
|
|
IdentificationError(ProtocolsHandlerUpgrErr<io::Error>),
|
2018-11-02 10:06:59 +01:00
|
|
|
}
|
|
|
|
|
2018-12-07 19:21:02 +01:00
|
|
|
impl<TSubstream> PeriodicIdHandler<TSubstream> {
|
|
|
|
/// Builds a new `PeriodicIdHandler`.
|
2018-11-02 10:06:59 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn new() -> Self {
|
2018-12-07 19:21:02 +01:00
|
|
|
PeriodicIdHandler {
|
2018-11-20 15:09:59 +01:00
|
|
|
config: IdentifyProtocolConfig,
|
2018-11-02 10:06:59 +01:00
|
|
|
pending_result: None,
|
|
|
|
next_id: Some(Delay::new(Instant::now() + DELAY_TO_FIRST_ID)),
|
|
|
|
marker: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-07 19:21:02 +01:00
|
|
|
impl<TSubstream> ProtocolsHandler for PeriodicIdHandler<TSubstream>
|
2018-11-02 10:06:59 +01:00
|
|
|
where
|
2018-11-16 13:59:56 +01:00
|
|
|
TSubstream: AsyncRead + AsyncWrite,
|
2018-11-02 10:06:59 +01:00
|
|
|
{
|
|
|
|
type InEvent = Void;
|
2018-12-07 19:21:02 +01:00
|
|
|
type OutEvent = PeriodicIdHandlerEvent;
|
2018-11-02 10:06:59 +01:00
|
|
|
type Substream = TSubstream;
|
2018-11-15 17:41:11 +01:00
|
|
|
type InboundProtocol = DeniedUpgrade;
|
2018-11-20 15:09:59 +01:00
|
|
|
type OutboundProtocol = IdentifyProtocolConfig;
|
2018-11-02 10:06:59 +01:00
|
|
|
type OutboundOpenInfo = ();
|
|
|
|
|
|
|
|
#[inline]
|
2018-11-15 17:41:11 +01:00
|
|
|
fn listen_protocol(&self) -> Self::InboundProtocol {
|
|
|
|
DeniedUpgrade
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inject_fully_negotiated_inbound(&mut self, protocol: Void) {
|
|
|
|
unreachable(protocol)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inject_fully_negotiated_outbound(
|
2018-11-02 10:06:59 +01:00
|
|
|
&mut self,
|
2018-11-15 17:41:11 +01:00
|
|
|
protocol: <Self::OutboundProtocol as OutboundUpgrade<TSubstream>>::Output,
|
|
|
|
_info: Self::OutboundOpenInfo,
|
2018-11-02 10:06:59 +01:00
|
|
|
) {
|
2018-12-07 19:21:02 +01:00
|
|
|
self.pending_result = Some(PeriodicIdHandlerEvent::Identified(protocol))
|
2018-11-02 10:06:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-11-05 16:53:04 +01:00
|
|
|
fn inject_event(&mut self, _: Self::InEvent) {}
|
2018-11-02 10:06:59 +01:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn inject_inbound_closed(&mut self) {}
|
|
|
|
|
|
|
|
#[inline]
|
2018-12-18 11:23:13 +01:00
|
|
|
fn inject_dial_upgrade_error(&mut self, _: Self::OutboundOpenInfo, err: ProtocolsHandlerUpgrErr<<Self::OutboundProtocol as OutboundUpgrade<Self::Substream>>::Error>) {
|
2018-12-07 19:21:02 +01:00
|
|
|
self.pending_result = Some(PeriodicIdHandlerEvent::IdentificationError(err));
|
2018-11-02 10:06:59 +01:00
|
|
|
if let Some(ref mut next_id) = self.next_id {
|
|
|
|
next_id.reset(Instant::now() + TRY_AGAIN_ON_ERR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn shutdown(&mut self) {
|
|
|
|
self.next_id = None;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll(
|
|
|
|
&mut self,
|
|
|
|
) -> Poll<
|
|
|
|
Option<
|
|
|
|
ProtocolsHandlerEvent<
|
2018-11-15 17:41:11 +01:00
|
|
|
Self::OutboundProtocol,
|
2018-11-02 10:06:59 +01:00
|
|
|
Self::OutboundOpenInfo,
|
2018-12-07 19:21:02 +01:00
|
|
|
PeriodicIdHandlerEvent,
|
2018-11-02 10:06:59 +01:00
|
|
|
>,
|
|
|
|
>,
|
|
|
|
io::Error,
|
|
|
|
> {
|
|
|
|
if let Some(pending_result) = self.pending_result.take() {
|
|
|
|
return Ok(Async::Ready(Some(ProtocolsHandlerEvent::Custom(
|
|
|
|
pending_result,
|
|
|
|
))));
|
|
|
|
}
|
|
|
|
|
|
|
|
let next_id = match self.next_id {
|
|
|
|
Some(ref mut nid) => nid,
|
|
|
|
None => return Ok(Async::Ready(None)),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Poll the future that fires when we need to identify the node again.
|
|
|
|
match next_id.poll() {
|
|
|
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
|
|
|
Ok(Async::Ready(())) => {
|
|
|
|
next_id.reset(Instant::now() + DELAY_TO_NEXT_ID);
|
2018-11-20 15:09:59 +01:00
|
|
|
let upgrade = self.config.clone();
|
2018-11-02 10:06:59 +01:00
|
|
|
let ev = ProtocolsHandlerEvent::OutboundSubstreamRequest { upgrade, info: () };
|
|
|
|
Ok(Async::Ready(Some(ev)))
|
|
|
|
}
|
|
|
|
Err(err) => Err(io::Error::new(io::ErrorKind::Other, err)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|