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
use serde::{Deserialize, Serialize};
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_dst(&self) -> bool {
matches!(self.aggregation, Aggregation::AtDestination)
}
pub fn aggregate_at_src(&self) -> bool {
matches!(self.aggregation, Aggregation::AtSource)
}
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 struct EndUser {
pub xorname: XorName,
pub socket_id: SocketId,
}
#[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.xorname == 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.xorname,
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),
DirectAndUnrouted,
}
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.xorname),
Self::Node(self_name) => name == self_name,
Self::Section(self_name) => prefix.matches(self_name),
Self::DirectAndUnrouted => true,
}
}
pub fn name(&self) -> Option<XorName> {
match self {
Self::EndUser(user) => Some(user.xorname),
Self::Node(name) => Some(*name),
Self::Section(name) => Some(*name),
Self::DirectAndUnrouted => None,
}
}
}