Apollo 11.0
自动驾驶开放平台
rtps_dispatcher.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_DISPATCHER_RTPS_DISPATCHER_H_
18#define CYBER_TRANSPORT_DISPATCHER_RTPS_DISPATCHER_H_
19
20#include <iostream>
21#include <memory>
22#include <mutex>
23#include <string>
24#include <unordered_map>
25
26#include "cyber/common/log.h"
27#include "cyber/common/macros.h"
29#include "cyber/time/time.h"
35
36namespace apollo {
37namespace cyber {
38namespace transport {
39
40struct Subscriber {
41 Subscriber() : sub(nullptr), sub_listener(nullptr) {}
42
43 eprosima::fastrtps::Subscriber* sub;
45};
46
47class RtpsDispatcher;
49
50class RtpsDispatcher : public Dispatcher {
51 public:
52 virtual ~RtpsDispatcher();
53
54 void Shutdown() override;
55
56 template <typename MessageT>
57 void AddListener(const RoleAttributes& self_attr,
58 const MessageListener<MessageT>& listener);
59
60 template <typename MessageT>
61 void AddListener(const RoleAttributes& self_attr,
62 const RoleAttributes& opposite_attr,
63 const MessageListener<MessageT>& listener);
64
65 void set_participant(const ParticipantPtr& participant) {
66 participant_ = participant;
67 }
68
69 private:
70 void OnMessage(uint64_t channel_id,
71 const std::shared_ptr<std::string>& msg_str,
72 const MessageInfo& msg_info);
73 void AddSubscriber(const RoleAttributes& self_attr);
74 // key: channel_id
75 std::unordered_map<uint64_t, Subscriber> subs_;
76 std::mutex subs_mutex_;
77
78 ParticipantPtr participant_;
79
81};
82
83template <typename MessageT>
85 const MessageListener<MessageT>& listener) {
86 auto listener_adapter = [listener, self_attr](
87 const std::shared_ptr<std::string>& msg_str,
88 const MessageInfo& msg_info) {
89 auto msg = std::make_shared<MessageT>();
90 RETURN_IF(!message::ParseFromString(*msg_str, msg.get()));
91 uint64_t recv_time = Time::Now().ToMicrosecond();
92 uint64_t send_time = msg_info.send_time();
93 if (send_time > recv_time) {
94 AWARN << "The message is received earlier than the message is sent";
95 } else {
96 uint64_t diff = recv_time - send_time;
97 // sample transport latency in microsecond
98 statistics::Statistics::Instance()->SamplingTranLatency<
99 uint64_t>(self_attr, diff);
100 }
101 statistics::Statistics::Instance()->SetProcStatus(self_attr, recv_time);
102 listener(msg, msg_info);
103 };
104
105 Dispatcher::AddListener<std::string>(self_attr, listener_adapter);
106 AddSubscriber(self_attr);
107}
108
109template <typename MessageT>
111 const RoleAttributes& opposite_attr,
112 const MessageListener<MessageT>& listener) {
113 auto listener_adapter = [listener, self_attr](
114 const std::shared_ptr<std::string>& msg_str,
115 const MessageInfo& msg_info) {
116 auto msg = std::make_shared<MessageT>();
117 RETURN_IF(!message::ParseFromString(*msg_str, msg.get()));
118 uint64_t recv_time = Time::Now().ToMicrosecond();
119 uint64_t send_time = msg_info.send_time();
120 if (send_time > recv_time) {
121 AWARN << "The message is received earlier than the message is sent";
122 } else {
123 uint64_t diff = recv_time - send_time;
124 // sample transport latency in microsecond
125 statistics::Statistics::Instance()->SamplingTranLatency<
126 uint64_t>(self_attr, diff);
127 }
128 statistics::Statistics::Instance()->SetProcStatus(self_attr, recv_time);
129 listener(msg, msg_info);
130 };
131
132 Dispatcher::AddListener<std::string>(self_attr, opposite_attr,
133 listener_adapter);
134 AddSubscriber(self_attr);
135}
136
137} // namespace transport
138} // namespace cyber
139} // namespace apollo
140
141#endif // CYBER_TRANSPORT_DISPATCHER_RTPS_DISPATCHER_H_
uint64_t ToMicrosecond() const
convert time to microsecond (us).
Definition time.cc:85
static Time Now()
get the current time.
Definition time.cc:57
void set_participant(const ParticipantPtr &participant)
void AddListener(const RoleAttributes &self_attr, const MessageListener< MessageT > &listener)
#define DECLARE_SINGLETON(classname)
Definition macros.h:52
#define RETURN_IF(condition)
Definition log.h:106
#define AWARN
Definition log.h:43
std::enable_if< HasParseFromString< T >::value, bool >::type ParseFromString(const std::string &str, T *message)
std::shared_ptr< Participant > ParticipantPtr
Definition participant.h:37
std::function< void(const std::shared_ptr< MessageT > &, const MessageInfo &)> MessageListener
Definition dispatcher.h:53
std::shared_ptr< SubListener > SubListenerPtr
class register implement
Definition arena_queue.h:37
eprosima::fastrtps::Subscriber * sub