Apollo  6.0
Open source self driving car software
rtps_transmitter.h
Go to the documentation of this file.
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 <memory>
21 #include <string>
22 
23 #include "cyber/common/log.h"
28 #include "fastrtps/Domain.h"
29 #include "fastrtps/attributes/PublisherAttributes.h"
30 #include "fastrtps/participant/Participant.h"
31 #include "fastrtps/publisher/Publisher.h"
32 
33 namespace apollo {
34 namespace cyber {
35 namespace transport {
36 
37 template <typename M>
38 class RtpsTransmitter : public Transmitter<M> {
39  public:
40  using MessagePtr = std::shared_ptr<M>;
41 
42  RtpsTransmitter(const RoleAttributes& attr,
43  const ParticipantPtr& participant);
44  virtual ~RtpsTransmitter();
45 
46  void Enable() override;
47  void Disable() override;
48 
49  bool Transmit(const MessagePtr& msg, const MessageInfo& msg_info) override;
50 
51  private:
52  bool Transmit(const M& msg, const MessageInfo& msg_info);
53 
54  ParticipantPtr participant_;
55  eprosima::fastrtps::Publisher* publisher_;
56 };
57 
58 template <typename M>
59 RtpsTransmitter<M>::RtpsTransmitter(const RoleAttributes& attr,
60  const ParticipantPtr& participant)
61  : Transmitter<M>(attr), participant_(participant), publisher_(nullptr) {}
62 
63 template <typename M>
65  Disable();
66 }
67 
68 template <typename M>
70  if (this->enabled_) {
71  return;
72  }
73 
74  RETURN_IF_NULL(participant_);
75 
76  eprosima::fastrtps::PublisherAttributes pub_attr;
78  this->attr_.channel_name(), this->attr_.qos_profile(), &pub_attr));
79  publisher_ = eprosima::fastrtps::Domain::createPublisher(
80  participant_->fastrtps_participant(), pub_attr);
81  RETURN_IF_NULL(publisher_);
82  this->enabled_ = true;
83 }
84 
85 template <typename M>
87  if (this->enabled_) {
88  publisher_ = nullptr;
89  this->enabled_ = false;
90  }
91 }
92 
93 template <typename M>
95  const MessageInfo& msg_info) {
96  return Transmit(*msg, msg_info);
97 }
98 
99 template <typename M>
100 bool RtpsTransmitter<M>::Transmit(const M& msg, const MessageInfo& msg_info) {
101  if (!this->enabled_) {
102  ADEBUG << "not enable.";
103  return false;
104  }
105 
106  UnderlayMessage m;
107  RETURN_VAL_IF(!message::SerializeToString(msg, &m.data()), false);
108 
109  eprosima::fastrtps::rtps::WriteParams wparams;
110 
111  char* ptr =
112  reinterpret_cast<char*>(&wparams.related_sample_identity().writer_guid());
113 
114  memcpy(ptr, msg_info.sender_id().data(), ID_SIZE);
115  memcpy(ptr + ID_SIZE, msg_info.spare_id().data(), ID_SIZE);
116 
117  wparams.related_sample_identity().sequence_number().high =
118  (int32_t)((msg_info.seq_num() & 0xFFFFFFFF00000000) >> 32);
119  wparams.related_sample_identity().sequence_number().low =
120  (int32_t)(msg_info.seq_num() & 0xFFFFFFFF);
121 
122  if (participant_->is_shutdown()) {
123  return false;
124  }
125  return publisher_->write(reinterpret_cast<void*>(&m), wparams);
126 }
127 
128 } // namespace transport
129 } // namespace cyber
130 } // namespace apollo
131 
132 #endif // CYBER_TRANSPORT_TRANSMITTER_RTPS_TRANSMITTER_H_
std::shared_ptr< M > MessagePtr
Definition: rtps_transmitter.h:40
bool enabled_
Definition: endpoint.h:45
This class represents the structure UnderlayMessage defined by the user in the IDL file...
Definition: underlay_message.h:38
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
#define RETURN_VAL_IF(condition, val)
Definition: log.h:114
constexpr uint8_t ID_SIZE
Definition: identity.h:28
#define RETURN_IF(condition)
Definition: log.h:106
Definition: rtps_transmitter.h:38
std::enable_if< HasSerializeToString< T >::value, bool >::type SerializeToString(const T &message, std::string *str)
Definition: message_traits.h:201
RoleAttributes attr_
Definition: endpoint.h:47
#define ADEBUG
Definition: log.h:41
void data(const std::string &_data)
This function copies the value in member data.
Definition: underlay_message.h:112
#define RETURN_IF_NULL(ptr)
Definition: log.h:90
RtpsTransmitter(const RoleAttributes &attr, const ParticipantPtr &participant)
Definition: rtps_transmitter.h:59
uint64_t seq_num() const
Definition: message_info.h:55
static bool FillInPubAttr(const std::string &channel_name, const QosProfile &qos, eprosima::fastrtps::PublisherAttributes *pub_attr)
const Identity & sender_id() const
Definition: message_info.h:49
void Enable() override
Definition: rtps_transmitter.h:69
Definition: message_info.h:30
void Disable() override
Definition: rtps_transmitter.h:86
bool Transmit(const MessagePtr &msg, const MessageInfo &msg_info) override
Definition: rtps_transmitter.h:94
std::shared_ptr< Participant > ParticipantPtr
Definition: participant.h:37
const char * data() const
Definition: identity.h:44
Definition: transmitter.h:36
virtual ~RtpsTransmitter()
Definition: rtps_transmitter.h:64
const Identity & spare_id() const
Definition: message_info.h:58