Skip to content

Commit

Permalink
Wrap Swarm type in order to implement Dialer trait
Browse files Browse the repository at this point in the history
  • Loading branch information
mikelsr committed Apr 17, 2024
1 parent 90681be commit 7acd9b1
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,34 @@ use std::{error::Error, time::Duration};

use anyhow::Result;
use futures::StreamExt;
use libp2p::{
PeerId,
swarm, identity, mdns, noise, ping, tcp, yamux,
swarm::dial_opts::DialOpts,
};
use libp2p::{identity, mdns, noise, ping, swarm, swarm::dial_opts::DialOpts, tcp, yamux, PeerId};
use std::ops::{Deref, DerefMut};
use tracing_subscriber::EnvFilter;

use ww_net;

struct DefaultSwarm(swarm::Swarm<ww_net::DefaultBehaviour>);

impl Deref for DefaultSwarm {
type Target = swarm::Swarm<ww_net::DefaultBehaviour>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for DefaultSwarm {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Start configuring a `fmt` subscriber
let subscriber = tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.compact() // use abbreviated log format
.compact() // use abbreviated log format
// .with_file(true)
// .with_thread_ids(true)
.with_max_level(tracing::Level::INFO)
Expand All @@ -39,7 +52,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
ping: ping_behaviour,
};

let mut swarm = libp2p::SwarmBuilder::with_existing_identity(id_keys)
let raw_swarm = libp2p::SwarmBuilder::with_existing_identity(id_keys)
.with_tokio()
.with_tcp(
tcp::Config::default(),
Expand All @@ -50,6 +63,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
.with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX)))
.build();

let mut swarm = DefaultSwarm(raw_swarm);

// Tell the swarm to listen on all interfaces and a random, OS-assigned
// port.
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
Expand All @@ -65,7 +80,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
swarm::SwarmEvent::Behaviour(ww_net::DefaultBehaviourEvent::Mdns(event)) => {
ww_net::mdns::default_handler(&mut swarm, event);
}

swarm::SwarmEvent::Behaviour(ww_net::DefaultBehaviourEvent::Ping(event)) => {
tracing::info!("got PING event: {event:?}");
}
Expand All @@ -76,7 +91,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
}

impl ww_net::mdns::Dialer for swarm::Swarm<ww_net::DefaultBehaviour> {
impl ww_net::mdns::Dialer for DefaultSwarm {
fn dial(&mut self, opts: DialOpts) -> Result<(), swarm::DialError> {
return self.dial(opts);
}
Expand Down

0 comments on commit 7acd9b1

Please sign in to comment.