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 : : #include "./modules/ddsc_conf.h" 25 : : 26 : : #include <map> 27 : : #include <memory> 28 : : #include <string> 29 : : 30 : : #include "./base/helpers.h" 31 : : #include "./base/logger.h" 32 : : #include "./ddsc_client_impl.hpp" 33 : : #include "./ddsc_getter_impl.hpp" 34 : : #include "./ddsc_publisher_impl.hpp" 35 : : #include "./ddsc_server_impl.hpp" 36 : : #include "./ddsc_setter_impl.hpp" 37 : : #include "./ddsc_subscriber_impl.hpp" 38 : : #include "./impl/conf_plugin_interface.h" 39 : : #include "./impl/url.h" 40 : : 41 : : namespace vlink { 42 : : 43 : : VLINK_DEFINE_GLOBAL_PROPERTY(DdscConf) 44 : : 45 : : std::map<std::string, Qos> DdscConf::qos_map_; 46 : : std::shared_mutex DdscConf::mtx_; 47 : : 48 : : // DdscConf 49 : 21 : void DdscConf::register_qos(const std::string& name, const Qos& qos) { 50 [ + - ]: 21 : std::lock_guard lock(mtx_); 51 : : 52 [ + - + + : 21 : if VUNLIKELY (qos_map_.find(name) != qos_map_.end() || name == "part" || name == "topic" || name == "pub" || - + + + - + + + - + + + - + + + - + + + - + + + + + + + ] 53 : : name == "sub" || name == "writer" || name == "reader" || name == "depth") { 54 [ + - - + ]: 4 : VLOG_F("DdscConf: Invalid qos name: '", name, "' (reserved or already registered)."); 55 : : } 56 : : 57 [ + - ]: 19 : register_qos_internal(name, qos); 58 : 21 : } 59 : : 60 : 147 : void DdscConf::register_qos_internal(const std::string& name, const Qos& qos) { qos_map_[name] = qos; } 61 : : 62 : 26 : const Qos& DdscConf::find_qos(const std::string& name) { 63 [ + - ]: 26 : std::shared_lock lock(mtx_); 64 : : 65 [ + - ]: 26 : auto iter = qos_map_.find(name); 66 : : 67 [ - + ]: 26 : if VUNLIKELY (iter == qos_map_.end()) { 68 [ # # # # ]: 0 : CLOG_E("DdscConf: Invalid qos [%s].", name.c_str()); 69 : : static Qos invalid_qos; 70 : 0 : return invalid_qos; 71 : : } 72 : : 73 : 26 : return iter->second; 74 : 26 : } 75 : : 76 : 0 : void DdscConf::global_init() { DdscFactory::get(); } 77 : : 78 : 93 : bool DdscConf::parse_protocol(struct Protocol* protocol) { 79 [ - + ]: 93 : if VUNLIKELY (get_impl_type() == kUnknownImplType) { 80 [ # # # # ]: 0 : VLOG_F("DdscConf: Unknown node type, cannot parse protocol."); 81 : 0 : return false; 82 : : } 83 : : 84 [ + + ]: 93 : if VUNLIKELY (protocol->host.empty()) { 85 : 1 : return false; 86 : : } 87 : : 88 [ + + + - : 92 : static int default_domain_id = DdscFactory::get_default_domain_id(); + - - - ] 89 : : 90 [ + + + - : 92 : topic = protocol->host + (protocol->path.empty() ? "" : "/" + protocol->path); + - + - + + - - ] 91 [ + - + - ]: 92 : domain = Helpers::to_int(protocol->dictionary["domain"], default_domain_id); 92 [ + - + - ]: 92 : depth = Helpers::to_int(protocol->dictionary["depth"]); 93 [ + - + - : 92 : qos = protocol->dictionary["qos"]; + - ] 94 : : 95 : 92 : return true; 96 : : } 97 : : 98 : 213 : bool DdscConf::is_valid() const { 99 [ + + + + : 213 : if VUNLIKELY (domain < 0 || topic.empty()) { + + ] 100 : 2 : return false; 101 : : } 102 : : 103 : 211 : return true; 104 : : } 105 : : 106 : 34 : std::unique_ptr<ServerImpl> DdscConf::create_server() const { return std::make_unique<DdscServerImpl>(*this); } 107 : : 108 : 33 : std::unique_ptr<ClientImpl> DdscConf::create_client() const { return std::make_unique<DdscClientImpl>(*this); } 109 : : 110 : 49 : std::unique_ptr<PublisherImpl> DdscConf::create_publisher() const { return std::make_unique<DdscPublisherImpl>(*this); } 111 : : 112 : 48 : std::unique_ptr<SubscriberImpl> DdscConf::create_subscriber() const { 113 : 48 : return std::make_unique<DdscSubscriberImpl>(*this); 114 : : } 115 : : 116 : 20 : std::unique_ptr<SetterImpl> DdscConf::create_setter() const { return std::make_unique<DdscSetterImpl>(*this); } 117 : : 118 : 25 : std::unique_ptr<GetterImpl> DdscConf::create_getter() const { return std::make_unique<DdscGetterImpl>(*this); } 119 : : 120 : 1 : std::ostream& operator<<(std::ostream& ostream, const DdscConf& conf) noexcept { 121 : 1 : std::ios_base::fmtflags f = ostream.flags(); 122 : : 123 : : ostream << "DdscConf:" 124 : 1 : << "[type]" << +conf.get_impl_type() << "[topic]" << conf.topic << "[domain]" << conf.domain << "[depth]" 125 : 1 : << conf.depth << "[qos]" << conf.qos; 126 : : 127 : 1 : ostream.flags(f); 128 : : 129 : 1 : return ostream; 130 : : } 131 : : 132 : : class ConfPluginDdsc : public ConfPluginInterface { 133 : : protected: 134 : 1 : TransportType get_transport_type() const override { return TransportType::kDdsc; } 135 : : 136 : 0 : std::unique_ptr<Conf> create() const override { return std::make_unique<DdscConf>(); } 137 : : }; 138 : : 139 : : #ifdef VLINK_LIBRARY 140 [ + + + - : 2 : VLINK_PLUGIN_DECLARE(ConfPluginDdsc, 1, 0) + - + - - + ] 141 : : #endif 142 : : 143 : : } // namespace vlink