aboutsummaryrefslogtreecommitdiff
path: root/p2p/src/monitor/event.rs
blob: d320e374ed17f678bb2bd01085cdd8f97192b0cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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",
        }
    }
}