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
use std::time::Duration;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StatusType {
Unknown,
Serving,
Down,
NotServing,
}
pub const HEALTH_POLL_WAIT: Duration = std::time::Duration::from_secs(1);
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ServiceStatus {
typ: ServiceType,
status: StatusType,
version: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ServiceType {
Gateway,
P2p,
Store,
}
impl ServiceType {
pub fn name(&self) -> &'static str {
match self {
ServiceType::Gateway => "gateway",
ServiceType::P2p => "p2p",
ServiceType::Store => "store",
}
}
}
impl ServiceStatus {
pub fn new<I: Into<String>>(typ: ServiceType, status: StatusType, version: I) -> Self {
Self {
typ,
status,
version: version.into(),
}
}
pub fn name(&self) -> &'static str {
self.typ.name()
}
pub fn status(&self) -> StatusType {
self.status.clone()
}
pub fn version(&self) -> &str {
if self.version.is_empty() {
"unknown"
} else {
&self.version
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ClientStatus {
pub gateway: ServiceStatus,
pub p2p: ServiceStatus,
pub store: ServiceStatus,
}
impl ClientStatus {
pub fn new(
gateway: Option<ServiceStatus>,
p2p: Option<ServiceStatus>,
store: Option<ServiceStatus>,
) -> Self {
Self {
gateway: gateway.unwrap_or_else(|| {
ServiceStatus::new(ServiceType::Gateway, StatusType::Unknown, "")
}),
p2p: p2p
.unwrap_or_else(|| ServiceStatus::new(ServiceType::P2p, StatusType::Unknown, "")),
store: store
.unwrap_or_else(|| ServiceStatus::new(ServiceType::Store, StatusType::Unknown, "")),
}
}
pub fn iter(&self) -> ClientStatusIterator<'_> {
ClientStatusIterator {
table: self,
iter: 0,
}
}
pub fn update(&mut self, s: ServiceStatus) {
match s.typ {
ServiceType::Gateway => self.gateway = s,
ServiceType::P2p => self.p2p = s,
ServiceType::Store => self.store = s,
}
}
}
#[derive(Debug)]
pub struct ClientStatusIterator<'a> {
table: &'a ClientStatus,
iter: usize,
}
impl Iterator for ClientStatusIterator<'_> {
type Item = ServiceStatus;
fn next(&mut self) -> Option<Self::Item> {
let current = match self.iter {
0 => Some(self.table.store.to_owned()),
1 => Some(self.table.p2p.to_owned()),
2 => Some(self.table.gateway.to_owned()),
_ => None,
};
self.iter += 1;
current
}
}
impl Default for ClientStatus {
fn default() -> Self {
Self::new(None, None, None)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn service_status_new() {
let expect = ServiceStatus {
typ: ServiceType::Gateway,
status: StatusType::Serving,
version: "0.1.0".to_string(),
};
assert_eq!(
expect,
ServiceStatus::new(ServiceType::Gateway, StatusType::Serving, "0.1.0")
);
}
#[test]
fn client_status_default() {
let expect = ClientStatus {
gateway: ServiceStatus {
typ: ServiceType::Gateway,
status: StatusType::Unknown,
version: "".to_string(),
},
p2p: ServiceStatus {
typ: ServiceType::P2p,
status: StatusType::Unknown,
version: "".to_string(),
},
store: ServiceStatus {
typ: ServiceType::Store,
status: StatusType::Unknown,
version: "".to_string(),
},
};
assert_eq!(expect, ClientStatus::default());
}
#[test]
fn status_table_new() {
let expect = ClientStatus {
gateway: ServiceStatus {
typ: ServiceType::Gateway,
status: StatusType::Unknown,
version: "test".to_string(),
},
p2p: ServiceStatus {
typ: ServiceType::P2p,
status: StatusType::Unknown,
version: "test".to_string(),
},
store: ServiceStatus {
typ: ServiceType::Store,
status: StatusType::Unknown,
version: "test".to_string(),
},
};
assert_eq!(
expect,
ClientStatus::new(
Some(ServiceStatus::new(
ServiceType::Gateway,
StatusType::Unknown,
"test"
)),
Some(ServiceStatus::new(
ServiceType::P2p,
StatusType::Unknown,
"test"
)),
Some(ServiceStatus::new(
ServiceType::Store,
StatusType::Unknown,
"test"
))
)
);
}
#[test]
fn status_table_update() {
let gateway = Some(ServiceStatus::new(
ServiceType::Gateway,
StatusType::Unknown,
"0.1.0",
));
let mut p2p = Some(ServiceStatus::new(
ServiceType::P2p,
StatusType::Unknown,
"0.1.0",
));
let mut store = Some(ServiceStatus::new(
ServiceType::Store,
StatusType::Unknown,
"0.1.0",
));
let mut got = ClientStatus::new(gateway.clone(), p2p.clone(), store.clone());
store.as_mut().unwrap().status = StatusType::Serving;
let expect = ClientStatus::new(gateway.clone(), p2p.clone(), store.clone());
got.update(store.clone().unwrap());
assert_eq!(expect, got);
p2p.as_mut().unwrap().status = StatusType::Down;
let expect = ClientStatus::new(gateway, p2p.clone(), store);
got.update(p2p.unwrap());
assert_eq!(expect, got);
}
#[test]
fn status_table_iter() {
let table = ClientStatus::default();
let rows: Vec<ServiceStatus> = table.iter().collect();
assert_eq!(
vec![
ServiceStatus {
typ: ServiceType::Store,
status: StatusType::Unknown,
version: "".to_string(),
},
ServiceStatus {
typ: ServiceType::P2p,
status: StatusType::Unknown,
version: "".to_string(),
},
ServiceStatus {
typ: ServiceType::Gateway,
status: StatusType::Unknown,
version: "".to_string(),
},
],
rows
);
}
}