Skip to content

Commit

Permalink
p2p/monitor: use struct instead of enum for monitor events
Browse files Browse the repository at this point in the history
  • Loading branch information
hozan23 committed Jun 24, 2024
1 parent 135968d commit e3d1f4f
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 211 deletions.
8 changes: 4 additions & 4 deletions p2p/src/discovery/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
get_msg_payload, FindPeerMsg, NetMsg, NetMsgCmd, PeerMsg, PeersMsg, PingMsg, PongMsg,
ShutdownMsg,
},
monitor::{ConnEvent, DiscoveryEvent, Monitor},
monitor::{ConnEvent, DiscvEvent, Monitor},
routing_table::RoutingTable,
slots::ConnectionSlots,
version::version_match,
Expand Down Expand Up @@ -131,7 +131,7 @@ impl LookupService {
pub async fn start_lookup(&self, endpoint: &Endpoint, peer_id: Option<PeerID>) -> Result<()> {
trace!("Lookup started {endpoint}");
self.monitor
.notify(DiscoveryEvent::LookupStarted(endpoint.clone()))
.notify(DiscvEvent::LookupStarted(endpoint.clone()))
.await;

let mut random_peers = vec![];
Expand All @@ -140,7 +140,7 @@ impl LookupService {
.await
{
self.monitor
.notify(DiscoveryEvent::LookupFailed(endpoint.clone()))
.notify(DiscvEvent::LookupFailed(endpoint.clone()))
.await;
return Err(err);
};
Expand All @@ -161,7 +161,7 @@ impl LookupService {
}

self.monitor
.notify(DiscoveryEvent::LookupSucceeded(
.notify(DiscvEvent::LookupSucceeded(
endpoint.clone(),
peer_buffer.len(),
))
Expand Down
4 changes: 2 additions & 2 deletions p2p/src/discovery/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use karyon_net::{udp, Connection, Endpoint, Error as NetError};
use crate::{
codec::RefreshMsgCodec,
message::RefreshMsg,
monitor::{ConnEvent, DiscoveryEvent, Monitor},
monitor::{ConnEvent, DiscvEvent, Monitor},
routing_table::{BucketEntry, Entry, RoutingTable, PENDING_ENTRY, UNREACHABLE_ENTRY},
Config, Error, Result,
};
Expand Down Expand Up @@ -116,7 +116,7 @@ impl RefreshService {
sleep(Duration::from_secs(self.config.refresh_interval)).await;
trace!("Start refreshing the routing table...");

self.monitor.notify(DiscoveryEvent::RefreshStarted).await;
self.monitor.notify(DiscvEvent::RefreshStarted).await;

let mut entries: Vec<BucketEntry> = vec![];
for bucket in self.table.buckets() {
Expand Down
198 changes: 0 additions & 198 deletions p2p/src/monitor.rs

This file was deleted.

95 changes: 95 additions & 0 deletions p2p/src/monitor/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
use karyon_net::Endpoint;

use crate::PeerID;

/// Defines connection-related events.
#[derive(Clone, Debug)]
pub enum ConnEvent {
Connected(Endpoint),
ConnectRetried(Endpoint),
ConnectFailed(Endpoint),
Accepted(Endpoint),
AcceptFailed,
Disconnected(Endpoint),
Listening(Endpoint),
ListenFailed(Endpoint),
}

/// Defines `PP` events.
#[derive(Clone, Debug)]
pub enum PPEvent {
NewPeer(PeerID),
RemovePeer(PeerID),
}

/// Defines `Discovery` events.
#[derive(Clone, Debug)]
pub enum DiscvEvent {
LookupStarted(Endpoint),
LookupFailed(Endpoint),
LookupSucceeded(Endpoint, usize),
RefreshStarted,
}

impl ConnEvent {
pub(super) fn get_endpoint(&self) -> Option<&Endpoint> {
match self {
ConnEvent::Connected(endpoint)
| ConnEvent::ConnectRetried(endpoint)
| ConnEvent::ConnectFailed(endpoint)
| ConnEvent::Accepted(endpoint)
| ConnEvent::Disconnected(endpoint)
| ConnEvent::Listening(endpoint)
| ConnEvent::ListenFailed(endpoint) => Some(endpoint),
ConnEvent::AcceptFailed => None,
}
}

pub(super) fn variant_name(&self) -> &'static str {
match self {
ConnEvent::Connected(_) => "Connected",
ConnEvent::ConnectRetried(_) => "ConnectRetried",
ConnEvent::ConnectFailed(_) => "ConnectFailed",
ConnEvent::Accepted(_) => "Accepted",
ConnEvent::AcceptFailed => "AcceptFailed",
ConnEvent::Disconnected(_) => "Disconnected",
ConnEvent::Listening(_) => "Listening",
ConnEvent::ListenFailed(_) => "ListenFailed",
}
}
}

impl PPEvent {
pub(super) fn get_peer_id(&self) -> Option<&PeerID> {
match self {
PPEvent::NewPeer(peer_id) | PPEvent::RemovePeer(peer_id) => Some(peer_id),
}
}
pub(super) fn variant_name(&self) -> &'static str {
match self {
PPEvent::NewPeer(_) => "NewPeer",
PPEvent::RemovePeer(_) => "RemovePeer",
}
}
}

impl DiscvEvent {
pub(super) fn get_endpoint_and_size(&self) -> (Option<&Endpoint>, Option<usize>) {
match self {
DiscvEvent::LookupStarted(endpoint) | DiscvEvent::LookupFailed(endpoint) => {
(Some(endpoint), None)
}
DiscvEvent::LookupSucceeded(endpoint, size) => (Some(endpoint), Some(*size)),
DiscvEvent::RefreshStarted => (None, None),
}
}

pub(super) fn variant_name(&self) -> &'static str {
match self {
DiscvEvent::LookupStarted(_) => "LookupStarted",
DiscvEvent::LookupFailed(_) => "LookupFailed",
DiscvEvent::LookupSucceeded(_, _) => "LookupSucceeded",
DiscvEvent::RefreshStarted => "RefreshStarted",
}
}
}
Loading

0 comments on commit e3d1f4f

Please sign in to comment.