zenoh_protocol/network/request.rs
1//
2// Copyright (c) 2022 ZettaScale Technology
3//
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
7// which is available at https://www.apache.org/licenses/LICENSE-2.0.
8//
9// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
10//
11// Contributors:
12// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
13//
14use core::sync::atomic::AtomicU32;
15
16use crate::{core::WireExpr, zenoh::RequestBody};
17
18/// The resolution of a RequestId
19pub type RequestId = u32;
20pub type AtomicRequestId = AtomicU32;
21
22pub mod flag {
23 pub const N: u8 = 1 << 5; // 0x20 Named if N==1 then the key expr has name/suffix
24 pub const M: u8 = 1 << 6; // 0x40 Mapping if M==1 then key expr mapping is the one declared by the sender, else it is the one declared by the receiver
25 pub const Z: u8 = 1 << 7; // 0x80 Extensions if Z==1 then an extension will follow
26}
27
28/// # Request message
29///
30/// ```text
31/// Flags:
32/// - N: Named if N==1 then the key expr has name/suffix
33/// - M: Mapping if M==1 then key expr mapping is the one declared by the sender, else it is the one declared by the receiver
34/// - Z: Extension if Z==1 then at least one extension is present
35///
36/// 7 6 5 4 3 2 1 0
37/// +-+-+-+-+-+-+-+-+
38/// |Z|M|N| Request |
39/// +-+-+-+---------+
40/// ~ request_id:z32~ (*)
41/// +---------------+
42/// ~ key_scope:z16 ~
43/// +---------------+
44/// ~ key_suffix ~ if N==1 -- <u8;z16>
45/// +---------------+
46/// ~ [req_exts] ~ if Z==1
47/// +---------------+
48/// ~ RequestBody ~ -- Payload
49/// +---------------+
50///
51/// (*) The resolution of the request id is negotiated during the session establishment.
52/// This implementation limits the resolution to 32bit.
53/// ```
54#[derive(Debug, Clone, PartialEq, Eq)]
55pub struct Request {
56 pub id: RequestId,
57 pub wire_expr: WireExpr<'static>,
58 pub ext_qos: ext::QoSType,
59 pub ext_tstamp: Option<ext::TimestampType>,
60 pub ext_nodeid: ext::NodeIdType,
61 pub ext_target: ext::QueryTarget,
62 pub ext_budget: Option<ext::BudgetType>,
63 pub ext_timeout: Option<ext::TimeoutType>,
64 pub payload: RequestBody,
65}
66
67pub mod ext {
68 use core::{num::NonZeroU32, time::Duration};
69
70 use serde::Deserialize;
71
72 use crate::{
73 common::{ZExtZ64, ZExtZBuf},
74 zextz64, zextzbuf,
75 };
76
77 pub type QoS = zextz64!(0x1, false);
78 pub type QoSType = crate::network::ext::QoSType<{ QoS::ID }>;
79
80 pub type Timestamp = zextzbuf!(0x2, false);
81 pub type TimestampType = crate::network::ext::TimestampType<{ Timestamp::ID }>;
82
83 pub type NodeId = zextz64!(0x3, true);
84 pub type NodeIdType = crate::network::ext::NodeIdType<{ NodeId::ID }>;
85
86 pub type Target = zextz64!(0x4, true);
87 // ```text
88 // - Target (0x03)
89 // 7 6 5 4 3 2 1 0
90 // +-+-+-+-+-+-+-+-+
91 // % target %
92 // +---------------+
93 // ```
94 // The `zenoh::queryable::Queryable`s that should be target of a `zenoh::Session::get()`.
95 #[repr(u8)]
96 #[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Deserialize)]
97 pub enum QueryTarget {
98 /// Let Zenoh find the BestMatching queryable capabale of serving the query.
99 #[default]
100 BestMatching,
101 /// Deliver the query to all queryables matching the query's key expression.
102 All,
103 /// Deliver the query to all queryables matching the query's key expression that are declared as complete.
104 AllComplete,
105 }
106
107 impl QueryTarget {
108 pub const DEFAULT: Self = Self::BestMatching;
109
110 #[cfg(feature = "test")]
111 pub fn rand() -> Self {
112 use rand::prelude::*;
113 let mut rng = rand::thread_rng();
114
115 *[
116 QueryTarget::All,
117 QueryTarget::AllComplete,
118 QueryTarget::BestMatching,
119 ]
120 .choose(&mut rng)
121 .unwrap()
122 }
123 }
124
125 // The maximum number of responses
126 pub type Budget = zextz64!(0x5, false);
127 pub type BudgetType = NonZeroU32;
128
129 // The timeout of the request
130 pub type Timeout = zextz64!(0x6, false);
131 pub type TimeoutType = Duration;
132}
133
134impl Request {
135 #[cfg(feature = "test")]
136 pub fn rand() -> Self {
137 use core::num::NonZeroU32;
138
139 use rand::Rng;
140
141 let mut rng = rand::thread_rng();
142 let wire_expr = WireExpr::rand();
143 let id: RequestId = rng.gen();
144 let payload = RequestBody::rand();
145 let ext_qos = ext::QoSType::rand();
146 let ext_tstamp = rng.gen_bool(0.5).then(ext::TimestampType::rand);
147 let ext_nodeid = ext::NodeIdType::rand();
148 let ext_target = ext::QueryTarget::rand();
149 let ext_budget = if rng.gen_bool(0.5) {
150 NonZeroU32::new(rng.gen())
151 } else {
152 None
153 };
154 let ext_timeout = if rng.gen_bool(0.5) {
155 Some(ext::TimeoutType::from_millis(rng.gen()))
156 } else {
157 None
158 };
159
160 Self {
161 wire_expr,
162 id,
163 payload,
164 ext_qos,
165 ext_tstamp,
166 ext_nodeid,
167 ext_target,
168 ext_budget,
169 ext_timeout,
170 }
171 }
172}