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 <dds/dds.h>
27 : : #include <dds/ddsi/ddsi_config.h>
28 : :
29 : : #include <map>
30 : : #include <memory>
31 : : #include <mutex>
32 : : #include <stdexcept>
33 : : #include <string>
34 : : #include <tuple>
35 : : #include <utility>
36 : : #include <vector>
37 : :
38 : : #include "./builtin/BuiltInRaw.h"
39 : : #include "./ddsc_proxy.hpp"
40 : : #include "./extension/qos.h"
41 : : #include "./impl/abstract_factory.h"
42 : : #include "./modules/ddsc_conf.h"
43 : :
44 : : namespace vlink {
45 : :
46 : : // DdscFactory
47 : : class DdscFactory final {
48 : : private:
49 : : DdscFactory();
50 : :
51 : : ~DdscFactory();
52 : :
53 : : public:
54 : : struct ReadMessage final {
55 : : Bytes bytes;
56 : : dds_sample_info_t info;
57 : : void* sample{nullptr};
58 : : uint64_t id{0};
59 : : uint64_t guid{0};
60 : : int64_t timestamp{0};
61 : : };
62 : :
63 : : static std::shared_ptr<ddsc::DomainParticipant> create_participant(uint8_t type, const DdscConf& conf,
64 : : const Conf::PropertiesMap& properties);
65 : :
66 : : static std::shared_ptr<ddsc::Topic> create_topic(uint8_t type, const DdscConf& conf, ddsc::DomainParticipant* part,
67 : : std::string topic = "");
68 : :
69 : : static std::pair<std::shared_ptr<ddsc::Topic>, std::shared_ptr<ddsc::Topic>> create_method_topic(
70 : : uint8_t type, const DdscConf& conf, ddsc::DomainParticipant* part);
71 : :
72 : : static std::shared_ptr<ddsc::Publisher> create_publisher(uint8_t type, const DdscConf& conf,
73 : : ddsc::DomainParticipant* part);
74 : :
75 : : static std::shared_ptr<ddsc::Subscriber> create_subscriber(uint8_t type, const DdscConf& conf,
76 : : ddsc::DomainParticipant* part);
77 : :
78 : : static std::shared_ptr<ddsc::DataWriter> create_datawriter(uint8_t type, const DdscConf& conf,
79 : : ddsc::Publisher* publisher, ddsc::Topic* topic,
80 : : dds_listener_t* listener = nullptr);
81 : :
82 : : static std::shared_ptr<ddsc::DataReader> create_datareader(uint8_t type, const DdscConf& conf,
83 : : ddsc::Subscriber* subscriber, ddsc::Topic* topic,
84 : : dds_listener_t* listener = nullptr);
85 : :
86 : : static bool write_data(dds_entity_t entity, const Bytes& bytes, uint64_t id);
87 : :
88 : : static bool take_data(dds_entity_t entity, ReadMessage& msg);
89 : :
90 : : static bool release_data(dds_entity_t entity, ReadMessage& msg);
91 : :
92 : : static uint64_t get_guid(const dds_guid_t* guid, uint32_t seq);
93 : :
94 : : static int get_default_domain_id();
95 : :
96 : : static std::string process_cyclone_dds_uri();
97 : :
98 : : private:
99 : : static void set_participant_qos(int32_t domain_id, dds_qos_t* dds_qos, const Conf::PropertiesMap& properties);
100 : :
101 : : template <typename MapT, typename KeyT, typename ValueT = typename MapT::mapped_type>
102 : 659 : static auto get_weak_ptr(MapT& map, const KeyT& key) -> ValueT {
103 [ + - ]: 659 : auto iter = map.find(key);
104 : :
105 [ + + ]: 659 : if (iter == map.end()) {
106 : 421 : return ValueT();
107 : : }
108 : :
109 : 238 : return iter->second;
110 : : }
111 : :
112 : : using PartFilter = std::tuple<uint8_t, int32_t, Conf::PropertiesMap>;
113 : : using TopicFilter = std::tuple<uint8_t, int32_t, std::string, ddsc::DomainParticipant*>;
114 : : using PublisherFilter = std::tuple<uint8_t, int32_t, std::string, ddsc::DomainParticipant*>;
115 : : using SubscriberFilter = std::tuple<uint8_t, int32_t, std::string, ddsc::DomainParticipant*>;
116 : :
117 : : struct DomainEntry final {
118 : : dds_entity_t entity{0};
119 : : size_t ref_count{0};
120 : : std::string ssl_keystore;
121 : : std::string ssl_key_pass;
122 : : std::string ssl_ciphers;
123 : : std::vector<std::string> network_interface_list;
124 : : std::unique_ptr<ddsi_config_network_interface_listelem[]> network_interface_elements;
125 : : std::vector<std::string> peer_list;
126 : : std::unique_ptr<ddsi_config_peer_listelem[]> peer_elements;
127 : : };
128 : :
129 : : std::map<int32_t, DomainEntry> domain_map_;
130 : : std::map<PartFilter, std::weak_ptr<ddsc::DomainParticipant>> part_map_;
131 : : std::map<TopicFilter, std::weak_ptr<ddsc::Topic>> topic_map_;
132 : : std::map<PublisherFilter, std::weak_ptr<ddsc::Publisher>> publisher_map_;
133 : : std::map<SubscriberFilter, std::weak_ptr<ddsc::Subscriber>> subscriber_map_;
134 : : std::mutex mtx_;
135 : :
136 : : Qos default_event_qos_;
137 : : Qos default_method_qos_;
138 : : Qos default_field_qos_;
139 : :
140 [ + + + - : 72 : VLINK_SINGLETON_DECLARE(DdscFactory)
+ - - - ]
141 : : };
142 : :
143 : : } // namespace vlink
|