VLink  2.0.0
A high-performance communication middleware
ddst_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 ddst_conf.h
26  * @brief Transport configuration for the @c ddst:// TravoDDS transport.
27  *
28  * @details
29  * @c DdstConf binds the @c ddst:// URL scheme to TravoDDS, a domestic open-source
30  * DDS implementation hosted at https://gitee.com/agiros/travodds. The API surface
31  * mirrors @c DdsConf so callers may swap between Fast-DDS, RTI Connext, and TravoDDS
32  * by changing only the URL scheme; choose @c ddst:// when locality of supply chain
33  * or specific TravoDDS extensions are required.
34  *
35  * @par Supported Node Types
36  *
37  * | Publisher | Subscriber | Server | Client | Getter | Setter |
38  * | :-------: | :--------: | :----: | :----: | :----: | :----: |
39  * | yes | yes | yes | yes | yes | yes |
40  *
41  * @par URL Format
42  * @code
43  * ddst://<topic>[?domain=<N>&depth=<N>&qos=<profile>]
44  * ddst://<topic>[?domain=<N>&part=<v>&topic=<v>&pub=<v>&sub=<v>&writer=<v>&reader=<v>]
45  * @endcode
46  *
47  * | Component | Description |
48  * | ---------- | ---------------------------------------------------------------------------- |
49  * | @c topic | TravoDDS topic name (URL host concatenated with path) |
50  * | @c domain | DDS Domain ID (@c ?domain=); defaults from the @c VLINK_DDS_DOMAIN env var |
51  * | @c depth | Optional history-depth override; @c 0 keeps the QoS-selected depth |
52  * | @c qos | Named QoS profile registered via @c register_qos() (@c ?qos=) |
53  * | @c qos_ext | Remaining query keys after @c domain, @c depth and @c qos are removed |
54  *
55  * @par Backend-Specific Options
56  *
57  * | Option | Purpose | Default |
58  * | ------------------------ | -------------------------------------------------- | ------- |
59  * | XML QoS profile file | Loaded via @c load_global_qos_file() | none |
60  * | Discovered topics query | Snapshot from @c get_discovered_topics() | n/a |
61  * | RPC reply suffix | Auto-derived response topic name | ___resp |
62  *
63  * @par Example
64  * @code
65  * vlink::DdstConf::load_global_qos_file("/etc/vlink/ddst_profile.xml");
66  *
67  * vlink::Qos qos;
68  * qos.reliability.kind = vlink::Qos::Reliability::kReliable;
69  * vlink::DdstConf::register_qos("reliable", qos);
70  *
71  * auto pub = vlink::Publisher<MyMsg>::create_unique("ddst://telemetry/imu?domain=1&qos=reliable");
72  * @endcode
73  *
74  * @note Compiled only when @c VLINK_SUPPORT_DDST is defined.
75  * @note @c qos and @c qos_ext are mutually exclusive; setting both forces @c is_valid() to @c false.
76  * @note RPC reply topics are derived by appending @c "___resp" to the topic name.
77  */
78 
79 #pragma once
80 
81 #ifdef VLINK_SUPPORT_DDST
82 
83 #include <cstdint>
84 #include <map>
85 #include <shared_mutex>
86 #include <string>
87 #include <tuple>
88 #include <vector>
89 
90 #include "../base/functional.h"
91 #include "../extension/qos.h"
92 #include "../impl/conf.h"
93 
94 namespace vlink {
95 
96 /**
97  * @struct DdstConf
98  * @brief Concrete @c Conf describing a TravoDDS endpoint addressed by a @c ddst:// URL.
99  *
100  * @details
101  * Holds the topic name, Domain ID, optional history-depth override, and either a
102  * named QoS profile key or a per-entity property map for fine-grained tuning.
103  */
104 struct VLINK_EXPORT DdstConf final : public Conf {
105  std::string topic; ///< TravoDDS topic name (URL host concatenated with path).
106  int32_t domain{0}; ///< DDS Domain ID joined by the underlying DomainParticipant.
107  int32_t depth{0}; ///< Optional history-depth override; @c 0 keeps the QoS-selected depth.
108  std::string qos; ///< Named QoS profile key registered via @c register_qos().
109  PropertiesMap qos_ext; ///< Per-entity property map; populated from query keys outside @c domain / @c depth / @c qos.
110 
111  /**
112  * @brief Builds a @c DdstConf from topic, Domain, depth, and optional named QoS profile.
113  *
114  * @param _topic TravoDDS topic name.
115  * @param _domain Domain ID; defaults to @c 0.
116  * @param _depth History-depth override; defaults to @c 0 (use QoS depth).
117  * @param _qos Named QoS profile key; empty by default.
118  */
119  explicit DdstConf(const std::string& _topic, int32_t _domain = 0, int32_t _depth = 0, const std::string& _qos = "");
120 
121  /**
122  * @brief Builds a @c DdstConf from topic, Domain, and a per-entity QoS property map.
123  *
124  * @param _topic TravoDDS topic name.
125  * @param _domain Domain ID.
126  * @param _qos_ext Property map carrying per-entity QoS overrides.
127  */
128  explicit DdstConf(const std::string& _topic, int32_t _domain, const PropertiesMap& _qos_ext);
129 
130  /**
131  * @brief Component-wise equality on all configuration fields.
132  *
133  * @param conf Configuration to compare with.
134  * @return @c true when @c topic, @c domain, @c depth, @c qos and @c qos_ext all match.
135  */
136  [[nodiscard]] bool operator==(const DdstConf& conf) const noexcept;
137 
138  /**
139  * @brief Logical negation of @c operator==.
140  *
141  * @param conf Configuration to compare with.
142  * @return @c true when any field differs from @p conf.
143  */
144  [[nodiscard]] bool operator!=(const DdstConf& conf) const noexcept;
145 
146  /**
147  * @brief Reports this object's transport tag.
148  *
149  * @return @c TransportType::kDdst.
150  */
151  [[nodiscard]] TransportType get_transport_type() const override;
152 
153  /**
154  * @brief Returns the topics currently discovered on the given DDS Domain.
155  *
156  * @details
157  * Each entry is a @c (topic_name, type_name) pair captured from the
158  * @c DdstFactory discovery cache. The snapshot may be empty before discovery
159  * settles.
160  *
161  * @param _domain DDS Domain ID to query.
162  * @return Vector of @c (topic_name, type_name) tuples; may be empty.
163  */
164  [[nodiscard]] static std::vector<std::tuple<std::string, std::string>> get_discovered_topics(int32_t _domain);
165 
166  /**
167  * @brief Loads a TravoDDS XML QoS profile file as the process-wide default.
168  *
169  * @details
170  * Must be called before any @c ddst:// participant is created. Profile names
171  * declared in the file become available to all TravoDDS endpoints in the process.
172  *
173  * @param filepath Path to the XML QoS profile file.
174  * @return @c true when the file was loaded successfully, @c false otherwise.
175  */
176  static bool load_global_qos_file(const std::string& filepath);
177 
178  /**
179  * @brief Registers a named QoS profile that endpoints may reference via @c ?qos=.
180  *
181  * @details
182  * Profile names share a global namespace. Collisions with reserved tokens
183  * (@c part, @c topic, @c pub, @c sub, @c writer, @c reader, @c depth) or with an
184  * already registered profile abort with a fatal log entry.
185  *
186  * @param name Unique profile key; must not collide with any reserved token.
187  * @param qos @c Qos value associated with the key.
188  */
189  static void register_qos(const std::string& name, const Qos& qos);
190 
191  private:
192  static void register_qos_internal(const std::string& name, const Qos& qos);
193 
194  static const Qos& find_qos(const std::string& name);
195 
196  friend class DdstFactory;
197  static std::map<std::string, Function<void*()>> type_support_map_;
198  static std::map<std::string, Qos> qos_map_;
199  static std::shared_mutex mtx_;
200  static constexpr const char* kRespSuffix{"___resp"};
201 #ifndef VLINK_ENABLE_C_INTERFACE
203 #endif
205  VLINK_CONF_IMPL(DdstConf)
206 };
207 
208 ////////////////////////////////////////////////////////////////
209 /// Details
210 ////////////////////////////////////////////////////////////////
211 
212 inline DdstConf::DdstConf(const std::string& _topic, int32_t _domain, int32_t _depth, const std::string& _qos)
213  : topic(_topic), domain(_domain), depth(_depth), qos(_qos) {}
214 
215 inline DdstConf::DdstConf(const std::string& _topic, int32_t _domain, const PropertiesMap& _qos_ext)
216  : topic(_topic), domain(_domain), qos_ext(_qos_ext) {}
217 
218 inline bool DdstConf::operator==(const DdstConf& conf) const noexcept {
219  return topic == conf.topic && domain == conf.domain && depth == conf.depth && qos == conf.qos &&
220  qos_ext == conf.qos_ext;
221 }
222 
223 inline bool DdstConf::operator!=(const DdstConf& conf) const noexcept { return !(*this == conf); }
224 
225 inline TransportType DdstConf::get_transport_type() const { return TransportType::kDdst; }
226 
227 } // namespace vlink
228 
229 #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