Apollo 10.0
自动驾驶开放平台
general_channel_message.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 <iomanip>
20#include <sstream>
21#include <string>
22#include <vector>
23
28
29namespace {
30constexpr int ReaderWriterOffset = 4;
31using apollo::cyber::record::kGB;
32using apollo::cyber::record::kKB;
33using apollo::cyber::record::kMB;
34} // namespace
35
38 const char* ret;
39 switch (errCode) {
41 ret = "Cannot Create Parser Object";
42 break;
43
45 ret = "Cannot Create Cyber Node";
46 break;
47
49 ret = "Cannot Create Cyber Reader";
50 break;
51
53 ret = "Message Type is Empty";
54 break;
55
57 ret = "Channel Name or Node Name is Empty";
58 break;
59
61 ret = "No Close Channel";
62 break;
63
64 default:
65 ret = "Unknown Error Code";
66 }
67 return ret;
68}
69
72 (GeneralChannelMessage::ErrorCode)(reinterpret_cast<intptr_t>(ptr));
73 switch (err) {
80 return true;
81
82 default: {
83 }
84 }
85 return false;
86}
87
89 if (!is_enabled() || !has_message_come()) {
90 return 0.0;
91 }
92 auto time_now = apollo::cyber::Time::MonoTime();
93 auto interval = time_now - time_last_calc_;
94 if (interval.ToNanosecond() > 1000000000) {
95 int old = frame_counter_;
96 while (!frame_counter_.compare_exchange_strong(old, 0)) {
97 }
98 if (old == 0) {
99 return 0.0;
100 }
101 auto curMsgTime = msg_time_;
102 auto deltaTime = curMsgTime - last_time_;
103 frame_ratio_ = old / deltaTime.ToSecond();
104 last_time_ = curMsgTime;
105 time_last_calc_ = time_now;
106 }
107 return frame_ratio_;
108}
109
110GeneralChannelMessage* GeneralChannelMessage::OpenChannel(
111 const std::string& channel_name) {
112 if (channel_name.empty() || node_name_.empty()) {
114 }
115 if (channel_node_ != nullptr || channel_reader_ != nullptr) {
117 }
118
119 channel_node_ = apollo::cyber::CreateNode(node_name_);
120 if (channel_node_ == nullptr) {
122 }
123
124 auto callback =
125 [this, channel_name](
126 const std::shared_ptr<apollo::cyber::message::RawMessage>& raw_msg) {
127 UpdateRawMessage(raw_msg);
128 UpdateChannelName(channel_name);
129 };
130
131 channel_reader_ =
132 channel_node_->CreateReader<apollo::cyber::message::RawMessage>(
133 channel_name, callback);
134 if (channel_reader_ == nullptr) {
135 channel_node_.reset();
137 }
138 return this;
139}
140
141int GeneralChannelMessage::Render(const Screen* s, int key) {
142 switch (key) {
143 case 'b':
144 case 'B':
145 current_state_ = State::ShowDebugString;
146 break;
147
148 case 'i':
149 case 'I':
150 current_state_ = State::ShowInfo;
151 break;
152
153 default: {
154 }
155 }
156
157 clear();
158
159 int line_no = 0;
160
162 s->AddStr(0, line_no++, "ChannelName: ");
163 s->AddStr(channel_reader_->GetChannelName().c_str());
164
165 s->AddStr(0, line_no++, "MessageType: ");
166 s->AddStr(message_type().c_str());
167
168 if (is_enabled()) {
169 switch (current_state_) {
170 case State::ShowDebugString:
171 RenderDebugString(s, key, &line_no);
172 break;
173 case State::ShowInfo:
174 RenderInfo(s, key, &line_no);
175 break;
176 }
177 } else {
178 s->AddStr(0, line_no++, "Channel has been closed");
179 }
181
182 return line_no;
183}
184
185void GeneralChannelMessage::RenderInfo(const Screen* s, int key, int* line_no) {
187 pages_ = static_cast<int>(readers_.size() + writers_.size() + *line_no) /
189 1;
190 SplitPages(key);
191
192 bool has_reader = true;
193 std::vector<std::string>* vec = &readers_;
194
195 auto iter = vec->cbegin();
196 unsigned int y = page_index_ * page_item_count_;
197 if (y < vec->size()) {
198 for (unsigned i = 0; i < y; ++i) {
199 ++iter;
200 }
201 } else {
202 y -= static_cast<unsigned int>(vec->size());
203 vec = &writers_;
204 iter = vec->cbegin();
205 while (y) {
206 ++iter;
207 --y;
208 }
209
210 has_reader = false;
211 }
212
213 if (has_reader) {
214 s->AddStr(0, (*line_no)++, "Readers:");
215 for (; iter != vec->cend(); ++iter) {
216 s->AddStr(ReaderWriterOffset, (*line_no)++, iter->c_str());
217 }
218
219 ++(*line_no);
220 vec = &writers_;
221 iter = vec->cbegin();
222 }
223
224 s->AddStr(0, (*line_no)++, "Writers:");
225 for (; iter != vec->cend(); ++iter) {
226 s->AddStr(ReaderWriterOffset, (*line_no)++, iter->c_str());
227 }
228}
229
230void GeneralChannelMessage::RenderDebugString(const Screen* s, int key,
231 int* line_no) {
232 if (has_message_come()) {
233 if (raw_msg_class_ == nullptr) {
234 auto rawFactory = apollo::cyber::message::ProtobufFactory::Instance();
235 raw_msg_class_ = rawFactory->GenerateMessageByType(message_type());
236 }
237
238 if (raw_msg_class_ == nullptr) {
239 s->AddStr(0, (*line_no)++, "Cannot Generate Message by Message Type");
240 } else {
241 s->AddStr(0, (*line_no)++, "FrameRatio: ");
242
243 std::ostringstream out_str;
244 out_str << std::fixed << std::setprecision(FrameRatio_Precision)
245 << frame_ratio();
246 s->AddStr(out_str.str().c_str());
247
248 decltype(channel_message_) channel_msg = CopyMsgPtr();
249
250 if (channel_msg->message.size()) {
251 s->AddStr(0, (*line_no)++, "RawMessage Size: ");
252 out_str.str("");
253 out_str << channel_msg->message.size() << " Bytes";
254 if (channel_msg->message.size() >= kGB) {
255 out_str << " ("
256 << static_cast<float>(channel_msg->message.size()) / kGB
257 << " GB)";
258 } else if (channel_msg->message.size() >= kMB) {
259 out_str << " ("
260 << static_cast<float>(channel_msg->message.size()) / kMB
261 << " MB)";
262 } else if (channel_msg->message.size() >= kKB) {
263 out_str << " ("
264 << static_cast<float>(channel_msg->message.size()) / kKB
265 << " KB)";
266 }
267 s->AddStr(out_str.str().c_str());
268 if (raw_msg_class_->ParseFromString(channel_msg->message)) {
269 int lcount = LineCount(*raw_msg_class_, s->Width());
271 pages_ = lcount / page_item_count_ + 1;
272 SplitPages(key);
273 int jump_lines = page_index_ * page_item_count_;
274 jump_lines <<= 2;
275 jump_lines /= 5;
276 GeneralMessageBase::PrintMessage(this, *raw_msg_class_, &jump_lines,
277 s, line_no, 0);
278 } else {
279 s->AddStr(0, (*line_no)++, "Cannot parse the raw message");
280 }
281 } else {
282 s->AddStr(0, (*line_no)++, "The size of this raw Message is Zero");
283 }
284 }
285 } else {
286 s->AddStr(0, (*line_no)++, "No Message Came");
287 }
288}
int Render(const Screen *s, int key) override
static bool IsErrorCode(void *ptr)
const std::string & message_type(void) const
static const char * ErrCode2Str(ErrorCode errCode)
double frame_ratio(void) override
static GeneralChannelMessage * CastErrorCode2Ptr(ErrorCode errCode)
static int LineCount(const google::protobuf::Message &msg, int screen_width)
static void PrintMessage(GeneralMessageBase *baseMsg, const google::protobuf::Message &msg, int *jump_lines, const Screen *s, int *line_no, int indent)
static constexpr int FrameRatio_Precision
void AddStr(int x, int y, ColorPair color, const char *str) const
Definition screen.cc:152
int Height(void) const
Definition screen.cc:122
int Width(void) const
Definition screen.cc:120
void SetCurrentColor(ColorPair color) const
Definition screen.cc:124
void ClearCurrentColor(void) const
Definition screen.cc:145
@ WHITE_BLACK
Definition screen.h:39
static Time MonoTime()
Definition time.cc:67
double ToSecond() const
convert time to second.
Definition time.cc:77
std::unique_ptr< Node > CreateNode(const std::string &node_name, const std::string &name_space)
Definition cyber.cc:33