bee_gossip/peer/info.rs
1// Copyright 2020-2021 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use libp2p_core::Multiaddr;
5
6/// Additional information about a peer.
7#[derive(Clone, Debug)]
8pub struct PeerInfo {
9 /// The peer's address.
10 pub address: Multiaddr,
11 /// The peer's alias.
12 pub alias: String,
13 /// The type of relation regarding this peer.
14 pub relation: PeerRelation,
15}
16
17/// Describes the relation with a peer.
18#[derive(Clone, Copy, Debug, Eq, PartialEq)]
19pub enum PeerRelation {
20 /// Represents a known peer.
21 ///
22 /// If the connection to such a peer drops, the network will try to reconnect.
23 Known,
24 /// Represents an unknown peer.
25 ///
26 /// If the connection to such a peer drops, the network won't try to reconnect.
27 Unknown,
28 /// Represents a discovered peer.
29 ///
30 /// If the connection to such a peer drops, the network won't try to reconnect.
31 Discovered,
32}
33
34impl PeerRelation {
35 /// Returns whether the peer is known.
36 pub fn is_known(&self) -> bool {
37 matches!(self, Self::Known)
38 }
39
40 /// Returns whether the peer is unknown.
41 pub fn is_unknown(&self) -> bool {
42 matches!(self, Self::Unknown)
43 }
44
45 /// Returns whether the peer is discovered.
46 pub fn is_discovered(&self) -> bool {
47 matches!(self, Self::Discovered)
48 }
49
50 /// Sets the relation to "known".
51 pub fn set_known(&mut self) {
52 *self = Self::Known;
53 }
54
55 /// Sets the relation to "unknown".
56 pub fn set_unknown(&mut self) {
57 *self = Self::Unknown;
58 }
59
60 /// Sets the relation to "discovered".
61 pub fn set_discovered(&mut self) {
62 *self = Self::Discovered;
63 }
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn is_and_set_api() {
72 let mut pr = PeerRelation::Unknown;
73 assert!(pr.is_unknown());
74
75 pr.set_known();
76 assert!(pr.is_known());
77
78 pr.set_unknown();
79 assert!(pr.is_unknown());
80
81 pr.set_discovered();
82 assert!(pr.is_discovered())
83 }
84}