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::{zextz64, zextzbuf};
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 pub fn payload_size(&self) -> usize {
75 match &self.payload {
76 ResponseBody::Reply(r) => match &r.payload {
77 ReplyBody::Put(p) => {
78 p.payload.len() + p.ext_attachment.as_ref().map_or(0, |a| a.buffer.len())
79 }
80 ReplyBody::Del(d) => d.ext_attachment.as_ref().map_or(0, |a| a.buffer.len()),
81 },
82 ResponseBody::Err(e) => e.payload.len(),
83 }
84 }
85
86 #[cfg(feature = "test")]
87 #[doc(hidden)]
88 pub fn rand() -> Self {
89 use rand::Rng;
90 let mut rng = rand::thread_rng();
91
92 let rid: RequestId = rng.gen();
93 let wire_expr = WireExpr::rand();
94 let payload = ResponseBody::rand();
95 let ext_qos = ext::QoSType::rand();
96 let ext_tstamp = rng.gen_bool(0.5).then(ext::TimestampType::rand);
97 let ext_respid = rng.gen_bool(0.5).then(ext::ResponderIdType::rand);
98
99 Self {
100 rid,
101 wire_expr,
102 payload,
103 ext_qos,
104 ext_tstamp,
105 ext_respid,
106 }
107 }
108}
109
110/// # ResponseFinal message
111///
112/// ```text
113/// Flags:
114/// - X: Reserved
115/// - X: Reserved
116/// - Z: Extension If Z==1 then at least one extension is present
117///
118/// 7 6 5 4 3 2 1 0
119/// +-+-+-+-+-+-+-+-+
120/// |Z|X|X| ResFinal|
121/// +-+-+-+---------+
122/// ~ request_id:z32~ (*)
123/// +---------------+
124/// ~ [reply_exts] ~ if Z==1
125/// +---------------+
126///
127/// (*) The resolution of the request id is negotiated during the session establishment.
128/// This implementation limits the resolution to 32bit.
129#[derive(Debug, Clone, PartialEq, Eq)]
130pub struct ResponseFinal {
131 pub rid: RequestId,
132 pub ext_qos: ext::QoSType,
133 pub ext_tstamp: Option<ext::TimestampType>,
134}
135
136impl ResponseFinal {
137 #[cfg(feature = "test")]
138 #[doc(hidden)]
139 pub fn rand() -> Self {
140 use rand::Rng;
141
142 let mut rng = rand::thread_rng();
143 let rid: RequestId = rng.gen();
144 let ext_qos = ext::QoSType::rand();
145 let ext_tstamp = rng.gen_bool(0.5).then(ext::TimestampType::rand);
146
147 Self {
148 rid,
149 ext_qos,
150 ext_tstamp,
151 }
152 }
153}