Skip to content

Commit

Permalink
Add register example that doesnt use the Identify protocol
Browse files Browse the repository at this point in the history
The external address is specified manually.
  • Loading branch information
rishflab committed Jul 8, 2021
1 parent 74a39f4 commit 4af8af7
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 28 deletions.
24 changes: 9 additions & 15 deletions protocols/rendezvous/examples/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
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::rendezvous::Rendezvous;
use libp2p::swarm::Swarm;
use libp2p::swarm::SwarmEvent;
use libp2p::{development_transport, rendezvous};
use libp2p::{Multiaddr, NetworkBehaviour};
use libp2p_rendezvous::Namespace;
use libp2p_swarm::AddressScore;
use std::time::Duration;

#[async_std::main]
Expand All @@ -45,16 +45,17 @@ async fn main() {
let mut swarm = Swarm::new(
development_transport(identity.clone()).await.unwrap(),
MyBehaviour {
identify: Identify::new(IdentifyConfig::new(
"rendezvous-example/1.0.0".to_string(),
identity.public(),
)),
rendezvous: Rendezvous::new(identity.clone(), rendezvous::Config::default()),
ping: Ping::new(PingConfig::new().with_interval(Duration::from_secs(1))),
},
PeerId::from(identity.public()),
);

// In production the external address should be the publicly facing IP address of the rendezvous point.
// This address is recorded in the registration entry by the rendezvous point.
let external_address = "/ip4/127.0.0.1/tcp/0".parse::<Multiaddr>().unwrap();
swarm.add_external_address(external_address, AddressScore::Infinite);

log::info!("Local peer id: {}", swarm.local_peer_id());

let _ = swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap());
Expand All @@ -73,14 +74,15 @@ async fn main() {
} if peer_id == rendezvous_point => {
log::error!("Lost connection to rendezvous point {}", error);
}
// once `/identify` did its job, we know our external address and can register
SwarmEvent::Behaviour(MyEvent::Identify(IdentifyEvent::Received { .. })) => {
SwarmEvent::ConnectionEstablished { peer_id, .. } if peer_id == rendezvous_point => {
swarm.behaviour_mut().rendezvous.register(
Namespace::from_static("rendezvous"),
rendezvous_point,
None,
);
log::info!("Connection established with rendezvous point {}", peer_id);
}
// once `/identify` did its job, we know our external address and can register
SwarmEvent::Behaviour(MyEvent::Rendezvous(rendezvous::Event::Registered {
namespace,
ttl,
Expand Down Expand Up @@ -115,7 +117,6 @@ async fn main() {
#[derive(Debug)]
enum MyEvent {
Rendezvous(rendezvous::Event),
Identify(IdentifyEvent),
Ping(PingEvent),
}

Expand All @@ -125,12 +126,6 @@ impl From<rendezvous::Event> for MyEvent {
}
}

impl From<IdentifyEvent> for MyEvent {
fn from(event: IdentifyEvent) -> Self {
MyEvent::Identify(event)
}
}

impl From<PingEvent> for MyEvent {
fn from(event: PingEvent) -> Self {
MyEvent::Ping(event)
Expand All @@ -141,7 +136,6 @@ impl From<PingEvent> for MyEvent {
#[behaviour(event_process = false)]
#[behaviour(out_event = "MyEvent")]
struct MyBehaviour {
identify: Identify,
rendezvous: Rendezvous,
ping: Ping,
}
147 changes: 147 additions & 0 deletions protocols/rendezvous/examples/register_with_identify.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Copyright 2021 COMIT Network.
//
// 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.

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::rendezvous::Rendezvous;
use libp2p::swarm::Swarm;
use libp2p::swarm::SwarmEvent;
use libp2p::{development_transport, rendezvous};
use libp2p::{Multiaddr, NetworkBehaviour};
use libp2p_rendezvous::Namespace;
use std::time::Duration;

#[async_std::main]
async fn main() {
env_logger::init();

let rendezvous_point_address = "/ip4/127.0.0.1/tcp/62649".parse::<Multiaddr>().unwrap();
let rendezvous_point = "12D3KooWDpJ7As7BWAwRMfu1VU2WCqNjvq387JEYKDBj4kx6nXTN"
.parse()
.unwrap();

let identity = identity::Keypair::generate_ed25519();

let mut swarm = Swarm::new(
development_transport(identity.clone()).await.unwrap(),
MyBehaviour {
identify: Identify::new(IdentifyConfig::new(
"rendezvous-example/1.0.0".to_string(),
identity.public(),
)),
rendezvous: Rendezvous::new(identity.clone(), rendezvous::Config::default()),
ping: Ping::new(PingConfig::new().with_interval(Duration::from_secs(1))),
},
PeerId::from(identity.public()),
);

log::info!("Local peer id: {}", swarm.local_peer_id());

let _ = swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap());

swarm.dial_addr(rendezvous_point_address).unwrap();

while let Some(event) = swarm.next().await {
match event {
SwarmEvent::NewListenAddr(addr) => {
log::info!("Listening on {}", addr);
}
SwarmEvent::ConnectionClosed {
peer_id,
cause: Some(error),
..
} if peer_id == rendezvous_point => {
log::error!("Lost connection to rendezvous point {}", error);
}
// once `/identify` did its job, we know our external address and can register
SwarmEvent::Behaviour(MyEvent::Identify(IdentifyEvent::Received { .. })) => {
swarm.behaviour_mut().rendezvous.register(
Namespace::from_static("rendezvous"),
rendezvous_point,
None,
);
}
SwarmEvent::Behaviour(MyEvent::Rendezvous(rendezvous::Event::Registered {
namespace,
ttl,
rendezvous_node,
})) => {
log::info!(
"Registered for namespace '{}' at rendezvous point {} for the next {} seconds",
namespace,
rendezvous_node,
ttl
);
}
SwarmEvent::Behaviour(MyEvent::Rendezvous(rendezvous::Event::RegisterFailed(
error,
))) => {
log::error!("Failed to register {}", error);
return;
}
SwarmEvent::Behaviour(MyEvent::Ping(PingEvent {
peer,
result: Ok(PingSuccess::Ping { rtt }),
})) if peer != rendezvous_point => {
log::info!("Ping to {} is {}ms", peer, rtt.as_millis())
}
other => {
log::debug!("Unhandled {:?}", other);
}
}
}
}

#[derive(Debug)]
enum MyEvent {
Rendezvous(rendezvous::Event),
Identify(IdentifyEvent),
Ping(PingEvent),
}

impl From<rendezvous::Event> for MyEvent {
fn from(event: rendezvous::Event) -> Self {
MyEvent::Rendezvous(event)
}
}

impl From<IdentifyEvent> for MyEvent {
fn from(event: IdentifyEvent) -> Self {
MyEvent::Identify(event)
}
}

impl From<PingEvent> for MyEvent {
fn from(event: PingEvent) -> Self {
MyEvent::Ping(event)
}
}

#[derive(NetworkBehaviour)]
#[behaviour(event_process = false)]
#[behaviour(out_event = "MyEvent")]
struct MyBehaviour {
identify: Identify,
rendezvous: Rendezvous,
ping: Ping,
}
13 changes: 0 additions & 13 deletions protocols/rendezvous/examples/rendezvous_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use futures::StreamExt;
use libp2p::core::identity;
use libp2p::core::PeerId;
use libp2p::identify::{Identify, IdentifyConfig, IdentifyEvent};
use libp2p::ping::{Ping, PingEvent};
use libp2p::rendezvous::Rendezvous;
use libp2p::swarm::{Swarm, SwarmEvent};
Expand All @@ -39,10 +38,6 @@ async fn main() {
let mut swarm = Swarm::new(
development_transport(identity.clone()).await.unwrap(),
MyBehaviour {
identify: Identify::new(IdentifyConfig::new(
"rendezvous-example/1.0.0".to_string(),
identity.public(),
)),
rendezvous: Rendezvous::new(identity.clone(), rendezvous::Config::default()),
ping: Ping::default(),
},
Expand Down Expand Up @@ -93,7 +88,6 @@ async fn main() {
#[derive(Debug)]
enum MyEvent {
Rendezvous(rendezvous::Event),
Identify(IdentifyEvent),
Ping(PingEvent),
}

Expand All @@ -103,12 +97,6 @@ impl From<rendezvous::Event> for MyEvent {
}
}

impl From<IdentifyEvent> for MyEvent {
fn from(event: IdentifyEvent) -> Self {
MyEvent::Identify(event)
}
}

impl From<PingEvent> for MyEvent {
fn from(event: PingEvent) -> Self {
MyEvent::Ping(event)
Expand All @@ -119,7 +107,6 @@ impl From<PingEvent> for MyEvent {
#[behaviour(event_process = false)]
#[behaviour(out_event = "MyEvent")]
struct MyBehaviour {
identify: Identify,
rendezvous: Rendezvous,
ping: Ping,
}

0 comments on commit 4af8af7

Please sign in to comment.