zenoh_protocol/network/
push.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, zenoh::PushBody};
15
16pub mod flag {
17    pub const N: u8 = 1 << 5; // 0x20 Named         if N==1 then the key expr has name/suffix
18    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
19    pub const Z: u8 = 1 << 7; // 0x80 Extensions    if Z==1 then an extension will follow
20}
21
22/// ```text
23/// Flags:
24/// - N: Named          If N==1 then the key expr has name/suffix
25/// - 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
26/// - Z: Extension      If Z==1 then at least one extension is present
27///
28///  7 6 5 4 3 2 1 0
29/// +-+-+-+-+-+-+-+-+
30/// |Z|M|N|  PUSH   |
31/// +-+-+-+---------+
32/// ~ key_scope:z16 ~
33/// +---------------+
34/// ~  key_suffix   ~  if N==1 -- <u8;z16>
35/// +---------------+
36/// ~  [push_exts]  ~  if Z==1
37/// +---------------+
38/// ~   PushBody    ~
39/// +---------------+
40/// ```
41#[derive(Debug, Clone, PartialEq, Eq)]
42pub struct Push {
43    pub wire_expr: WireExpr<'static>,
44    pub ext_qos: ext::QoSType,
45    pub ext_tstamp: Option<ext::TimestampType>,
46    pub ext_nodeid: ext::NodeIdType,
47    pub payload: PushBody,
48}
49
50pub mod ext {
51    use crate::{
52        common::{ZExtZ64, ZExtZBuf},
53        zextz64, zextzbuf,
54    };
55
56    pub type QoS = zextz64!(0x1, false);
57    pub type QoSType = crate::network::ext::QoSType<{ QoS::ID }>;
58
59    pub type Timestamp = zextzbuf!(0x2, false);
60    pub type TimestampType = crate::network::ext::TimestampType<{ Timestamp::ID }>;
61
62    pub type NodeId = zextz64!(0x3, true);
63    pub type NodeIdType = crate::network::ext::NodeIdType<{ NodeId::ID }>;
64}
65
66impl Push {
67    #[cfg(feature = "test")]
68    pub fn rand() -> Self {
69        use rand::Rng;
70
71        let mut rng = rand::thread_rng();
72        let wire_expr = WireExpr::rand();
73        let payload = PushBody::rand();
74        let ext_qos = ext::QoSType::rand();
75        let ext_tstamp = rng.gen_bool(0.5).then(ext::TimestampType::rand);
76        let ext_nodeid = ext::NodeIdType::rand();
77
78        Self {
79            wire_expr,
80            payload,
81            ext_tstamp,
82            ext_qos,
83            ext_nodeid,
84        }
85    }
86}