zenoh_protocol/network/response.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 crate::{core::WireExpr, network::RequestId, zenoh::ResponseBody};
15
16/// # Response message
17///
18/// ```text
19/// Flags:
20/// - N: Named If N==1 then the key expr has name/suffix
21/// - 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
22/// - Z: Extension If Z==1 then at least one extension is present
23///
24/// 7 6 5 4 3 2 1 0
25/// +-+-+-+-+-+-+-+-+
26/// |Z|M|N| Response|
27/// +-+-+-+---------+
28/// ~ request_id:z32~ (*)
29/// +---------------+
30/// ~ key_scope:z16 ~
31/// +---------------+
32/// ~ key_suffix ~ if N==1 -- <u8;z16>
33/// +---------------+
34/// ~ [reply_exts] ~ if Z==1
35/// +---------------+
36/// ~ ResponseBody ~ -- Payload
37/// +---------------+
38///
39/// (*) The resolution of the request id is negotiated during the session establishment.
40/// This implementation limits the resolution to 32bit.
41/// ```
42pub mod flag {
43 pub const N: u8 = 1 << 5; // 0x20 Named if N==1 then the key expr has name/suffix
44 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
45 pub const Z: u8 = 1 << 7; // 0x80 Extensions if Z==1 then an extension will follow
46}
47
48#[derive(Debug, Clone, PartialEq, Eq)]
49pub struct Response {
50 pub rid: RequestId,
51 pub wire_expr: WireExpr<'static>,
52 pub payload: ResponseBody,
53 pub ext_qos: ext::QoSType,
54 pub ext_tstamp: Option<ext::TimestampType>,
55 pub ext_respid: Option<ext::ResponderIdType>,
56}
57
58pub mod ext {
59 use crate::{
60 common::{ZExtZ64, ZExtZBuf},
61 zextz64, zextzbuf,
62 };
63 pub type QoS = zextz64!(0x1, false);
64 pub type QoSType = crate::network::ext::QoSType<{ QoS::ID }>;
65
66 pub type Timestamp = zextzbuf!(0x2, false);
67 pub type TimestampType = crate::network::ext::TimestampType<{ Timestamp::ID }>;
68
69 pub type ResponderId = zextzbuf!(0x3, false);
70 pub type ResponderIdType = crate::network::ext::EntityGlobalIdType<{ ResponderId::ID }>;
71}
72
73impl Response {
74 #[cfg(feature = "test")]
75 #[doc(hidden)]
76 pub fn rand() -> Self {
77 use rand::Rng;
78 let mut rng = rand::thread_rng();
79
80 let rid: RequestId = rng.gen();
81 let wire_expr = WireExpr::rand();
82 let payload = ResponseBody::rand();
83 let ext_qos = ext::QoSType::rand();
84 let ext_tstamp = rng.gen_bool(0.5).then(ext::TimestampType::rand);
85 let ext_respid = rng.gen_bool(0.5).then(ext::ResponderIdType::rand);
86
87 Self {
88 rid,
89 wire_expr,
90 payload,
91 ext_qos,
92 ext_tstamp,
93 ext_respid,
94 }
95 }
96}
97
98/// # ResponseFinal message
99///
100/// ```text
101/// Flags:
102/// - X: Reserved
103/// - X: Reserved
104/// - Z: Extension If Z==1 then at least one extension is present
105///
106/// 7 6 5 4 3 2 1 0
107/// +-+-+-+-+-+-+-+-+
108/// |Z|X|X| ResFinal|
109/// +-+-+-+---------+
110/// ~ request_id:z32~ (*)
111/// +---------------+
112/// ~ [reply_exts] ~ if Z==1
113/// +---------------+
114///
115/// (*) The resolution of the request id is negotiated during the session establishment.
116/// This implementation limits the resolution to 32bit.
117#[derive(Debug, Clone, PartialEq, Eq)]
118pub struct ResponseFinal {
119 pub rid: RequestId,
120 pub ext_qos: ext::QoSType,
121 pub ext_tstamp: Option<ext::TimestampType>,
122}
123
124impl ResponseFinal {
125 #[cfg(feature = "test")]
126 #[doc(hidden)]
127 pub fn rand() -> Self {
128 use rand::Rng;
129
130 let mut rng = rand::thread_rng();
131 let rid: RequestId = rng.gen();
132 let ext_qos = ext::QoSType::rand();
133 let ext_tstamp = rng.gen_bool(0.5).then(ext::TimestampType::rand);
134
135 Self {
136 rid,
137 ext_qos,
138 ext_tstamp,
139 }
140 }
141}