Skip to content

Commit

Permalink
examples: update ping example to use And
Browse files Browse the repository at this point in the history
instead of a custom structure that derives `NetworkBehaviour`.
  • Loading branch information
jxs committed Jan 21, 2023
1 parent 321e448 commit 6b729f9
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions examples/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
//! and begin pinging each other.

use futures::prelude::*;
use libp2p::swarm::{NetworkBehaviour, Swarm, SwarmEvent};
use libp2p::swarm::{Swarm, SwarmEvent};
use libp2p::{identity, ping, Multiaddr, PeerId};
use libp2p_swarm::keep_alive;
use libp2p_swarm::{behaviour::And, keep_alive};
use std::error::Error;

#[async_std::main]
Expand All @@ -54,7 +54,12 @@ async fn main() -> Result<(), Box<dyn Error>> {

let transport = libp2p::development_transport(local_key).await?;

let mut swarm = Swarm::with_async_std_executor(transport, Behaviour::default(), local_peer_id);
// Our network behaviour.
//
// For illustrative purposes, this includes the [`KeepAlive`](behaviour::KeepAlive) behaviour so a continuous sequence of
// pings can be observed.
let behaviour = And::<keep_alive::Behaviour, ping::Behaviour>::default();
let mut swarm = Swarm::with_async_std_executor(transport, behaviour, local_peer_id);

// Tell the swarm to listen on all interfaces and a random, OS-assigned
// port.
Expand All @@ -76,13 +81,3 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
}
}

/// Our network behaviour.
///
/// For illustrative purposes, this includes the [`KeepAlive`](behaviour::KeepAlive) behaviour so a continuous sequence of
/// pings can be observed.
#[derive(NetworkBehaviour, Default)]
struct Behaviour {
keep_alive: keep_alive::Behaviour,
ping: ping::Behaviour,
}

0 comments on commit 6b729f9

Please sign in to comment.