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
use crate::events::NodeInfo;
use crate::value::Value;
use crate::OID;
use rand::seq::SliceRandom;
use rand::thread_rng;
use serde::{Deserialize, Deserializer, Serialize};
use std::sync::atomic;
use std::sync::Arc;
use uuid::Uuid;
pub fn deserialize_uuid<'de, D>(deserializer: D) -> Result<Uuid, D::Error>
where
D: Deserializer<'de>,
{
let val: Value = Deserialize::deserialize(deserializer)?;
Uuid::deserialize(val).map_err(serde::de::Error::custom)
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct NodeData {
svc: Option<String>,
#[serde(
deserialize_with = "crate::tools::deserialize_arc_atomic_bool",
serialize_with = "crate::tools::serialize_atomic_bool"
)]
online: Arc<atomic::AtomicBool>,
#[serde(skip_serializing_if = "Option::is_none")]
info: Option<NodeInfo>,
}
impl NodeData {
#[inline]
pub fn new(svc: Option<&str>, online: bool, info: Option<NodeInfo>) -> Self {
Self {
svc: svc.map(ToOwned::to_owned),
online: Arc::new(atomic::AtomicBool::new(online)),
info,
}
}
#[inline]
pub fn svc(&self) -> Option<&str> {
self.svc.as_deref()
}
#[inline]
pub fn online(&self) -> bool {
self.online.load(atomic::Ordering::SeqCst)
}
#[inline]
pub fn online_beacon(&self) -> Arc<atomic::AtomicBool> {
self.online.clone()
}
#[inline]
pub fn info(&self) -> Option<&NodeInfo> {
self.info.as_ref()
}
#[inline]
pub fn set_online(&self, online: bool) {
self.online.store(online, atomic::Ordering::SeqCst);
}
#[inline]
pub fn update_info(&mut self, info: NodeInfo) {
self.info.replace(info);
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ParamsId<'a> {
#[serde(borrow)]
pub i: &'a str,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ParamsIdOwned {
pub i: String,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum IdOrList<'a> {
Single(&'a str),
Multi(Vec<&'a str>),
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum IdOrListOwned {
Single(String),
Multi(Vec<String>),
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ParamsIdOrList<'a> {
#[serde(borrow)]
pub i: IdOrList<'a>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ParamsIdOrListOwned {
pub i: IdOrListOwned,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ParamsIdList<'a> {
#[serde(borrow)]
pub i: Vec<&'a str>,
}
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct ParamsIdListOwned {
pub i: Vec<String>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ParamsOID {
pub i: OID,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ParamsUuid {
pub u: Uuid,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ParamsUuidAny {
#[serde(deserialize_with = "deserialize_uuid")]
pub u: Uuid,
}
#[derive(Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum HostPath {
Single(String),
Multiple(Vec<String>),
}
impl Default for HostPath {
#[inline]
fn default() -> Self {
HostPath::Multiple(Vec::new())
}
}
impl HostPath {
pub fn iter(&self) -> HostPathIter {
HostPathIter {
path: self,
curr: 0,
}
}
pub fn shuffle(&mut self) {
if let HostPath::Multiple(ref mut v) = self {
v.shuffle(&mut thread_rng());
}
}
}
pub struct HostPathIter<'a> {
path: &'a HostPath,
curr: usize,
}
impl<'a> Iterator for HostPathIter<'a> {
type Item = &'a str;
fn next(&mut self) -> Option<Self::Item> {
let res = match self.path {
HostPath::Single(v) => {
if self.curr == 0 {
Some(v.as_str())
} else {
None
}
}
HostPath::Multiple(v) => v.get(self.curr).map(String::as_str),
};
self.curr += 1;
res
}
}