2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-12-08 12:20:18 +01:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2017-03-07 19:48:07 +01:00
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
2016-12-08 12:20:18 +01:00
|
|
|
|
|
|
|
//! I/O and event context generalizations.
|
|
|
|
|
2016-12-10 16:51:50 +01:00
|
|
|
use network::{NetworkContext, PeerId, NodeId};
|
2016-12-08 12:20:18 +01:00
|
|
|
|
|
|
|
use super::{Announcement, LightProtocol, ReqId};
|
|
|
|
use super::error::Error;
|
2017-04-05 15:02:44 +02:00
|
|
|
use request::NetworkRequests as Requests;
|
2016-12-08 12:20:18 +01:00
|
|
|
|
2016-12-10 16:51:50 +01:00
|
|
|
/// An I/O context which allows sending and receiving packets as well as
|
2016-12-08 12:20:18 +01:00
|
|
|
/// disconnecting peers. This is used as a generalization of the portions
|
|
|
|
/// of a p2p network which the light protocol structure makes use of.
|
|
|
|
pub trait IoContext {
|
2016-12-11 15:40:31 +01:00
|
|
|
/// Send a packet to a specific peer.
|
|
|
|
fn send(&self, peer: PeerId, packet_id: u8, packet_body: Vec<u8>);
|
2016-12-08 12:20:18 +01:00
|
|
|
|
2016-12-11 15:40:31 +01:00
|
|
|
/// Respond to a peer's message. Only works if this context is a byproduct
|
|
|
|
/// of a packet handler.
|
|
|
|
fn respond(&self, packet_id: u8, packet_body: Vec<u8>);
|
2016-12-08 12:20:18 +01:00
|
|
|
|
2016-12-11 15:40:31 +01:00
|
|
|
/// Disconnect a peer.
|
|
|
|
fn disconnect_peer(&self, peer: PeerId);
|
2016-12-08 12:20:18 +01:00
|
|
|
|
2016-12-11 15:40:31 +01:00
|
|
|
/// Disable a peer -- this is a disconnect + a time-out.
|
|
|
|
fn disable_peer(&self, peer: PeerId);
|
2016-12-09 15:04:54 +01:00
|
|
|
|
2016-12-11 15:40:31 +01:00
|
|
|
/// Get a peer's protocol version.
|
|
|
|
fn protocol_version(&self, peer: PeerId) -> Option<u8>;
|
2016-12-10 16:51:50 +01:00
|
|
|
|
|
|
|
/// Persistent peer id
|
2016-12-11 17:31:56 +01:00
|
|
|
fn persistent_peer_id(&self, peer: PeerId) -> Option<NodeId>;
|
2016-12-08 12:20:18 +01:00
|
|
|
}
|
|
|
|
|
2016-12-11 17:31:56 +01:00
|
|
|
|
2016-12-08 12:20:18 +01:00
|
|
|
impl<'a> IoContext for NetworkContext<'a> {
|
2016-12-11 15:40:31 +01:00
|
|
|
fn send(&self, peer: PeerId, packet_id: u8, packet_body: Vec<u8>) {
|
|
|
|
if let Err(e) = self.send(peer, packet_id, packet_body) {
|
2017-03-07 20:58:23 +01:00
|
|
|
debug!(target: "pip", "Error sending packet to peer {}: {}", peer, e);
|
2016-12-11 15:40:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn respond(&self, packet_id: u8, packet_body: Vec<u8>) {
|
|
|
|
if let Err(e) = self.respond(packet_id, packet_body) {
|
2017-03-07 20:58:23 +01:00
|
|
|
debug!(target: "pip", "Error responding to peer message: {}", e);
|
2016-12-11 15:40:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn disconnect_peer(&self, peer: PeerId) {
|
2017-03-23 15:44:16 +01:00
|
|
|
trace!(target: "pip", "Initiating disconnect of peer {}", peer);
|
2016-12-11 15:40:31 +01:00
|
|
|
NetworkContext::disconnect_peer(self, peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn disable_peer(&self, peer: PeerId) {
|
2017-03-23 15:44:16 +01:00
|
|
|
trace!(target: "pip", "Initiating disable of peer {}", peer);
|
2016-12-11 15:40:31 +01:00
|
|
|
NetworkContext::disable_peer(self, peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn protocol_version(&self, peer: PeerId) -> Option<u8> {
|
|
|
|
self.protocol_version(self.subprotocol_name(), peer)
|
|
|
|
}
|
2016-12-11 17:31:56 +01:00
|
|
|
|
|
|
|
fn persistent_peer_id(&self, peer: PeerId) -> Option<NodeId> {
|
|
|
|
self.session_info(peer).and_then(|info| info.id)
|
2016-12-10 16:51:50 +01:00
|
|
|
}
|
2016-12-08 12:20:18 +01:00
|
|
|
}
|
|
|
|
|
2017-01-20 12:41:49 +01:00
|
|
|
/// Basic context for the protocol.
|
2016-12-15 19:25:52 +01:00
|
|
|
pub trait BasicContext {
|
2016-12-10 16:51:50 +01:00
|
|
|
/// Returns the relevant's peer persistent Id (aka NodeId).
|
2016-12-11 17:31:56 +01:00
|
|
|
fn persistent_peer_id(&self, peer: PeerId) -> Option<NodeId>;
|
2016-12-10 16:51:50 +01:00
|
|
|
|
2016-12-11 15:40:31 +01:00
|
|
|
/// Make a request from a peer.
|
2017-03-08 17:37:07 +01:00
|
|
|
///
|
|
|
|
/// Fails on: nonexistent peer, network error, peer not server,
|
|
|
|
/// insufficient credits. Does not check capabilities before sending.
|
|
|
|
/// On success, returns a request id which can later be coordinated
|
|
|
|
/// with an event.
|
|
|
|
fn request_from(&self, peer: PeerId, request: Requests) -> Result<ReqId, Error>;
|
2016-12-08 12:20:18 +01:00
|
|
|
|
2016-12-11 15:40:31 +01:00
|
|
|
/// Make an announcement of new capabilities to the rest of the peers.
|
|
|
|
// TODO: maybe just put this on a timer in LightProtocol?
|
|
|
|
fn make_announcement(&self, announcement: Announcement);
|
2016-12-08 12:20:18 +01:00
|
|
|
|
2016-12-11 15:40:31 +01:00
|
|
|
/// Disconnect a peer.
|
|
|
|
fn disconnect_peer(&self, peer: PeerId);
|
2016-12-08 12:20:18 +01:00
|
|
|
|
2016-12-11 15:40:31 +01:00
|
|
|
/// Disable a peer.
|
|
|
|
fn disable_peer(&self, peer: PeerId);
|
2016-12-08 12:20:18 +01:00
|
|
|
}
|
|
|
|
|
2016-12-15 19:25:52 +01:00
|
|
|
/// Context for a protocol event which has a peer ID attached.
|
|
|
|
pub trait EventContext: BasicContext {
|
|
|
|
/// Get the peer relevant to the event e.g. message sender,
|
|
|
|
/// disconnected/connected peer.
|
|
|
|
fn peer(&self) -> PeerId;
|
2016-12-15 21:51:08 +01:00
|
|
|
|
|
|
|
/// Treat the event context as a basic context.
|
|
|
|
fn as_basic(&self) -> &BasicContext;
|
2016-12-15 19:25:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Basic context.
|
|
|
|
pub struct TickCtx<'a> {
|
|
|
|
/// Io context to enable dispatch.
|
|
|
|
pub io: &'a IoContext,
|
|
|
|
/// Protocol implementation.
|
|
|
|
pub proto: &'a LightProtocol,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> BasicContext for TickCtx<'a> {
|
|
|
|
fn persistent_peer_id(&self, id: PeerId) -> Option<NodeId> {
|
|
|
|
self.io.persistent_peer_id(id)
|
|
|
|
}
|
|
|
|
|
2017-03-08 17:37:07 +01:00
|
|
|
fn request_from(&self, peer: PeerId, requests: Requests) -> Result<ReqId, Error> {
|
|
|
|
self.proto.request_from(self.io, &peer, requests)
|
2016-12-15 19:25:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn make_announcement(&self, announcement: Announcement) {
|
|
|
|
self.proto.make_announcement(self.io, announcement);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn disconnect_peer(&self, peer: PeerId) {
|
|
|
|
self.io.disconnect_peer(peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn disable_peer(&self, peer: PeerId) {
|
|
|
|
self.io.disable_peer(peer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-10 16:51:50 +01:00
|
|
|
/// Concrete implementation of `EventContext` over the light protocol struct and
|
2016-12-08 12:20:18 +01:00
|
|
|
/// an io context.
|
|
|
|
pub struct Ctx<'a> {
|
2016-12-11 15:40:31 +01:00
|
|
|
/// Io context to enable immediate response to events.
|
|
|
|
pub io: &'a IoContext,
|
|
|
|
/// Protocol implementation.
|
|
|
|
pub proto: &'a LightProtocol,
|
|
|
|
/// Relevant peer for event.
|
2016-12-11 17:31:56 +01:00
|
|
|
pub peer: PeerId,
|
2016-12-08 12:20:18 +01:00
|
|
|
}
|
|
|
|
|
2016-12-15 19:25:52 +01:00
|
|
|
impl<'a> BasicContext for Ctx<'a> {
|
2016-12-11 17:31:56 +01:00
|
|
|
fn persistent_peer_id(&self, id: PeerId) -> Option<NodeId> {
|
2016-12-10 21:25:08 +01:00
|
|
|
self.io.persistent_peer_id(id)
|
2016-12-10 16:51:50 +01:00
|
|
|
}
|
2016-12-08 12:20:18 +01:00
|
|
|
|
2017-03-08 17:37:07 +01:00
|
|
|
fn request_from(&self, peer: PeerId, requests: Requests) -> Result<ReqId, Error> {
|
|
|
|
self.proto.request_from(self.io, &peer, requests)
|
2016-12-11 15:40:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn make_announcement(&self, announcement: Announcement) {
|
|
|
|
self.proto.make_announcement(self.io, announcement);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn disconnect_peer(&self, peer: PeerId) {
|
|
|
|
self.io.disconnect_peer(peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn disable_peer(&self, peer: PeerId) {
|
|
|
|
self.io.disable_peer(peer);
|
|
|
|
}
|
2016-12-10 16:51:50 +01:00
|
|
|
}
|
2016-12-15 19:25:52 +01:00
|
|
|
|
|
|
|
impl<'a> EventContext for Ctx<'a> {
|
|
|
|
fn peer(&self) -> PeerId {
|
|
|
|
self.peer
|
|
|
|
}
|
2016-12-15 21:51:08 +01:00
|
|
|
|
|
|
|
fn as_basic(&self) -> &BasicContext {
|
|
|
|
&*self
|
|
|
|
}
|
2016-12-15 19:25:52 +01:00
|
|
|
}
|