Add inject_dial_failure and make addresses_of_peer mut (#901)

* Add inject_dial_failure and make addresses_of_peer mut

* Fix tests
This commit is contained in:
Pierre Krieger
2019-01-30 14:55:39 +01:00
committed by GitHub
parent 62fd5cd514
commit a77da73010
7 changed files with 38 additions and 11 deletions

View File

@ -54,7 +54,7 @@ use crate::{
}; };
use futures::prelude::*; use futures::prelude::*;
use smallvec::SmallVec; use smallvec::SmallVec;
use std::{fmt, io, ops::{Deref, DerefMut}}; use std::{error, fmt, io, ops::{Deref, DerefMut}};
pub use crate::nodes::raw_swarm::ConnectedPoint; pub use crate::nodes::raw_swarm::ConnectedPoint;
@ -261,8 +261,12 @@ where TBehaviour: NetworkBehaviour,
}, },
Async::Ready(RawSwarmEvent::ListenerClosed { .. }) => {}, Async::Ready(RawSwarmEvent::ListenerClosed { .. }) => {},
Async::Ready(RawSwarmEvent::IncomingConnectionError { .. }) => {}, Async::Ready(RawSwarmEvent::IncomingConnectionError { .. }) => {},
Async::Ready(RawSwarmEvent::DialError { .. }) => {}, Async::Ready(RawSwarmEvent::DialError { peer_id, multiaddr, error, .. }) => {
Async::Ready(RawSwarmEvent::UnknownPeerDialError { .. }) => {}, self.behaviour.inject_dial_failure(Some(&peer_id), &multiaddr, &error);
},
Async::Ready(RawSwarmEvent::UnknownPeerDialError { multiaddr, error, .. }) => {
self.behaviour.inject_dial_failure(None, &multiaddr, &error);
},
} }
let behaviour_poll = { let behaviour_poll = {
@ -319,7 +323,7 @@ pub trait NetworkBehaviour {
/// Addresses that this behaviour is aware of for this specific peer, and that may allow /// Addresses that this behaviour is aware of for this specific peer, and that may allow
/// reaching the peer. /// reaching the peer.
fn addresses_of_peer(&self, peer_id: &PeerId) -> Vec<Multiaddr>; fn addresses_of_peer(&mut self, peer_id: &PeerId) -> Vec<Multiaddr>;
/// Indicates the behaviour that we connected to the node with the given peer id through the /// Indicates the behaviour that we connected to the node with the given peer id through the
/// given endpoint. /// given endpoint.
@ -339,6 +343,10 @@ pub trait NetworkBehaviour {
event: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent event: <<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::OutEvent
); );
/// Indicates to the behaviour that we tried to reach a node, but failed.
fn inject_dial_failure(&mut self, _peer_id: Option<&PeerId>, _addr: &Multiaddr, _error: &dyn error::Error) {
}
/// Polls for things that swarm should do. /// Polls for things that swarm should do.
/// ///
/// This API mimics the API of the `Stream` trait. /// This API mimics the API of the `Stream` trait.
@ -549,7 +557,7 @@ mod tests {
DummyProtocolsHandler::default() DummyProtocolsHandler::default()
} }
fn addresses_of_peer(&self, _: &PeerId) -> Vec<Multiaddr> { fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
Vec::new() Vec::new()
} }

View File

@ -191,6 +191,20 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
}) })
}; };
// Build the list of statements to put in the body of `inject_dial_failure()`.
let inject_dial_failure_stmts = {
data_struct.fields.iter().enumerate().filter_map(move |(field_n, field)| {
if is_ignored(&field) {
return None;
}
Some(match field.ident {
Some(ref i) => quote!{ self.#i.inject_dial_failure(peer_id, addr, error); },
None => quote!{ self.#field_n.inject_dial_failure(peer_id, addr, error); },
})
})
};
// Build the list of variants to put in the body of `inject_node_event()`. // Build the list of variants to put in the body of `inject_node_event()`.
// //
// The event type is a construction of nested `#either_ident`s of the events of the children. // The event type is a construction of nested `#either_ident`s of the events of the children.
@ -337,7 +351,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
#new_handler #new_handler
} }
fn addresses_of_peer(&self, peer_id: &#peer_id) -> Vec<#multiaddr> { fn addresses_of_peer(&mut self, peer_id: &#peer_id) -> Vec<#multiaddr> {
let mut out = Vec::new(); let mut out = Vec::new();
#(#addresses_of_peer_stmts);* #(#addresses_of_peer_stmts);*
out out
@ -353,6 +367,11 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
#(#inject_disconnected_stmts);* #(#inject_disconnected_stmts);*
} }
#[inline]
fn inject_dial_failure(&mut self, peer_id: Option<&#peer_id>, addr: &#multiaddr, error: &dyn std::error::Error) {
#(#inject_dial_failure_stmts);*
}
#[inline] #[inline]
fn inject_node_event( fn inject_node_event(
&mut self, &mut self,

View File

@ -149,7 +149,7 @@ where
DummyProtocolsHandler::default() DummyProtocolsHandler::default()
} }
fn addresses_of_peer(&self, peer_id: &PeerId) -> Vec<Multiaddr> { fn addresses_of_peer(&mut self, peer_id: &PeerId) -> Vec<Multiaddr> {
let now = Instant::now(); let now = Instant::now();
self.discovered_nodes self.discovered_nodes
.iter() .iter()

View File

@ -217,7 +217,7 @@ where
Default::default() Default::default()
} }
fn addresses_of_peer(&self, _: &PeerId) -> Vec<Multiaddr> { fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
Vec::new() Vec::new()
} }

View File

@ -75,7 +75,7 @@ where
IdentifyListenHandler::new().select(PeriodicIdHandler::new()) IdentifyListenHandler::new().select(PeriodicIdHandler::new())
} }
fn addresses_of_peer(&self, _: &PeerId) -> Vec<Multiaddr> { fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
Vec::new() Vec::new()
} }

View File

@ -282,7 +282,7 @@ where
KademliaHandler::dial_and_listen() KademliaHandler::dial_and_listen()
} }
fn addresses_of_peer(&self, peer_id: &PeerId) -> Vec<Multiaddr> { fn addresses_of_peer(&mut self, peer_id: &PeerId) -> Vec<Multiaddr> {
self.kbuckets self.kbuckets
.get(peer_id) .get(peer_id)
.map(|l| l.iter().cloned().collect::<Vec<_>>()) .map(|l| l.iter().cloned().collect::<Vec<_>>())

View File

@ -90,7 +90,7 @@ where
OneShotHandler::default() OneShotHandler::default()
} }
fn addresses_of_peer(&self, _peer_id: &PeerId) -> Vec<Multiaddr> { fn addresses_of_peer(&mut self, _peer_id: &PeerId) -> Vec<Multiaddr> {
Vec::new() Vec::new()
} }