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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
//
// Copyright (c) 2024 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
//
use std::sync::{atomic::Ordering, Arc};
use zenoh_protocol::{
core::WhatAmI,
network::{
declare::ext,
interest::{InterestId, InterestMode, InterestOptions},
Declare, DeclareBody, DeclareFinal, Interest,
},
};
use zenoh_sync::get_mut_unchecked;
use super::{
face_hat, face_hat_mut, initial_interest, pubsub::declare_sub_interest,
queries::declare_qabl_interest, token::declare_token_interest, HatCode, HatFace,
INITIAL_INTEREST_ID,
};
use crate::net::routing::{
dispatcher::{
face::{FaceState, InterestState},
interests::{
CurrentInterest, CurrentInterestCleanup, PendingCurrentInterest, RemoteInterest,
},
resource::Resource,
tables::{Tables, TablesLock},
},
hat::{CurrentFutureTrait, HatInterestTrait, SendDeclare},
RoutingContext,
};
pub(super) fn interests_new_face(tables: &mut Tables, face: &mut Arc<FaceState>) {
if face.whatami != WhatAmI::Client {
for mut src_face in tables
.faces
.values()
.cloned()
.collect::<Vec<Arc<FaceState>>>()
{
if face.whatami == WhatAmI::Router {
for RemoteInterest { res, options, .. } in
face_hat_mut!(&mut src_face).remote_interests.values()
{
let id = face_hat!(face).next_id.fetch_add(1, Ordering::SeqCst);
let face_id = face.id;
get_mut_unchecked(face).local_interests.insert(
id,
InterestState::new(face_id, *options, res.clone(), false),
);
let wire_expr = res.as_ref().map(|res| {
Resource::decl_key(res, face, super::push_declaration_profile(face))
});
face.primitives.send_interest(RoutingContext::with_expr(
&mut Interest {
id,
mode: InterestMode::CurrentFuture,
options: *options,
wire_expr,
ext_qos: ext::QoSType::DECLARE,
ext_tstamp: None,
ext_nodeid: ext::NodeIdType::DEFAULT,
},
res.as_ref()
.map(|res| res.expr().to_string())
.unwrap_or_default(),
));
}
}
}
}
}
impl HatInterestTrait for HatCode {
fn declare_interest(
&self,
tables: &mut Tables,
tables_ref: &Arc<TablesLock>,
face: &mut Arc<FaceState>,
id: InterestId,
res: Option<&mut Arc<Resource>>,
mode: InterestMode,
options: InterestOptions,
send_declare: &mut SendDeclare,
) {
if options.subscribers() {
declare_sub_interest(
tables,
face,
id,
res.as_deref().cloned().as_mut(),
mode,
options.aggregate(),
send_declare,
)
}
if options.queryables() {
declare_qabl_interest(
tables,
face,
id,
res.as_deref().cloned().as_mut(),
mode,
options.aggregate(),
send_declare,
)
}
if options.tokens() {
// Note: aggregation is forbidden for tokens. The flag is ignored.
declare_token_interest(
tables,
face,
id,
res.as_deref().cloned().as_mut(),
mode,
send_declare,
)
}
if mode.future() {
face_hat_mut!(face).remote_interests.insert(
id,
RemoteInterest {
res: res.as_ref().map(|res| (*res).clone()),
options,
mode,
},
);
}
let interest = Arc::new(CurrentInterest {
src_face: face.clone(),
src_interest_id: id,
mode,
});
if face.whatami == WhatAmI::Client {
let propagated_mode = if mode.future() {
InterestMode::CurrentFuture
} else {
mode
};
for dst_face in tables.faces.values_mut().filter(|f| {
f.whatami == WhatAmI::Router
|| (f.whatami == WhatAmI::Peer
&& options.tokens()
&& mode == InterestMode::Current
&& !initial_interest(f).map(|i| i.finalized).unwrap_or(true))
}) {
let id = face_hat!(dst_face).next_id.fetch_add(1, Ordering::SeqCst);
let dst_face_id = dst_face.id;
get_mut_unchecked(dst_face).local_interests.insert(
id,
InterestState::new(
dst_face_id,
options,
res.as_deref().cloned(),
propagated_mode == InterestMode::Future,
),
);
if mode.current() {
let dst_face_mut = get_mut_unchecked(dst_face);
let cancellation_token = dst_face_mut.task_controller.get_cancellation_token();
let rejection_token = dst_face_mut.task_controller.get_cancellation_token();
dst_face_mut.pending_current_interests.insert(
id,
PendingCurrentInterest {
interest: interest.clone(),
cancellation_token,
rejection_token,
},
);
CurrentInterestCleanup::spawn_interest_clean_up_task(
dst_face,
tables_ref,
id,
tables.interests_timeout,
);
}
let wire_expr = res.as_ref().map(|res| {
Resource::decl_key(res, dst_face, super::push_declaration_profile(dst_face))
});
dst_face.primitives.send_interest(RoutingContext::with_expr(
&mut Interest {
id,
mode: propagated_mode,
options,
wire_expr,
ext_qos: ext::QoSType::DECLARE,
ext_tstamp: None,
ext_nodeid: ext::NodeIdType::DEFAULT,
},
res.as_ref()
.map(|res| res.expr().to_string())
.unwrap_or_default(),
));
}
}
if mode.current() {
if let Some(interest) = Arc::into_inner(interest) {
tracing::debug!(
"Propagate DeclareFinal {}:{}",
interest.src_face,
interest.src_interest_id
);
send_declare(
&interest.src_face.primitives,
RoutingContext::new(Declare {
interest_id: Some(interest.src_interest_id),
ext_qos: ext::QoSType::DECLARE,
ext_tstamp: None,
ext_nodeid: ext::NodeIdType::DEFAULT,
body: DeclareBody::DeclareFinal(DeclareFinal),
}),
);
}
}
}
fn undeclare_interest(&self, tables: &mut Tables, face: &mut Arc<FaceState>, id: InterestId) {
if let Some(interest) = face_hat_mut!(face).remote_interests.remove(&id) {
if !tables.faces.values().any(|f| {
f.whatami == WhatAmI::Client
&& face_hat!(f)
.remote_interests
.values()
.any(|i| *i == interest)
}) {
for dst_face in tables
.faces
.values_mut()
.filter(|f| f.whatami == WhatAmI::Router)
.map(get_mut_unchecked)
{
dst_face.local_interests.retain(|id, local_interest| {
if *local_interest == interest {
dst_face.primitives.send_interest(RoutingContext::with_expr(
&mut Interest {
id: *id,
mode: InterestMode::Final,
// Note: InterestMode::Final options are undefined in the current protocol specification,
// they are initialized here for internal use by local egress interceptors.
options: interest.options,
wire_expr: None,
ext_qos: ext::QoSType::DECLARE,
ext_tstamp: None,
ext_nodeid: ext::NodeIdType::DEFAULT,
},
local_interest
.res
.as_ref()
.map(|res| res.expr().to_string())
.unwrap_or_default(),
));
return false;
}
true
});
}
}
}
}
fn declare_final(&self, tables: &mut Tables, face: &mut Arc<FaceState>, id: InterestId) {
if id == INITIAL_INTEREST_ID {
zenoh_runtime::ZRuntime::Net.block_in_place(async move {
if let Some(runtime) = &tables.runtime {
if let Some(runtime) = runtime.upgrade() {
runtime
.start_conditions()
.terminate_peer_connector_zid(face.zid)
.await
}
}
});
}
}
}