zenoh_protocol/zenoh/
mod.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//
14pub mod del;
15pub mod err;
16pub mod put;
17pub mod query;
18pub mod reply;
19
20pub use del::Del;
21pub use err::Err;
22pub use put::Put;
23pub use query::{ConsolidationMode, Query};
24pub use reply::Reply;
25
26use crate::core::Encoding;
27
28pub mod id {
29    pub const OAM: u8 = 0x00;
30    pub const PUT: u8 = 0x01;
31    pub const DEL: u8 = 0x02;
32    pub const QUERY: u8 = 0x03;
33    pub const REPLY: u8 = 0x04;
34    pub const ERR: u8 = 0x05;
35}
36
37// DataInfo
38#[derive(Debug, Clone, PartialEq, Eq)]
39pub struct DataInfo {
40    pub encoding: Encoding,
41}
42
43// Push
44#[derive(Debug, Clone, PartialEq, Eq)]
45pub enum PushBody {
46    Put(Put),
47    Del(Del),
48}
49
50impl PushBody {
51    #[cfg(feature = "test")]
52    #[doc(hidden)]
53    pub fn rand() -> Self {
54        use rand::Rng;
55
56        let mut rng = rand::thread_rng();
57
58        match rng.gen_range(0..2) {
59            0 => PushBody::Put(Put::rand()),
60            1 => PushBody::Del(Del::rand()),
61            _ => unreachable!(),
62        }
63    }
64}
65
66impl From<Put> for PushBody {
67    fn from(p: Put) -> PushBody {
68        PushBody::Put(p)
69    }
70}
71
72impl From<Del> for PushBody {
73    fn from(d: Del) -> PushBody {
74        PushBody::Del(d)
75    }
76}
77
78// Request
79#[derive(Debug, Clone, PartialEq, Eq)]
80pub enum RequestBody {
81    Query(Query),
82}
83
84impl RequestBody {
85    #[cfg(feature = "test")]
86    #[doc(hidden)]
87    pub fn rand() -> Self {
88        use rand::Rng;
89
90        let mut rng = rand::thread_rng();
91
92        match rng.gen_range(0..1) {
93            0 => RequestBody::Query(Query::rand()),
94            _ => unreachable!(),
95        }
96    }
97}
98
99impl From<Query> for RequestBody {
100    fn from(q: Query) -> RequestBody {
101        RequestBody::Query(q)
102    }
103}
104
105// Response
106#[derive(Debug, Clone, PartialEq, Eq)]
107pub enum ResponseBody {
108    Reply(Reply),
109    Err(Err),
110}
111
112impl ResponseBody {
113    #[cfg(feature = "test")]
114    #[doc(hidden)]
115    pub fn rand() -> Self {
116        use rand::Rng;
117        let mut rng = rand::thread_rng();
118
119        match rng.gen_range(0..2) {
120            0 => ResponseBody::Reply(Reply::rand()),
121            1 => ResponseBody::Err(Err::rand()),
122            _ => unreachable!(),
123        }
124    }
125}
126
127impl From<Reply> for ResponseBody {
128    fn from(r: Reply) -> ResponseBody {
129        ResponseBody::Reply(r)
130    }
131}
132
133impl From<Err> for ResponseBody {
134    fn from(r: Err) -> ResponseBody {
135        ResponseBody::Err(r)
136    }
137}
138
139pub mod ext {
140    use zenoh_buffers::ZBuf;
141
142    use crate::core::{Encoding, EntityGlobalIdProto};
143
144    /// ```text
145    ///  7 6 5 4 3 2 1 0
146    /// +-+-+-+-+-+-+-+-+
147    /// |zid_len|X|X|X|X|
148    /// +-------+-+-+---+
149    /// ~      zid      ~
150    /// +---------------+
151    /// %      eid      %  -- Counter decided by the Zenoh Node
152    /// +---------------+
153    /// %      sn       %
154    /// +---------------+
155    /// ```
156    #[derive(Debug, Clone, PartialEq, Eq)]
157    pub struct SourceInfoType<const ID: u8> {
158        pub id: EntityGlobalIdProto,
159        pub sn: u32,
160    }
161
162    impl<const ID: u8> SourceInfoType<{ ID }> {
163        #[cfg(feature = "test")]
164        #[doc(hidden)]
165        pub fn rand() -> Self {
166            use rand::Rng;
167            let mut rng = rand::thread_rng();
168
169            let id = EntityGlobalIdProto::rand();
170            let sn: u32 = rng.gen();
171            Self { id, sn }
172        }
173    }
174
175    ///  7 6 5 4 3 2 1 0
176    /// +-+-+-+-+-+-+-+-+
177    /// +-+-+-+-+-+-+-+-+
178    #[cfg(feature = "shared-memory")]
179    #[derive(Debug, Clone, PartialEq, Eq)]
180    pub struct ShmType<const ID: u8>;
181
182    #[cfg(feature = "shared-memory")]
183    impl<const ID: u8> ShmType<{ ID }> {
184        pub const fn new() -> Self {
185            Self
186        }
187
188        #[cfg(feature = "test")]
189        pub const fn rand() -> Self {
190            Self
191        }
192    }
193    #[cfg(feature = "shared-memory")]
194    impl<const ID: u8> Default for ShmType<{ ID }> {
195        fn default() -> Self {
196            Self::new()
197        }
198    }
199
200    /// ```text
201    ///   7 6 5 4 3 2 1 0
202    ///  +-+-+-+-+-+-+-+-+
203    ///  ~   encoding    ~
204    ///  +---------------+
205    ///  ~ pl: [u8;z32]  ~  -- Payload
206    ///  +---------------+
207    /// ```
208    #[derive(Debug, Clone, PartialEq, Eq)]
209    pub struct ValueType<const VID: u8, const SID: u8> {
210        #[cfg(feature = "shared-memory")]
211        pub ext_shm: Option<ShmType<{ SID }>>,
212        pub encoding: Encoding,
213        pub payload: ZBuf,
214    }
215
216    impl<const VID: u8, const SID: u8> ValueType<{ VID }, { SID }> {
217        pub const VID: u8 = VID;
218        pub const SID: u8 = SID;
219
220        #[cfg(feature = "test")]
221        #[doc(hidden)]
222        pub fn rand() -> Self {
223            use rand::Rng;
224            let mut rng = rand::thread_rng();
225
226            #[cfg(feature = "shared-memory")]
227            let ext_shm = rng.gen_bool(0.5).then_some(ShmType::rand());
228            let encoding = Encoding::rand();
229            let payload = ZBuf::rand(rng.gen_range(1..=64));
230
231            Self {
232                #[cfg(feature = "shared-memory")]
233                ext_shm,
234                encoding,
235                payload,
236            }
237        }
238    }
239
240    /// ```text
241    /// 7 6 5 4 3 2 1 0
242    /// +-+-+-+-+-+-+-+-+
243    /// %   num elems   %
244    /// +-------+-+-+---+
245    /// ~ key: <u8;z16> ~
246    /// +---------------+
247    /// ~ val: <u8;z32> ~
248    /// +---------------+
249    ///       ...         -- N times (key, value) tuples
250    /// ```
251    #[derive(Debug, Clone, PartialEq, Eq)]
252    pub struct AttachmentType<const ID: u8> {
253        pub buffer: ZBuf,
254    }
255
256    impl<const ID: u8> AttachmentType<{ ID }> {
257        #[cfg(feature = "test")]
258        #[doc(hidden)]
259        pub fn rand() -> Self {
260            use rand::Rng;
261            let mut rng = rand::thread_rng();
262
263            Self {
264                buffer: ZBuf::rand(rng.gen_range(3..=1_024)),
265            }
266        }
267    }
268}