protocols/ping: Revise naming of symbols (#2215)

Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
Thomas Eizinger
2021-09-07 00:10:48 +10:00
committed by GitHub
parent 6924e5ef7a
commit c1ae8a046c
8 changed files with 140 additions and 123 deletions

View File

@ -40,62 +40,78 @@
//! [`Swarm`]: libp2p_swarm::Swarm
//! [`Transport`]: libp2p_core::Transport
pub mod handler;
pub mod protocol;
use handler::PingHandler;
pub use handler::{PingConfig, PingFailure, PingResult, PingSuccess};
mod handler;
mod protocol;
use handler::Handler;
pub use handler::{Config, Failure, Success};
use libp2p_core::{connection::ConnectionId, PeerId};
use libp2p_swarm::{NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use std::{collections::VecDeque, task::Context, task::Poll};
use std::{
collections::VecDeque,
task::{Context, Poll},
};
/// `Ping` is a [`NetworkBehaviour`] that responds to inbound pings and
#[deprecated(
since = "0.30.0",
note = "Use re-exports that omit `Ping` prefix, i.e. `libp2p::ping::Config` etc"
)]
pub use self::{
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;
/// The result of an inbound or outbound ping.
pub type Result = std::result::Result<Success, Failure>;
/// A [`NetworkBehaviour`] that responds to inbound pings and
/// periodically sends outbound pings on every established connection.
///
/// See the crate root documentation for more information.
pub struct Ping {
pub struct Behaviour {
/// Configuration for outbound pings.
config: PingConfig,
config: Config,
/// Queue of events to yield to the swarm.
events: VecDeque<PingEvent>,
events: VecDeque<Event>,
}
/// Event generated by the `Ping` network behaviour.
#[derive(Debug)]
pub struct PingEvent {
pub struct Event {
/// The peer ID of the remote.
pub peer: PeerId,
/// The result of an inbound or outbound ping.
pub result: PingResult,
pub result: Result,
}
impl Ping {
impl Behaviour {
/// Creates a new `Ping` network behaviour with the given configuration.
pub fn new(config: PingConfig) -> Self {
Ping {
pub fn new(config: Config) -> Self {
Self {
config,
events: VecDeque::new(),
}
}
}
impl Default for Ping {
impl Default for Behaviour {
fn default() -> Self {
Ping::new(PingConfig::new())
Self::new(Config::new())
}
}
impl NetworkBehaviour for Ping {
type ProtocolsHandler = PingHandler;
type OutEvent = PingEvent;
impl NetworkBehaviour for Behaviour {
type ProtocolsHandler = Handler;
type OutEvent = Event;
fn new_handler(&mut self) -> Self::ProtocolsHandler {
PingHandler::new(self.config.clone())
Handler::new(self.config.clone())
}
fn inject_event(&mut self, peer: PeerId, _: ConnectionId, result: PingResult) {
self.events.push_front(PingEvent { peer, result })
fn inject_event(&mut self, peer: PeerId, _: ConnectionId, result: Result) {
self.events.push_front(Event { peer, result })
}
fn poll(