Skip to content

Commit

Permalink
feat: implement mdns and rendezvous peer discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
fbozic committed May 20, 2024
1 parent b64b015 commit dd286fe
Show file tree
Hide file tree
Showing 11 changed files with 445 additions and 382 deletions.
3 changes: 3 additions & 0 deletions examples/chat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ license = "MIT OR Apache-2.0"
clap = { version = "4.5.4", features = ["derive", "env"] }
eyre = "0.6.12"
libp2p = { version = "0.53.2", features = [
"autonat",
"dcutr",
"dns",
"gossipsub",
"identify",
"macros",
"mdns",
"noise",
"ping",
"quic",
"rendezvous",
"relay",
"tokio",
"tcp",
Expand Down
33 changes: 14 additions & 19 deletions examples/chat/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ struct Opt {

/// The listening address of a relay server to connect to.
#[clap(long)]
relay_address: Multiaddr,
boot_nodes: Vec<Multiaddr>,

/// The listening address of a relay server to connect to.
#[clap(long, default_value = "/calimero/devnet/examples/chat")]
rendezvous_namespace: String,

/// Optional list of peer addresses to dial immediately after network bootstrap.
#[clap(long)]
Expand All @@ -53,7 +57,7 @@ async fn main() -> eyre::Result<()> {
tracing_subscriber::registry()
// "info,chat_example=debug,{}",
.with(EnvFilter::builder().parse(format!(
"info,chat_example=debug,{}",
"info,chat_example=info,libp2p_mdns=warn,{}",
std::env::var("RUST_LOG").unwrap_or_default()
))?)
.with(tracing_subscriber::fmt::layer())
Expand All @@ -63,8 +67,14 @@ async fn main() -> eyre::Result<()> {

let keypair = generate_ed25519(opt.secret_key_seed);

let (network_client, mut network_events) =
network::run(keypair.clone(), opt.port, opt.relay_address.clone()).await?;
let (network_client, mut network_events) = network::run(
keypair.clone(),
opt.port,
libp2p::rendezvous::Namespace::new(opt.rendezvous_namespace)?,
opt.boot_nodes.clone(),
opt.boot_nodes.clone(),
)
.await?;

if let Some(peer_addrs) = opt.dial_peer_addrs {
for addr in peer_addrs {
Expand Down Expand Up @@ -128,18 +138,6 @@ async fn handle_network_event(
is_echo: bool,
) -> eyre::Result<()> {
match event {
network::types::NetworkEvent::IdentifySent { peer_id } => {
debug!("Identify sent to {:?}", peer_id);
}
network::types::NetworkEvent::IdentifyReceived {
peer_id,
observed_addr,
} => {
debug!(
"Identify received from {:?} at {:?}",
peer_id, observed_addr
);
}
network::types::NetworkEvent::Message { message, .. } => {
let text = String::from_utf8_lossy(&message.data);
println!("{LINE_START} Received message: {:?}", text);
Expand All @@ -166,9 +164,6 @@ async fn handle_network_event(
network::types::NetworkEvent::ListeningOn { address, .. } => {
info!("Listening on: {}", address);
}
event => {
info!("Unhandled event: {:?}", event);
}
}
Ok(())
}
Expand Down
Loading

0 comments on commit dd286fe

Please sign in to comment.