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
use std::convert::TryInto;
use std::str;
use crate::peer;
use crate::peerindex::Error;
use crate::service::Service;
pub static GROUP_NAME: &str = "index/principal";
lazy_static! {
static ref SERVICE: Service = Service::register("peerindex/principal");
}
pub async fn register(listener: Box<dyn Fn(&str, &str)>) {
peer::register_group(GROUP_NAME, listener).await;
SERVICE.send_info(&[]).await;
}
pub async fn instances() -> Result<Vec<String>, Error> {
SERVICE
.call(&[], |reply: &[u8]| {
if reply.len() < 4 {
return Err(Error::new(0));
}
let error = i16::from_le_bytes(reply[..2].try_into().unwrap());
if error != 0 {
return Err(Error::new(error));
}
let count = u16::from_le_bytes(reply[2..4].try_into().unwrap());
let mut list = Vec::with_capacity(count as usize);
let mut reply = &reply[4..];
for _ in 0..count {
let size = reply[0] as usize;
reply = &reply[1..];
list.push(String::from_utf8(reply[..size].into()).unwrap());
reply = &reply[size..];
}
Ok(list)
})
.await
}