mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-13 18:11:22 +00:00
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:
@ -54,7 +54,7 @@ use crate::{
|
||||
};
|
||||
use futures::prelude::*;
|
||||
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;
|
||||
|
||||
@ -261,8 +261,12 @@ where TBehaviour: NetworkBehaviour,
|
||||
},
|
||||
Async::Ready(RawSwarmEvent::ListenerClosed { .. }) => {},
|
||||
Async::Ready(RawSwarmEvent::IncomingConnectionError { .. }) => {},
|
||||
Async::Ready(RawSwarmEvent::DialError { .. }) => {},
|
||||
Async::Ready(RawSwarmEvent::UnknownPeerDialError { .. }) => {},
|
||||
Async::Ready(RawSwarmEvent::DialError { peer_id, multiaddr, error, .. }) => {
|
||||
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 = {
|
||||
@ -319,7 +323,7 @@ pub trait NetworkBehaviour {
|
||||
|
||||
/// Addresses that this behaviour is aware of for this specific peer, and that may allow
|
||||
/// 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
|
||||
/// given endpoint.
|
||||
@ -339,6 +343,10 @@ pub trait NetworkBehaviour {
|
||||
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.
|
||||
///
|
||||
/// This API mimics the API of the `Stream` trait.
|
||||
@ -549,7 +557,7 @@ mod tests {
|
||||
DummyProtocolsHandler::default()
|
||||
}
|
||||
|
||||
fn addresses_of_peer(&self, _: &PeerId) -> Vec<Multiaddr> {
|
||||
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
|
@ -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()`.
|
||||
//
|
||||
// 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
|
||||
}
|
||||
|
||||
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();
|
||||
#(#addresses_of_peer_stmts);*
|
||||
out
|
||||
@ -353,6 +367,11 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
|
||||
#(#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]
|
||||
fn inject_node_event(
|
||||
&mut self,
|
||||
|
@ -149,7 +149,7 @@ where
|
||||
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();
|
||||
self.discovered_nodes
|
||||
.iter()
|
||||
|
@ -217,7 +217,7 @@ where
|
||||
Default::default()
|
||||
}
|
||||
|
||||
fn addresses_of_peer(&self, _: &PeerId) -> Vec<Multiaddr> {
|
||||
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
|
@ -75,7 +75,7 @@ where
|
||||
IdentifyListenHandler::new().select(PeriodicIdHandler::new())
|
||||
}
|
||||
|
||||
fn addresses_of_peer(&self, _: &PeerId) -> Vec<Multiaddr> {
|
||||
fn addresses_of_peer(&mut self, _: &PeerId) -> Vec<Multiaddr> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
|
@ -282,7 +282,7 @@ where
|
||||
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
|
||||
.get(peer_id)
|
||||
.map(|l| l.iter().cloned().collect::<Vec<_>>())
|
||||
|
@ -90,7 +90,7 @@ where
|
||||
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()
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user