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