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
mod links;
mod packet;
mod protocols;
mod route;
use crate::link::Link;
use alloc::boxed::Box;
use alloc::sync::Arc;
use links::Links;
pub use packet::Id;
use packet::{Field, Packet};
use protocols::{Protocols, RecvCallback};
use route::Routes;
pub const ID_PARENT: Id = [0; 8];
pub const ID_CHILD: Id = [0xFF; 8];
struct InnerNet {
relay: bool,
id: Id,
routes: spin::RwLock<Routes>,
links: spin::RwLock<Links>,
protocols: spin::RwLock<Protocols>,
}
#[derive(Clone)]
pub struct Net {
inner: Arc<InnerNet>,
}
impl Net {
pub fn new(id: &Id, relay: bool) -> Self {
let inner = InnerNet {
relay: relay,
id: id.clone(),
routes: spin::RwLock::new(Routes::new()),
links: spin::RwLock::new(Links::new()),
protocols: spin::RwLock::new(Protocols::new()),
};
Net {
inner: Arc::new(inner),
}
}
pub fn run(&self) {
let net = &self.inner;
let links = net.links.read();
links.recv();
}
pub fn add_link(&self, link: Link, is_parent: bool) {
let net_tmp = self.clone();
link.set_recv_cb(Box::new(move |link: Link, load: &[u8]| {
net_tmp.recv(link, load);
}));
let net = &self.inner;
let mut links = net.links.write();
links.add(link, is_parent);
}
pub fn register_protocol(&self, id: u8, cb: Box<RecvCallback>) {
let net = &self.inner;
let mut protocols = net.protocols.write();
protocols.add_protocol(id, cb);
}
fn send_common(
&self,
packet: &mut Packet,
dst: Option<&Id>,
dst_link: Option<&Link>,
origin: Option<&Link>,
) {
let net = &self.inner;
let mut link: Option<Link> = None;
let id: &Id = if let Some(id) = dst {
packet.set_var(Field::Dst, id);
id
} else {
&packet.dst
};
if let None = dst_link {
let routes = net.routes.read();
if let Some(l) = routes.find(id) {
link = Some(l);
}
}
let buf = packet.to_buf();
if let Some(link) = link {
if let Some(origin) = origin {
if origin.eq(&link) {
return;
}
}
link.send(&buf);
} else {
if let Some(origin) = origin {
for parent in &net.links.read().parents {
if origin.eq(&parent) {
return;
}
}
}
for parent in &net.links.read().parents {
parent.send(&buf);
}
}
}
pub fn send(&self, protocol: u8, data: &[u8], id: Option<&Id>, link: Option<&Link>) {
let mut packet = Packet::new();
packet.set_var(Field::Protocol, &[protocol]);
packet.set_var(Field::Data, data);
packet.set_var(Field::Src, &self.inner.id);
packet.set_var(Field::Ttl, &[8]);
self.send_common(&mut packet, id, link, None);
}
pub fn broadcast(&self, protocol: u8, data: &[u8]) {
let mut packet = Packet::new();
packet.set_var(Field::Protocol, &[protocol]);
packet.set_var(Field::Data, data);
packet.set_var(Field::Src, &self.inner.id);
packet.set_var(Field::Dst, &ID_CHILD);
packet.set_var(Field::Ttl, &[0]);
let buf = packet.to_buf();
let routes = self.inner.routes.read();
for (_id, route) in routes.iter() {
route.link.send(&buf);
}
}
fn recv(&self, link: Link, buf: &[u8]) {
let packet = Packet::try_from_buf(buf);
let mut packet = if let Ok(packet) = packet {
packet
} else {
return;
};
self.update_route(&packet.src, &link);
let net = &self.inner;
if packet.dst == net.id {
let protocols = net.protocols.read();
protocols.distribute(packet.protocol, &packet.src, &packet.data);
} else if net.relay {
if packet.ttl > 0 {
packet.set_var(Field::Ttl, &[packet.ttl - 1]);
self.send_common(&mut packet, None, None, Some(&link));
} else {
}
}
}
pub fn connect(&self, id: &Id, link: &Link) {
let net = &self.inner;
let mut routes = net.routes.write();
routes.remove(id);
routes.update(id, link.clone());
routes.fix(id);
}
pub fn update_route(&self, id: &Id, link: &Link) {
let net = &self.inner;
for parent in &net.links.read().parents {
if link.eq(&parent) {
return;
}
}
let mut routes = net.routes.write();
let route = routes.find(id);
if let Some(route) = route {
if route.uid() != link.uid() {
routes.update(id, link.clone());
}
} else {
routes.update(id, link.clone());
}
}
}