Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ec2/libp2p ping #91

Merged
merged 3 commits into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion node/ferret-libp2p/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@ use libp2p::core::identity::Keypair;
use libp2p::core::PeerId;
use libp2p::gossipsub::{Gossipsub, GossipsubConfig, GossipsubEvent, Topic, TopicHash};
use libp2p::mdns::{Mdns, MdnsEvent};
use libp2p::ping::{
handler::{PingFailure, PingSuccess},
Ping, PingEvent,
};
use libp2p::swarm::{NetworkBehaviourAction, NetworkBehaviourEventProcess};
use libp2p::tokio_io::{AsyncRead, AsyncWrite};
use libp2p::NetworkBehaviour;
use slog::{debug, Logger};

#[derive(NetworkBehaviour)]
#[behaviour(out_event = "MyBehaviourEvent", poll_method = "poll")]
pub struct MyBehaviour<TSubstream: AsyncRead + AsyncWrite> {
pub gossipsub: Gossipsub<TSubstream>,
pub mdns: Mdns<TSubstream>,
pub ping: Ping<TSubstream>,
#[behaviour(ignore)]
events: Vec<MyBehaviourEvent>,
#[behaviour(ignore)]
log: Logger,
}

pub enum MyBehaviourEvent {
Expand Down Expand Up @@ -61,6 +69,41 @@ impl<TSubstream: AsyncRead + AsyncWrite> NetworkBehaviourEventProcess<GossipsubE
}
}

impl<TSubstream: AsyncRead + AsyncWrite> NetworkBehaviourEventProcess<PingEvent>
for MyBehaviour<TSubstream>
{
fn inject_event(&mut self, event: PingEvent) {
match event.result {
Result::Ok(PingSuccess::Ping { rtt }) => {
debug!(
self.log,
"PingSuccess::Ping rtt to {} is {} ms",
event.peer.to_base58(),
rtt.as_millis()
);
}
Result::Ok(PingSuccess::Pong) => {
debug!(
self.log,
"PingSuccess::Pong from {}",
event.peer.to_base58()
);
}
Result::Err(PingFailure::Timeout) => {
debug!(self.log, "PingFailure::Timeout {}", event.peer.to_base58());
}
Result::Err(PingFailure::Other { error }) => {
debug!(
self.log,
"PingFailure::Other {}: {}",
event.peer.to_base58(),
error
);
}
}
}
}

impl<TSubstream: AsyncRead + AsyncWrite> MyBehaviour<TSubstream> {
/// Consumes the events list when polled.
fn poll<TBehaviourIn>(
Expand All @@ -74,12 +117,14 @@ impl<TSubstream: AsyncRead + AsyncWrite> MyBehaviour<TSubstream> {
}

impl<TSubstream: AsyncRead + AsyncWrite> MyBehaviour<TSubstream> {
pub fn new(local_key: &Keypair) -> Self {
pub fn new(log: Logger, local_key: &Keypair) -> Self {
let local_peer_id = local_key.public().into_peer_id();
let gossipsub_config = GossipsubConfig::default();
MyBehaviour {
gossipsub: Gossipsub::new(local_peer_id, gossipsub_config),
mdns: Mdns::new().expect("Failed to create mDNS service"),
ping: Ping::default(),
log,
events: vec![],
}
}
Expand Down
4 changes: 2 additions & 2 deletions node/ferret-libp2p/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Libp2pService {
let transport = build_transport(local_key.clone());

let mut swarm = {
let be = MyBehaviour::new(&local_key);
let be = MyBehaviour::new(log.clone(), &local_key);
Swarm::new(transport, be, local_peer_id)
};

Expand Down Expand Up @@ -102,7 +102,7 @@ pub enum NetworkEvent {
},
}

fn build_transport(local_key: identity::Keypair) -> Boxed<(PeerId, StreamMuxerBox), Error> {
pub fn build_transport(local_key: identity::Keypair) -> Boxed<(PeerId, StreamMuxerBox), Error> {
let transport = libp2p::tcp::TcpConfig::new().nodelay(true);
let transport = libp2p::dns::DnsConfig::new(transport);

Expand Down