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 "./dds_publisher_impl.h" 25 : : 26 : : #include "./base/message_loop.h" 27 : : 28 : : namespace vlink { 29 : : 30 : : // WriterListener 31 : 77 : DdsPublisherImpl::WriterListener::WriterListener(NodeImpl* impl) : DdsWriterListener(impl) {} 32 : : 33 : 105 : void DdsPublisherImpl::WriterListener::on_publication_matched(dds::DataWriter* writer, 34 : : const dds::PublicationMatchedStatus& status) { 35 : 105 : auto* instance = static_cast<DdsPublisherImpl*>(get_impl()); 36 : 105 : auto* message_loop = instance->get_message_loop(); 37 : : 38 : 105 : instance->session_count_.store(status.current_count, std::memory_order_release); 39 : : 40 [ + + ]: 105 : if (message_loop) { 41 [ + - + - ]: 1 : message_loop->post_task([instance]() { 42 [ - + ]: 1 : if VUNLIKELY (!instance->get_message_loop()) { 43 : 0 : return; 44 : : } 45 : : 46 : 1 : instance->update_subscribers(); 47 : : }); 48 : : } else { 49 : 104 : instance->update_subscribers(); 50 : : } 51 : : 52 : 105 : DdsWriterListener::on_publication_matched(writer, status); 53 : 105 : } 54 : : 55 : : // DdsPublisherImpl 56 [ + - + - ]: 82 : DdsPublisherImpl::DdsPublisherImpl(const DdsConf& conf) : conf_(conf) {} 57 : : 58 : 80 : void DdsPublisherImpl::init() { 59 [ - + - - : 80 : if VUNLIKELY (is_cdr_type && is_security_type) { - + ] 60 [ # # # # ]: 0 : VLOG_F("Cdr type does not support security."); 61 : : } 62 : : 63 [ + - ]: 80 : participant_ = DdsFactory::create_participant(kPublisher | kSubscriber, conf_, get_all_properties()); 64 : : 65 [ + - + - ]: 80 : topic_ = DdsFactory::create_topic(kPublisher | kSubscriber, conf_, participant_.get(), is_cdr_type); 66 : : 67 : 80 : publisher_ = DdsFactory::create_publisher(kPublisher, conf_, participant_.get()); 68 : : 69 [ + + + + : 80 : if VUNLIKELY (!participant_ || !topic_) { + + ] 70 [ + - + - ]: 6 : VLOG_E("DdsPublisherImpl::init(): participant/topic creation failed; publisher left uninitialised."); 71 : : 72 : 3 : return; 73 : : } 74 : : 75 [ + - ]: 77 : listener_.emplace(this); 76 : : 77 : 77 : writer_ = DdsFactory::create_datawriter(kPublisher, conf_, publisher_.get(), topic_.get(), &listener_.value()); 78 : : } 79 : : 80 : 80 : void DdsPublisherImpl::deinit() { 81 : 80 : detach(); 82 : : 83 : 80 : writer_.reset(); 84 : 80 : listener_.reset(); 85 : 80 : publisher_.reset(); 86 : 80 : topic_.reset(); 87 : 80 : participant_.reset(); 88 : 80 : } 89 : : 90 : 0 : const Conf* DdsPublisherImpl::get_conf() const { return &conf_; } 91 : : 92 : 17 : const AbstractNode* DdsPublisherImpl::get_abstract_node() const { return this; } 93 : : 94 : 6 : Status::BasePtr DdsPublisherImpl::get_status(Status::Type type) const { 95 [ + + ]: 6 : if VUNLIKELY (!writer_) { 96 : 1 : return std::make_shared<Status::Unknown>(); 97 : : } 98 : : 99 : 5 : return WriterListener::get_status(writer_.get(), type); 100 : : } 101 : : 102 : 11 : std::any DdsPublisherImpl::get_native_handle() const { return publisher_; } 103 : : 104 : 854 : bool DdsPublisherImpl::has_subscribers() const { return session_count_.load(std::memory_order_acquire) > 0; } 105 : : 106 : 112 : bool DdsPublisherImpl::write(const Bytes& msg_data) { 107 [ - + ]: 112 : if (is_cdr_type) { 108 : 0 : return DdsFactory::write_cdr_data(writer_.get(), msg_data); 109 : : } 110 : : 111 : 224 : return DdsFactory::write_data(writer_.get(), msg_data, seq_.fetch_add(1, std::memory_order_relaxed)); 112 : : } 113 : : 114 : : } // namespace vlink