zenoh_protocol/network/response.rs
1use zenoh_buffers::buffer::Buffer;
2
3use crate::zenoh::reply::ReplyBody;
4//
5// Copyright (c) 2022 ZettaScale Technology
6//
7// This program and the accompanying materials are made available under the
8// terms of the Eclipse Public License 2.0 which is available at
9// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
10// which is available at https://www.apache.org/licenses/LICENSE-2.0.
11//
12// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
13//
14// Contributors:
15// ZettaScale Zenoh Team, <zenoh@zettascale.tech>
16//
17use crate::{core::WireExpr, network::RequestId, zenoh::ResponseBody};
18
19/// # Response message
20///
21/// ```text
22/// Flags:
23/// - N: Named If N==1 then the key expr has name/suffix
24/// - 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
25/// - Z: Extension If Z==1 then at least one extension is present
26///
27/// 7 6 5 4 3 2 1 0
28/// +-+-+-+-+-+-+-+-+
29/// |Z|M|N| Response|
30/// +-+-+-+---------+
31/// ~ request_id:z32~ (*)
32/// +---------------+
33/// ~ key_scope:z16 ~
34/// +---------------+
35/// ~ key_suffix ~ if N==1 -- <u8;z16>
36/// +---------------+
37/// ~ [reply_exts] ~ if Z==1
38/// +---------------+
39/// ~ ResponseBody ~ -- Payload
40/// +---------------+
41///
42/// (*) The resolution of the request id is negotiated during the session establishment.
43/// This implementation limits the resolution to 32bit.
44/// ```
45pub mod flag {
46 pub const N: u8 = 1 << 5; // 0x20 Named if N==1 then the key expr has name/suffix
47 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
48 pub const Z: u8 = 1 << 7; // 0x80 Extensions if Z==1 then an extension will follow
49}
50
51#[derive(Debug, Clone, PartialEq, Eq)]
52pub struct Response {
53 pub rid: RequestId,
54 pub wire_expr: WireExpr<'static>,
55 pub payload: ResponseBody,
56 pub ext_qos: ext::QoSType,
57 pub ext_tstamp: Option<ext::TimestampType>,
58 pub ext_respid: Option<ext::ResponderIdType>,
59}
60
61pub mod ext {
62 use crate::{
63 common::{ZExtZ64, ZExtZBuf},
64 zextz64, zextzbuf,
65 };
66 pub type QoS = zextz64!(0x1, false);
67 pub type QoSType = crate::network::ext::QoSType<{ QoS::ID }>;
68
69 pub type Timestamp = zextzbuf!(0x2, false);
70 pub type TimestampType = crate::network::ext::TimestampType<{ Timestamp::ID }>;
71
72 pub type ResponderId = zextzbuf!(0x3, false);
73 pub type ResponderIdType = crate::network::ext::EntityGlobalIdType<{ ResponderId::ID }>;
74}
75
76impl Response {
77 pub fn payload_size(&self) -> usize {
78 match &self.payload {
79 ResponseBody::Reply(r) => match &r.payload {
80 ReplyBody::Put(p) => {
81 p.payload.len() + p.ext_attachment.as_ref().map_or(0, |a| a.buffer.len())
82 }
83 ReplyBody::Del(d) => d.ext_attachment.as_ref().map_or(0, |a| a.buffer.len()),
84 },
85 ResponseBody::Err(e) => e.payload.len(),
86 }
87 }
88
89 #[cfg(feature = "test")]
90 #[doc(hidden)]
91 pub fn rand() -> Self {
92 use rand::Rng;
93 let mut rng = rand::thread_rng();
94
95 let rid: RequestId = rng.gen();
96 let wire_expr = WireExpr::rand();
97 let payload = ResponseBody::rand();
98 let ext_qos = ext::QoSType::rand();
99 let ext_tstamp = rng.gen_bool(0.5).then(ext::TimestampType::rand);
100 let ext_respid = rng.gen_bool(0.5).then(ext::ResponderIdType::rand);
101
102 Self {
103 rid,
104 wire_expr,
105 payload,
106 ext_qos,
107 ext_tstamp,
108 ext_respid,
109 }
110 }
111}
112
113/// # ResponseFinal message
114///
115/// ```text
116/// Flags:
117/// - X: Reserved
118/// - X: Reserved
119/// - Z: Extension If Z==1 then at least one extension is present
120///
121/// 7 6 5 4 3 2 1 0
122/// +-+-+-+-+-+-+-+-+
123/// |Z|X|X| ResFinal|
124/// +-+-+-+---------+
125/// ~ request_id:z32~ (*)
126/// +---------------+
127/// ~ [reply_exts] ~ if Z==1
128/// +---------------+
129///
130/// (*) The resolution of the request id is negotiated during the session establishment.
131/// This implementation limits the resolution to 32bit.
132#[derive(Debug, Clone, PartialEq, Eq)]
133pub struct ResponseFinal {
134 pub rid: RequestId,
135 pub ext_qos: ext::QoSType,
136 pub ext_tstamp: Option<ext::TimestampType>,
137}
138
139impl ResponseFinal {
140 #[cfg(feature = "test")]
141 #[doc(hidden)]
142 pub fn rand() -> Self {
143 use rand::Rng;
144
145 let mut rng = rand::thread_rng();
146 let rid: RequestId = rng.gen();
147 let ext_qos = ext::QoSType::rand();
148 let ext_tstamp = rng.gen_bool(0.5).then(ext::TimestampType::rand);
149
150 Self {
151 rid,
152 ext_qos,
153 ext_tstamp,
154 }
155 }
156}