2021-09-08 00:36:52 +10:00
|
|
|
// 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;
|
2023-03-08 16:17:33 +08:00
|
|
|
use libp2p::{
|
|
|
|
core::transport::upgrade::Version,
|
|
|
|
identify, identity, noise, ping, rendezvous,
|
2023-03-13 20:53:14 +01:00
|
|
|
swarm::{keep_alive, NetworkBehaviour, SwarmBuilder, SwarmEvent},
|
2023-03-08 16:17:33 +08:00
|
|
|
tcp, yamux, Multiaddr, PeerId, Transport,
|
|
|
|
};
|
2021-09-08 00:36:52 +10:00
|
|
|
use std::time::Duration;
|
|
|
|
|
2021-09-25 18:39:49 +02:00
|
|
|
#[tokio::main]
|
2021-09-08 00:36:52 +10:00
|
|
|
async fn main() {
|
|
|
|
env_logger::init();
|
|
|
|
|
2023-03-08 16:17:33 +08:00
|
|
|
let key_pair = identity::Keypair::generate_ed25519();
|
2021-09-08 00:36:52 +10:00
|
|
|
let rendezvous_point_address = "/ip4/127.0.0.1/tcp/62649".parse::<Multiaddr>().unwrap();
|
|
|
|
let rendezvous_point = "12D3KooWDpJ7As7BWAwRMfu1VU2WCqNjvq387JEYKDBj4kx6nXTN"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
|
|
|
|
2023-03-13 20:53:14 +01:00
|
|
|
let mut swarm = SwarmBuilder::with_tokio_executor(
|
2023-03-08 16:17:33 +08:00
|
|
|
tcp::tokio::Transport::default()
|
2023-04-21 11:58:08 +02:00
|
|
|
.upgrade(Version::V1Lazy)
|
2023-04-28 15:50:31 +02:00
|
|
|
.authenticate(noise::Config::new(&key_pair).unwrap())
|
2023-05-01 04:25:52 +02:00
|
|
|
.multiplex(yamux::Config::default())
|
2022-12-13 07:58:01 +11:00
|
|
|
.boxed(),
|
2021-09-08 00:36:52 +10:00
|
|
|
MyBehaviour {
|
2022-10-04 01:17:31 +01:00
|
|
|
identify: identify::Behaviour::new(identify::Config::new(
|
2021-09-08 00:36:52 +10:00
|
|
|
"rendezvous-example/1.0.0".to_string(),
|
2023-03-08 16:17:33 +08:00
|
|
|
key_pair.public(),
|
2021-09-08 00:36:52 +10:00
|
|
|
)),
|
2023-03-08 16:17:33 +08:00
|
|
|
rendezvous: rendezvous::client::Behaviour::new(key_pair.clone()),
|
2022-10-06 03:50:11 +11:00
|
|
|
ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))),
|
|
|
|
keep_alive: keep_alive::Behaviour,
|
2021-09-08 00:36:52 +10:00
|
|
|
},
|
2023-03-08 16:17:33 +08:00
|
|
|
PeerId::from(key_pair.public()),
|
2023-03-13 20:53:14 +01:00
|
|
|
)
|
|
|
|
.build();
|
2021-09-08 00:36:52 +10:00
|
|
|
|
|
|
|
log::info!("Local peer id: {}", swarm.local_peer_id());
|
|
|
|
|
|
|
|
let _ = swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap());
|
|
|
|
|
2023-03-08 16:17:33 +08:00
|
|
|
swarm.dial(rendezvous_point_address.clone()).unwrap();
|
2021-09-08 00:36:52 +10:00
|
|
|
|
|
|
|
while let Some(event) = swarm.next().await {
|
|
|
|
match event {
|
|
|
|
SwarmEvent::NewListenAddr { address, .. } => {
|
|
|
|
log::info!("Listening on {}", address);
|
|
|
|
}
|
|
|
|
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
|
2023-03-08 16:17:33 +08:00
|
|
|
SwarmEvent::Behaviour(MyBehaviourEvent::Identify(identify::Event::Received {
|
|
|
|
..
|
|
|
|
})) => {
|
2023-06-17 14:19:41 +03:00
|
|
|
if let Err(error) = swarm.behaviour_mut().rendezvous.register(
|
2021-09-08 00:36:52 +10:00
|
|
|
rendezvous::Namespace::from_static("rendezvous"),
|
|
|
|
rendezvous_point,
|
|
|
|
None,
|
2023-06-17 14:19:41 +03:00
|
|
|
) {
|
|
|
|
log::error!("Failed to register: {error}");
|
|
|
|
return;
|
|
|
|
}
|
2021-09-08 00:36:52 +10:00
|
|
|
}
|
2023-03-08 16:17:33 +08:00
|
|
|
SwarmEvent::Behaviour(MyBehaviourEvent::Rendezvous(
|
|
|
|
rendezvous::client::Event::Registered {
|
|
|
|
namespace,
|
|
|
|
ttl,
|
|
|
|
rendezvous_node,
|
|
|
|
},
|
|
|
|
)) => {
|
2021-09-08 00:36:52 +10:00
|
|
|
log::info!(
|
|
|
|
"Registered for namespace '{}' at rendezvous point {} for the next {} seconds",
|
|
|
|
namespace,
|
|
|
|
rendezvous_node,
|
|
|
|
ttl
|
|
|
|
);
|
|
|
|
}
|
2023-03-08 16:17:33 +08:00
|
|
|
SwarmEvent::Behaviour(MyBehaviourEvent::Rendezvous(
|
2023-06-17 14:19:41 +03:00
|
|
|
rendezvous::client::Event::RegisterFailed {
|
|
|
|
rendezvous_node,
|
|
|
|
namespace,
|
|
|
|
error,
|
|
|
|
},
|
2021-09-08 00:36:52 +10:00
|
|
|
)) => {
|
2023-06-17 14:19:41 +03:00
|
|
|
log::error!(
|
|
|
|
"Failed to register: rendezvous_node={}, namespace={}, error_code={:?}",
|
|
|
|
rendezvous_node,
|
|
|
|
namespace,
|
|
|
|
error
|
|
|
|
);
|
2021-09-08 00:36:52 +10:00
|
|
|
return;
|
|
|
|
}
|
2023-03-08 16:17:33 +08:00
|
|
|
SwarmEvent::Behaviour(MyBehaviourEvent::Ping(ping::Event {
|
2021-09-08 00:36:52 +10:00
|
|
|
peer,
|
2023-05-24 14:33:18 +02:00
|
|
|
result: Ok(rtt),
|
|
|
|
..
|
2021-09-08 00:36:52 +10:00
|
|
|
})) if peer != rendezvous_point => {
|
|
|
|
log::info!("Ping to {} is {}ms", peer, rtt.as_millis())
|
|
|
|
}
|
|
|
|
other => {
|
|
|
|
log::debug!("Unhandled {:?}", other);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(NetworkBehaviour)]
|
|
|
|
struct MyBehaviour {
|
2022-10-04 01:17:31 +01:00
|
|
|
identify: identify::Behaviour,
|
2021-09-08 00:36:52 +10:00
|
|
|
rendezvous: rendezvous::client::Behaviour,
|
2022-10-01 00:19:34 +10:00
|
|
|
ping: ping::Behaviour,
|
2022-10-06 03:50:11 +11:00
|
|
|
keep_alive: keep_alive::Behaviour,
|
2021-09-08 00:36:52 +10:00
|
|
|
}
|