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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use crate::{
protocols::{
direct_send::Message,
rpc::{error::RpcError, OutboundRpcRequest},
},
ProtocolId,
};
use aptos_types::{network_address::NetworkAddress, PeerId};
use bytes::Bytes;
use channel::{self, aptos_channel};
use futures::channel::oneshot;
use std::time::Duration;
use crate::peer_manager::{types::PeerManagerRequest, ConnectionRequest, PeerManagerError};
#[derive(Clone, Debug)]
pub struct PeerManagerRequestSender {
inner: aptos_channel::Sender<(PeerId, ProtocolId), PeerManagerRequest>,
}
#[derive(Clone, Debug)]
pub struct ConnectionRequestSender {
inner: aptos_channel::Sender<PeerId, ConnectionRequest>,
}
impl PeerManagerRequestSender {
pub fn new(inner: aptos_channel::Sender<(PeerId, ProtocolId), PeerManagerRequest>) -> Self {
Self { inner }
}
pub fn send_to(
&self,
peer_id: PeerId,
protocol_id: ProtocolId,
mdata: Bytes,
) -> Result<(), PeerManagerError> {
self.inner.push(
(peer_id, protocol_id),
PeerManagerRequest::SendDirectSend(peer_id, Message { protocol_id, mdata }),
)?;
Ok(())
}
pub fn send_to_many(
&self,
recipients: impl Iterator<Item = PeerId>,
protocol_id: ProtocolId,
mdata: Bytes,
) -> Result<(), PeerManagerError> {
let msg = Message { protocol_id, mdata };
for recipient in recipients {
self.inner.push(
(recipient, protocol_id),
PeerManagerRequest::SendDirectSend(recipient, msg.clone()),
)?;
}
Ok(())
}
pub async fn send_rpc(
&self,
peer_id: PeerId,
protocol_id: ProtocolId,
req: Bytes,
timeout: Duration,
) -> Result<Bytes, RpcError> {
let (res_tx, res_rx) = oneshot::channel();
let request = OutboundRpcRequest {
protocol_id,
data: req,
res_tx,
timeout,
};
self.inner.push(
(peer_id, protocol_id),
PeerManagerRequest::SendRpc(peer_id, request),
)?;
res_rx.await?
}
}
impl ConnectionRequestSender {
pub fn new(inner: aptos_channel::Sender<PeerId, ConnectionRequest>) -> Self {
Self { inner }
}
pub async fn dial_peer(
&self,
peer: PeerId,
addr: NetworkAddress,
) -> Result<(), PeerManagerError> {
let (oneshot_tx, oneshot_rx) = oneshot::channel();
self.inner
.push(peer, ConnectionRequest::DialPeer(peer, addr, oneshot_tx))?;
oneshot_rx.await?
}
pub async fn disconnect_peer(&self, peer: PeerId) -> Result<(), PeerManagerError> {
let (oneshot_tx, oneshot_rx) = oneshot::channel();
self.inner
.push(peer, ConnectionRequest::DisconnectPeer(peer, oneshot_tx))?;
oneshot_rx.await?
}
}