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