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 "./ddsc_publisher_impl.hpp" 25 : : 26 : : #include "./base/message_loop.h" 27 : : 28 : : namespace vlink { 29 : : 30 : : // WriterListener 31 : 47 : DdscPublisherImpl::WriterListener::WriterListener(NodeImpl* impl) : DdscWriterListener(impl) {} 32 : : 33 : 74 : void DdscPublisherImpl::WriterListener::on_publication_matched(NodeImpl* impl, 34 : : const dds_publication_matched_status_t& status) { 35 : 74 : auto* instance = static_cast<DdscPublisherImpl*>(impl); 36 : 74 : auto* message_loop = instance->get_message_loop(); 37 : : 38 : 74 : instance->session_count_.store(status.current_count, std::memory_order_release); 39 : : 40 [ + + ]: 74 : 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 : 73 : instance->update_subscribers(); 50 : : } 51 : : 52 : 74 : DdscWriterListener::on_publication_matched(impl, status); 53 : 74 : } 54 : : 55 : : // DdscPublisherImpl 56 [ + - + - ]: 49 : DdscPublisherImpl::DdscPublisherImpl(const DdscConf& conf) : conf_(conf) {} 57 : : 58 : 47 : void DdscPublisherImpl::init() { 59 [ + - ]: 47 : participant_ = DdscFactory::create_participant(kPublisher | kSubscriber, conf_, get_all_properties()); 60 : : 61 [ + - + - ]: 47 : topic_ = DdscFactory::create_topic(kPublisher | kSubscriber, conf_, participant_.get()); 62 : : 63 : 47 : publisher_ = DdscFactory::create_publisher(kPublisher, conf_, participant_.get()); 64 : : 65 [ + - - + : 47 : if VUNLIKELY (!participant_ || !topic_) { - + ] 66 [ # # # # ]: 0 : VLOG_E("DdscPublisherImpl::init(): participant/topic creation failed; publisher left uninitialised."); 67 : : 68 : 0 : return; 69 : : } 70 : : 71 [ + - ]: 47 : listener_.emplace(this); 72 : : 73 : 47 : writer_ = DdscFactory::create_datawriter(kPublisher, conf_, publisher_.get(), topic_.get(), listener_->get_ptr()); 74 : : } 75 : : 76 : 47 : void DdscPublisherImpl::deinit() { 77 : 47 : detach(); 78 : : 79 : 47 : writer_.reset(); 80 : 47 : listener_.reset(); 81 : 47 : publisher_.reset(); 82 : 47 : topic_.reset(); 83 : 47 : participant_.reset(); 84 : 47 : } 85 : : 86 : 0 : const Conf* DdscPublisherImpl::get_conf() const { return &conf_; } 87 : : 88 : 11 : const AbstractNode* DdscPublisherImpl::get_abstract_node() const { return this; } 89 : : 90 : 6 : Status::BasePtr DdscPublisherImpl::get_status(Status::Type type) const { 91 [ + + ]: 6 : if VUNLIKELY (!writer_) { 92 : 1 : return std::make_shared<Status::Unknown>(); 93 : : } 94 : : 95 : 5 : return WriterListener::get_status(writer_->entity, type); 96 : : } 97 : : 98 : 8 : std::any DdscPublisherImpl::get_native_handle() const { return publisher_; } 99 : : 100 : 380 : bool DdscPublisherImpl::has_subscribers() const { return session_count_.load(std::memory_order_acquire) > 0; } 101 : : 102 : 82 : bool DdscPublisherImpl::write(const Bytes& msg_data) { 103 : 164 : return DdscFactory::write_data(writer_->entity, msg_data, seq_.fetch_add(1, std::memory_order_relaxed)); 104 : : } 105 : : 106 : : } // namespace vlink