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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use futures::channel::{mpsc, oneshot};
use futures::SinkExt;
use async_trait::async_trait;
use libp2prs_core::routing::{IRouting, Routing};
use libp2prs_core::transport::TransportError;
use libp2prs_core::{Multiaddr, PeerId};
use crate::kad::{KBucketView, KademliaStats, StorageStats};
use crate::protocol::{KadMessengerView, KadPeer};
use crate::query::PeerRecord;
use crate::{record, KadError};
type Result<T> = std::result::Result<T, KadError>;
pub(crate) enum ControlCommand {
Bootstrap(Vec<(PeerId, Multiaddr)>, Option<oneshot::Sender<Result<()>>>),
Lookup(record::Key, oneshot::Sender<Result<Vec<KadPeer>>>),
FindPeer(PeerId, oneshot::Sender<Result<KadPeer>>),
FindProviders(record::Key, usize, oneshot::Sender<Result<Vec<KadPeer>>>),
Providing(record::Key, oneshot::Sender<Result<()>>),
Unprovide(record::Key),
PutValue(record::Key, Vec<u8>, oneshot::Sender<Result<()>>),
GetValue(record::Key, oneshot::Sender<Result<PeerRecord>>),
Dump(DumpCommand),
AddNode(PeerId, Vec<Multiaddr>),
RemoveNode(PeerId),
}
#[derive(Debug)]
pub enum DumpCommand {
Storage(oneshot::Sender<StorageStats>),
Entries(oneshot::Sender<Vec<KBucketView>>),
Statistics(oneshot::Sender<KademliaStats>),
Messengers(oneshot::Sender<Vec<KadMessengerView>>),
}
#[derive(Clone)]
pub struct Control {
control_sender: mpsc::UnboundedSender<ControlCommand>,
}
impl Control {
pub(crate) fn new(control_sender: mpsc::UnboundedSender<ControlCommand>) -> Self {
Control { control_sender }
}
pub fn close(&mut self) {
self.control_sender.close_channel();
}
pub async fn add_node(&mut self, peer_id: PeerId, addrs: Vec<Multiaddr>) {
let _ = self.control_sender.send(ControlCommand::AddNode(peer_id, addrs)).await;
}
pub async fn remove_node(&mut self, peer_id: PeerId) {
let _ = self.control_sender.send(ControlCommand::RemoveNode(peer_id)).await;
}
pub async fn dump_storage(&mut self) -> Result<StorageStats> {
let (tx, rx) = oneshot::channel();
self.control_sender.send(ControlCommand::Dump(DumpCommand::Storage(tx))).await?;
Ok(rx.await?)
}
pub async fn dump_kbuckets(&mut self) -> Result<Vec<KBucketView>> {
let (tx, rx) = oneshot::channel();
self.control_sender.send(ControlCommand::Dump(DumpCommand::Entries(tx))).await?;
Ok(rx.await?)
}
pub async fn dump_statistics(&mut self) -> Result<KademliaStats> {
let (tx, rx) = oneshot::channel();
self.control_sender.send(ControlCommand::Dump(DumpCommand::Statistics(tx))).await?;
Ok(rx.await?)
}
pub async fn dump_messengers(&mut self) -> Result<Vec<KadMessengerView>> {
let (tx, rx) = oneshot::channel();
self.control_sender.send(ControlCommand::Dump(DumpCommand::Messengers(tx))).await?;
Ok(rx.await?)
}
pub async fn bootstrap(&mut self, boot: Vec<(PeerId, Multiaddr)>) {
let _ = self.control_sender.send(ControlCommand::Bootstrap(boot, None)).await;
}
pub async fn bootstrap_wait(&mut self, boot: Vec<(PeerId, Multiaddr)>) -> Result<()> {
let (tx, rx) = oneshot::channel();
let _ = self.control_sender.send(ControlCommand::Bootstrap(boot, Some(tx))).await;
rx.await?
}
pub async fn lookup(&mut self, key: record::Key) -> Result<Vec<KadPeer>> {
let (tx, rx) = oneshot::channel();
self.control_sender.send(ControlCommand::Lookup(key, tx)).await?;
rx.await?
}
pub async fn find_peer(&mut self, peer_id: &PeerId) -> Result<KadPeer> {
let (tx, rx) = oneshot::channel();
self.control_sender.send(ControlCommand::FindPeer(*peer_id, tx)).await?;
rx.await?
}
pub async fn put_value(&mut self, key: Vec<u8>, value: Vec<u8>) -> Result<()> {
let (tx, rx) = oneshot::channel();
let key = record::Key::from(key);
self.control_sender.send(ControlCommand::PutValue(key, value, tx)).await?;
rx.await?
}
pub async fn get_value(&mut self, key: Vec<u8>) -> Result<Vec<u8>> {
let (tx, rx) = oneshot::channel();
let key = record::Key::from(key);
self.control_sender.send(ControlCommand::GetValue(key, tx)).await?;
rx.await?.map(|t| t.record.value)
}
pub async fn provide(&mut self, key: Vec<u8>) -> Result<()> {
let (tx, rx) = oneshot::channel();
let key = record::Key::from(key);
self.control_sender.send(ControlCommand::Providing(key, tx)).await?;
rx.await?
}
pub async fn unprovide(&mut self, key: Vec<u8>) -> Result<()> {
let key = record::Key::from(key);
self.control_sender.send(ControlCommand::Unprovide(key)).await?;
Ok(())
}
pub async fn find_providers(&mut self, key: Vec<u8>, count: usize) -> Result<Vec<KadPeer>> {
let (tx, rx) = oneshot::channel();
let key = record::Key::from(key);
self.control_sender.send(ControlCommand::FindProviders(key, count, tx)).await?;
rx.await?
}
}
#[async_trait]
impl Routing for Control {
async fn find_peer(&mut self, peer_id: &PeerId) -> std::result::Result<Vec<Multiaddr>, TransportError> {
let kad_peer = self.find_peer(peer_id).await.map_err(|e| TransportError::Routing(e.into()))?;
Ok(kad_peer.multiaddrs)
}
async fn find_providers(&mut self, key: Vec<u8>, count: usize) -> std::result::Result<Vec<PeerId>, TransportError> {
let addrs = self
.find_providers(key, count)
.await
.map_err(|e| TransportError::Routing(e.into()))?;
Ok(addrs.into_iter().map(|peer| peer.node_id).collect())
}
async fn provide(&mut self, key: Vec<u8>) -> std::result::Result<(), TransportError> {
let _ = self.provide(key).await.map_err(|e| TransportError::Routing(e.into()))?;
Ok(())
}
fn box_clone(&self) -> IRouting {
Box::new(self.clone())
}
}