mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-20 21:36:31 +00:00
Add NetworkBehaviour methods for listened addresses (#1061)
This commit is contained in:
@ -80,6 +80,15 @@ pub trait NetworkBehaviour {
|
||||
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.
|
||||
///
|
||||
/// This API mimics the API of the `Stream` trait.
|
||||
|
@ -274,9 +274,11 @@ where TBehaviour: NetworkBehaviour<ProtocolsHandler = THandler>,
|
||||
if !self.listened_addrs.contains(&listen_addr) {
|
||||
self.listened_addrs.push(listen_addr.clone())
|
||||
}
|
||||
self.behaviour.inject_new_listen_addr(&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::IncomingConnectionError { .. }) => {},
|
||||
|
@ -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<'_>)
|
||||
-> Async<NetworkBehaviourAction<<<Self::ProtocolsHandler as IntoProtocolsHandler>::Handler as ProtocolsHandler>::InEvent, Self::OutEvent>>
|
||||
{
|
||||
|
@ -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()`.
|
||||
//
|
||||
// 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 OutEvent = #out_event;
|
||||
|
||||
#[inline]
|
||||
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
||||
use #into_protocols_handler;
|
||||
#new_handler
|
||||
@ -394,32 +421,34 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
|
||||
out
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_connected(&mut self, peer_id: #peer_id, endpoint: #connected_point) {
|
||||
#(#inject_connected_stmts);*
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_disconnected(&mut self, peer_id: &#peer_id, endpoint: #connected_point) {
|
||||
#(#inject_disconnected_stmts);*
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_replaced(&mut self, peer_id: #peer_id, closed_endpoint: #connected_point, new_endpoint: #connected_point) {
|
||||
#(#inject_replaced_stmts);*
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_addr_reach_failure(&mut self, peer_id: Option<&#peer_id>, addr: &#multiaddr, error: &dyn std::error::Error) {
|
||||
#(#inject_addr_reach_failure_stmts);*
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn inject_dial_failure(&mut self, peer_id: &#peer_id) {
|
||||
#(#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(
|
||||
&mut self,
|
||||
peer_id: #peer_id,
|
||||
|
Reference in New Issue
Block a user