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
use crate::application::{
storage::PeerMetadataStorage,
types::{PeerError, PeerInfo, PeerState},
};
use async_trait::async_trait;
use diem_types::PeerId;
use std::collections::{hash_map::Entry, HashMap};
#[async_trait]
pub trait NetworkInterface {
type Sender;
type AppData;
fn peer_metadata_storage(&self) -> &PeerMetadataStorage;
fn sender(&self) -> Self::Sender;
fn connected_peers(&self) -> HashMap<PeerId, PeerInfo> {
self.filtered_peers(|(_, peer_info)| peer_info.status == PeerState::Connected)
}
fn filtered_peers<F: FnMut(&(&PeerId, &PeerInfo)) -> bool>(
&self,
filter: F,
) -> HashMap<PeerId, PeerInfo> {
self.peer_metadata_storage().read_filtered(filter)
}
fn peers(&self) -> HashMap<PeerId, PeerInfo> {
self.peer_metadata_storage().read_all()
}
fn insert_app_data(&self, peer_id: PeerId, data: Self::AppData);
fn remove_app_data(&self, peer_id: &PeerId);
fn read_app_data(&self, peer_id: &PeerId) -> Option<Self::AppData>;
fn write_app_data<F: FnOnce(&mut Entry<PeerId, Self::AppData>) -> Result<(), PeerError>>(
&self,
peer_id: PeerId,
modifier: F,
) -> Result<(), PeerError>;
}