zenoh_protocol/network/interest.rs
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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
//
// Copyright (c) 2022 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 core::{
fmt::{self, Debug},
ops::{Add, AddAssign, Sub, SubAssign},
sync::atomic::AtomicU32,
};
use crate::{common::imsg, core::WireExpr, network::Mapping};
pub type InterestId = u32;
pub mod flag {
pub const Z: u8 = 1 << 7; // 0x80 Extensions if Z==1 then an extension will follow
}
/// The INTEREST message is sent to request the transmission of current and optionally future
/// declarations of a given kind matching a target keyexpr. E.g., an interest could be
/// sent to request the transmission of all current subscriptions matching `a/*`.
///
/// The behaviour of a INTEREST depends on the INTEREST MODE.
///
/// E.g., the message flow is the following for an [`Interest`] with mode [`InterestMode::Current`]:
///
/// ```text
/// A B
/// | INTEREST |
/// |------------------>| -- Mode: Current
/// | | This is an Interest e.g. for subscriber declarations.
/// | |
/// | DECL SUBSCRIBER |
/// |<------------------| -- With interest_id field set
/// | DECL SUBSCRIBER |
/// |<------------------| -- With interest_id field set
/// | DECL SUBSCRIBER |
/// |<------------------| -- With interest_id field set
/// | |
/// | DECL FINAL |
/// |<------------------| -- With interest_id field set
/// | |
/// ```
///
/// And the message flow is the following for an [`Interest`] with mode [`InterestMode::CurrentFuture`]:
///
/// ```text
/// A B
/// | INTEREST |
/// |------------------>| -- This is a DeclareInterest e.g. for subscriber declarations/undeclarations.
/// | |
/// | DECL SUBSCRIBER |
/// |<------------------| -- With interest_id field set
/// | DECL SUBSCRIBER |
/// |<------------------| -- With interest_id field set
/// | DECL SUBSCRIBER |
/// |<------------------| -- With interest_id field set
/// | |
/// | DECL FINAL |
/// |<------------------| -- With interest_id field set
/// | |
/// | DECL SUBSCRIBER |
/// |<------------------| -- With interest_id field not set
/// | UNDECL SUBSCRIBER |
/// |<------------------| -- With interest_id field not set
/// | |
/// | ... |
/// | |
/// | INTEREST FINAL |
/// |------------------>| -- Mode: Final
/// | | This stops the transmission of subscriber declarations/undeclarations.
/// | |
/// ```
///
/// And the message flow is the following for an [`Interest`] with mode [`InterestMode::Future`]:
///
/// ```text
/// A B
/// | INTEREST |
/// |------------------>| -- This is a DeclareInterest e.g. for subscriber declarations/undeclarations.
/// | |
/// | DECL SUBSCRIBER |
/// |<------------------| -- With interest_id field not set
/// | UNDECL SUBSCRIBER |
/// |<------------------| -- With interest_id field not set
/// | |
/// | ... |
/// | |
/// | INTEREST FINAL |
/// |------------------>| -- Mode: Final
/// | | This stops the transmission of subscriber declarations/undeclarations.
/// | |
/// ```
///
/// ```text
/// Flags:
/// - |: Mode The mode of the interest*
/// -/
/// - Z: Extension If Z==1 then at least one extension is present
///
/// 7 6 5 4 3 2 1 0
/// +-+-+-+-+-+-+-+-+
/// |Z|Mod|INTEREST |
/// +-+-+-+---------+
/// ~ id:z32 ~
/// +---------------+
/// |A|M|N|R|T|Q|S|K| if Mod!=Final (*)
/// +---------------+
/// ~ key_scope:z16 ~ if Mod!=Final && R==1
/// +---------------+
/// ~ key_suffix ~ if Mod!=Final && R==1 && N==1 -- <u8;z16>
/// +---------------+
/// ~ [int_exts] ~ if Z==1
/// +---------------+
///
/// Mode of declaration:
/// - Mode 0b00: Final
/// - Mode 0b01: Current
/// - Mode 0b10: Future
/// - Mode 0b11: CurrentFuture
///
/// (*) - if K==1 then the interest refers to key expressions
/// - if S==1 then the interest refers to subscribers
/// - if Q==1 then the interest refers to queryables
/// - if T==1 then the interest refers to tokens
/// - if R==1 then the interest is restricted to the matching key expression, else it is for all key expressions.
/// - if N==1 then the key expr has name/suffix. If R==0 then N should be set to 0.
/// - if M==1 then key expr mapping is the one declared by the sender, else it is the one declared by the receiver.
/// If R==0 then M should be set to 0.
/// - if A==1 then the replies SHOULD be aggregated
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Interest {
pub id: InterestId,
pub mode: InterestMode,
pub options: InterestOptions,
pub wire_expr: Option<WireExpr<'static>>,
pub ext_qos: ext::QoSType,
pub ext_tstamp: Option<ext::TimestampType>,
pub ext_nodeid: ext::NodeIdType,
}
/// The resolution of a RequestId
pub type DeclareRequestId = u32;
pub type AtomicDeclareRequestId = AtomicU32;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InterestMode {
Final,
Current,
Future,
CurrentFuture,
}
impl InterestMode {
#[cfg(feature = "test")]
pub fn rand() -> Self {
use rand::Rng;
let mut rng = rand::thread_rng();
match rng.gen_range(0..4) {
0 => InterestMode::Final,
1 => InterestMode::Current,
2 => InterestMode::Future,
3 => InterestMode::CurrentFuture,
_ => unreachable!(),
}
}
}
pub mod ext {
use crate::{
common::{ZExtZ64, ZExtZBuf},
zextz64, zextzbuf,
};
pub type QoS = zextz64!(0x1, false);
pub type QoSType = crate::network::ext::QoSType<{ QoS::ID }>;
pub type Timestamp = zextzbuf!(0x2, false);
pub type TimestampType = crate::network::ext::TimestampType<{ Timestamp::ID }>;
pub type NodeId = zextz64!(0x3, true);
pub type NodeIdType = crate::network::ext::NodeIdType<{ NodeId::ID }>;
}
impl Interest {
pub fn options(&self) -> u8 {
let mut interest = self.options;
if let Some(we) = self.wire_expr.as_ref() {
interest += InterestOptions::RESTRICTED;
if we.has_suffix() {
interest += InterestOptions::NAMED;
}
if let Mapping::Sender = we.mapping {
interest += InterestOptions::MAPPING;
}
}
interest.options
}
#[cfg(feature = "test")]
pub fn rand() -> Self {
use rand::Rng;
let mut rng = rand::thread_rng();
let id = rng.gen::<InterestId>();
let mode = InterestMode::rand();
let options = if mode == InterestMode::Final {
InterestOptions::empty()
} else {
InterestOptions::rand()
};
let wire_expr = options.restricted().then_some(WireExpr::rand());
let ext_qos = ext::QoSType::rand();
let ext_tstamp = rng.gen_bool(0.5).then(ext::TimestampType::rand);
let ext_nodeid = ext::NodeIdType::rand();
Self {
id,
mode,
wire_expr,
options,
ext_qos,
ext_tstamp,
ext_nodeid,
}
}
}
#[repr(transparent)]
#[derive(Clone, Copy)]
pub struct InterestOptions {
options: u8,
}
impl InterestOptions {
// Flags
pub const KEYEXPRS: InterestOptions = InterestOptions::options(1);
pub const SUBSCRIBERS: InterestOptions = InterestOptions::options(1 << 1);
pub const QUERYABLES: InterestOptions = InterestOptions::options(1 << 2);
pub const TOKENS: InterestOptions = InterestOptions::options(1 << 3);
const RESTRICTED: InterestOptions = InterestOptions::options(1 << 4);
const NAMED: InterestOptions = InterestOptions::options(1 << 5);
const MAPPING: InterestOptions = InterestOptions::options(1 << 6);
pub const AGGREGATE: InterestOptions = InterestOptions::options(1 << 7);
pub const ALL: InterestOptions = InterestOptions::options(
InterestOptions::KEYEXPRS.options
| InterestOptions::SUBSCRIBERS.options
| InterestOptions::QUERYABLES.options
| InterestOptions::TOKENS.options,
);
const fn options(options: u8) -> Self {
Self { options }
}
pub const fn empty() -> Self {
Self { options: 0 }
}
pub const fn keyexprs(&self) -> bool {
imsg::has_flag(self.options, Self::KEYEXPRS.options)
}
pub const fn subscribers(&self) -> bool {
imsg::has_flag(self.options, Self::SUBSCRIBERS.options)
}
pub const fn queryables(&self) -> bool {
imsg::has_flag(self.options, Self::QUERYABLES.options)
}
pub const fn tokens(&self) -> bool {
imsg::has_flag(self.options, Self::TOKENS.options)
}
pub const fn restricted(&self) -> bool {
imsg::has_flag(self.options, Self::RESTRICTED.options)
}
pub const fn named(&self) -> bool {
imsg::has_flag(self.options, Self::NAMED.options)
}
pub const fn mapping(&self) -> bool {
imsg::has_flag(self.options, Self::MAPPING.options)
}
pub const fn aggregate(&self) -> bool {
imsg::has_flag(self.options, Self::AGGREGATE.options)
}
#[cfg(feature = "test")]
pub fn rand() -> Self {
use rand::Rng;
let mut rng = rand::thread_rng();
let mut s = Self::empty();
if rng.gen_bool(0.5) {
s += InterestOptions::KEYEXPRS;
}
if rng.gen_bool(0.5) {
s += InterestOptions::SUBSCRIBERS;
}
if rng.gen_bool(0.5) {
s += InterestOptions::TOKENS;
}
if rng.gen_bool(0.5) {
s += InterestOptions::AGGREGATE;
}
s
}
}
impl PartialEq for InterestOptions {
fn eq(&self, other: &Self) -> bool {
self.keyexprs() == other.keyexprs()
&& self.subscribers() == other.subscribers()
&& self.queryables() == other.queryables()
&& self.tokens() == other.tokens()
&& self.aggregate() == other.aggregate()
}
}
impl Debug for InterestOptions {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Interest {{ ")?;
if self.keyexprs() {
write!(f, "K:Y, ")?;
} else {
write!(f, "K:N, ")?;
}
if self.subscribers() {
write!(f, "S:Y, ")?;
} else {
write!(f, "S:N, ")?;
}
if self.queryables() {
write!(f, "Q:Y, ")?;
} else {
write!(f, "Q:N, ")?;
}
if self.tokens() {
write!(f, "T:Y, ")?;
} else {
write!(f, "T:N, ")?;
}
if self.aggregate() {
write!(f, "A:Y")?;
} else {
write!(f, "A:N")?;
}
write!(f, " }}")?;
Ok(())
}
}
impl Eq for InterestOptions {}
impl Add for InterestOptions {
type Output = Self;
#[allow(clippy::suspicious_arithmetic_impl)] // Allows to implement Add & Sub for Interest
fn add(self, rhs: Self) -> Self::Output {
Self {
options: self.options | rhs.options,
}
}
}
impl AddAssign for InterestOptions {
#[allow(clippy::suspicious_op_assign_impl)] // Allows to implement Add & Sub for Interest
fn add_assign(&mut self, rhs: Self) {
self.options |= rhs.options;
}
}
impl Sub for InterestOptions {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self {
options: self.options & !rhs.options,
}
}
}
impl SubAssign for InterestOptions {
fn sub_assign(&mut self, rhs: Self) {
self.options &= !rhs.options;
}
}
impl From<u8> for InterestOptions {
fn from(options: u8) -> Self {
Self { options }
}
}