Apollo 11.0
自动驾驶开放平台
sub_listener.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
18
19#include "cyber/common/log.h"
20#include "cyber/common/util.h"
21#include "cyber/time/time.h"
22
23namespace apollo {
24namespace cyber {
25namespace transport {
26
28 : callback_(callback) {}
29
31
32void SubListener::onNewDataMessage(eprosima::fastrtps::Subscriber* sub) {
33 RETURN_IF_NULL(sub);
34 RETURN_IF_NULL(callback_);
35 std::lock_guard<std::mutex> lock(mutex_);
36
37 // fetch channel name
38 auto channel_id = common::Hash(sub->getAttributes().topic.getTopicName());
39 eprosima::fastrtps::SampleInfo_t m_info;
41
42 RETURN_IF(!sub->takeNextData(reinterpret_cast<void*>(&m), &m_info));
43 RETURN_IF(m_info.sampleKind != eprosima::fastrtps::ALIVE);
44
45 // fetch MessageInfo
46 char* ptr =
47 reinterpret_cast<char*>(&m_info.related_sample_identity.writer_guid());
48 Identity sender_id(false);
49 sender_id.set_data(ptr);
50 msg_info_.set_sender_id(sender_id);
51
52 Identity spare_id(false);
53 spare_id.set_data(ptr + ID_SIZE);
54 msg_info_.set_spare_id(spare_id);
55
56 uint64_t seq_num =
57 ((int64_t)m_info.related_sample_identity.sequence_number().high) << 32 |
58 m_info.related_sample_identity.sequence_number().low;
59 msg_info_.set_seq_num(seq_num);
60
61 // fetch message string
62 std::shared_ptr<std::string> msg_str =
63 std::make_shared<std::string>(m.data());
64
65 uint64_t recv_time = Time::Now().ToNanosecond();
66 uint64_t base_time = recv_time & 0xfffffff0000000;
67 int32_t send_time_low = m.timestamp();
68 uint64_t send_time = base_time | send_time_low;
69 int32_t msg_seq_num = m.seq();
70
71 msg_info_.set_msg_seq_num(msg_seq_num);
72 msg_info_.set_send_time(send_time);
73
74 // callback
75 callback_(channel_id, msg_str, msg_info_);
76}
77
79 eprosima::fastrtps::Subscriber* sub,
80 eprosima::fastrtps::MatchingInfo& info) {
81 (void)sub;
82 (void)info;
83}
84
85} // namespace transport
86} // namespace cyber
87} // namespace apollo
uint64_t ToNanosecond() const
convert time to nanosecond.
Definition time.cc:83
static Time Now()
get the current time.
Definition time.cc:57
void set_data(const char *data)
Definition identity.h:45
void set_msg_seq_num(int32_t msg_seq_num)
void set_seq_num(uint64_t seq_num)
void set_spare_id(const Identity &spare_id)
void set_send_time(uint64_t send_time)
void set_sender_id(const Identity &sender_id)
std::function< void(uint64_t channel_id, const std::shared_ptr< std::string > &msg_str, const MessageInfo &msg_info)> NewMsgCallback
SubListener(const NewMsgCallback &callback)
void onSubscriptionMatched(eprosima::fastrtps::Subscriber *sub, eprosima::fastrtps::MatchingInfo &info)
void onNewDataMessage(eprosima::fastrtps::Subscriber *sub)
This class represents the structure UnderlayMessage defined by the user in the IDL file.
void timestamp(int32_t _timestamp)
This function sets a value in member timestamp
void seq(int32_t _seq)
This function sets a value in member seq
void data(const std::string &_data)
This function copies the value in member data
#define RETURN_IF_NULL(ptr)
Definition log.h:90
#define RETURN_IF(condition)
Definition log.h:106
std::size_t Hash(const std::string &key)
Definition util.h:27
constexpr uint8_t ID_SIZE
Definition identity.h:28
class register implement
Definition arena_queue.h:37