From bf7956eb532e21b0549b8bf4806490de577b21c6 Mon Sep 17 00:00:00 2001 From: mikelsr Date: Mon, 27 May 2024 13:23:50 +0200 Subject: [PATCH] DefaultSwarm to ww_net package --- src/main.rs | 31 ++----------------------------- ww_net/src/lib.rs | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/main.rs b/src/main.rs index b31478e..b51cb82 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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); - -impl Deref for DefaultSwarm { - type Target = swarm::Swarm; - - 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 = env::args().collect(); return args.iter().any(|arg| arg == "--kad-client"); @@ -82,7 +61,7 @@ async fn main() -> Result<(), Box> { .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() { @@ -123,9 +102,3 @@ async fn main() -> Result<(), Box> { } } } - -impl ww_net::net::Dialer for DefaultSwarm { - fn dial(&mut self, opts: DialOpts) -> Result<(), swarm::DialError> { - self.0.dial(opts) - } -} diff --git a/ww_net/src/lib.rs b/ww_net/src/lib.rs index 8309d1c..be59c49 100644 --- a/ww_net/src/lib.rs +++ b/ww_net/src/lib.rs @@ -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); + +impl Deref for DefaultSwarm { + type Target = swarm::Swarm; + + 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> { + self.0.select_next_some() + } +} #[derive(swarm::NetworkBehaviour)] #[behaviour(to_swarm = "DefaultBehaviourEvent")]