2018-11-12 17:12:47 +01:00
|
|
|
// Copyright 2018 Parity Technologies (UK) Ltd.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
#![recursion_limit = "256"]
|
|
|
|
|
|
|
|
extern crate proc_macro;
|
|
|
|
|
2019-02-11 14:58:15 +01:00
|
|
|
use quote::quote;
|
|
|
|
use proc_macro::TokenStream;
|
|
|
|
use syn::{parse_macro_input, DeriveInput, Data, DataStruct, Ident};
|
2018-11-12 17:12:47 +01:00
|
|
|
|
2020-01-14 13:48:16 +02:00
|
|
|
/// Generates a delegating `NetworkBehaviour` implementation for the struct this is used for. See
|
|
|
|
/// the trait documentation for better description.
|
2018-11-12 17:12:47 +01:00
|
|
|
#[proc_macro_derive(NetworkBehaviour, attributes(behaviour))]
|
|
|
|
pub fn hello_macro_derive(input: TokenStream) -> TokenStream {
|
|
|
|
let ast = parse_macro_input!(input as DeriveInput);
|
|
|
|
build(&ast)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The actual implementation.
|
|
|
|
fn build(ast: &DeriveInput) -> TokenStream {
|
|
|
|
match ast.data {
|
|
|
|
Data::Struct(ref s) => build_struct(ast, s),
|
2018-11-16 12:59:57 +01:00
|
|
|
Data::Enum(_) => unimplemented!("Deriving NetworkBehaviour is not implemented for enums"),
|
|
|
|
Data::Union(_) => unimplemented!("Deriving NetworkBehaviour is not implemented for unions"),
|
2018-11-12 17:12:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The version for structs
|
|
|
|
fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> TokenStream {
|
|
|
|
let name = &ast.ident;
|
|
|
|
let (_, ty_generics, where_clause) = ast.generics.split_for_impl();
|
2019-01-26 23:57:53 +01:00
|
|
|
let multiaddr = quote!{::libp2p::core::Multiaddr};
|
2019-07-04 14:47:59 +02:00
|
|
|
let trait_to_impl = quote!{::libp2p::swarm::NetworkBehaviour};
|
|
|
|
let net_behv_event_proc = quote!{::libp2p::swarm::NetworkBehaviourEventProcess};
|
2018-11-12 17:12:47 +01:00
|
|
|
let either_ident = quote!{::libp2p::core::either::EitherOutput};
|
2019-07-04 14:47:59 +02:00
|
|
|
let network_behaviour_action = quote!{::libp2p::swarm::NetworkBehaviourAction};
|
|
|
|
let into_protocols_handler = quote!{::libp2p::swarm::IntoProtocolsHandler};
|
|
|
|
let protocols_handler = quote!{::libp2p::swarm::ProtocolsHandler};
|
|
|
|
let into_proto_select_ident = quote!{::libp2p::swarm::IntoProtocolsHandlerSelect};
|
2018-11-12 17:12:47 +01:00
|
|
|
let peer_id = quote!{::libp2p::core::PeerId};
|
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
|
|
|
let connection_id = quote!{::libp2p::core::connection::ConnectionId};
|
2019-07-04 14:47:59 +02:00
|
|
|
let connected_point = quote!{::libp2p::core::ConnectedPoint};
|
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
|
|
|
let listener_id = quote!{::libp2p::core::connection::ListenerId};
|
2018-11-12 17:12:47 +01:00
|
|
|
|
2019-07-04 14:47:59 +02:00
|
|
|
let poll_parameters = quote!{::libp2p::swarm::PollParameters};
|
2018-12-01 13:34:57 +01:00
|
|
|
|
2018-11-12 17:12:47 +01:00
|
|
|
// Build the generics.
|
|
|
|
let impl_generics = {
|
|
|
|
let tp = ast.generics.type_params();
|
|
|
|
let lf = ast.generics.lifetimes();
|
|
|
|
let cst = ast.generics.const_params();
|
2020-02-07 16:29:30 +01:00
|
|
|
quote!{<#(#lf,)* #(#tp,)* #(#cst,)*>}
|
2018-11-12 17:12:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Build the `where ...` clause of the trait implementation.
|
|
|
|
let where_clause = {
|
2020-02-07 16:29:30 +01:00
|
|
|
let additional = data_struct.fields.iter()
|
2018-11-15 17:41:11 +01:00
|
|
|
.filter(|x| !is_ignored(x))
|
2018-11-27 16:10:34 +01:00
|
|
|
.flat_map(|field| {
|
2018-11-15 17:41:11 +01:00
|
|
|
let ty = &field.ty;
|
|
|
|
vec![
|
2019-01-26 23:57:53 +01:00
|
|
|
quote!{#ty: #trait_to_impl},
|
|
|
|
quote!{Self: #net_behv_event_proc<<#ty as #trait_to_impl>::OutEvent>},
|
2018-11-15 17:41:11 +01:00
|
|
|
]
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2018-11-12 17:12:47 +01:00
|
|
|
|
|
|
|
if let Some(where_clause) = where_clause {
|
2019-01-21 10:56:01 +00:00
|
|
|
if where_clause.predicates.trailing_punct() {
|
|
|
|
Some(quote!{#where_clause #(#additional),*})
|
|
|
|
} else {
|
|
|
|
Some(quote!{#where_clause, #(#additional),*})
|
|
|
|
}
|
2018-11-12 17:12:47 +01:00
|
|
|
} else {
|
|
|
|
Some(quote!{where #(#additional),*})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// The final out event.
|
|
|
|
// If we find a `#[behaviour(out_event = "Foo")]` attribute on the struct, we set `Foo` as
|
|
|
|
// the out event. Otherwise we use `()`.
|
|
|
|
let out_event = {
|
|
|
|
let mut out = quote!{()};
|
|
|
|
for meta_items in ast.attrs.iter().filter_map(get_meta_items) {
|
|
|
|
for meta_item in meta_items {
|
|
|
|
match meta_item {
|
2019-11-25 10:45:04 +01:00
|
|
|
syn::NestedMeta::Meta(syn::Meta::NameValue(ref m)) if m.path.is_ident("out_event") => {
|
2018-11-12 17:12:47 +01:00
|
|
|
if let syn::Lit::Str(ref s) = m.lit {
|
2019-02-06 15:45:19 +01:00
|
|
|
let ident: syn::Type = syn::parse_str(&s.value()).unwrap();
|
2018-11-12 17:12:47 +01:00
|
|
|
out = quote!{#ident};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out
|
|
|
|
};
|
|
|
|
|
2019-01-26 23:57:53 +01:00
|
|
|
// Build the list of statements to put in the body of `addresses_of_peer()`.
|
|
|
|
let addresses_of_peer_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!{ out.extend(self.#i.addresses_of_peer(peer_id)); },
|
|
|
|
None => quote!{ out.extend(self.#field_n.addresses_of_peer(peer_id)); },
|
|
|
|
})
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2018-11-12 17:12:47 +01:00
|
|
|
// Build the list of statements to put in the body of `inject_connected()`.
|
|
|
|
let inject_connected_stmts = {
|
|
|
|
data_struct.fields.iter().enumerate().filter_map(move |(field_n, field)| {
|
|
|
|
if is_ignored(&field) {
|
|
|
|
return None;
|
|
|
|
}
|
2020-03-31 15:41:13 +02:00
|
|
|
Some(match field.ident {
|
|
|
|
Some(ref i) => quote!{ self.#i.inject_connected(peer_id); },
|
|
|
|
None => quote!{ self.#field_n.inject_connected(peer_id); },
|
2018-11-12 17:12:47 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
// Build the list of statements to put in the body of `inject_disconnected()`.
|
|
|
|
let inject_disconnected_stmts = {
|
|
|
|
data_struct.fields.iter().enumerate().filter_map(move |(field_n, field)| {
|
|
|
|
if is_ignored(&field) {
|
|
|
|
return None;
|
|
|
|
}
|
2020-03-31 15:41:13 +02:00
|
|
|
Some(match field.ident {
|
|
|
|
Some(ref i) => quote!{ self.#i.inject_disconnected(peer_id); },
|
|
|
|
None => quote!{ self.#field_n.inject_disconnected(peer_id); },
|
|
|
|
})
|
|
|
|
})
|
|
|
|
};
|
2018-11-12 17:12:47 +01:00
|
|
|
|
2020-03-31 15:41:13 +02:00
|
|
|
// Build the list of statements to put in the body of `inject_connection_established()`.
|
|
|
|
let inject_connection_established_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_connection_established(peer_id, connection_id, endpoint); },
|
|
|
|
None => quote!{ self.#field_n.inject_connection_established(peer_id, connection_id, endpoint); },
|
|
|
|
})
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
// Build the list of statements to put in the body of `inject_connection_closed()`.
|
|
|
|
let inject_connection_closed_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_connection_closed(peer_id, connection_id, endpoint); },
|
|
|
|
None => quote!{ self.#field_n.inject_connection_closed(peer_id, connection_id, endpoint); },
|
2018-11-12 17:12:47 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2019-03-20 20:28:55 +01:00
|
|
|
// Build the list of statements to put in the body of `inject_addr_reach_failure()`.
|
|
|
|
let inject_addr_reach_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_addr_reach_failure(peer_id, addr, error); },
|
|
|
|
None => quote!{ self.#field_n.inject_addr_reach_failure(peer_id, addr, error); },
|
|
|
|
})
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2019-01-30 14:55:39 +01:00
|
|
|
// 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 {
|
2019-03-20 20:28:55 +01:00
|
|
|
Some(ref i) => quote!{ self.#i.inject_dial_failure(peer_id); },
|
|
|
|
None => quote!{ self.#field_n.inject_dial_failure(peer_id); },
|
2019-01-30 14:55:39 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2019-04-16 15:36:08 +02:00
|
|
|
// 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); },
|
|
|
|
})
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2019-04-16 17:00:20 +02:00
|
|
|
// 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); },
|
|
|
|
})
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2019-08-13 15:41:12 +02:00
|
|
|
// Build the list of statements to put in the body of `inject_listener_error()`.
|
|
|
|
let inject_listener_error_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_listener_error(id, err);),
|
|
|
|
None => quote!(self.#field_n.inject_listener_error(id, err);)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
// Build the list of statements to put in the body of `inject_listener_closed()`.
|
|
|
|
let inject_listener_closed_stmts = {
|
|
|
|
data_struct.fields.iter().enumerate().filter_map(move |(field_n, field)| {
|
|
|
|
if is_ignored(&field) {
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
Some(match field.ident {
|
2020-04-01 22:49:42 +10:00
|
|
|
Some(ref i) => quote!(self.#i.inject_listener_closed(id, reason);),
|
|
|
|
None => quote!(self.#field_n.inject_listener_closed(id, reason);)
|
2019-08-13 15:41:12 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
|
|
|
// Build the list of variants to put in the body of `inject_event()`.
|
2018-11-12 17:12:47 +01:00
|
|
|
//
|
|
|
|
// The event type is a construction of nested `#either_ident`s of the events of the children.
|
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
|
|
|
// We call `inject_event` on the corresponding child.
|
2018-11-12 17:12:47 +01:00
|
|
|
let inject_node_event_stmts = data_struct.fields.iter().enumerate().filter(|f| !is_ignored(&f.1)).enumerate().map(|(enum_n, (field_n, field))| {
|
|
|
|
let mut elem = if enum_n != 0 {
|
|
|
|
quote!{ #either_ident::Second(ev) }
|
|
|
|
} else {
|
|
|
|
quote!{ ev }
|
|
|
|
};
|
|
|
|
|
2020-04-28 11:38:25 +02:00
|
|
|
for _ in 0 .. data_struct.fields.iter().filter(|f| !is_ignored(f)).count() - 1 - enum_n {
|
2018-11-12 17:12:47 +01:00
|
|
|
elem = quote!{ #either_ident::First(#elem) };
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(match field.ident {
|
2020-04-10 00:01:26 +10:00
|
|
|
Some(ref i) => quote!{ #elem => #trait_to_impl::inject_event(&mut self.#i, peer_id, connection_id, ev) },
|
|
|
|
None => quote!{ #elem => #trait_to_impl::inject_event(&mut self.#field_n, peer_id, connection_id, ev) },
|
2018-11-12 17:12:47 +01:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
// The `ProtocolsHandler` associated type.
|
|
|
|
let protocols_handler_ty = {
|
|
|
|
let mut ph_ty = None;
|
|
|
|
for field in data_struct.fields.iter() {
|
|
|
|
if is_ignored(&field) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let ty = &field.ty;
|
2019-01-26 23:57:53 +01:00
|
|
|
let field_info = quote!{ <#ty as #trait_to_impl>::ProtocolsHandler };
|
2018-11-12 17:12:47 +01:00
|
|
|
match ph_ty {
|
2019-01-14 14:22:25 +01:00
|
|
|
Some(ev) => ph_ty = Some(quote!{ #into_proto_select_ident<#ev, #field_info> }),
|
2018-11-12 17:12:47 +01:00
|
|
|
ref mut ev @ None => *ev = Some(field_info),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ph_ty.unwrap_or(quote!{()}) // TODO: `!` instead
|
|
|
|
};
|
|
|
|
|
|
|
|
// The content of `new_handler()`.
|
|
|
|
// Example output: `self.field1.select(self.field2.select(self.field3))`.
|
|
|
|
let new_handler = {
|
|
|
|
let mut out_handler = None;
|
|
|
|
|
|
|
|
for (field_n, field) in data_struct.fields.iter().enumerate() {
|
|
|
|
if is_ignored(&field) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let field_name = match field.ident {
|
|
|
|
Some(ref i) => quote!{ self.#i },
|
|
|
|
None => quote!{ self.#field_n },
|
|
|
|
};
|
|
|
|
|
|
|
|
let builder = quote! {
|
|
|
|
#field_name.new_handler()
|
|
|
|
};
|
|
|
|
|
|
|
|
match out_handler {
|
2020-02-24 16:56:36 +01:00
|
|
|
Some(h) => out_handler = Some(quote!{ #into_protocols_handler::select(#h, #builder) }),
|
2018-11-12 17:12:47 +01:00
|
|
|
ref mut h @ None => *h = Some(builder),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out_handler.unwrap_or(quote!{()}) // TODO: incorrect
|
|
|
|
};
|
|
|
|
|
|
|
|
// The method to use to poll.
|
|
|
|
// If we find a `#[behaviour(poll_method = "poll")]` attribute on the struct, we call
|
|
|
|
// `self.poll()` at the end of the polling.
|
|
|
|
let poll_method = {
|
2019-11-25 10:45:04 +01:00
|
|
|
let mut poll_method = quote!{std::task::Poll::Pending};
|
2018-11-12 17:12:47 +01:00
|
|
|
for meta_items in ast.attrs.iter().filter_map(get_meta_items) {
|
|
|
|
for meta_item in meta_items {
|
|
|
|
match meta_item {
|
2019-11-25 10:45:04 +01:00
|
|
|
syn::NestedMeta::Meta(syn::Meta::NameValue(ref m)) if m.path.is_ident("poll_method") => {
|
2018-11-12 17:12:47 +01:00
|
|
|
if let syn::Lit::Str(ref s) = m.lit {
|
|
|
|
let ident: Ident = syn::parse_str(&s.value()).unwrap();
|
2020-02-13 13:17:55 +03:00
|
|
|
poll_method = quote!{#name::#ident(self, cx, poll_params)};
|
2018-11-12 17:12:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
poll_method
|
|
|
|
};
|
|
|
|
|
|
|
|
// List of statements to put in `poll()`.
|
|
|
|
//
|
|
|
|
// We poll each child one by one and wrap around the output.
|
|
|
|
let poll_stmts = data_struct.fields.iter().enumerate().filter(|f| !is_ignored(&f.1)).enumerate().map(|(enum_n, (field_n, field))| {
|
|
|
|
let field_name = match field.ident {
|
|
|
|
Some(ref i) => quote!{ self.#i },
|
|
|
|
None => quote!{ self.#field_n },
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut wrapped_event = if enum_n != 0 {
|
|
|
|
quote!{ #either_ident::Second(event) }
|
|
|
|
} else {
|
|
|
|
quote!{ event }
|
|
|
|
};
|
2020-04-28 11:38:25 +02:00
|
|
|
for _ in 0 .. data_struct.fields.iter().filter(|f| !is_ignored(f)).count() - 1 - enum_n {
|
2018-11-12 17:12:47 +01:00
|
|
|
wrapped_event = quote!{ #either_ident::First(#wrapped_event) };
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(quote!{
|
|
|
|
loop {
|
2019-11-25 10:45:04 +01:00
|
|
|
match #field_name.poll(cx, poll_params) {
|
|
|
|
std::task::Poll::Ready(#network_behaviour_action::GenerateEvent(event)) => {
|
2018-12-20 15:21:13 +01:00
|
|
|
#net_behv_event_proc::inject_event(self, event)
|
2018-11-12 17:12:47 +01:00
|
|
|
}
|
2019-11-25 10:45:04 +01:00
|
|
|
std::task::Poll::Ready(#network_behaviour_action::DialAddress { address }) => {
|
|
|
|
return std::task::Poll::Ready(#network_behaviour_action::DialAddress { address });
|
2018-11-12 17:12:47 +01:00
|
|
|
}
|
2020-03-31 15:41:13 +02:00
|
|
|
std::task::Poll::Ready(#network_behaviour_action::DialPeer { peer_id, condition }) => {
|
|
|
|
return std::task::Poll::Ready(#network_behaviour_action::DialPeer { peer_id, condition });
|
2018-11-12 17:12:47 +01:00
|
|
|
}
|
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
|
|
|
std::task::Poll::Ready(#network_behaviour_action::NotifyHandler { peer_id, handler, event }) => {
|
|
|
|
return std::task::Poll::Ready(#network_behaviour_action::NotifyHandler {
|
2018-11-12 17:12:47 +01:00
|
|
|
peer_id,
|
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
|
|
|
handler,
|
2018-11-12 17:12:47 +01:00
|
|
|
event: #wrapped_event,
|
|
|
|
});
|
|
|
|
}
|
2019-11-25 10:45:04 +01:00
|
|
|
std::task::Poll::Ready(#network_behaviour_action::ReportObservedAddr { address }) => {
|
|
|
|
return std::task::Poll::Ready(#network_behaviour_action::ReportObservedAddr { address });
|
2018-12-01 13:34:57 +01:00
|
|
|
}
|
2019-11-25 10:45:04 +01:00
|
|
|
std::task::Poll::Pending => break,
|
2018-11-12 17:12:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
// Now the magic happens.
|
|
|
|
let final_quote = quote!{
|
2019-01-26 23:57:53 +01:00
|
|
|
impl #impl_generics #trait_to_impl for #name #ty_generics
|
2018-11-12 17:12:47 +01:00
|
|
|
#where_clause
|
|
|
|
{
|
|
|
|
type ProtocolsHandler = #protocols_handler_ty;
|
|
|
|
type OutEvent = #out_event;
|
|
|
|
|
|
|
|
fn new_handler(&mut self) -> Self::ProtocolsHandler {
|
2019-01-14 14:22:25 +01:00
|
|
|
use #into_protocols_handler;
|
2018-11-12 17:12:47 +01:00
|
|
|
#new_handler
|
|
|
|
}
|
|
|
|
|
2019-01-30 14:55:39 +01:00
|
|
|
fn addresses_of_peer(&mut self, peer_id: &#peer_id) -> Vec<#multiaddr> {
|
2019-01-26 23:57:53 +01:00
|
|
|
let mut out = Vec::new();
|
|
|
|
#(#addresses_of_peer_stmts);*
|
|
|
|
out
|
|
|
|
}
|
|
|
|
|
2020-03-31 15:41:13 +02:00
|
|
|
fn inject_connected(&mut self, peer_id: &#peer_id) {
|
2018-11-12 17:12:47 +01:00
|
|
|
#(#inject_connected_stmts);*
|
|
|
|
}
|
|
|
|
|
2020-03-31 15:41:13 +02:00
|
|
|
fn inject_disconnected(&mut self, peer_id: &#peer_id) {
|
2018-11-12 17:12:47 +01:00
|
|
|
#(#inject_disconnected_stmts);*
|
|
|
|
}
|
|
|
|
|
2020-03-31 15:41:13 +02:00
|
|
|
fn inject_connection_established(&mut self, peer_id: &#peer_id, connection_id: &#connection_id, endpoint: &#connected_point) {
|
|
|
|
#(#inject_connection_established_stmts);*
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inject_connection_closed(&mut self, peer_id: &#peer_id, connection_id: &#connection_id, endpoint: &#connected_point) {
|
|
|
|
#(#inject_connection_closed_stmts);*
|
|
|
|
}
|
|
|
|
|
2019-03-20 20:28:55 +01:00
|
|
|
fn inject_addr_reach_failure(&mut self, peer_id: Option<&#peer_id>, addr: &#multiaddr, error: &dyn std::error::Error) {
|
|
|
|
#(#inject_addr_reach_failure_stmts);*
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inject_dial_failure(&mut self, peer_id: &#peer_id) {
|
2019-01-30 14:55:39 +01:00
|
|
|
#(#inject_dial_failure_stmts);*
|
|
|
|
}
|
|
|
|
|
2019-04-16 15:36:08 +02:00
|
|
|
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);*
|
|
|
|
}
|
|
|
|
|
2019-04-16 17:00:20 +02:00
|
|
|
fn inject_new_external_addr(&mut self, addr: &#multiaddr) {
|
|
|
|
#(#inject_new_external_addr_stmts);*
|
|
|
|
}
|
|
|
|
|
2019-08-13 15:41:12 +02:00
|
|
|
fn inject_listener_error(&mut self, id: #listener_id, err: &(dyn std::error::Error + 'static)) {
|
|
|
|
#(#inject_listener_error_stmts);*
|
|
|
|
}
|
|
|
|
|
2020-04-01 22:49:42 +10:00
|
|
|
fn inject_listener_closed(&mut self, id: #listener_id, reason: Result<(), &std::io::Error>) {
|
2019-08-13 15:41:12 +02:00
|
|
|
#(#inject_listener_closed_stmts);*
|
|
|
|
}
|
|
|
|
|
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
|
|
|
fn inject_event(
|
2018-11-12 17:12:47 +01:00
|
|
|
&mut self,
|
|
|
|
peer_id: #peer_id,
|
Multiple connections per peer (#1440)
* Allow multiple connections per peer in libp2p-core.
Instead of trying to enforce a single connection per peer,
which involves quite a bit of additional complexity e.g.
to prioritise simultaneously opened connections and can
have other undesirable consequences [1], we now
make multiple connections per peer a feature.
The gist of these changes is as follows:
The concept of a "node" with an implicit 1-1 correspondence
to a connection has been replaced with the "first-class"
concept of a "connection". The code from `src/nodes` has moved
(with varying degrees of modification) to `src/connection`.
A `HandledNode` has become a `Connection`, a `NodeHandler` a
`ConnectionHandler`, the `CollectionStream` was the basis for
the new `connection::Pool`, and so forth.
Conceptually, a `Network` contains a `connection::Pool` which
in turn internally employs the `connection::Manager` for
handling the background `connection::manager::Task`s, one
per connection, as before. These are all considered implementation
details. On the public API, `Peer`s are managed as before through
the `Network`, except now the API has changed with the shift of focus
to (potentially multiple) connections per peer. The `NetworkEvent`s have
accordingly also undergone changes.
The Swarm APIs remain largely unchanged, except for the fact that
`inject_replaced` is no longer called. It may now practically happen
that multiple `ProtocolsHandler`s are associated with a single
`NetworkBehaviour`, one per connection. If implementations of
`NetworkBehaviour` rely somehow on communicating with exactly
one `ProtocolsHandler`, this may cause issues, but it is unlikely.
[1]: https://github.com/paritytech/substrate/issues/4272
* Fix intra-rustdoc links.
* Update core/src/connection/pool.rs
Co-Authored-By: Max Inden <mail@max-inden.de>
* Address some review feedback and fix doc links.
* Allow responses to be sent on the same connection.
* Remove unnecessary remainders of inject_replaced.
* Update swarm/src/behaviour.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update swarm/src/lib.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/manager.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Update core/src/connection/pool.rs
Co-Authored-By: Pierre Krieger <pierre.krieger1708@gmail.com>
* Incorporate more review feedback.
* Move module declaration below imports.
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Update core/src/connection/manager.rs
Co-Authored-By: Toralf Wittner <tw@dtex.org>
* Simplify as per review.
* Fix rustoc link.
* Add try_notify_handler and simplify.
* Relocate DialingConnection and DialingAttempt.
For better visibility constraints.
* Small cleanup.
* Small cleanup. More robust EstablishedConnectionIter.
* Clarify semantics of `DialingPeer::connect`.
* Don't call inject_disconnected on InvalidPeerId.
To preserve the previous behavior and ensure calls to
`inject_disconnected` are always paired with calls to
`inject_connected`.
* Provide public ConnectionId constructor.
Mainly needed for testing purposes, e.g. in substrate.
* Move the established connection limit check to the right place.
* Clean up connection error handling.
Separate connection errors into those occuring during
connection setup or upon rejecting a newly established
connection (the `PendingConnectionError`) and those
errors occurring on previously established connections,
i.e. for which a `ConnectionEstablished` event has
been emitted by the connection pool earlier.
* Revert change in log level and clarify an invariant.
* Remove inject_replaced entirely.
* Allow notifying all connection handlers.
Thereby simplify by introducing a new enum `NotifyHandler`,
used with a single constructor `NetworkBehaviourAction::NotifyHandler`.
* Finishing touches.
Small API simplifications and code deduplication.
Some more useful debug logging.
Co-authored-by: Max Inden <mail@max-inden.de>
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Toralf Wittner <tw@dtex.org>
2020-03-04 13:49:25 +01:00
|
|
|
connection_id: #connection_id,
|
2019-01-14 14:22:25 +01:00
|
|
|
event: <<Self::ProtocolsHandler as #into_protocols_handler>::Handler as #protocols_handler>::OutEvent
|
2018-11-12 17:12:47 +01:00
|
|
|
) {
|
|
|
|
match event {
|
|
|
|
#(#inject_node_event_stmts),*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-16 11:08:44 +02:00
|
|
|
fn poll(&mut self, cx: &mut std::task::Context, poll_params: &mut impl #poll_parameters) -> std::task::Poll<#network_behaviour_action<<<Self::ProtocolsHandler as #into_protocols_handler>::Handler as #protocols_handler>::InEvent, Self::OutEvent>> {
|
2018-11-12 17:12:47 +01:00
|
|
|
use libp2p::futures::prelude::*;
|
|
|
|
#(#poll_stmts)*
|
2019-09-16 11:08:44 +02:00
|
|
|
let f: std::task::Poll<#network_behaviour_action<<<Self::ProtocolsHandler as #into_protocols_handler>::Handler as #protocols_handler>::InEvent, Self::OutEvent>> = #poll_method;
|
2018-11-12 17:12:47 +01:00
|
|
|
f
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
final_quote.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_meta_items(attr: &syn::Attribute) -> Option<Vec<syn::NestedMeta>> {
|
|
|
|
if attr.path.segments.len() == 1 && attr.path.segments[0].ident == "behaviour" {
|
2019-11-25 10:45:04 +01:00
|
|
|
match attr.parse_meta() {
|
|
|
|
Ok(syn::Meta::List(ref meta)) => Some(meta.nested.iter().cloned().collect()),
|
|
|
|
Ok(_) => None,
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("error parsing attribute metadata: {}", e);
|
2018-11-12 17:12:47 +01:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true if a field is marked as ignored by the user.
|
|
|
|
fn is_ignored(field: &syn::Field) -> bool {
|
|
|
|
for meta_items in field.attrs.iter().filter_map(get_meta_items) {
|
|
|
|
for meta_item in meta_items {
|
|
|
|
match meta_item {
|
2019-11-25 10:45:04 +01:00
|
|
|
syn::NestedMeta::Meta(syn::Meta::Path(ref m)) if m.is_ident("ignore") => {
|
2018-11-12 17:12:47 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|