Add NetworkBehaviour methods for listened addresses (#1061)

This commit is contained in:
Pierre Krieger
2019-04-16 15:36:08 +02:00
committed by GitHub
parent 889f003a3f
commit 9b6336672b
4 changed files with 61 additions and 8 deletions

View File

@ -80,6 +80,15 @@ pub trait NetworkBehaviour {
fn inject_dial_failure(&mut self, _peer_id: &PeerId) { fn inject_dial_failure(&mut self, _peer_id: &PeerId) {
} }
/// Indicates to the behaviour that we have started listening on a new multiaddr.
fn inject_new_listen_addr(&mut self, _addr: &Multiaddr) {
}
/// Indicates to the behaviour that a new multiaddr we were listening on has expired,
/// which means that we are no longer listening in it.
fn inject_expired_listen_addr(&mut self, _addr: &Multiaddr) {
}
/// 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.

View File

@ -274,9 +274,11 @@ where TBehaviour: NetworkBehaviour<ProtocolsHandler = THandler>,
if !self.listened_addrs.contains(&listen_addr) { if !self.listened_addrs.contains(&listen_addr) {
self.listened_addrs.push(listen_addr.clone()) self.listened_addrs.push(listen_addr.clone())
} }
self.behaviour.inject_new_listen_addr(&listen_addr);
} }
Async::Ready(RawSwarmEvent::ExpiredListenerAddress { listen_addr }) => { Async::Ready(RawSwarmEvent::ExpiredListenerAddress { listen_addr }) => {
self.listened_addrs.retain(|a| a != &listen_addr) self.listened_addrs.retain(|a| a != &listen_addr);
self.behaviour.inject_expired_listen_addr(&listen_addr);
} }
Async::Ready(RawSwarmEvent::ListenerClosed { .. }) => {}, Async::Ready(RawSwarmEvent::ListenerClosed { .. }) => {},
Async::Ready(RawSwarmEvent::IncomingConnectionError { .. }) => {}, Async::Ready(RawSwarmEvent::IncomingConnectionError { .. }) => {},

View File

@ -98,6 +98,19 @@ where
} }
} }
fn inject_new_listen_addr(&mut self, addr: &Multiaddr) {
if let Some(inner) = self.inner.as_mut() {
inner.inject_new_listen_addr(addr)
}
}
fn inject_expired_listen_addr(&mut self, addr: &Multiaddr) {
if let Some(inner) = self.inner.as_mut() {
inner.inject_expired_listen_addr(addr)
}
}
fn poll(&mut self, params: &mut PollParameters<'_>) fn poll(&mut self, params: &mut PollParameters<'_>)
-> Async<NetworkBehaviourAction<<<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent, Self::OutEvent>> -> Async<NetworkBehaviourAction<<<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent, Self::OutEvent>>
{ {

View File

@ -242,6 +242,34 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
}) })
}; };
// Build the list of statements to put in the body of `inject_new_listen_addr()`.
let inject_new_listen_addr_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_new_listen_addr(addr); },
None => quote!{ self.#field_n.inject_new_listen_addr(addr); },
})
})
};
// Build the list of statements to put in the body of `inject_expired_listen_addr()`.
let inject_expired_listen_addr_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_expired_listen_addr(addr); },
None => quote!{ self.#field_n.inject_expired_listen_addr(addr); },
})
})
};
// 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.
@ -382,7 +410,6 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
type ProtocolsHandler = #protocols_handler_ty; type ProtocolsHandler = #protocols_handler_ty;
type OutEvent = #out_event; type OutEvent = #out_event;
#[inline]
fn new_handler(&mut self) -> Self::ProtocolsHandler { fn new_handler(&mut self) -> Self::ProtocolsHandler {
use #into_protocols_handler; use #into_protocols_handler;
#new_handler #new_handler
@ -394,32 +421,34 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
out out
} }
#[inline]
fn inject_connected(&mut self, peer_id: #peer_id, endpoint: #connected_point) { fn inject_connected(&mut self, peer_id: #peer_id, endpoint: #connected_point) {
#(#inject_connected_stmts);* #(#inject_connected_stmts);*
} }
#[inline]
fn inject_disconnected(&mut self, peer_id: &#peer_id, endpoint: #connected_point) { fn inject_disconnected(&mut self, peer_id: &#peer_id, endpoint: #connected_point) {
#(#inject_disconnected_stmts);* #(#inject_disconnected_stmts);*
} }
#[inline]
fn inject_replaced(&mut self, peer_id: #peer_id, closed_endpoint: #connected_point, new_endpoint: #connected_point) { fn inject_replaced(&mut self, peer_id: #peer_id, closed_endpoint: #connected_point, new_endpoint: #connected_point) {
#(#inject_replaced_stmts);* #(#inject_replaced_stmts);*
} }
#[inline]
fn inject_addr_reach_failure(&mut self, peer_id: Option<&#peer_id>, addr: &#multiaddr, error: &dyn std::error::Error) { fn inject_addr_reach_failure(&mut self, peer_id: Option<&#peer_id>, addr: &#multiaddr, error: &dyn std::error::Error) {
#(#inject_addr_reach_failure_stmts);* #(#inject_addr_reach_failure_stmts);*
} }
#[inline]
fn inject_dial_failure(&mut self, peer_id: &#peer_id) { fn inject_dial_failure(&mut self, peer_id: &#peer_id) {
#(#inject_dial_failure_stmts);* #(#inject_dial_failure_stmts);*
} }
#[inline] fn inject_new_listen_addr(&mut self, addr: &#multiaddr) {
#(#inject_new_listen_addr_stmts);*
}
fn inject_expired_listen_addr(&mut self, addr: &#multiaddr) {
#(#inject_expired_listen_addr_stmts);*
}
fn inject_node_event( fn inject_node_event(
&mut self, &mut self,
peer_id: #peer_id, peer_id: #peer_id,