Add a KademliaHandler (#580)

* Rework Kademlia for the new design

* Minor work on protocol.rs

* More work

* Remove QueryTarget::FindValue

* Finish work on query

* Query timeout test

* Work on topology

* More work

* Update protocols/kad/src/topology.rs

Co-Authored-By: tomaka <pierre.krieger1708@gmail.com>

* Fix trailing whitespaces

* Use if let
This commit is contained in:
Pierre Krieger
2018-11-29 12:11:35 +01:00
committed by GitHub
parent ab192cdca7
commit 3aa1fcbdc6
21 changed files with 2357 additions and 1666 deletions

View File

@ -37,12 +37,12 @@ pub use crate::nodes::raw_swarm::ConnectedPoint;
/// Contains the state of the network, plus the way it should behave.
pub struct Swarm<TTransport, TBehaviour, TTopology>
where TTransport: Transport,
TBehaviour: NetworkBehaviour,
TBehaviour: NetworkBehaviour<TTopology>,
{
raw_swarm: RawSwarm<
TTransport,
<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as ProtocolsHandler>::InEvent,
<<TBehaviour as NetworkBehaviour>::ProtocolsHandler as ProtocolsHandler>::OutEvent,
<<TBehaviour as NetworkBehaviour<TTopology>>::ProtocolsHandler as ProtocolsHandler>::InEvent,
<<TBehaviour as NetworkBehaviour<TTopology>>::ProtocolsHandler as ProtocolsHandler>::OutEvent,
NodeHandlerWrapper<TBehaviour::ProtocolsHandler>,
>,
@ -57,7 +57,7 @@ where TTransport: Transport,
impl<TTransport, TBehaviour, TTopology> Deref for Swarm<TTransport, TBehaviour, TTopology>
where TTransport: Transport,
TBehaviour: NetworkBehaviour,
TBehaviour: NetworkBehaviour<TTopology>,
{
type Target = TBehaviour;
@ -69,7 +69,7 @@ where TTransport: Transport,
impl<TTransport, TBehaviour, TTopology> DerefMut for Swarm<TTransport, TBehaviour, TTopology>
where TTransport: Transport,
TBehaviour: NetworkBehaviour,
TBehaviour: NetworkBehaviour<TTopology>,
{
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
@ -78,7 +78,7 @@ where TTransport: Transport,
}
impl<TTransport, TBehaviour, TMuxer, TTopology> Swarm<TTransport, TBehaviour, TTopology>
where TBehaviour: NetworkBehaviour,
where TBehaviour: NetworkBehaviour<TTopology>,
TMuxer: StreamMuxer + Send + Sync + 'static,
<TMuxer as StreamMuxer>::OutboundSubstream: Send + 'static,
<TMuxer as StreamMuxer>::Substream: Send + 'static,
@ -171,7 +171,7 @@ where TBehaviour: NetworkBehaviour,
}
impl<TTransport, TBehaviour, TMuxer, TTopology> Stream for Swarm<TTransport, TBehaviour, TTopology>
where TBehaviour: NetworkBehaviour,
where TBehaviour: NetworkBehaviour<TTopology>,
TMuxer: StreamMuxer + Send + Sync + 'static,
<TMuxer as StreamMuxer>::OutboundSubstream: Send + 'static,
<TMuxer as StreamMuxer>::Substream: Send + 'static,
@ -230,7 +230,7 @@ where TBehaviour: NetworkBehaviour,
Async::Ready(RawSwarmEvent::UnknownPeerDialError { .. }) => {},
}
match self.behaviour.poll() {
match self.behaviour.poll(&mut self.topology) {
Async::NotReady if raw_swarm_not_ready => return Ok(Async::NotReady),
Async::NotReady => (),
Async::Ready(NetworkBehaviourAction::GenerateEvent(event)) => {
@ -256,7 +256,7 @@ where TBehaviour: NetworkBehaviour,
///
/// This trait has been designed to be composable. Multiple implementations can be combined into
/// one that handles all the behaviours at once.
pub trait NetworkBehaviour {
pub trait NetworkBehaviour<TTopology> {
/// Handler for all the protocols the network supports.
type ProtocolsHandler: ProtocolsHandler;
/// Event generated by the swarm.
@ -286,7 +286,7 @@ pub trait NetworkBehaviour {
/// Polls for things that swarm should do.
///
/// This API mimics the API of the `Stream` trait.
fn poll(&mut self) -> Async<NetworkBehaviourAction<<Self::ProtocolsHandler as ProtocolsHandler>::InEvent, Self::OutEvent>>;
fn poll(&mut self, topology: &mut TTopology) -> Async<NetworkBehaviourAction<<Self::ProtocolsHandler as ProtocolsHandler>::InEvent, Self::OutEvent>>;
}
/// Action to perform.