      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192

    // Copyright 2015-2017 Parity Technologies (UK) Ltd.
    // 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
    // along with Parity.  If not, see <http://www.gnu.org/licenses/>.

    //! I/O and event context generalizations.

    use network::{NetworkContext, PeerId, NodeId};

    use super::{Announcement, LightProtocol, ReqId};
    use super::error::Error;
    use request::{self, Request};

    /// An I/O context which allows sending and receiving packets as well as
    /// 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 {
    	/// Send a packet to a specific peer.
    	fn send(&self, peer: PeerId, packet_id: u8, packet_body: Vec<u8>);

    	/// 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>);

    	/// Disconnect a peer.
    	fn disconnect_peer(&self, peer: PeerId);

    	/// Disable a peer -- this is a disconnect + a time-out.
    	fn disable_peer(&self, peer: PeerId);

    	/// Get a peer's protocol version.
    	fn protocol_version(&self, peer: PeerId) -> Option<u8>;

    	/// Persistent peer id
    	fn persistent_peer_id(&self, peer: PeerId) -> Option<NodeId>;
    }

    impl<'a> IoContext for NetworkContext<'a> {
    	fn send(&self, peer: PeerId, packet_id: u8, packet_body: Vec<u8>) {
    		if let Err(e) = self.send(peer, packet_id, packet_body) {
    			debug!(target: "les", "Error sending packet to peer {}: {}", peer, e);
    		}
    	}

    	fn respond(&self, packet_id: u8, packet_body: Vec<u8>) {
    		if let Err(e) = self.respond(packet_id, packet_body) {
    			debug!(target: "les", "Error responding to peer message: {}", e);
    		}
    	}

    	fn disconnect_peer(&self, peer: PeerId) {
    		NetworkContext::disconnect_peer(self, peer);
    	}

    	fn disable_peer(&self, peer: PeerId) {
    		NetworkContext::disable_peer(self, peer);
    	}

    	fn protocol_version(&self, peer: PeerId) -> Option<u8> {
    		self.protocol_version(self.subprotocol_name(), peer)
    	}

    	fn persistent_peer_id(&self, peer: PeerId) -> Option<NodeId> {
    		self.session_info(peer).and_then(|info| info.id)
    	}
    }

    /// Basic context for the protocol.
    pub trait BasicContext {
    	/// Returns the relevant's peer persistent Id (aka NodeId).
    	fn persistent_peer_id(&self, peer: PeerId) -> Option<NodeId>;

    	/// Make a request from a peer.
    	fn request_from(&self, peer: PeerId, request: Request) -> Result<ReqId, Error>;

    	/// 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);

    	/// Find the maximum number of requests of a specific type which can be made from
    	/// supplied peer.
    	fn max_requests(&self, peer: PeerId, kind: request::Kind) -> usize;

    	/// Disconnect a peer.
    	fn disconnect_peer(&self, peer: PeerId);

    	/// Disable a peer.
    	fn disable_peer(&self, peer: PeerId);
    }

    /// 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;

    	/// Treat the event context as a basic context.
    	fn as_basic(&self) -> &BasicContext;
    }

    /// 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)
    	}

    	fn request_from(&self, peer: PeerId, request: Request) -> Result<ReqId, Error> {
    		self.proto.request_from(self.io, &peer, request)
    	}

    	fn make_announcement(&self, announcement: Announcement) {
    		self.proto.make_announcement(self.io, announcement);
    	}

    	fn max_requests(&self, peer: PeerId, kind: request::Kind) -> usize {
    		self.proto.max_requests(peer, kind)
    	}

    	fn disconnect_peer(&self, peer: PeerId) {
    		self.io.disconnect_peer(peer);
    	}

    	fn disable_peer(&self, peer: PeerId) {
    		self.io.disable_peer(peer);
    	}
    }

    /// Concrete implementation of `EventContext` over the light protocol struct and
    /// an io context.
    pub struct Ctx<'a> {
    	/// Io context to enable immediate response to events.
    	pub io: &'a IoContext,
    	/// Protocol implementation.
    	pub proto: &'a LightProtocol,
    	/// Relevant peer for event.
    	pub peer: PeerId,
    }

    impl<'a> BasicContext for Ctx<'a> {
    	fn persistent_peer_id(&self, id: PeerId) -> Option<NodeId> {
    		self.io.persistent_peer_id(id)
    	}

    	fn request_from(&self, peer: PeerId, request: Request) -> Result<ReqId, Error> {
    		self.proto.request_from(self.io, &peer, request)
    	}

    	fn make_announcement(&self, announcement: Announcement) {
    		self.proto.make_announcement(self.io, announcement);
    	}

    	fn max_requests(&self, peer: PeerId, kind: request::Kind) -> usize {
    		self.proto.max_requests(peer, kind)
    	}

    	fn disconnect_peer(&self, peer: PeerId) {
    		self.io.disconnect_peer(peer);
    	}

    	fn disable_peer(&self, peer: PeerId) {
    		self.io.disable_peer(peer);
    	}
    }

    impl<'a> EventContext for Ctx<'a> {
    	fn peer(&self) -> PeerId {
    		self.peer
    	}

    	fn as_basic(&self) -> &BasicContext {
    		&*self
    	}
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)