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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//! The core kitsune implementation provided by Kitsune2.
use kitsune2_api::*;
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, RwLock};
/// The core kitsune implementation provided by Kitsune2.
/// You probably will have no reason to use something other than this.
/// This abstraction is mainly here for testing purposes.
#[derive(Debug)]
pub struct CoreKitsuneFactory {}
impl CoreKitsuneFactory {
/// Construct a new CoreKitsuneFactory.
pub fn create() -> DynKitsuneFactory {
let out: DynKitsuneFactory = Arc::new(CoreKitsuneFactory {});
out
}
}
impl KitsuneFactory for CoreKitsuneFactory {
fn default_config(&self, _config: &mut Config) -> K2Result<()> {
Ok(())
}
fn validate_config(&self, _config: &Config) -> K2Result<()> {
Ok(())
}
fn create(
&self,
builder: Arc<Builder>,
) -> BoxFut<'static, K2Result<DynKitsune>> {
Box::pin(async move {
let out: DynKitsune = Arc::new(CoreKitsune::new(builder));
Ok(out)
})
}
}
#[derive(Debug)]
struct TxHandlerTranslator(DynKitsuneHandler);
impl TxBaseHandler for TxHandlerTranslator {
fn new_listening_address(&self, this_url: Url) -> BoxFut<'static, ()> {
self.0.new_listening_address(this_url)
}
fn peer_disconnect(&self, peer: Url, reason: Option<String>) {
self.0.peer_disconnect(peer, reason);
}
}
impl TxHandler for TxHandlerTranslator {
fn preflight_gather_outgoing(
&self,
peer_url: Url,
) -> K2Result<bytes::Bytes> {
self.0.preflight_gather_outgoing(peer_url)
}
fn preflight_validate_incoming(
&self,
peer_url: Url,
data: bytes::Bytes,
) -> K2Result<()> {
self.0.preflight_validate_incoming(peer_url, data)
}
}
type SpaceFut = futures::future::Shared<BoxFut<'static, K2Result<DynSpace>>>;
type Map = HashMap<SpaceId, SpaceFut>;
#[derive(Debug)]
struct CoreKitsune {
builder: Arc<Builder>,
handler: std::sync::OnceLock<DynKitsuneHandler>,
map: std::sync::Mutex<Map>,
tx: std::sync::OnceLock<DynTransport>,
}
impl CoreKitsune {
pub fn new(builder: Arc<Builder>) -> Self {
Self {
builder,
handler: std::sync::OnceLock::new(),
map: std::sync::Mutex::new(HashMap::new()),
tx: std::sync::OnceLock::new(),
}
}
}
impl Kitsune for CoreKitsune {
fn register_handler(
&self,
handler: DynKitsuneHandler,
) -> BoxFut<'_, K2Result<()>> {
Box::pin(async move {
const ERR: &str = "handler already registered";
if self.handler.get().is_some() {
return Err(K2Error::other(ERR));
}
let tx = self
.builder
.transport
.create(
self.builder.clone(),
Arc::new(TxHandlerTranslator(handler.clone())),
)
.await?;
self.handler.set(handler).map_err(|_| K2Error::other(ERR))?;
self.tx.set(tx).map_err(|_| K2Error::other(ERR))?;
Ok(())
})
}
fn list_spaces(&self) -> Vec<SpaceId> {
self.map.lock().unwrap().keys().cloned().collect()
}
fn space(&self, space: SpaceId) -> BoxFut<'_, K2Result<DynSpace>> {
Box::pin(async move {
const ERR: &str = "handler not registered";
use std::collections::hash_map::Entry;
// This is quick, we don't hold the lock very long,
// because we're just constructing the future here,
// not awaiting it.
let fut = match self.map.lock().unwrap().entry(space.clone()) {
Entry::Occupied(e) => e.get().clone(),
Entry::Vacant(e) => {
let builder = self.builder.clone();
let handler = self
.handler
.get()
.ok_or_else(|| K2Error::other(ERR))?
.clone();
let tx = self
.tx
.get()
.ok_or_else(|| K2Error::other(ERR))?
.clone();
e.insert(futures::future::FutureExt::shared(Box::pin(
async move {
let sh =
handler.create_space(space.clone()).await?;
let s = builder
.space
.create(builder.clone(), sh, space, tx)
.await?;
Ok(s)
},
)))
.clone()
}
};
fut.await
})
}
fn space_if_exists(&self, space: SpaceId) -> BoxFut<'_, Option<DynSpace>> {
Box::pin(async move {
let fut = self.map.lock().unwrap().get(&space)?.clone();
fut.await.ok()
})
}
fn remove_space(&self, space: SpaceId) -> BoxFut<'_, K2Result<()>> {
Box::pin(async move {
let fut = self.map.lock().unwrap().get(&space).cloned();
if let Some(fut) = fut {
if let Ok(s) = fut.await {
// Just a point in time check, try to help the user out if they accidentally
// try to remove a space with active local agents.
// Those agents need to leave the space before it should be removed.
if !s.local_agent_store().get_all().await?.is_empty() {
return Err(K2Error::other(
"Cannot remove space with local agents",
));
}
// Checks passed, remove our reference to the space.
self.map.lock().unwrap().remove(&space);
// Unregister the space and its module handlers from the transport.
self.transport().await?.unregister_space(space).await;
// Get all the peer URLs of connected peers.
let connected_peer_urls = Arc::new(RwLock::new(
self.transport()
.await?
.get_connected_peers()
.await?
.into_iter()
.collect::<HashSet<_>>(),
));
// Remove any peer URLs that are connected for another space.
let spaces = self
.map
.lock()
.unwrap()
.iter()
.map(|s| s.1.clone())
.collect::<Vec<_>>();
futures::future::join_all(spaces.into_iter().map(
|fut| -> BoxFut<'_, K2Result<()>> {
let connected_peer_urls =
connected_peer_urls.clone();
Box::pin(async move {
if let Ok(s) = fut.await {
let check_our_peers = connected_peer_urls
.read()
.unwrap()
.clone();
let space_agent_infos =
s.peer_store().get_all().await?;
let to_remove = space_agent_infos
.into_iter()
.filter_map(|a| {
if let Some(url) = &a.url {
check_our_peers
.contains(url)
.then_some(url.clone())
} else {
None
}
})
.collect::<HashSet<_>>();
connected_peer_urls
.write()
.unwrap()
.retain(|u| !to_remove.contains(u));
}
Ok(())
})
},
))
.await;
// Disconnect from any remaining peers. I.e. those that aren't in use by
// another space.
let transport = self.transport().await?;
let to_remove = connected_peer_urls.read().unwrap().clone();
for url in to_remove {
tracing::info!(
"Disconnecting peer {url} from transport"
);
transport
.disconnect(
url.clone(),
Some("Space is being removed".to_string()),
)
.await;
}
} else {
tracing::warn!(
"Space exists, but failed to get a handle to it"
);
}
} else {
tracing::info!("Space {space:?} not found");
}
Ok(())
})
}
fn transport(&self) -> BoxFut<'_, K2Result<DynTransport>> {
Box::pin(async move {
self.tx
.get()
.cloned()
.ok_or_else(|| K2Error::other("Transport not registered yet"))
})
}
}
#[cfg(test)]
mod test {
use kitsune2_test_utils::space::TEST_SPACE_ID;
#[tokio::test(flavor = "multi_thread")]
async fn happy_space_construct() {
use kitsune2_api::*;
use std::sync::Arc;
#[derive(Debug)]
struct S;
impl SpaceHandler for S {
fn recv_notify(
&self,
_peer: Url,
_space: SpaceId,
_data: bytes::Bytes,
) -> K2Result<()> {
// this test is a bit of a stub for now until we have the
// transport module implemented and can send/receive messages.
Ok(())
}
}
#[derive(Debug)]
struct K;
impl KitsuneHandler for K {
fn create_space(
&self,
_space: SpaceId,
) -> BoxFut<'_, K2Result<DynSpaceHandler>> {
Box::pin(async move {
let s: DynSpaceHandler = Arc::new(S);
Ok(s)
})
}
}
let h: DynKitsuneHandler = Arc::new(K);
let k = crate::default_test_builder()
.with_default_config()
.unwrap()
.build()
.await
.unwrap();
k.register_handler(h).await.unwrap();
k.space(TEST_SPACE_ID).await.unwrap();
}
}