VLink  2.0.0
A high-performance communication middleware
zenoh_conf.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2026 by Thun Lu. All rights reserved.
3  * Author: Thun Lu <thun.lu@zohomail.cn>
4  * Repo: https://github.com/thun-res/vlink
5  * _ __ __ _ __
6  * | | / / / / (_) ____ / /__
7  * | | / / / / / / / __ \ / //_/
8  * | |/ / / /___ / / / / / / / ,<
9  * |___/ /_____/ /_/ /_/ /_/ /_/|_|
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 
24 /**
25  * @file zenoh_conf.h
26  * @brief Transport configuration for the @c zenoh:// Eclipse Zenoh transport.
27  *
28  * @details
29  * @c ZenohConf binds the @c zenoh:// URL scheme to Eclipse Zenoh, a unified data
30  * protocol that combines publish/subscribe, queryable storage, and computed query
31  * patterns under a single key-expression namespace. Zenoh scales from constrained
32  * micro-controllers to cloud-side routers and is well suited to multi-site fleet
33  * and edge deployments. Optional shared-memory acceleration lowers latency for
34  * large payloads exchanged between processes on the same host.
35  *
36  * @par Supported Node Types
37  *
38  * | Publisher | Subscriber | Server | Client | Getter | Setter |
39  * | :-------: | :--------: | :----: | :----: | :----: | :----: |
40  * | yes | yes | yes | yes | yes | yes |
41  *
42  * @par Routing Modes
43  *
44  * | Mode | Topology | Typical use case |
45  * | --------- | ------------------------------------------------- | ------------------------------ |
46  * | @c peer | Fully meshed P2P between participants | LAN with no router |
47  * | @c client | Connects to a router as a leaf node | Fleet edge connecting to cloud |
48  * | @c router | Forwards and stores data for connected clients | Gateway / aggregation point |
49  *
50  * @par URL Format
51  * @code
52  * zenoh://<address>[?event=<name>&domain=<N>&qos=<profile>&depth=<N>&shm=<bool>
53  * &shm_mode=<lazy|init>&shm_size=<N>&shm_threshold=<N>
54  * &shm_loan_threshold=<N>&shm_blocking=<bool>][#<fragment>]
55  * @endcode
56  *
57  * | Component | Description |
58  * | --------------------- | ---------------------------------------------------------------------- |
59  * | @c address | Zenoh key expression (URL host concatenated with path) |
60  * | @c event | Optional secondary event filter (@c ?event=) |
61  * | @c domain | Zenoh session/domain identifier (@c ?domain=); factory default applied |
62  * | @c qos | Named QoS profile registered via @c register_qos() |
63  * | @c depth | TX queue override; @c 0 uses the QoS-selected history depth |
64  * | @c shm | Enable Zenoh shared-memory acceleration (boolean) |
65  * | @c shm_mode | Pool init strategy; @c lazy or @c init |
66  * | @c shm_size | SHM pool size; accepts bytes, K, M, or G suffixes |
67  * | @c shm_threshold | Minimum payload size to switch the SHM path on |
68  * | @c shm_loan_threshold | Minimum size for VLink SHM loan buffers |
69  * | @c shm_blocking | Whether @c loan() blocks when the pool is exhausted |
70  * | @c fragment | Optional transport hint or session-config fragment |
71  *
72  * @par QoS Registration
73  * @code
74  * vlink::Qos qos;
75  * qos.reliability.kind = vlink::Qos::Reliability::kReliable;
76  * vlink::ZenohConf::register_qos("reliable", qos);
77  *
78  * auto pub = vlink::Publisher<MyMsg>::create_unique("zenoh://vehicle/speed?qos=reliable");
79  * @endcode
80  *
81  * @note Compiled only when @c VLINK_SUPPORT_ZENOH is defined.
82  * @note @c is_valid() returns @c false when @c address is empty or @c domain is negative.
83  */
84 
85 #pragma once
86 
87 #ifdef VLINK_SUPPORT_ZENOH
88 
89 #include <cstdint>
90 #include <functional>
91 #include <map>
92 #include <shared_mutex>
93 #include <string>
94 
95 #include "../extension/qos.h"
96 #include "../impl/conf.h"
97 
98 namespace vlink {
99 
100 /**
101  * @struct ZenohConf
102  * @brief Concrete @c Conf describing a Zenoh endpoint addressed by a @c zenoh:// URL.
103  *
104  * @details
105  * Stores the Zenoh key expression, an optional secondary event filter, the session
106  * domain identifier, an optional named QoS profile and depth override, plus the
107  * tunable SHM-acceleration knobs exposed through URL query keys.
108  */
109 struct VLINK_EXPORT ZenohConf final : public Conf {
110  std::string address; ///< Zenoh key expression (URL host concatenated with path).
111  std::string event; ///< Optional secondary event filter string.
112  int32_t domain{0}; ///< Zenoh session / domain identifier (non-negative).
113  int32_t depth{0}; ///< TX queue override; @c 0 uses the QoS-selected history depth.
114  std::string qos; ///< Named QoS profile key registered via @c register_qos().
115  std::string fragment; ///< Optional transport hint or session-config fragment.
116  std::string shm; ///< Optional SHM acceleration enable (string boolean).
117  std::string shm_mode; ///< Optional SHM pool init strategy; @c lazy or @c init.
118  std::string shm_size; ///< Optional SHM pool size; accepts bytes, K, M, or G suffixes.
119  std::string shm_threshold; ///< Optional minimum payload size before SHM path engages.
120  std::string shm_loan_threshold; ///< Optional minimum size for VLink SHM loan buffers.
121  std::string shm_blocking; ///< Optional blocking behaviour for @c loan() when the pool is full.
122 
123  /**
124  * @brief Builds a @c ZenohConf from the URL's primary fields.
125  *
126  * @param _address Zenoh key expression.
127  * @param _event Optional secondary event filter; empty by default.
128  * @param _domain Domain identifier; defaults to @c 0.
129  * @param _qos Named QoS profile key; empty by default.
130  * @param _fragment Optional transport-hint fragment; empty by default.
131  */
132  explicit ZenohConf(const std::string& _address, const std::string& _event = "", int32_t _domain = 0,
133  const std::string& _qos = "", const std::string& _fragment = "");
134 
135  /**
136  * @brief Component-wise equality on all configuration fields, including SHM tunables.
137  *
138  * @param conf Configuration to compare with.
139  * @return @c true when every field of @c *this matches @p conf.
140  */
141  [[nodiscard]] bool operator==(const ZenohConf& conf) const noexcept;
142 
143  /**
144  * @brief Logical negation of @c operator==.
145  *
146  * @param conf Configuration to compare with.
147  * @return @c true when any field differs from @p conf.
148  */
149  [[nodiscard]] bool operator!=(const ZenohConf& conf) const noexcept;
150 
151  /**
152  * @brief Reports this object's transport tag.
153  *
154  * @return @c TransportType::kZenoh.
155  */
156  [[nodiscard]] TransportType get_transport_type() const override;
157 
158  /**
159  * @brief Copies non-empty Zenoh SHM tunables into a property map.
160  *
161  * @details
162  * Lets URL-supplied @c shm* query keys and explicit @c set_property("zenoh.*", ...)
163  * calls share the same factory property path without enlarging the factory key set.
164  *
165  * @param properties Destination property map; entries are added for each non-empty SHM field.
166  */
167  void append_properties(PropertiesMap& properties) const;
168 
169  /**
170  * @brief Registers a named QoS profile that endpoints may reference via @c ?qos=.
171  *
172  * @details
173  * Profile names share a global namespace. Collisions with reserved tokens
174  * (@c part, @c topic, @c pub, @c sub, @c writer, @c reader) or with an
175  * already registered profile abort with a fatal log entry.
176  *
177  * @param name Unique profile key; must not collide with any reserved token.
178  * @param qos @c Qos value associated with the key.
179  */
180  static void register_qos(const std::string& name, const Qos& qos);
181 
182  private:
183  static void register_qos_internal(const std::string& name, const Qos& qos);
184 
185  static const Qos& find_qos(const std::string& name);
186 
187  friend class ZenohFactory;
188  static std::map<std::string, Qos> qos_map_;
189  static std::shared_mutex mtx_;
190  static constexpr const char* kRespSuffix{"___resp"};
191 #ifndef VLINK_ENABLE_C_INTERFACE
193 #endif
195  VLINK_CONF_IMPL(ZenohConf)
196 };
197 
198 ////////////////////////////////////////////////////////////////
199 /// Details
200 ////////////////////////////////////////////////////////////////
201 
202 inline ZenohConf::ZenohConf(const std::string& _address, const std::string& _event, int32_t _domain,
203  const std::string& _qos, const std::string& _fragment)
204  : address(_address), event(_event), domain(_domain), qos(_qos), fragment(_fragment) {}
205 
206 inline bool ZenohConf::operator==(const ZenohConf& conf) const noexcept {
207  return address == conf.address && event == conf.event && domain == conf.domain && depth == conf.depth &&
208  qos == conf.qos && fragment == conf.fragment && shm == conf.shm && shm_mode == conf.shm_mode &&
209  shm_size == conf.shm_size && shm_threshold == conf.shm_threshold &&
210  shm_loan_threshold == conf.shm_loan_threshold && shm_blocking == conf.shm_blocking;
211 }
212 
213 inline bool ZenohConf::operator!=(const ZenohConf& conf) const noexcept { return !(*this == conf); }
214 
215 inline TransportType ZenohConf::get_transport_type() const { return TransportType::kZenoh; }
216 
217 inline void ZenohConf::append_properties(PropertiesMap& properties) const {
218  if (!shm.empty()) {
219  properties["zenoh.shm"] = shm;
220  }
221 
222  if (!shm_mode.empty()) {
223  properties["zenoh.shm_mode"] = shm_mode;
224  }
225 
226  if (!shm_size.empty()) {
227  properties["zenoh.shm_size"] = shm_size;
228  }
229 
230  if (!shm_threshold.empty()) {
231  properties["zenoh.shm_threshold"] = shm_threshold;
232  }
233 
234  if (!shm_loan_threshold.empty()) {
235  properties["zenoh.shm_loan_threshold"] = shm_loan_threshold;
236  }
237 
238  if (!shm_blocking.empty()) {
239  properties["zenoh.shm_blocking"] = shm_blocking;
240  }
241 }
242 
243 } // namespace vlink
244 
245 #endif
#define VLINK_CONF_IMPL(classname)
Convenience macro that emits the standard concrete conf boilerplate.
Definition: conf.h:251
#define VLINK_DECLARE_GLOBAL_PROPERTY()
Declares per-transport static configuration storage and access helpers.
Definition: conf.h:309
#define VLINK_ALLOW_IMPL_TYPE(type)
Records the bitmask of ImplType values supported by a conf.
Definition: conf.h:292
#define VLINK_EXPORT
Definition: macros.h:81