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
mod parser;
pub use parser::parse_json;
pub mod config;
use config::{Endpoint, Id, Network, Params};
use std::{collections::BTreeMap, net::SocketAddr};
pub struct NetBuilder {
id_ctr: Id,
endpoints: BTreeMap<Id, Endpoint>,
}
impl NetBuilder {
pub fn new() -> Self {
Self {
id_ctr: 0,
endpoints: BTreeMap::new(),
}
}
pub fn endpoint(mut self, epb: EpBuilder) -> Self {
let (id, ep) = epb.build(&mut self.id_ctr);
self.endpoints.insert(id, ep);
self
}
pub fn build(self) -> Network {
Network {
endpoints: self.endpoints,
patches: Default::default(),
}
}
}
pub struct EpBuilder {
p: Params,
}
impl EpBuilder {
pub fn virt() -> Self {
Self { p: Params::Virtual }
}
pub fn tcp(addr: String, port: u16, dynamic: bool) -> Self {
Self {
p: Params::Tcp {
addr,
port,
peers: vec![],
dynamic,
},
}
}
pub fn local_udp(addr: String) -> Self {
Self {
p: Params::LocalUpd { addr },
}
}
#[cfg(feature = "android")]
pub fn wifi_direct() -> Self {
Self {
p: Params::WifiDirect,
}
}
fn build(self, id: &mut Id) -> (Id, Endpoint) {
let this = *id;
*id += 1;
(
this,
Endpoint {
id: this,
params: self.p,
},
)
}
}