Branch data Line data Source code
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 : : #pragma once
25 : :
26 : : #include <fastdds/dds/core/LoanableSequence.hpp>
27 : :
28 : : #ifdef VLINK_SUPPORT_DDS_V3
29 : : #include <fastdds/dds/domain/DomainParticipant.hpp>
30 : : #include <fastdds/dds/domain/DomainParticipantFactory.hpp>
31 : : #include <fastdds/dds/publisher/DataWriter.hpp>
32 : : #include <fastdds/dds/publisher/DataWriterListener.hpp>
33 : : #include <fastdds/dds/publisher/Publisher.hpp>
34 : : #include <fastdds/dds/subscriber/DataReader.hpp>
35 : : #include <fastdds/dds/subscriber/DataReaderListener.hpp>
36 : : #include <fastdds/dds/subscriber/Subscriber.hpp>
37 : : #include <fastdds/rtps/common/WriteParams.hpp>
38 : : #include <fastdds/rtps/transport/TCPv4TransportDescriptor.hpp>
39 : : #include <fastdds/rtps/transport/TCPv6TransportDescriptor.hpp>
40 : : #include <fastdds/rtps/transport/UDPv4TransportDescriptor.hpp>
41 : : #include <fastdds/rtps/transport/UDPv6TransportDescriptor.hpp>
42 : : #include <fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.hpp>
43 : :
44 : : #include "./builtin/3_x/BuiltInRawPubSubTypes.hpp"
45 : : #else
46 : : #include <fastdds/rtps/transport/TCPv4TransportDescriptor.h>
47 : : #include <fastdds/rtps/transport/TCPv6TransportDescriptor.h>
48 : : #include <fastdds/rtps/transport/UDPv4TransportDescriptor.h>
49 : : #include <fastdds/rtps/transport/UDPv6TransportDescriptor.h>
50 : : #include <fastdds/rtps/transport/shared_mem/SharedMemTransportDescriptor.h>
51 : :
52 : : #include <fastdds/dds/domain/DomainParticipant.hpp>
53 : : #include <fastdds/dds/domain/DomainParticipantFactory.hpp>
54 : : #include <fastdds/dds/publisher/DataWriter.hpp>
55 : : #include <fastdds/dds/publisher/DataWriterListener.hpp>
56 : : #include <fastdds/dds/publisher/Publisher.hpp>
57 : : #include <fastdds/dds/subscriber/DataReader.hpp>
58 : : #include <fastdds/dds/subscriber/DataReaderListener.hpp>
59 : : #include <fastdds/dds/subscriber/Subscriber.hpp>
60 : :
61 : : #include "./builtin/2_x/BuiltInRawPubSubTypes.hpp"
62 : : #endif
63 : :
64 : : #include <cstdint>
65 : : #include <map>
66 : : #include <memory>
67 : : #include <optional>
68 : : #include <string>
69 : : #include <thread>
70 : : #include <tuple>
71 : : #include <utility>
72 : : #include <vector>
73 : :
74 : : #include "./extension/qos.h"
75 : : #include "./impl/abstract_factory.h"
76 : : #include "./modules/dds_conf.h"
77 : :
78 : : namespace vlink {
79 : :
80 : : #ifdef VLINK_SUPPORT_DDS_V3
81 : : namespace dds = eprosima::fastdds::dds;
82 : : namespace rtps = eprosima::fastdds::rtps;
83 : : namespace rtps2 = eprosima::fastdds::rtps;
84 : : #else
85 : : namespace dds = eprosima::fastdds::dds;
86 : : namespace rtps = eprosima::fastrtps::rtps;
87 : : namespace rtps2 = eprosima::fastdds::rtps;
88 : : #endif
89 : :
90 : : // DdsFactory
91 : : class DdsFactory final {
92 : : private:
93 : : DdsFactory();
94 : :
95 : : ~DdsFactory();
96 : :
97 : : public:
98 : : struct ReadCdrMessage final {
99 : : dds::LoanableSequence<Bytes> samples;
100 : : dds::SampleInfoSeq infos;
101 : : uint64_t id{0};
102 : : int64_t timestamp{0};
103 : : };
104 : :
105 : : struct ReadMessage final {
106 : : dds::LoanableSequence<BuiltInRaw> samples;
107 : : dds::SampleInfoSeq infos;
108 : : uint64_t id{0};
109 : : int64_t timestamp{0};
110 : : };
111 : :
112 : : static std::vector<std::tuple<std::string, std::string>> get_discovered_topics(int32_t _domain);
113 : :
114 : : static bool load_global_qos_file(const std::string& filepath);
115 : :
116 : : static std::shared_ptr<dds::DomainParticipant> create_participant(uint8_t type, const DdsConf& conf,
117 : : const Conf::PropertiesMap& properties);
118 : :
119 : : static std::shared_ptr<dds::Topic> create_topic(uint8_t type, const DdsConf& conf, dds::DomainParticipant* part,
120 : : bool is_cdr_type, std::string topic = "",
121 : : std::string cdr_type_name = "");
122 : :
123 : : static std::pair<std::shared_ptr<dds::Topic>, std::shared_ptr<dds::Topic>> create_method_topic(
124 : : uint8_t type, const DdsConf& conf, dds::DomainParticipant* part, bool is_cdr_type,
125 : : const std::string& cdr_type_names = "");
126 : :
127 : : static std::shared_ptr<dds::Publisher> create_publisher(uint8_t type, const DdsConf& conf,
128 : : dds::DomainParticipant* part);
129 : :
130 : : static std::shared_ptr<dds::Subscriber> create_subscriber(uint8_t type, const DdsConf& conf,
131 : : dds::DomainParticipant* part);
132 : :
133 : : static std::shared_ptr<dds::DataWriter> create_datawriter(uint8_t type, const DdsConf& conf,
134 : : dds::Publisher* publisher, dds::Topic* topic,
135 : : dds::DataWriterListener* listener, bool is_cdr_type);
136 : :
137 : : static std::shared_ptr<dds::DataReader> create_datareader(uint8_t type, const DdsConf& conf,
138 : : dds::Subscriber* subscriber, dds::Topic* topic,
139 : : dds::DataReaderListener* listener, bool is_cdr_type);
140 : :
141 : : static bool write_data(dds::DataWriter* writer, const Bytes& bytes, uint64_t id);
142 : :
143 : : static bool write_cdr_data(dds::DataWriter* writer, const Bytes& bytes, rtps::WriteParams* params = nullptr);
144 : :
145 : : static bool take_data(dds::DataReader* reader, ReadMessage& msg);
146 : :
147 : : static void return_data_loan(dds::DataReader* reader, ReadMessage& msg);
148 : :
149 : : static bool take_cdr_data(dds::DataReader* reader, ReadCdrMessage& msg);
150 : :
151 : : static void return_cdr_loan(dds::DataReader* reader, ReadCdrMessage& msg);
152 : :
153 : : static uint64_t get_guid(const rtps::GUID_t& guid, uint32_t seq);
154 : :
155 : : static int get_default_domain_id();
156 : :
157 : : private:
158 : : static void set_participant_qos(dds::DomainParticipantQos& dds_qos, const Conf::PropertiesMap& properties);
159 : :
160 : : static rtps::LocatorList_t get_locators(const std::vector<std::string>& list);
161 : :
162 : : static std::string get_qos_ext(const Conf::PropertiesMap& ext, const std::string& key);
163 : :
164 : : template <typename MapT, typename KeyT, typename ValueT = typename MapT::mapped_type>
165 : 858 : static auto get_weak_ptr(MapT& map, const KeyT& key) -> ValueT {
166 [ + - ]: 858 : auto iter = map.find(key);
167 : :
168 [ + + ]: 858 : if (iter == map.end()) {
169 : 574 : return ValueT();
170 : : }
171 : :
172 : 284 : return iter->second;
173 : : }
174 : :
175 : : using PartFilter = std::tuple<uint8_t, int32_t, std::string, Conf::PropertiesMap>;
176 : : using TopicFilter = std::tuple<uint8_t, int32_t, std::string, dds::DomainParticipant*>;
177 : : using PublisherFilter = std::tuple<uint8_t, int32_t, std::string, std::string, std::string, dds::DomainParticipant*>;
178 : : using SubscriberFilter = std::tuple<uint8_t, int32_t, std::string, std::string, std::string, dds::DomainParticipant*>;
179 : :
180 : : dds::DomainParticipantFactory* dds_factory_{nullptr};
181 : : std::map<PartFilter, std::weak_ptr<dds::DomainParticipant>> part_map_;
182 : : std::map<TopicFilter, std::weak_ptr<dds::Topic>> topic_map_;
183 : : std::map<PublisherFilter, std::weak_ptr<dds::Publisher>> publisher_map_;
184 : : std::map<SubscriberFilter, std::weak_ptr<dds::Subscriber>> subscriber_map_;
185 : : dds::TypeSupport raw_typesupport_;
186 : : std::recursive_mutex participant_mtx_;
187 : : std::mutex raw_typesupport_mtx_;
188 : : std::mutex mtx_;
189 : :
190 : : Qos default_event_qos_;
191 : : Qos default_method_qos_;
192 : : Qos default_field_qos_;
193 : :
194 [ + + + - : 103 : VLINK_SINGLETON_DECLARE(DdsFactory)
+ - - - ]
195 : : };
196 : :
197 : : } // namespace vlink
|