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