Apollo 10.0
自动驾驶开放平台
publisher.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2024 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18#include "cyber/common/log.h"
19
20namespace apollo {
21namespace cyber {
22namespace transport {
23
24Publisher::Publisher(const std::string& channel_name,
25 const proto::QosProfile& qos,
26 eprosima::fastdds::dds::DomainParticipant* participant)
27 : channel_name_(channel_name),
28 qos_(qos),
29 shutdown_(false),
30 participant_(participant),
31 publisher_(nullptr),
32 topic_(nullptr),
33 writer_(nullptr) {}
34
36
38 eprosima::fastdds::dds::PublisherQos pub_qos;
40 !QosFiller::FillInPubQos(this->channel_name_, this->qos_, &pub_qos),
41 false);
42 publisher_ = participant_->create_publisher(pub_qos, nullptr);
43 if (publisher_ == nullptr) {
44 AINFO << "something went wrong while creating the publisher...";
45 return false;
46 }
47
48 if (!EnsureCreateTopic(this->channel_name_)) {
49 AINFO << "something went wrong while creating the topic...";
50 return false;
51 }
52
53 eprosima::fastdds::dds::DataWriterQos writer_qos;
55 !QosFiller::FillInWriterQos(this->channel_name_, this->qos_, &writer_qos),
56 false);
57 writer_ = publisher_->create_datawriter(topic_, writer_qos, nullptr);
58 if (writer_ == nullptr) {
59 AINFO << "something went wrong while creating the datawriter...";
60 return false;
61 }
62
63 return true;
64}
65
66bool Publisher::Write(const UnderlayMessage& msg, bool is_topo_msg) {
67 RETURN_VAL_IF(shutdown_.load(), false);
68 if (is_topo_msg) {
69 AINFO << "FastDDSPublisher::Write data size: " << msg.data().size();
70 }
71 return writer_->write(
72 reinterpret_cast<void*>(const_cast<UnderlayMessage*>(&msg)));
73}
74
75bool Publisher::Write(const UnderlayMessage& msg, const MessageInfo& msg_info,
76 bool is_topo_msg) {
77 RETURN_VAL_IF(shutdown_.load(), false);
78 if (is_topo_msg) {
79 AINFO << "FastDDSPublisher::Write data size: " << msg.data().size();
80 }
81
82 eprosima::fastrtps::rtps::WriteParams wparams;
83
84 char* ptr =
85 reinterpret_cast<char*>(&wparams.related_sample_identity().writer_guid());
86
87 memcpy(ptr, msg_info.sender_id().data(), ID_SIZE);
88 memcpy(ptr + ID_SIZE, msg_info.spare_id().data(), ID_SIZE);
89
90 wparams.related_sample_identity().sequence_number().high =
91 (int32_t)((msg_info.seq_num() & 0xFFFFFFFF00000000) >> 32);
92 wparams.related_sample_identity().sequence_number().low =
93 (int32_t)(msg_info.seq_num() & 0xFFFFFFFF);
94
95 return writer_->write(
96 reinterpret_cast<void*>(const_cast<UnderlayMessage*>(&msg)), wparams);
97}
98
100 RETURN_IF(shutdown_.exchange(true));
101
102 if (publisher_ != nullptr && writer_ != nullptr) {
103 publisher_->delete_datawriter(writer_);
104 writer_ = nullptr;
105 }
106 if (participant_ != nullptr && publisher_ != nullptr) {
107 if (participant_->delete_publisher(publisher_) ==
108 eprosima::fastrtps::types::ReturnCode_t::RETCODE_OK) {
109 publisher_ = nullptr;
110 } else {
111 AERROR << channel_name_ << ": Failed to delete the publisher.";
112 }
113 }
114 if (participant_ != nullptr && topic_ != nullptr) {
115 participant_->delete_topic(topic_);
116 topic_ = nullptr;
117 }
118}
119
120bool Publisher::EnsureCreateTopic(const std::string& channel_name) {
121 topic_ = dynamic_cast<eprosima::fastdds::dds::Topic*>(
122 participant_->lookup_topicdescription(channel_name));
123 if (topic_ == nullptr) {
124 eprosima::fastdds::dds::TopicQos topic_qos;
126 !QosFiller::FillInTopicQos(channel_name, this->qos_, &topic_qos),
127 false);
128 topic_ =
129 participant_->create_topic(channel_name, "UnderlayMessage", topic_qos);
130 }
131 return (topic_ != nullptr);
132}
133
134} // namespace transport
135} // namespace cyber
136} // namespace apollo
const char * data() const
Definition identity.h:44
const Identity & sender_id() const
const Identity & spare_id() const
bool Write(const UnderlayMessage &msg, bool is_topo_msg=false)
Definition publisher.cc:66
Publisher(const std::string &channel_name, const proto::QosProfile &qos, eprosima::fastdds::dds::DomainParticipant *participant)
Definition publisher.cc:24
static bool FillInTopicQos(const std::string &channel_name, const proto::QosProfile &qos, eprosima::fastdds::dds::TopicQos *topic_qos)
static bool FillInPubQos(const std::string &channel_name, const proto::QosProfile &qos, eprosima::fastdds::dds::PublisherQos *pub_qos)
static bool FillInWriterQos(const std::string &channel_name, const proto::QosProfile &qos, eprosima::fastdds::dds::DataWriterQos *writer_qos)
This class represents the structure UnderlayMessage defined by the user in the IDL file.
void data(const std::string &_data)
This function copies the value in member data
#define RETURN_VAL_IF(condition, val)
Definition log.h:114
#define AERROR
Definition log.h:44
#define RETURN_IF(condition)
Definition log.h:106
#define AINFO
Definition log.h:42
constexpr uint8_t ID_SIZE
Definition identity.h:28
class register implement
Definition arena_queue.h:37