Apollo 10.0
自动驾驶开放平台
rtps_transmitter.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 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
17#ifndef CYBER_TRANSPORT_TRANSMITTER_RTPS_TRANSMITTER_H_
18#define CYBER_TRANSPORT_TRANSMITTER_RTPS_TRANSMITTER_H_
19
20#include <cstddef>
21#include <memory>
22#include <string>
23
24#include "cyber/common/log.h"
27#include "cyber/time/time.h"
33
34namespace apollo {
35namespace cyber {
36namespace transport {
37
38template <typename M>
39class RtpsTransmitter : public Transmitter<M> {
40 public:
41 using MessagePtr = std::shared_ptr<M>;
42 using ParticipantPtr = std::shared_ptr<Participant>;
43
45 const ParticipantPtr& participant);
46 virtual ~RtpsTransmitter();
47
48 void Enable() override;
49 void Disable() override;
50
51 void Enable(const RoleAttributes& opposite_attr) override;
52 void Disable(const RoleAttributes& opposite_attr) override;
53
54 bool Transmit(const MessagePtr& msg, const MessageInfo& msg_info) override;
55
56 bool AcquireMessage(std::shared_ptr<M>& msg);
57
58 private:
59 bool Transmit(const M& msg, const MessageInfo& msg_info);
60
61 ParticipantPtr participant_;
62 PublisherPtr publisher_;
63};
64
65template <typename M>
66bool RtpsTransmitter<M>::AcquireMessage(std::shared_ptr<M>& msg) {
67 return false;
68}
69
70template <typename M>
72 const ParticipantPtr& participant)
73 : Transmitter<M>(attr), participant_(participant), publisher_(nullptr) {}
74
75template <typename M>
79
80template <typename M>
81void RtpsTransmitter<M>::Enable(const RoleAttributes& opposite_attr) {
82 (void)opposite_attr;
83 this->Enable();
84}
85
86template <typename M>
87void RtpsTransmitter<M>::Disable(const RoleAttributes& opposite_attr) {
88 (void)opposite_attr;
89 this->Disable();
90}
91
92template <typename M>
94 if (this->enabled_) {
95 return;
96 }
97
98 RETURN_IF_NULL(participant_);
99
100 publisher_ = participant_->CreatePublisher(this->attr_.channel_name(),
101 this->attr_.qos_profile());
102
103 RETURN_IF_NULL(publisher_);
104 this->enabled_ = true;
105}
106
107template <typename M>
109 if (this->enabled_) {
110 publisher_ = nullptr;
111 this->enabled_ = false;
112 }
113}
114
115template <typename M>
117 const MessageInfo& msg_info) {
118 return Transmit(*msg, msg_info);
119}
120
121template <typename M>
122bool RtpsTransmitter<M>::Transmit(const M& msg, const MessageInfo& msg_info) {
123 if (!this->enabled_) {
124 ADEBUG << "RtpsTransmitter not enable.";
125 return false;
126 }
127
128 UnderlayMessage m;
129 RETURN_VAL_IF(!message::SerializeToString(msg, &m.data()), false);
130 m.timestamp(msg_info.send_time());
131 m.seq(msg_info.seq_num());
132
133 if (participant_->is_shutdown()) {
134 return false;
135 }
136
137 RETURN_VAL_IF_NULL(publisher_, NULL);
138 return publisher_->Write(m, msg_info);
139}
140
141} // namespace transport
142} // namespace cyber
143} // namespace apollo
144
145#endif // CYBER_TRANSPORT_TRANSMITTER_RTPS_TRANSMITTER_H_
bool Transmit(const MessagePtr &msg, const MessageInfo &msg_info) override
RtpsTransmitter(const RoleAttributes &attr, const ParticipantPtr &participant)
bool AcquireMessage(std::shared_ptr< M > &msg)
std::shared_ptr< Participant > ParticipantPtr
#define RETURN_VAL_IF_NULL(ptr, val)
Definition log.h:98
#define RETURN_IF_NULL(ptr)
Definition log.h:90
#define RETURN_VAL_IF(condition, val)
Definition log.h:114
#define ADEBUG
Definition log.h:41
std::enable_if< HasSerializeToString< T >::value, bool >::type SerializeToString(const T &message, std::string *str)
std::shared_ptr< Publisher > PublisherPtr
Definition publisher.h:41
class register implement
Definition arena_queue.h:37