Apollo 9.0
自动驾驶开放平台
rtps_dispatcher.cc
浏览该文件的文档.
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
20
21namespace apollo {
22namespace cyber {
23namespace transport {
24
25RtpsDispatcher::RtpsDispatcher() : participant_(nullptr) {}
26
27RtpsDispatcher::~RtpsDispatcher() { Shutdown(); }
28
29void RtpsDispatcher::Shutdown() {
30 if (is_shutdown_.exchange(true)) {
31 return;
32 }
33
34 {
35 std::lock_guard<std::mutex> lock(subs_mutex_);
36 for (auto& item : subs_) {
37 item.second.sub = nullptr;
38 }
39 }
40
41 participant_ = nullptr;
42}
43
44void RtpsDispatcher::AddSubscriber(const RoleAttributes& self_attr) {
45 if (participant_ == nullptr) {
46 AWARN << "please set participant firstly.";
47 return;
48 }
49
50 uint64_t channel_id = self_attr.channel_id();
51 std::lock_guard<std::mutex> lock(subs_mutex_);
52 if (subs_.count(channel_id) > 0) {
53 return;
54 }
55
56 Subscriber new_sub;
57 eprosima::fastrtps::SubscriberAttributes sub_attr;
58 auto& qos = self_attr.qos_profile();
59 RETURN_IF(!AttributesFiller::FillInSubAttr(self_attr.channel_name(), qos,
60 &sub_attr));
61
62 auto listener_adapter = [this, self_attr](uint64_t channel_id,
63 const std::shared_ptr<std::string>& msg_str,
64 const MessageInfo& msg_info) {
65 statistics::Statistics::Instance()->AddRecvCount(
66 self_attr, msg_info.msg_seq_num());
67 statistics::Statistics::Instance()->SetTotalMsgsStatus(
68 self_attr, msg_info.msg_seq_num());
69 this->OnMessage(channel_id, msg_str, msg_info);
70 };
71
72 new_sub.sub_listener = std::make_shared<SubListener>(listener_adapter);
73
74 new_sub.sub = eprosima::fastrtps::Domain::createSubscriber(
75 participant_->fastrtps_participant(), sub_attr,
76 new_sub.sub_listener.get());
77 RETURN_IF_NULL(new_sub.sub);
78 subs_[channel_id] = new_sub;
79}
80
81void RtpsDispatcher::OnMessage(uint64_t channel_id,
82 const std::shared_ptr<std::string>& msg_str,
83 const MessageInfo& msg_info) {
84 if (is_shutdown_.load()) {
85 return;
86 }
87
88 ListenerHandlerBasePtr* handler_base = nullptr;
89 if (msg_listeners_.Get(channel_id, &handler_base)) {
90 auto handler =
91 std::dynamic_pointer_cast<ListenerHandler<std::string>>(*handler_base);
92 handler->Run(msg_str, msg_info);
93 }
94}
95
96} // namespace transport
97} // namespace cyber
98} // namespace apollo
#define RETURN_IF_NULL(ptr)
Definition log.h:90
#define RETURN_IF(condition)
Definition log.h:106
#define AWARN
Definition log.h:43
std::shared_ptr< ListenerHandlerBase > ListenerHandlerBasePtr
class register implement