VLink  2.0.0
A high-performance communication middleware
ddsc_conf.h
浏览该文件的文档.
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 ddsc_conf.h
26  * @brief Transport configuration for the @c ddsc:// CycloneDDS transport.
27  *
28  * @details
29  * @c DdscConf binds the @c ddsc:// URL scheme to Eclipse CycloneDDS, the lightweight,
30  * production-grade open-source DDS implementation from the Eclipse Foundation. The
31  * backend is fully cross-machine: writers and readers participate in a standards-
32  * compliant RTPS Domain and discover each other automatically over UDP multicast
33  * (or via a configured discovery server). Use it whenever a DDS deployment is
34  * required and a small footprint is preferred over the broader Fast-DDS feature set.
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 URL Format
43  * @code
44  * ddsc://<topic>[?domain=<N>&depth=<N>&qos=<profile>]
45  * @endcode
46  *
47  * | Component | Description |
48  * | --------- | ---------------------------------------------------------------------------- |
49  * | @c topic | CycloneDDS topic name, assembled from the URL host plus path |
50  * | @c domain | DDS Domain ID (@c ?domain=); falls back to the @c VLINK_DDS_DOMAIN env var |
51  * | @c depth | Optional history-depth override; @c 0 keeps the depth of the selected QoS |
52  * | @c qos | Named QoS profile previously registered via @c register_qos() |
53  *
54  * @par Backend-Specific Options
55  *
56  * | Option | Purpose | Default |
57  * | --------------------- | -------------------------------------------------- | --------- |
58  * | Domain ID | Isolates discovery traffic between deployments | 0 |
59  * | History depth | Per-instance KeepLast retention | from QoS |
60  * | Response topic suffix | Auto-derived RPC reply topic name | ___resp |
61  *
62  * @par Example
63  * @code
64  * vlink::Qos qos;
65  * qos.reliability.kind = vlink::Qos::Reliability::kReliable;
66  * vlink::DdscConf::register_qos("reliable_profile", qos);
67  *
68  * auto pub = vlink::Publisher<MyMsg>::create_unique("ddsc://sensors/lidar?domain=7&qos=reliable_profile");
69  * auto sub = vlink::Subscriber<MyMsg>::create_unique("ddsc://sensors/lidar?domain=7&qos=reliable_profile");
70  * @endcode
71  *
72  * @note Compiled only when @c VLINK_SUPPORT_DDSC is defined at build time.
73  * @note Unlike @c DdsConf, no external XML profile loading or @c register_topic() helper is exposed.
74  */
75 
76 #pragma once
77 
78 #ifdef VLINK_SUPPORT_DDSC
79 
80 #include <cstdint>
81 #include <functional>
82 #include <map>
83 #include <shared_mutex>
84 #include <string>
85 
86 #include "../extension/qos.h"
87 #include "../impl/conf.h"
88 
89 namespace vlink {
90 
91 /**
92  * @struct DdscConf
93  * @brief Concrete @c Conf describing a CycloneDDS endpoint addressed by a @c ddsc:// URL.
94  *
95  * @details
96  * Carries the four parameters that fully identify a CycloneDDS DataReader or
97  * DataWriter: the topic name, the Domain ID, an optional history-depth override,
98  * and an optional named QoS profile. Instances may be created either directly or
99  * via @c Url URL parsing inside the @c DdscFactory.
100  */
101 struct VLINK_EXPORT DdscConf final : public Conf {
102  std::string topic; ///< CycloneDDS topic name (URL host concatenated with the path).
103  int32_t domain{0}; ///< DDS Domain ID participated in by readers and writers (non-negative).
104  int32_t depth{0}; ///< Optional history-depth override; @c 0 keeps the QoS-selected depth.
105  std::string qos; ///< Key of a named QoS profile registered through @c register_qos().
106 
107  /**
108  * @brief Builds a @c DdscConf from its four logical fields.
109  *
110  * @param _topic CycloneDDS topic name.
111  * @param _domain Domain ID; defaults to @c 0.
112  * @param _depth History-depth override; defaults to @c 0 (use QoS depth).
113  * @param _qos Named QoS profile key; empty by default.
114  */
115  explicit DdscConf(const std::string& _topic, int32_t _domain = 0, int32_t _depth = 0, const std::string& _qos = "");
116 
117  /**
118  * @brief Component-wise equality on @c topic, @c domain, @c depth and @c qos.
119  *
120  * @param conf Configuration to compare with.
121  * @return @c true when every field of @c *this matches @p conf.
122  */
123  [[nodiscard]] bool operator==(const DdscConf& conf) const noexcept;
124 
125  /**
126  * @brief Logical negation of @c operator==.
127  *
128  * @param conf Configuration to compare with.
129  * @return @c true when any field differs from @p conf.
130  */
131  [[nodiscard]] bool operator!=(const DdscConf& conf) const noexcept;
132 
133  /**
134  * @brief Reports this object's transport tag.
135  *
136  * @return @c TransportType::kDdsc.
137  */
138  [[nodiscard]] TransportType get_transport_type() const override;
139 
140  /**
141  * @brief Registers a named QoS profile that nodes may select through @c ?qos= in the URL.
142  *
143  * @details
144  * Profile names share a global namespace. Names that collide with DDS-reserved
145  * tokens (@c part, @c topic, @c pub, @c sub, @c writer, @c reader, @c depth) or
146  * with an already registered profile abort with a fatal log entry.
147  *
148  * @param name Unique profile key; must not collide with any reserved token.
149  * @param qos The @c Qos value to associate with @p name.
150  */
151  static void register_qos(const std::string& name, const Qos& qos);
152 
153  private:
154  static void register_qos_internal(const std::string& name, const Qos& qos);
155 
156  static const Qos& find_qos(const std::string& name);
157 
158  friend class DdscFactory;
159  static std::map<std::string, Qos> qos_map_;
160  static std::shared_mutex mtx_;
161  static constexpr const char* kRespSuffix{"___resp"};
162 #ifndef VLINK_ENABLE_C_INTERFACE
164 #endif
166  VLINK_CONF_IMPL(DdscConf)
167 };
168 
169 ////////////////////////////////////////////////////////////////
170 /// Details
171 ////////////////////////////////////////////////////////////////
172 
173 inline DdscConf::DdscConf(const std::string& _topic, int32_t _domain, int32_t _depth, const std::string& _qos)
174  : topic(_topic), domain(_domain), depth(_depth), qos(_qos) {}
175 
176 inline bool DdscConf::operator==(const DdscConf& conf) const noexcept {
177  return topic == conf.topic && domain == conf.domain && depth == conf.depth && qos == conf.qos;
178 }
179 
180 inline bool DdscConf::operator!=(const DdscConf& conf) const noexcept { return !(*this == conf); }
181 
182 inline TransportType DdscConf::get_transport_type() const { return TransportType::kDdsc; }
183 
184 } // namespace vlink
185 
186 #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