bee_network/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 persistent peer. If the connection to such a peer drops, the network will try to reconnect.
21 Known,
22 /// Represents an ephemeral peer. If the connection to such a peer drops, the network won't try to reconnect.
23 Unknown,
24}
25
26impl PeerRelation {
27 /// Returns whether the peer is known.
28 pub fn is_known(&self) -> bool {
29 matches!(self, Self::Known)
30 }
31
32 /// Returns whether the peer is unknown.
33 pub fn is_unknown(&self) -> bool {
34 matches!(self, Self::Unknown)
35 }
36
37 /// Sets the relation to `PeerRelation::Known`.
38 pub fn set_known(&mut self) {
39 *self = Self::Known;
40 }
41
42 /// Sets the relation to `PeerRelation::Unknown`.
43 pub fn set_unknown(&mut self) {
44 *self = Self::Unknown;
45 }
46}
47
48#[cfg(test)]
49mod tests {
50 use super::*;
51
52 #[test]
53 fn is_and_set_api() {
54 let mut pr = PeerRelation::Unknown;
55 assert!(pr.is_unknown());
56
57 pr.set_known();
58 assert!(pr.is_known());
59
60 pr.set_unknown();
61 assert!(pr.is_unknown());
62 }
63}