Skip to content

Commit

Permalink
feat: split discovery into state and eventloop methods
Browse files Browse the repository at this point in the history
  • Loading branch information
fbozic committed May 31, 2024
1 parent 96a468d commit d7c4463
Show file tree
Hide file tree
Showing 8 changed files with 458 additions and 375 deletions.
32 changes: 20 additions & 12 deletions examples/chat/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use libp2p::{
use multiaddr::Multiaddr;
use tokio::sync::{mpsc, oneshot};
use tokio::time;
use tracing::{debug, info, trace, warn};
use tracing::{debug, trace, warn};

pub mod client;
pub mod discovery;
Expand Down Expand Up @@ -98,8 +98,7 @@ async fn init(
),
kad: {
let mut kademlia_config = kad::Config::default();
kademlia_config
.set_protocol_names(std::iter::once(CALIMERO_KAD_PROTO_NAME).collect());
kademlia_config.set_protocol_names(vec![CALIMERO_KAD_PROTO_NAME]);

let mut kademlia = kad::Behaviour::with_config(
peer_id,
Expand Down Expand Up @@ -148,7 +147,7 @@ pub(crate) struct EventLoop {
swarm: Swarm<Behaviour>,
command_receiver: mpsc::Receiver<Command>,
event_sender: mpsc::Sender<types::NetworkEvent>,
discovery_state: discovery::DiscoveryState,
discovery: discovery::Discovery,
pending_dial: HashMap<PeerId, oneshot::Sender<eyre::Result<Option<()>>>>,
}

Expand All @@ -163,13 +162,21 @@ impl EventLoop {
swarm,
command_receiver,
event_sender,
discovery_state: discovery::DiscoveryState::new(rendezvous_namespace),
// discovery_state: discovery::DiscoveryState::new(rendezvous_namespace, 0.5),
discovery: discovery::Discovery::new(discovery::DiscoveryConfig::new(
discovery::RendezvousConfig::new(
rendezvous_namespace,
Duration::from_secs(90),
0.5,
),
)),
pending_dial: Default::default(),
}
}

pub(crate) async fn run(mut self) {
let mut rendezvous_discover_tick = tokio::time::interval(Duration::from_secs(90));
let mut rendezvous_discover_tick =
tokio::time::interval(self.discovery.config.rendezvous.discovery_interval);

loop {
tokio::select! {
Expand Down Expand Up @@ -247,7 +254,7 @@ impl EventLoop {

let _ = sender.send(Ok(id));
}
Command::PeerInfo { sender } => {
Command::PeersInfo { sender } => {
let peers = self
.swarm
.connected_peers()
Expand All @@ -257,7 +264,8 @@ impl EventLoop {
let count = peers.len();

let discovered_peers = self
.discovery_state
.discovery
.state
.get_peers()
.map(|(id, peer)| (id.clone(), peer.clone()))
.collect::<Vec<_>>();
Expand All @@ -270,7 +278,7 @@ impl EventLoop {
discovered_peers,
});
}
Command::MeshPeerCount { topic, sender } => {
Command::MeshPeersCount { topic, sender } => {
let peers = self
.swarm
.behaviour_mut()
Expand Down Expand Up @@ -309,10 +317,10 @@ pub(crate) enum Command {
data: Vec<u8>,
sender: oneshot::Sender<eyre::Result<gossipsub::MessageId>>,
},
PeerInfo {
PeersInfo {
sender: oneshot::Sender<PeersInfo>,
},
MeshPeerCount {
MeshPeersCount {
topic: gossipsub::TopicHash,
sender: oneshot::Sender<MeshPeersInfo>,
},
Expand All @@ -324,7 +332,7 @@ pub(crate) struct PeersInfo {
count: usize,
peers: Vec<PeerId>,
discovered_count: usize,
discovered_peers: Vec<(PeerId, discovery::PeerInfo)>,
discovered_peers: Vec<(PeerId, discovery::state::PeerInfo)>,
}

#[allow(dead_code)] // Info structs for pretty printing
Expand Down
4 changes: 2 additions & 2 deletions examples/chat/src/network/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl NetworkClient {
let (sender, receiver) = oneshot::channel();

self.sender
.send(Command::PeerInfo { sender })
.send(Command::PeersInfo { sender })
.await
.expect("Command receiver not to be dropped.");

Expand All @@ -92,7 +92,7 @@ impl NetworkClient {
let (sender, receiver) = oneshot::channel();

self.sender
.send(Command::MeshPeerCount { topic, sender })
.send(Command::MeshPeersCount { topic, sender })
.await
.expect("Command receiver not to be dropped.");

Expand Down
Loading

0 comments on commit d7c4463

Please sign in to comment.