Skip to content

Commit

Permalink
DefaultSwarm to ww_net package
Browse files Browse the repository at this point in the history
  • Loading branch information
mikelsr committed May 27, 2024
1 parent 8721425 commit bf7956e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
31 changes: 2 additions & 29 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
use std::{env, error::Error, time::Duration};

use anyhow::Result;
use core::ops::{Deref, DerefMut};
use futures::StreamExt;
use libp2p::{
identify, identity, kad, mdns, noise, ping, swarm, swarm::dial_opts::DialOpts, tcp, yamux,
PeerId,
};
use libp2p::{identify, identity, kad, mdns, noise, ping, swarm, tcp, yamux, PeerId};
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
}
}

fn is_kad_client() -> bool {
let args: Vec<String> = env::args().collect();
return args.iter().any(|arg| arg == "--kad-client");
Expand Down Expand Up @@ -82,7 +61,7 @@ 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);
let mut swarm = ww_net::DefaultSwarm(raw_swarm);

// Configure Kademlia mode, defaults to server.
let kad_mode = if is_kad_client() {
Expand Down Expand Up @@ -123,9 +102,3 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
}
}

impl ww_net::net::Dialer for DefaultSwarm {
fn dial(&mut self, opts: DialOpts) -> Result<(), swarm::DialError> {
self.0.dial(opts)
}
}
34 changes: 33 additions & 1 deletion ww_net/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,38 @@
pub mod net;

use libp2p::swarm;
use core::ops::{Deref, DerefMut};

use futures::stream::SelectNextSome;
use futures::StreamExt;
use libp2p::{swarm, Swarm};

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

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

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

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

impl net::Dialer for DefaultSwarm {
fn dial(&mut self, opts: swarm::dial_opts::DialOpts) -> Result<(), swarm::DialError> {
self.0.dial(opts)
}
}

impl DefaultSwarm {
pub fn select_next_some(&mut self) -> SelectNextSome<'_, Swarm<DefaultBehaviour>> {
self.0.select_next_some()
}
}

#[derive(swarm::NetworkBehaviour)]
#[behaviour(to_swarm = "DefaultBehaviourEvent")]
Expand Down

0 comments on commit bf7956e

Please sign in to comment.