Apollo 10.0
自动驾驶开放平台
general_channel_message.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 TOOLS_CVT_MONITOR_GENERAL_CHANNEL_MESSAGE_H_
18#define TOOLS_CVT_MONITOR_GENERAL_CHANNEL_MESSAGE_H_
19
20#include <atomic>
21#include <memory>
22#include <string>
23#include <vector>
24
25#include "cyber/cyber.h"
28
30class GeneralMessage;
31
33 public:
42
43 static const char* ErrCode2Str(ErrorCode errCode);
44 static bool IsErrorCode(void* ptr);
45
46 static ErrorCode CastPtr2ErrorCode(void* ptr) {
47 assert(IsErrorCode(ptr));
48 return static_cast<ErrorCode>(reinterpret_cast<intptr_t>(ptr));
49 }
51 return reinterpret_cast<GeneralChannelMessage*>(
52 static_cast<intptr_t>(errCode));
53 }
54
56 channel_node_.reset();
57 channel_reader_.reset();
58 channel_message_.reset();
59 if (raw_msg_class_) {
60 delete raw_msg_class_;
61 raw_msg_class_ = nullptr;
62 }
63 }
64
65 std::string GetChannelName(void) const {
66 return channel_reader_->GetChannelName();
67 }
68
69 void set_message_type(const std::string& msgTypeName) {
70 message_type_ = msgTypeName;
71 }
72 const std::string& message_type(void) const { return message_type_; }
73
74 bool is_enabled(void) const { return channel_reader_ != nullptr; }
75 bool has_message_come(void) const { return has_message_come_; }
76
77 double frame_ratio(void) override;
78
79 const std::string& NodeName(void) const { return node_name_; }
80
81 void add_reader(const std::string& reader) { DoAdd(&readers_, reader); }
82 void del_reader(const std::string& reader) { DoDelete(&readers_, reader); }
83
84 void add_writer(const std::string& writer) { DoAdd(&writers_, writer); }
85 void del_writer(const std::string& writer) {
86 DoDelete(&writers_, writer);
87 if (!writers_.size()) {
88 set_has_message_come(false);
89 }
90 }
91
92 int Render(const Screen* s, int key) override;
93
94 void CloseChannel(void) {
95 if (channel_reader_ != nullptr) {
96 channel_reader_.reset();
97 }
98
99 if (channel_node_ != nullptr) {
100 channel_node_.reset();
101 }
102 }
103
104 private:
105 explicit GeneralChannelMessage(const std::string& node_name,
106 RenderableMessage* parent = nullptr)
108 current_state_(State::ShowDebugString),
109 has_message_come_(false),
110 message_type_(),
111 frame_counter_(0),
112 last_time_(apollo::cyber::Time::MonoTime()),
113 msg_time_(last_time_.ToNanosecond() + 1),
114 channel_node_(nullptr),
115 node_name_(node_name),
116 readers_(),
117 writers_(),
118 channel_message_(nullptr),
119 channel_reader_(nullptr),
120 inner_lock_(),
121 raw_msg_class_(nullptr) {}
122
124 GeneralChannelMessage& operator=(const GeneralChannelMessage&) = delete;
125
126 static void DoDelete(std::vector<std::string>* vec, const std::string& str) {
127 for (auto iter = vec->begin(); iter != vec->end(); ++iter) {
128 if (*iter == str) {
129 vec->erase(iter);
130 break;
131 }
132 }
133 }
134
135 static void DoAdd(std::vector<std::string>* vec, const std::string& str) {
136 for (const auto& item : *vec) {
137 if (item == str) {
138 return;
139 }
140 }
141
142 vec->emplace_back(str);
143 }
144
145 void UpdateRawMessage(
146 const std::shared_ptr<apollo::cyber::message::RawMessage>& raw_msg) {
147 set_has_message_come(true);
148 msg_time_ = apollo::cyber::Time::MonoTime();
149 ++frame_counter_;
150 std::lock_guard<std::mutex> _g(inner_lock_);
151 channel_message_.reset();
152 channel_message_ = raw_msg;
153 }
154 void UpdateChannelName(const std::string& channel_name) {
155 std::lock_guard<std::mutex> _g(inner_lock_);
156 channel_name_ = channel_name;
157 }
158
159 std::shared_ptr<apollo::cyber::message::RawMessage> CopyMsgPtr(void) const {
160 decltype(channel_message_) channel_msg;
161 {
162 std::lock_guard<std::mutex> g(inner_lock_);
163 channel_msg = channel_message_;
164 }
165 return channel_msg;
166 }
167
168 GeneralChannelMessage* OpenChannel(const std::string& channel_name);
169
170 void RenderDebugString(const Screen* s, int key, int* line_no);
171 void RenderInfo(const Screen* s, int key, int* line_no);
172
173 void set_has_message_come(bool b) { has_message_come_ = b; }
174
175 enum class State { ShowDebugString, ShowInfo } current_state_;
176
177 bool has_message_come_;
178 std::string message_type_;
179 std::atomic<int> frame_counter_;
180 apollo::cyber::Time last_time_;
181 apollo::cyber::Time msg_time_;
183
184 std::unique_ptr<apollo::cyber::Node> channel_node_;
185
186 std::string node_name_;
187
188 std::vector<std::string> readers_;
189 std::vector<std::string> writers_;
190
191 std::shared_ptr<apollo::cyber::message::RawMessage> channel_message_;
192 std::string channel_name_;
193 std::shared_ptr<apollo::cyber::Reader<apollo::cyber::message::RawMessage>>
194 channel_reader_;
195 mutable std::mutex inner_lock_;
196
197 google::protobuf::Message* raw_msg_class_;
198
200 friend class GeneralMessage;
201}; // GeneralChannelMessage
202
203#endif // TOOLS_CVT_MONITOR_GENERAL_CHANNEL_MESSAGE_H_
int Render(const Screen *s, int key) override
static bool IsErrorCode(void *ptr)
void del_reader(const std::string &reader)
const std::string & message_type(void) const
const std::string & NodeName(void) const
void add_writer(const std::string &writer)
void set_message_type(const std::string &msgTypeName)
static ErrorCode CastPtr2ErrorCode(void *ptr)
static const char * ErrCode2Str(ErrorCode errCode)
double frame_ratio(void) override
static GeneralChannelMessage * CastErrorCode2Ptr(ErrorCode errCode)
void del_writer(const std::string &writer)
std::string GetChannelName(void) const
void add_reader(const std::string &reader)
RenderableMessage * parent(void) const
Cyber has builtin time type Time.
Definition time.h:31
static Time MonoTime()
Definition time.cc:67
class register implement
Definition arena_queue.h:37