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