VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
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:// Zenoh protocol backend.
27 *
28 * @details
29 * @c ZenohConf configures the Eclipse Zenoh transport, a protocol designed for
30 * unified data management across robots, edge nodes, and cloud infrastructure.
31 * Zenoh supports routed pub/sub and queryable patterns with optional peer-to-peer
32 * connectivity.
33 *
34 * @par Supported Node Types
35 * @c zenoh:// supports all six node types: @c kPublisher, @c kSubscriber, @c kServer,
36 * @c kClient, @c kSetter, and @c kGetter.
37 *
38 * @par URL Format
39 * @code
40 * zenoh://<address>[?event=<name>&domain=<N>&qos=<name>&depth=<N>&shm=<0|1>&shm_size=<N>][#<fragment>]
41 * @endcode
42 *
43 * | Component | Description |
44 * | ------------ | ------------------------------------------------------------------------- |
45 * | @c address | Zenoh key expression; formed from @c host + @c "/" + @c path |
46 * | @c event | Optional secondary event filter (@c ?event=) |
47 * | @c domain | Zenoh domain/session identifier (@c ?domain=, default from factory) |
48 * | @c qos | Named QoS profile registered via @c register_qos() (@c ?qos=) |
49 * | @c depth | Optional Zenoh session-level TX queue size (data and real_time) |
50 * | @c shm | Optional Zenoh shared-memory transport optimization enable (@c ?shm=1) |
51 * | @c shm_size | Optional SHM pool size; accepts bytes, K, M, or G suffixes |
52 * | @c fragment | Optional transport hint or config fragment passed to the Zenoh session |
53 *
54 * @par QoS Registration
55 * @code
56 * vlink::Qos my_qos;
57 * my_qos.reliability = vlink::Reliability::kReliable;
58 * vlink::ZenohConf::register_qos("my_profile", my_qos);
59 *
60 * vlink::Publisher<MyMsg> pub("zenoh://vehicle/speed?qos=my_profile");
61 * @endcode
62 *
63 * @note This header is compiled only when @c VLINK_SUPPORT_ZENOH is defined.
64 */
65
66#pragma once
67
68#ifdef VLINK_SUPPORT_ZENOH
69
70#include <cstdint>
71#include <functional>
72#include <map>
73#include <shared_mutex>
74#include <string>
75
76#include "../extension/qos.h"
77#include "../impl/conf.h"
78
79namespace vlink {
80
81/**
82 * @struct ZenohConf
83 * @brief Configuration for the @c zenoh:// Zenoh transport.
84 *
85 * @details
86 * Can be constructed directly or parsed from a URL string via @c Url.
87 */
88struct VLINK_EXPORT ZenohConf final : public Conf {
89 std::string address; ///< Zenoh key expression (host + "/" + path from URL).
90 std::string event; ///< Optional secondary event filter string.
91 int32_t domain{0}; ///< Zenoh session/domain identifier (non-negative).
92 int32_t depth{0}; ///< Optional TX queue size override; 0 means use selected QoS history depth.
93 std::string qos; ///< Named QoS profile key registered via @c register_qos().
94 std::string fragment; ///< Optional transport hint passed as the URL fragment.
95 std::string shm; ///< Optional SHM enable override from @c ?shm=.
96 std::string shm_mode; ///< Optional SHM init mode (@c lazy or @c init).
97 std::string shm_size; ///< Optional SHM transport pool size in bytes, K, M, or G.
98 std::string shm_threshold; ///< Optional SHM transport optimization threshold.
99 std::string shm_loan_threshold; ///< Optional minimum size for VLink SHM loan buffers.
100 std::string shm_blocking; ///< Optional blocking allocation mode for @c loan().
101
102 /**
103 * @brief Constructs a @c ZenohConf with explicit parameters.
104 *
105 * @param _address Zenoh key expression.
106 * @param _event Optional event filter; empty by default.
107 * @param _domain Domain identifier; default 0.
108 * @param _qos Named QoS profile key; empty by default.
109 * @param _fragment Optional transport hint fragment; empty by default.
110 */
111 explicit ZenohConf(const std::string& _address, const std::string& _event = "", int32_t _domain = 0,
112 const std::string& _qos = "", const std::string& _fragment = "");
113
114 /**
115 * @brief Returns @c true if all fields equal those of @p conf.
116 *
117 * @param conf Configuration to compare.
118 * @return @c true if @c address, @c event, @c domain, @c qos, and @c fragment all match.
119 */
120 [[nodiscard]] bool operator==(const ZenohConf& conf) const noexcept;
121
122 /**
123 * @brief Returns @c true if any field differs from @p conf.
124 *
125 * @param conf Configuration to compare.
126 * @return Logical negation of @c operator==.
127 */
128 [[nodiscard]] bool operator!=(const ZenohConf& conf) const noexcept;
129
130 /**
131 * @brief Returns @c TransportType::kZenoh identifying this transport.
132 *
133 * @return @c TransportType::kZenoh.
134 */
135 [[nodiscard]] TransportType get_transport_type() const override;
136
137 /**
138 * @brief Adds URL-level Zenoh tuning options to a property map.
139 *
140 * @details
141 * This keeps URL query parameters and @c set_property("zenoh.*", ...) on the
142 * same factory path without expanding the factory object key shape.
143 */
144 void append_properties(PropertiesMap& properties) const;
145
146 /**
147 * @brief Registers a named QoS profile for use by @c zenoh:// nodes.
148 *
149 * @details
150 * The @p name is associated with the @p qos object and can be referenced in URL
151 * query strings as @c ?qos=name. Names that conflict with reserved keys
152 * (@c part, @c topic, @c pub, @c sub, @c writer, @c reader) or that are already
153 * registered cause a fatal log and are rejected.
154 *
155 * @param name Unique profile name; must not be one of the reserved keys.
156 * @param qos @c Qos object describing the quality-of-service settings.
157 */
158 static void register_qos(const std::string& name, const Qos& qos);
159
160 private:
161 static void register_qos_internal(const std::string& name, const Qos& qos);
162
163 static const Qos& find_qos(const std::string& name);
164
165 friend class ZenohFactory;
166 static std::map<std::string, Qos> qos_map_;
167 static std::shared_mutex mtx_;
168 static constexpr const char* kRespSuffix{"___resp"};
169#ifndef VLINK_ENABLE_C_INTERFACE
171#endif
172 VLINK_ALLOW_IMPL_TYPE(kServer | kClient | kPublisher | kSubscriber | kSetter | kGetter)
173 VLINK_CONF_IMPL(ZenohConf)
174};
175
176////////////////////////////////////////////////////////////////
177/// Details
178////////////////////////////////////////////////////////////////
179
180inline ZenohConf::ZenohConf(const std::string& _address, const std::string& _event, int32_t _domain,
181 const std::string& _qos, const std::string& _fragment)
182 : address(_address), event(_event), domain(_domain), qos(_qos), fragment(_fragment) {}
183
184inline bool ZenohConf::operator==(const ZenohConf& conf) const noexcept {
185 return address == conf.address && event == conf.event && domain == conf.domain && depth == conf.depth &&
186 qos == conf.qos && fragment == conf.fragment && shm == conf.shm && shm_mode == conf.shm_mode &&
187 shm_size == conf.shm_size && shm_threshold == conf.shm_threshold &&
188 shm_loan_threshold == conf.shm_loan_threshold && shm_blocking == conf.shm_blocking;
189}
190
191inline bool ZenohConf::operator!=(const ZenohConf& conf) const noexcept { return !(*this == conf); }
192
193inline TransportType ZenohConf::get_transport_type() const { return TransportType::kZenoh; }
194
195inline void ZenohConf::append_properties(PropertiesMap& properties) const {
196 if (!shm.empty()) {
197 properties["zenoh.shm"] = shm;
198 }
199
200 if (!shm_mode.empty()) {
201 properties["zenoh.shm_mode"] = shm_mode;
202 }
203
204 if (!shm_size.empty()) {
205 properties["zenoh.shm_size"] = shm_size;
206 }
207
208 if (!shm_threshold.empty()) {
209 properties["zenoh.shm_threshold"] = shm_threshold;
210 }
211
212 if (!shm_loan_threshold.empty()) {
213 properties["zenoh.shm_loan_threshold"] = shm_loan_threshold;
214 }
215
216 if (!shm_blocking.empty()) {
217 properties["zenoh.shm_blocking"] = shm_blocking;
218 }
219}
220
221} // namespace vlink
222
223#endif
Abstract transport configuration base class and associated helper macros.
#define VLINK_CONF_IMPL(classname)
Standard boilerplate for concrete Conf subclass declarations.
Definition conf.h:227
#define VLINK_DECLARE_GLOBAL_PROPERTY()
Declares per-transport global state: thread count and property storage.
Definition conf.h:292
#define VLINK_ALLOW_IMPL_TYPE(type)
Declares a static constexpr bitmask of supported ImplType values.
Definition conf.h:268
#define VLINK_EXPORT
Definition macros.h:85
Quality of Service (QoS) policy aggregate for VLink publishers and subscribers.