protocols/ping: Properly deprecate types with Ping prefix (#2937)

Co-authored-by: Elena Frank <elena.frank@protonmail.com>
Co-authored-by:  João Oliveira <hello@jxs.pt>
This commit is contained in:
Thomas Eizinger
2022-10-01 00:19:34 +10:00
committed by GitHub
parent cce296e55e
commit 1da75b2b25
17 changed files with 162 additions and 159 deletions

View File

@ -42,8 +42,7 @@ use libp2p::{
identify::{Identify, IdentifyConfig, IdentifyEvent},
identity,
multiaddr::Protocol,
noise,
ping::{self, PingEvent},
noise, ping,
pnet::{PnetConfig, PreSharedKey},
swarm::SwarmEvent,
tcp::TcpTransport,
@ -165,7 +164,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
enum MyBehaviourEvent {
Gossipsub(GossipsubEvent),
Identify(IdentifyEvent),
Ping(PingEvent),
Ping(ping::Event),
}
impl From<GossipsubEvent> for MyBehaviourEvent {
@ -180,8 +179,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
}
impl From<PingEvent> for MyBehaviourEvent {
fn from(event: PingEvent) -> Self {
impl From<ping::Event> for MyBehaviourEvent {
fn from(event: ping::Event) -> Self {
MyBehaviourEvent::Ping(event)
}
}

View File

@ -52,7 +52,7 @@ use futures::executor::block_on;
use futures::stream::StreamExt;
use libp2p::core::Multiaddr;
use libp2p::metrics::{Metrics, Recorder};
use libp2p::ping::{Ping, PingConfig};
use libp2p::ping;
use libp2p::swarm::SwarmEvent;
use libp2p::{identity, PeerId, Swarm};
use prometheus_client::registry::Registry;
@ -72,7 +72,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut swarm = Swarm::new(
block_on(libp2p::development_transport(local_key))?,
Ping::new(PingConfig::new().with_keep_alive(true)),
ping::Behaviour::new(ping::Config::new().with_keep_alive(true)),
local_peer_id,
);

View File

@ -126,8 +126,8 @@ impl Recorder<libp2p_kad::KademliaEvent> for Metrics {
}
#[cfg(feature = "ping")]
impl Recorder<libp2p_ping::PingEvent> for Metrics {
fn record(&self, event: &libp2p_ping::PingEvent) {
impl Recorder<libp2p_ping::Event> for Metrics {
fn record(&self, event: &libp2p_ping::Event) {
self.ping.record(event)
}
}

View File

@ -29,16 +29,16 @@ struct FailureLabels {
reason: Failure,
}
impl From<&libp2p_ping::PingFailure> for FailureLabels {
fn from(failure: &libp2p_ping::PingFailure) -> Self {
impl From<&libp2p_ping::Failure> for FailureLabels {
fn from(failure: &libp2p_ping::Failure) -> Self {
match failure {
libp2p_ping::PingFailure::Timeout => FailureLabels {
libp2p_ping::Failure::Timeout => FailureLabels {
reason: Failure::Timeout,
},
libp2p_ping::PingFailure::Unsupported => FailureLabels {
libp2p_ping::Failure::Unsupported => FailureLabels {
reason: Failure::Unsupported,
},
libp2p_ping::PingFailure::Other { .. } => FailureLabels {
libp2p_ping::Failure::Other { .. } => FailureLabels {
reason: Failure::Other,
},
}
@ -92,13 +92,13 @@ impl Metrics {
}
}
impl super::Recorder<libp2p_ping::PingEvent> for Metrics {
fn record(&self, event: &libp2p_ping::PingEvent) {
impl super::Recorder<libp2p_ping::Event> for Metrics {
fn record(&self, event: &libp2p_ping::Event) {
match &event.result {
Ok(libp2p_ping::PingSuccess::Pong) => {
Ok(libp2p_ping::Success::Pong) => {
self.pong_received.inc();
}
Ok(libp2p_ping::PingSuccess::Ping { rtt }) => {
Ok(libp2p_ping::Success::Ping { rtt }) => {
self.rtt.observe(rtt.as_secs_f64());
}
Err(failure) => {

View File

@ -25,15 +25,14 @@ use futures::stream::StreamExt;
use libp2p::core::multiaddr::{Multiaddr, Protocol};
use libp2p::core::transport::OrTransport;
use libp2p::core::upgrade;
use libp2p::dcutr;
use libp2p::dns::DnsConfig;
use libp2p::identify::{Identify, IdentifyConfig, IdentifyEvent, IdentifyInfo};
use libp2p::noise;
use libp2p::ping::{Ping, PingConfig, PingEvent};
use libp2p::relay::v2::client::{self, Client};
use libp2p::swarm::{SwarmBuilder, SwarmEvent};
use libp2p::tcp::{GenTcpConfig, TcpTransport};
use libp2p::Transport;
use libp2p::{dcutr, ping};
use libp2p::{identity, NetworkBehaviour, PeerId};
use log::info;
use std::convert::TryInto;
@ -108,21 +107,21 @@ fn main() -> Result<(), Box<dyn Error>> {
#[behaviour(out_event = "Event", event_process = false)]
struct Behaviour {
relay_client: Client,
ping: Ping,
ping: ping::Behaviour,
identify: Identify,
dcutr: dcutr::behaviour::Behaviour,
}
#[derive(Debug)]
enum Event {
Ping(PingEvent),
Ping(ping::Event),
Identify(IdentifyEvent),
Relay(client::Event),
Dcutr(dcutr::behaviour::Event),
}
impl From<PingEvent> for Event {
fn from(e: PingEvent) -> Self {
impl From<ping::Event> for Event {
fn from(e: ping::Event) -> Self {
Event::Ping(e)
}
}
@ -147,7 +146,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let behaviour = Behaviour {
relay_client: client,
ping: Ping::new(PingConfig::new()),
ping: ping::Behaviour::new(ping::Config::new()),
identify: Identify::new(IdentifyConfig::new(
"/TODO/0.0.1".to_string(),
local_key.public(),

View File

@ -1,12 +1,15 @@
# 0.40.0 [unreleased]
- Bump rand to 0.8 and quickcheck to 1. See [PR 2857].
- Deprecate types with `Ping` prefix. Prefer importing them via the `ping` namespace, i.e. `libp2p::ping::Event` instead
of `libp2p::ping::PingEvent`. See [PR 2937].
- Update to `libp2p-core` `v0.37.0`.
- Update to `libp2p-swarm` `v0.40.0`.
[PR 2857]: https://github.com/libp2p/rust-libp2p/pull/2857
[PR 2937]: https://github.com/libp2p/rust-libp2p/pull/2937
# 0.39.0

View File

@ -56,7 +56,7 @@ pub struct Config {
}
impl Config {
/// Creates a new `PingConfig` with the following default settings:
/// Creates a new [`Config`] with the following default settings:
///
/// * [`Config::with_interval`] 15s
/// * [`Config::with_timeout`] 20s
@ -185,7 +185,7 @@ pub struct Handler {
/// Each successful ping resets this counter to 0.
failures: u32,
/// The outbound ping state.
outbound: Option<PingState>,
outbound: Option<OutboundState>,
/// The inbound pong handler, i.e. if there is an inbound
/// substream, this is always a future that waits for the
/// next inbound ping to be answered.
@ -208,7 +208,7 @@ enum State {
}
impl Handler {
/// Builds a new `PingHandler` with the given configuration.
/// Builds a new [`Handler`] with the given configuration.
pub fn new(config: Config) -> Self {
Handler {
config,
@ -241,7 +241,7 @@ impl ConnectionHandler for Handler {
fn inject_fully_negotiated_outbound(&mut self, stream: NegotiatedSubstream, (): ()) {
self.timer.reset(self.config.timeout);
self.outbound = Some(PingState::Ping(protocol::send_ping(stream).boxed()));
self.outbound = Some(OutboundState::Ping(protocol::send_ping(stream).boxed()));
}
fn inject_event(&mut self, _: Void) {}
@ -330,19 +330,19 @@ impl ConnectionHandler for Handler {
// Continue outbound pings.
match self.outbound.take() {
Some(PingState::Ping(mut ping)) => match ping.poll_unpin(cx) {
Some(OutboundState::Ping(mut ping)) => match ping.poll_unpin(cx) {
Poll::Pending => {
if self.timer.poll_unpin(cx).is_ready() {
self.pending_errors.push_front(Failure::Timeout);
} else {
self.outbound = Some(PingState::Ping(ping));
self.outbound = Some(OutboundState::Ping(ping));
break;
}
}
Poll::Ready(Ok((stream, rtt))) => {
self.failures = 0;
self.timer.reset(self.config.interval);
self.outbound = Some(PingState::Idle(stream));
self.outbound = Some(OutboundState::Idle(stream));
return Poll::Ready(ConnectionHandlerEvent::Custom(Ok(Success::Ping {
rtt,
})));
@ -352,22 +352,23 @@ impl ConnectionHandler for Handler {
.push_front(Failure::Other { error: Box::new(e) });
}
},
Some(PingState::Idle(stream)) => match self.timer.poll_unpin(cx) {
Some(OutboundState::Idle(stream)) => match self.timer.poll_unpin(cx) {
Poll::Pending => {
self.outbound = Some(PingState::Idle(stream));
self.outbound = Some(OutboundState::Idle(stream));
break;
}
Poll::Ready(()) => {
self.timer.reset(self.config.timeout);
self.outbound = Some(PingState::Ping(protocol::send_ping(stream).boxed()));
self.outbound =
Some(OutboundState::Ping(protocol::send_ping(stream).boxed()));
}
},
Some(PingState::OpenStream) => {
self.outbound = Some(PingState::OpenStream);
Some(OutboundState::OpenStream) => {
self.outbound = Some(OutboundState::OpenStream);
break;
}
None => {
self.outbound = Some(PingState::OpenStream);
self.outbound = Some(OutboundState::OpenStream);
let protocol = SubstreamProtocol::new(ReadyUpgrade::new(PROTOCOL_NAME), ())
.with_timeout(self.config.timeout);
return Poll::Ready(ConnectionHandlerEvent::OutboundSubstreamRequest {
@ -385,7 +386,7 @@ type PingFuture = BoxFuture<'static, Result<(NegotiatedSubstream, Duration), io:
type PongFuture = BoxFuture<'static, Result<NegotiatedSubstream, io::Error>>;
/// The current state w.r.t. outbound pings.
enum PingState {
enum OutboundState {
/// A new substream is being negotiated for the ping protocol.
OpenStream,
/// The substream is idle, waiting to send the next ping.

View File

@ -26,16 +26,16 @@
//!
//! # Usage
//!
//! The [`Ping`] struct implements the [`NetworkBehaviour`] trait. When used with a [`Swarm`],
//! The [`Behaviour`] struct implements the [`NetworkBehaviour`] trait. When used with a [`Swarm`],
//! it will respond to inbound ping requests and as necessary periodically send outbound
//! ping requests on every established connection. If a configurable number of consecutive
//! pings fail, the connection will be closed.
//!
//! The `Ping` network behaviour produces [`PingEvent`]s, which may be consumed from the `Swarm`
//! The [`Behaviour`] network behaviour produces [`Event`]s, which may be consumed from the [`Swarm`]
//! by an application, e.g. to collect statistics.
//!
//! > **Note**: The ping protocol does not keep otherwise idle connections alive
//! > by default, see [`PingConfig::with_keep_alive`] for changing this behaviour.
//! > by default, see [`Config::with_keep_alive`] for changing this behaviour.
//!
//! [`Swarm`]: libp2p_swarm::Swarm
//! [`Transport`]: libp2p_core::Transport
@ -52,16 +52,25 @@ use std::{
task::{Context, Poll},
};
#[deprecated(
since = "0.30.0",
note = "Use re-exports that omit `Ping` prefix, i.e. `libp2p::ping::Config` etc"
)]
pub use self::{
protocol::PROTOCOL_NAME, Config as PingConfig, Event as PingEvent, Failure as PingFailure,
Result as PingResult, Success as PingSuccess,
};
#[deprecated(since = "0.30.0", note = "Use libp2p::ping::Behaviour instead.")]
pub use Behaviour as Ping;
#[deprecated(since = "0.39.1", note = "Use libp2p::ping::Config instead.")]
pub type PingConfig = Config;
#[deprecated(since = "0.39.1", note = "Use libp2p::ping::Event instead.")]
pub type PingEvent = Event;
#[deprecated(since = "0.39.1", note = "Use libp2p::ping::Success instead.")]
pub type PingSuccess = Success;
#[deprecated(since = "0.39.1", note = "Use libp2p::ping::Failure instead.")]
pub type PingFailure = Failure;
#[deprecated(since = "0.39.1", note = "Use libp2p::ping::Result instead.")]
pub type PingResult = Result;
#[deprecated(since = "0.39.1", note = "Use libp2p::ping::Behaviour instead.")]
pub type Ping = Behaviour;
pub use self::protocol::PROTOCOL_NAME;
/// The result of an inbound or outbound ping.
pub type Result = std::result::Result<Success, Failure>;

View File

@ -25,13 +25,12 @@ use futures::stream::StreamExt;
use libp2p::core::upgrade;
use libp2p::identify::{Identify, IdentifyConfig, IdentifyEvent};
use libp2p::multiaddr::Protocol;
use libp2p::ping::{Ping, PingConfig, PingEvent};
use libp2p::relay::v2::relay::{self, Relay};
use libp2p::swarm::{Swarm, SwarmEvent};
use libp2p::tcp::TcpTransport;
use libp2p::Transport;
use libp2p::{identity, NetworkBehaviour, PeerId};
use libp2p::{noise, Multiaddr};
use libp2p::{ping, Transport};
use std::error::Error;
use std::net::{Ipv4Addr, Ipv6Addr};
@ -59,7 +58,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let behaviour = Behaviour {
relay: Relay::new(local_peer_id, Default::default()),
ping: Ping::new(PingConfig::new()),
ping: ping::Behaviour::new(ping::Config::new()),
identify: Identify::new(IdentifyConfig::new(
"/TODO/0.0.1".to_string(),
local_key.public(),
@ -96,19 +95,19 @@ fn main() -> Result<(), Box<dyn Error>> {
#[behaviour(out_event = "Event", event_process = false)]
struct Behaviour {
relay: Relay,
ping: Ping,
ping: ping::Behaviour,
identify: Identify,
}
#[derive(Debug)]
enum Event {
Ping(PingEvent),
Ping(ping::Event),
Identify(IdentifyEvent),
Relay(relay::Event),
}
impl From<PingEvent> for Event {
fn from(e: PingEvent) -> Self {
impl From<ping::Event> for Event {
fn from(e: ping::Event) -> Self {
Event::Ping(e)
}
}

View File

@ -29,12 +29,11 @@ use libp2p::core::transport::choice::OrTransport;
use libp2p::core::transport::{Boxed, MemoryTransport, Transport};
use libp2p::core::PublicKey;
use libp2p::core::{identity, upgrade, PeerId};
use libp2p::ping::{Ping, PingConfig, PingEvent};
use libp2p::plaintext::PlainText2Config;
use libp2p::relay::v2::client;
use libp2p::relay::v2::relay;
use libp2p::swarm::{AddressScore, NetworkBehaviour, Swarm, SwarmEvent};
use libp2p::NetworkBehaviour;
use libp2p::{ping, NetworkBehaviour};
use std::time::Duration;
#[test]
@ -217,7 +216,7 @@ fn connect() {
match src.select_next_some().await {
SwarmEvent::Dialing(peer_id) if peer_id == relay_peer_id => {}
SwarmEvent::ConnectionEstablished { peer_id, .. } if peer_id == relay_peer_id => {}
SwarmEvent::Behaviour(ClientEvent::Ping(PingEvent { peer, .. }))
SwarmEvent::Behaviour(ClientEvent::Ping(ping::Event { peer, .. }))
if peer == dst_peer_id =>
{
break
@ -225,7 +224,7 @@ fn connect() {
SwarmEvent::Behaviour(ClientEvent::Relay(
client::Event::OutboundCircuitEstablished { .. },
)) => {}
SwarmEvent::Behaviour(ClientEvent::Ping(PingEvent { peer, .. }))
SwarmEvent::Behaviour(ClientEvent::Ping(ping::Event { peer, .. }))
if peer == relay_peer_id => {}
SwarmEvent::ConnectionEstablished { peer_id, .. } if peer_id == dst_peer_id => {
break
@ -299,7 +298,7 @@ fn build_relay() -> Swarm<Relay> {
Swarm::new(
transport,
Relay {
ping: Ping::new(PingConfig::new()),
ping: ping::Behaviour::new(ping::Config::new()),
relay: relay::Relay::new(
local_peer_id,
relay::Config {
@ -326,7 +325,7 @@ fn build_client() -> Swarm<Client> {
Swarm::new(
transport,
Client {
ping: Ping::new(PingConfig::new()),
ping: ping::Behaviour::new(ping::Config::new()),
relay: behaviour,
},
local_peer_id,
@ -351,13 +350,13 @@ where
#[behaviour(out_event = "RelayEvent", event_process = false)]
struct Relay {
relay: relay::Relay,
ping: Ping,
ping: ping::Behaviour,
}
#[derive(Debug)]
enum RelayEvent {
Relay(relay::Event),
Ping(PingEvent),
Ping(ping::Event),
}
impl From<relay::Event> for RelayEvent {
@ -366,8 +365,8 @@ impl From<relay::Event> for RelayEvent {
}
}
impl From<PingEvent> for RelayEvent {
fn from(event: PingEvent) -> Self {
impl From<ping::Event> for RelayEvent {
fn from(event: ping::Event) -> Self {
RelayEvent::Ping(event)
}
}
@ -376,13 +375,13 @@ impl From<PingEvent> for RelayEvent {
#[behaviour(out_event = "ClientEvent", event_process = false)]
struct Client {
relay: client::Client,
ping: Ping,
ping: ping::Behaviour,
}
#[derive(Debug)]
enum ClientEvent {
Relay(client::Event),
Ping(PingEvent),
Ping(ping::Event),
}
impl From<client::Event> for ClientEvent {
@ -391,8 +390,8 @@ impl From<client::Event> for ClientEvent {
}
}
impl From<PingEvent> for ClientEvent {
fn from(event: PingEvent) -> Self {
impl From<ping::Event> for ClientEvent {
fn from(event: ping::Event) -> Self {
ClientEvent::Ping(event)
}
}

View File

@ -22,7 +22,7 @@ use futures::StreamExt;
use libp2p::core::identity;
use libp2p::core::PeerId;
use libp2p::multiaddr::Protocol;
use libp2p::ping::{Ping, PingConfig, PingEvent, PingSuccess};
use libp2p::ping;
use libp2p::swarm::SwarmEvent;
use libp2p::Swarm;
use libp2p::{development_transport, rendezvous, Multiaddr};
@ -44,8 +44,8 @@ async fn main() {
development_transport(identity.clone()).await.unwrap(),
MyBehaviour {
rendezvous: rendezvous::client::Behaviour::new(identity.clone()),
ping: Ping::new(
PingConfig::new()
ping: ping::Behaviour::new(
ping::Config::new()
.with_interval(Duration::from_secs(1))
.with_keep_alive(true),
),
@ -100,9 +100,9 @@ async fn main() {
}
}
}
SwarmEvent::Behaviour(MyEvent::Ping(PingEvent {
SwarmEvent::Behaviour(MyEvent::Ping(ping::Event {
peer,
result: Ok(PingSuccess::Ping { rtt }),
result: Ok(ping::Success::Ping { rtt }),
})) if peer != rendezvous_point => {
log::info!("Ping to {} is {}ms", peer, rtt.as_millis())
}
@ -124,7 +124,7 @@ async fn main() {
#[derive(Debug)]
enum MyEvent {
Rendezvous(rendezvous::client::Event),
Ping(PingEvent),
Ping(ping::Event),
}
impl From<rendezvous::client::Event> for MyEvent {
@ -133,8 +133,8 @@ impl From<rendezvous::client::Event> for MyEvent {
}
}
impl From<PingEvent> for MyEvent {
fn from(event: PingEvent) -> Self {
impl From<ping::Event> for MyEvent {
fn from(event: ping::Event) -> Self {
MyEvent::Ping(event)
}
}
@ -144,5 +144,5 @@ impl From<PingEvent> for MyEvent {
#[behaviour(out_event = "MyEvent")]
struct MyBehaviour {
rendezvous: rendezvous::client::Behaviour,
ping: Ping,
ping: ping::Behaviour,
}

View File

@ -21,7 +21,7 @@
use futures::StreamExt;
use libp2p::core::identity;
use libp2p::core::PeerId;
use libp2p::ping::{Ping, PingConfig, PingEvent, PingSuccess};
use libp2p::ping;
use libp2p::swarm::{Swarm, SwarmEvent};
use libp2p::{development_transport, rendezvous};
use libp2p::{Multiaddr, NetworkBehaviour};
@ -43,7 +43,7 @@ async fn main() {
development_transport(identity.clone()).await.unwrap(),
MyBehaviour {
rendezvous: rendezvous::client::Behaviour::new(identity.clone()),
ping: Ping::new(PingConfig::new().with_interval(Duration::from_secs(1))),
ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))),
},
PeerId::from(identity.public()),
);
@ -98,9 +98,9 @@ async fn main() {
log::error!("Failed to register {}", error);
return;
}
SwarmEvent::Behaviour(MyEvent::Ping(PingEvent {
SwarmEvent::Behaviour(MyEvent::Ping(ping::Event {
peer,
result: Ok(PingSuccess::Ping { rtt }),
result: Ok(ping::Success::Ping { rtt }),
})) if peer != rendezvous_point => {
log::info!("Ping to {} is {}ms", peer, rtt.as_millis())
}
@ -114,7 +114,7 @@ async fn main() {
#[derive(Debug)]
enum MyEvent {
Rendezvous(rendezvous::client::Event),
Ping(PingEvent),
Ping(ping::Event),
}
impl From<rendezvous::client::Event> for MyEvent {
@ -123,8 +123,8 @@ impl From<rendezvous::client::Event> for MyEvent {
}
}
impl From<PingEvent> for MyEvent {
fn from(event: PingEvent) -> Self {
impl From<ping::Event> for MyEvent {
fn from(event: ping::Event) -> Self {
MyEvent::Ping(event)
}
}
@ -134,5 +134,5 @@ impl From<PingEvent> for MyEvent {
#[behaviour(out_event = "MyEvent")]
struct MyBehaviour {
rendezvous: rendezvous::client::Behaviour,
ping: Ping,
ping: ping::Behaviour,
}

View File

@ -22,7 +22,7 @@ use futures::StreamExt;
use libp2p::core::identity;
use libp2p::core::PeerId;
use libp2p::identify::{Identify, IdentifyConfig, IdentifyEvent};
use libp2p::ping::{Ping, PingConfig, PingEvent, PingSuccess};
use libp2p::ping;
use libp2p::swarm::{Swarm, SwarmEvent};
use libp2p::{development_transport, rendezvous};
use libp2p::{Multiaddr, NetworkBehaviour};
@ -47,8 +47,8 @@ async fn main() {
identity.public(),
)),
rendezvous: rendezvous::client::Behaviour::new(identity.clone()),
ping: Ping::new(
PingConfig::new()
ping: ping::Behaviour::new(
ping::Config::new()
.with_interval(Duration::from_secs(1))
.with_keep_alive(true),
),
@ -100,9 +100,9 @@ async fn main() {
log::error!("Failed to register {}", error);
return;
}
SwarmEvent::Behaviour(MyEvent::Ping(PingEvent {
SwarmEvent::Behaviour(MyEvent::Ping(ping::Event {
peer,
result: Ok(PingSuccess::Ping { rtt }),
result: Ok(ping::Success::Ping { rtt }),
})) if peer != rendezvous_point => {
log::info!("Ping to {} is {}ms", peer, rtt.as_millis())
}
@ -117,7 +117,7 @@ async fn main() {
enum MyEvent {
Rendezvous(rendezvous::client::Event),
Identify(IdentifyEvent),
Ping(PingEvent),
Ping(ping::Event),
}
impl From<rendezvous::client::Event> for MyEvent {
@ -132,8 +132,8 @@ impl From<IdentifyEvent> for MyEvent {
}
}
impl From<PingEvent> for MyEvent {
fn from(event: PingEvent) -> Self {
impl From<ping::Event> for MyEvent {
fn from(event: ping::Event) -> Self {
MyEvent::Ping(event)
}
}
@ -144,5 +144,5 @@ impl From<PingEvent> for MyEvent {
struct MyBehaviour {
identify: Identify,
rendezvous: rendezvous::client::Behaviour,
ping: Ping,
ping: ping::Behaviour,
}

View File

@ -25,7 +25,6 @@ use libp2p::identify::Identify;
use libp2p::identify::IdentifyConfig;
use libp2p::identify::IdentifyEvent;
use libp2p::ping;
use libp2p::ping::{Ping, PingEvent};
use libp2p::swarm::{Swarm, SwarmEvent};
use libp2p::NetworkBehaviour;
use libp2p::{development_transport, rendezvous};
@ -54,7 +53,7 @@ async fn main() {
identity.public(),
)),
rendezvous: rendezvous::server::Behaviour::new(rendezvous::server::Config::default()),
ping: Ping::new(ping::Config::new().with_keep_alive(true)),
ping: ping::Behaviour::new(ping::Config::new().with_keep_alive(true)),
},
PeerId::from(identity.public()),
);
@ -104,7 +103,7 @@ async fn main() {
#[derive(Debug)]
enum MyEvent {
Rendezvous(rendezvous::server::Event),
Ping(PingEvent),
Ping(ping::Event),
Identify(IdentifyEvent),
}
@ -114,8 +113,8 @@ impl From<rendezvous::server::Event> for MyEvent {
}
}
impl From<PingEvent> for MyEvent {
fn from(event: PingEvent) -> Self {
impl From<ping::Event> for MyEvent {
fn from(event: ping::Event) -> Self {
MyEvent::Ping(event)
}
}
@ -132,5 +131,5 @@ impl From<IdentifyEvent> for MyEvent {
struct MyBehaviour {
identify: Identify,
rendezvous: rendezvous::server::Behaviour,
ping: Ping,
ping: ping::Behaviour,
}

View File

@ -127,12 +127,11 @@
//! _what_ bytes to send on the network.
//!
//! To make this more concrete, let's take a look at a simple implementation of
//! the [`NetworkBehaviour`] trait: the [`Ping`](crate::ping::Ping)
//! [`NetworkBehaviour`]. As you might have guessed, similar to the good old
//! `ping` network tool, libp2p [`Ping`](crate::ping::Ping) sends a ping to a
//! peer and expects to receive a pong in turn. The
//! [`Ping`](crate::ping::Ping) [`NetworkBehaviour`] does not care _how_ the
//! ping and pong messages are sent on the network, whether they are sent via
//! the [`NetworkBehaviour`] trait: the [`ping::Behaviour`](crate::ping::Behaviour).
//! As you might have guessed, similar to the good old `ping` network tool,
//! libp2p [`ping::Behaviour`](crate::ping::Behaviour) sends a ping to a peer and expects
//! to receive a pong in turn. The [`ping::Behaviour`](crate::ping::Behaviour) does not care _how_
//! the ping and pong messages are sent on the network, whether they are sent via
//! TCP, whether they are encrypted via [noise](crate::noise) or just in
//! [plaintext](crate::plaintext). It only cares about _what_ messages are sent
//! on the network.
@ -140,12 +139,10 @@
//! The two traits [`Transport`] and [`NetworkBehaviour`] allow us to cleanly
//! separate _how_ to send bytes from _what_ bytes to send.
//!
//! With the above in mind, let's extend our example, creating a
//! [`Ping`](crate::ping::Ping) [`NetworkBehaviour`] at the end:
//! With the above in mind, let's extend our example, creating a [`ping::Behaviour`](crate::ping::Behaviour) at the end:
//!
//! ```rust
//! use libp2p::{identity, PeerId};
//! use libp2p::ping::{Ping, PingConfig};
//! use libp2p::{identity, PeerId, ping};
//! use std::error::Error;
//!
//! #[async_std::main]
@ -161,7 +158,7 @@
//! // For illustrative purposes, the ping protocol is configured to
//! // keep the connection alive, so a continuous sequence of pings
//! // can be observed.
//! let behaviour = Ping::new(PingConfig::new().with_keep_alive(true));
//! let behaviour = ping::Behaviour::new(ping::Config::new().with_keep_alive(true));
//!
//! Ok(())
//! }
@ -177,8 +174,7 @@
//! [`Transport`] to the [`NetworkBehaviour`].
//!
//! ```rust
//! use libp2p::{identity, PeerId};
//! use libp2p::ping::{Ping, PingConfig};
//! use libp2p::{identity, PeerId, ping};
//! use libp2p::swarm::Swarm;
//! use std::error::Error;
//!
@ -195,7 +191,7 @@
//! // For illustrative purposes, the ping protocol is configured to
//! // keep the connection alive, so a continuous sequence of pings
//! // can be observed.
//! let behaviour = Ping::new(PingConfig::new().with_keep_alive(true));
//! let behaviour = ping::Behaviour::new(ping::Config::new().with_keep_alive(true));
//!
//! let mut swarm = Swarm::new(transport, behaviour, local_peer_id);
//!
@ -230,8 +226,7 @@
//! remote peer.
//!
//! ```rust
//! use libp2p::{identity, Multiaddr, PeerId};
//! use libp2p::ping::{Ping, PingConfig};
//! use libp2p::{identity, Multiaddr, PeerId, ping};
//! use libp2p::swarm::{Swarm, dial_opts::DialOpts};
//! use std::error::Error;
//!
@ -248,7 +243,7 @@
//! // For illustrative purposes, the ping protocol is configured to
//! // keep the connection alive, so a continuous sequence of pings
//! // can be observed.
//! let behaviour = Ping::new(PingConfig::new().with_keep_alive(true));
//! let behaviour = ping::Behaviour::new(ping::Config::new().with_keep_alive(true));
//!
//! let mut swarm = Swarm::new(transport, behaviour, local_peer_id);
//!
@ -276,9 +271,8 @@
//!
//! ```no_run
//! use futures::prelude::*;
//! use libp2p::ping::{Ping, PingConfig};
//! use libp2p::swarm::{Swarm, SwarmEvent, dial_opts::DialOpts};
//! use libp2p::{identity, Multiaddr, PeerId};
//! use libp2p::{identity, Multiaddr, PeerId, ping};
//! use std::error::Error;
//!
//! #[async_std::main]
@ -294,7 +288,7 @@
//! // For illustrative purposes, the ping protocol is configured to
//! // keep the connection alive, so a continuous sequence of pings
//! // can be observed.
//! let behaviour = Ping::new(PingConfig::new().with_keep_alive(true));
//! let behaviour = ping::Behaviour::new(ping::Config::new().with_keep_alive(true));
//!
//! let mut swarm = Swarm::new(transport, behaviour, local_peer_id);
//!

View File

@ -19,6 +19,7 @@
// DEALINGS IN THE SOFTWARE.
use futures::prelude::*;
use libp2p::ping;
use libp2p::swarm::{NetworkBehaviour, SwarmEvent};
use libp2p_swarm_derive::*;
use std::fmt::Debug;
@ -40,7 +41,7 @@ fn one_field() {
#[allow(dead_code)]
#[derive(NetworkBehaviour)]
struct Foo {
ping: libp2p::ping::Ping,
ping: ping::Behaviour,
}
#[allow(dead_code)]
@ -48,7 +49,7 @@ fn one_field() {
fn foo() {
let _out_event: <Foo as NetworkBehaviour>::OutEvent = unimplemented!();
match _out_event {
FooEvent::Ping(libp2p::ping::Event { .. }) => {}
FooEvent::Ping(ping::Event { .. }) => {}
}
}
}
@ -58,7 +59,7 @@ fn two_fields() {
#[allow(dead_code)]
#[derive(NetworkBehaviour)]
struct Foo {
ping: libp2p::ping::Ping,
ping: ping::Behaviour,
identify: libp2p::identify::Identify,
}
@ -67,7 +68,7 @@ fn two_fields() {
fn foo() {
let _out_event: <Foo as NetworkBehaviour>::OutEvent = unimplemented!();
match _out_event {
FooEvent::Ping(libp2p::ping::Event { .. }) => {}
FooEvent::Ping(ping::Event { .. }) => {}
FooEvent::Identify(event) => {
let _: libp2p::identify::IdentifyEvent = event;
}
@ -80,7 +81,7 @@ fn three_fields() {
#[allow(dead_code)]
#[derive(NetworkBehaviour)]
struct Foo {
ping: libp2p::ping::Ping,
ping: ping::Behaviour,
identify: libp2p::identify::Identify,
kad: libp2p::kad::Kademlia<libp2p::kad::record::store::MemoryStore>,
}
@ -90,7 +91,7 @@ fn three_fields() {
fn foo() {
let _out_event: <Foo as NetworkBehaviour>::OutEvent = unimplemented!();
match _out_event {
FooEvent::Ping(libp2p::ping::Event { .. }) => {}
FooEvent::Ping(ping::Event { .. }) => {}
FooEvent::Identify(event) => {
let _: libp2p::identify::IdentifyEvent = event;
}
@ -107,17 +108,17 @@ fn custom_event() {
#[derive(NetworkBehaviour)]
#[behaviour(out_event = "MyEvent")]
struct Foo {
ping: libp2p::ping::Ping,
ping: ping::Behaviour,
identify: libp2p::identify::Identify,
}
enum MyEvent {
Ping(libp2p::ping::PingEvent),
Ping(ping::Event),
Identify(libp2p::identify::IdentifyEvent),
}
impl From<libp2p::ping::PingEvent> for MyEvent {
fn from(event: libp2p::ping::PingEvent) -> Self {
impl From<ping::Event> for MyEvent {
fn from(event: ping::Event) -> Self {
MyEvent::Ping(event)
}
}
@ -140,17 +141,17 @@ fn custom_event_mismatching_field_names() {
#[derive(NetworkBehaviour)]
#[behaviour(out_event = "MyEvent")]
struct Foo {
a: libp2p::ping::Ping,
a: ping::Behaviour,
b: libp2p::identify::Identify,
}
enum MyEvent {
Ping(libp2p::ping::PingEvent),
Ping(ping::Event),
Identify(libp2p::identify::IdentifyEvent),
}
impl From<libp2p::ping::PingEvent> for MyEvent {
fn from(event: libp2p::ping::PingEvent) -> Self {
impl From<ping::Event> for MyEvent {
fn from(event: ping::Event) -> Self {
MyEvent::Ping(event)
}
}
@ -175,7 +176,7 @@ fn bound() {
where
<T as NetworkBehaviour>::OutEvent: Debug,
{
ping: libp2p::ping::Ping,
ping: ping::Behaviour,
bar: T,
}
}
@ -189,7 +190,7 @@ fn where_clause() {
T: Copy + NetworkBehaviour,
<T as NetworkBehaviour>::OutEvent: Debug,
{
ping: libp2p::ping::Ping,
ping: ping::Behaviour,
bar: T,
}
}
@ -199,7 +200,7 @@ fn nested_derives_with_import() {
#[allow(dead_code)]
#[derive(NetworkBehaviour)]
struct Foo {
ping: libp2p::ping::Ping,
ping: ping::Behaviour,
}
#[allow(dead_code)]
@ -213,7 +214,7 @@ fn nested_derives_with_import() {
fn foo() {
let _out_event: <Bar as NetworkBehaviour>::OutEvent = unimplemented!();
match _out_event {
BarEvent::Foo(FooEvent::Ping(libp2p::ping::Event { .. })) => {}
BarEvent::Foo(FooEvent::Ping(ping::Event { .. })) => {}
}
}
}
@ -221,12 +222,12 @@ fn nested_derives_with_import() {
#[test]
fn custom_event_emit_event_through_poll() {
enum BehaviourOutEvent {
Ping(libp2p::ping::PingEvent),
Ping(ping::Event),
Identify(libp2p::identify::IdentifyEvent),
}
impl From<libp2p::ping::PingEvent> for BehaviourOutEvent {
fn from(event: libp2p::ping::PingEvent) -> Self {
impl From<ping::Event> for BehaviourOutEvent {
fn from(event: ping::Event) -> Self {
BehaviourOutEvent::Ping(event)
}
}
@ -241,7 +242,7 @@ fn custom_event_emit_event_through_poll() {
#[derive(NetworkBehaviour)]
#[behaviour(out_event = "BehaviourOutEvent")]
struct Foo {
ping: libp2p::ping::Ping,
ping: ping::Behaviour,
identify: libp2p::identify::Identify,
}
@ -272,7 +273,7 @@ fn with_toggle() {
#[derive(NetworkBehaviour)]
struct Foo {
identify: libp2p::identify::Identify,
ping: Toggle<libp2p::ping::Ping>,
ping: Toggle<ping::Behaviour>,
}
#[allow(dead_code)]
@ -289,7 +290,7 @@ fn with_either() {
#[derive(NetworkBehaviour)]
struct Foo {
kad: libp2p::kad::Kademlia<libp2p::kad::record::store::MemoryStore>,
ping_or_identify: Either<libp2p::ping::Ping, libp2p::identify::Identify>,
ping_or_identify: Either<ping::Behaviour, libp2p::identify::Identify>,
}
#[allow(dead_code)]
@ -304,7 +305,7 @@ fn custom_event_with_either() {
enum BehaviourOutEvent {
Kad(libp2p::kad::KademliaEvent),
PingOrIdentify(Either<libp2p::ping::PingEvent, libp2p::identify::IdentifyEvent>),
PingOrIdentify(Either<ping::Event, libp2p::identify::IdentifyEvent>),
}
impl From<libp2p::kad::KademliaEvent> for BehaviourOutEvent {
@ -313,8 +314,8 @@ fn custom_event_with_either() {
}
}
impl From<Either<libp2p::ping::PingEvent, libp2p::identify::IdentifyEvent>> for BehaviourOutEvent {
fn from(event: Either<libp2p::ping::PingEvent, libp2p::identify::IdentifyEvent>) -> Self {
impl From<Either<ping::Event, libp2p::identify::IdentifyEvent>> for BehaviourOutEvent {
fn from(event: Either<ping::Event, libp2p::identify::IdentifyEvent>) -> Self {
BehaviourOutEvent::PingOrIdentify(event)
}
}
@ -324,7 +325,7 @@ fn custom_event_with_either() {
#[behaviour(out_event = "BehaviourOutEvent")]
struct Foo {
kad: libp2p::kad::Kademlia<libp2p::kad::record::store::MemoryStore>,
ping_or_identify: Either<libp2p::ping::Ping, libp2p::identify::Identify>,
ping_or_identify: Either<ping::Behaviour, libp2p::identify::Identify>,
}
#[allow(dead_code)]
@ -338,7 +339,7 @@ fn generated_out_event_derive_debug() {
#[allow(dead_code)]
#[derive(NetworkBehaviour)]
struct Foo {
ping: libp2p::ping::Ping,
ping: ping::Behaviour,
}
fn require_debug<T>()

View File

@ -91,18 +91,18 @@ pub(crate) type THandlerOutEvent<THandler> =
///
/// ``` rust
/// # use libp2p::identify::{Identify, IdentifyEvent};
/// # use libp2p::ping::{Ping, PingEvent};
/// # use libp2p::ping;
/// # use libp2p::NetworkBehaviour;
/// #[derive(NetworkBehaviour)]
/// #[behaviour(out_event = "Event")]
/// struct MyBehaviour {
/// identify: Identify,
/// ping: Ping,
/// ping: ping::Behaviour,
/// }
///
/// enum Event {
/// Identify(IdentifyEvent),
/// Ping(PingEvent),
/// Ping(ping::Event),
/// }
///
/// impl From<IdentifyEvent> for Event {
@ -111,8 +111,8 @@ pub(crate) type THandlerOutEvent<THandler> =
/// }
/// }
///
/// impl From<PingEvent> for Event {
/// fn from(event: PingEvent) -> Self {
/// impl From<ping::Event> for Event {
/// fn from(event: ping::Event) -> Self {
/// Self::Ping(event)
/// }
/// }