Add NetworkBehaviour::inject_new_external_addr (#1063)

This commit is contained in:
Pierre Krieger
2019-04-16 17:00:20 +02:00
committed by GitHub
parent bee5c58b27
commit a953b613cf
4 changed files with 28 additions and 0 deletions

View File

@ -89,6 +89,10 @@ pub trait NetworkBehaviour {
fn inject_expired_listen_addr(&mut self, _addr: &Multiaddr) { fn inject_expired_listen_addr(&mut self, _addr: &Multiaddr) {
} }
/// Indicates to the behaviour that we have discovered a new external address for us.
fn inject_new_external_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

@ -325,6 +325,7 @@ where TBehaviour: NetworkBehaviour<ProtocolsHandler = THandler>,
Async::Ready(NetworkBehaviourAction::ReportObservedAddr { address }) => { Async::Ready(NetworkBehaviourAction::ReportObservedAddr { address }) => {
for addr in self.raw_swarm.nat_traversal(&address) { for addr in self.raw_swarm.nat_traversal(&address) {
if self.external_addrs.iter().all(|a| *a != addr) { if self.external_addrs.iter().all(|a| *a != addr) {
self.behaviour.inject_new_external_addr(&addr);
self.external_addrs.push(addr); self.external_addrs.push(addr);
} }
} }

View File

@ -115,7 +115,12 @@ where
if let Some(inner) = self.inner.as_mut() { if let Some(inner) = self.inner.as_mut() {
inner.inject_expired_listen_addr(addr) inner.inject_expired_listen_addr(addr)
} }
}
fn inject_new_external_addr(&mut self, addr: &Multiaddr) {
if let Some(inner) = self.inner.as_mut() {
inner.inject_new_external_addr(addr)
}
} }
fn poll(&mut self, params: &mut PollParameters<'_>) fn poll(&mut self, params: &mut PollParameters<'_>)

View File

@ -270,6 +270,20 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
}) })
}; };
// Build the list of statements to put in the body of `inject_new_external_addr()`.
let inject_new_external_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_external_addr(addr); },
None => quote!{ self.#field_n.inject_new_external_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.
@ -449,6 +463,10 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
#(#inject_expired_listen_addr_stmts);* #(#inject_expired_listen_addr_stmts);*
} }
fn inject_new_external_addr(&mut self, addr: &#multiaddr) {
#(#inject_new_external_addr_stmts);*
}
fn inject_node_event( fn inject_node_event(
&mut self, &mut self,
peer_id: #peer_id, peer_id: #peer_id,