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
use serde::{Deserialize, Serialize};
use sn_data_types::PublicKey;
use xor_name::{Prefix, XorName};
type SocketId = XorName;
#[derive(Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, Debug)]
pub struct Itinerary {
pub src: SrcLocation,
pub dst: DstLocation,
pub aggregation: Aggregation,
}
impl Itinerary {
pub fn aggregate_at_src(&self) -> bool {
matches!(self.aggregation, Aggregation::AtSource)
}
pub fn aggregate_at_dst(&self) -> bool {
matches!(self.aggregation, Aggregation::AtDestination)
}
pub fn src_name(&self) -> XorName {
self.src.name()
}
pub fn dst_name(&self) -> Option<XorName> {
self.dst.name()
}
}
#[derive(Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, Debug)]
pub enum Aggregation {
None,
AtSource,
AtDestination,
}
#[derive(Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, Debug)]
pub enum EndUser {
AllClients(PublicKey),
Client {
public_key: PublicKey,
socket_id: SocketId,
},
}
impl EndUser {
pub fn id(&self) -> &PublicKey {
match self {
Self::Client { public_key, .. } => public_key,
Self::AllClients(public_key) => public_key,
}
}
pub fn name(&self) -> XorName {
(*self.id()).into()
}
pub fn equals(&self, name: &XorName) -> bool {
match self {
Self::Client { public_key, .. } => name == &(*public_key).into(),
Self::AllClients(public_key) => name == &(*public_key).into(),
}
}
}
#[derive(Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, Debug)]
pub enum SrcLocation {
EndUser(EndUser),
Node(XorName),
Section(XorName),
}
impl SrcLocation {
pub fn is_section(&self) -> bool {
matches!(self, Self::Section(_))
}
pub fn is_user(&self) -> bool {
matches!(self, Self::EndUser(_))
}
pub fn equals(&self, name: &XorName) -> bool {
match self {
Self::EndUser(user) => user.equals(name),
Self::Node(self_name) => name == self_name,
Self::Section(some_name) => name == some_name,
}
}
pub fn name(&self) -> XorName {
match self {
Self::EndUser(user) => user.name(),
Self::Node(name) => *name,
Self::Section(name) => *name,
}
}
pub fn to_dst(&self) -> DstLocation {
match self {
Self::EndUser(user) => DstLocation::EndUser(*user),
Self::Node(name) => DstLocation::Node(*name),
Self::Section(name) => DstLocation::Section(*name),
}
}
}
#[derive(Clone, Copy, Eq, PartialEq, Hash, Serialize, Deserialize, Debug)]
pub enum DstLocation {
EndUser(EndUser),
Node(XorName),
Section(XorName),
Direct,
}
impl DstLocation {
pub fn is_section(&self) -> bool {
matches!(self, Self::Section(_))
}
pub fn is_user(&self) -> bool {
matches!(self, Self::EndUser(_))
}
pub fn contains(&self, name: &XorName, prefix: &Prefix) -> bool {
if !prefix.matches(name) {
return false;
}
match self {
Self::EndUser(user) => prefix.matches(&user.name()),
Self::Node(self_name) => name == self_name,
Self::Section(self_name) => prefix.matches(self_name),
Self::Direct => true,
}
}
pub fn name(&self) -> Option<XorName> {
match self {
Self::EndUser(user) => Some(user.name()),
Self::Node(name) => Some(*name),
Self::Section(name) => Some(*name),
Self::Direct => None,
}
}
}