Apollo 10.0
自动驾驶开放平台
cyber_topology_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 <iostream>
21
22#include "cyber/proto/role_attributes.pb.h"
23#include "cyber/proto/topology_change.pb.h"
24
28
29constexpr int SecondColumnOffset = 4;
30
32 : RenderableMessage(nullptr, 1),
33 second_column_(SecondColumnType::MessageFrameRatio),
34 pid_(getpid()),
35 col1_width_(8),
36 specified_channel_(channel),
37 all_channels_map_() {}
38
40 for (auto item : all_channels_map_) {
41 if (!GeneralChannelMessage::IsErrorCode(item.second)) {
42 delete item.second;
43 }
44 }
45}
46
47bool CyberTopologyMessage::IsFromHere(const std::string& node_name) {
48 std::ostringstream out_str;
49 out_str << "MonitorReader" << pid_;
50
51 std::string templateName = out_str.str();
52 const std::string baseName = node_name.substr(0, templateName.size());
53
54 return (templateName.compare(baseName) == 0);
55}
56
58 RenderableMessage* ret = nullptr;
59 auto iter = FindChild(line_no);
60 if (iter != all_channels_map_.cend() &&
62 iter->second->is_enabled()) {
63 ret = iter->second;
64 }
65 return ret;
66}
67
68std::map<std::string, GeneralChannelMessage*>::const_iterator
69CyberTopologyMessage::FindChild(int line_no) const {
70 --line_no;
71
72 std::map<std::string, GeneralChannelMessage*>::const_iterator ret =
73 all_channels_map_.cend();
74
75 if (line_no > -1 && line_no < page_item_count_) {
76 int i = 0;
77
78 auto iter = all_channels_map_.cbegin();
79 while (i < page_index_ * page_item_count_) {
80 ++iter;
81 ++i;
82 }
83
84 for (i = 0; iter != all_channels_map_.cend(); ++iter) {
85 if (i == line_no) {
86 ret = iter;
87 break;
88 }
89 ++i;
90 }
91 }
92 return ret;
93}
94
96 const apollo::cyber::proto::ChangeMsg& changeMsg) {
98 changeMsg.operate_type()) {
99 bool isWriter = true;
101 isWriter = false;
102 AddReaderWriter(changeMsg.role_attr(), isWriter);
103 } else {
104 auto iter = all_channels_map_.find(changeMsg.role_attr().channel_name());
105
106 if (iter != all_channels_map_.cend() &&
107 !GeneralChannelMessage::IsErrorCode(iter->second)) {
108 const std::string& node_name = changeMsg.role_attr().node_name();
110 changeMsg.role_type()) {
111 iter->second->del_writer(node_name);
112 } else {
113 iter->second->del_reader(node_name);
114 }
115 }
116 }
117}
118
120 const apollo::cyber::proto::RoleAttributes& role, bool isWriter) {
121 const std::string& channel_name = role.channel_name();
122
123 if (!specified_channel_.empty() && specified_channel_ != channel_name) {
124 return;
125 }
126
127 if (static_cast<int>(channel_name.length()) > col1_width_) {
128 col1_width_ = static_cast<int>(channel_name.length());
129 }
130
131 const std::string& node_name = role.node_name();
132 if (IsFromHere(node_name)) {
133 return;
134 }
135
136 GeneralChannelMessage* channel_msg = nullptr;
137 const std::string& msgTypeName = role.message_type();
138 auto iter = all_channels_map_.find(channel_name);
139 if (iter == all_channels_map_.cend()) {
140 static int index = 0;
141
142 std::ostringstream out_str;
143 out_str << "MonitorReader" << pid_ << '-' << index++;
144
145 channel_msg = new GeneralChannelMessage(out_str.str(), this);
146
147 if (channel_msg != nullptr) {
149 channel_msg->OpenChannel(channel_name))) {
150 channel_msg->set_message_type(msgTypeName);
151 channel_msg->add_reader(channel_msg->NodeName());
152 }
153 } else {
156 }
157 all_channels_map_[channel_name] = channel_msg;
158 } else {
159 channel_msg = iter->second;
160 }
161
162 if (!GeneralChannelMessage::IsErrorCode(channel_msg)) {
163 if (isWriter) {
164 if (msgTypeName != apollo::cyber::message::MessageType<
166 channel_msg->set_message_type(msgTypeName);
167 }
168
169 channel_msg->add_writer(node_name);
170 } else {
171 channel_msg->add_reader(node_name);
172 }
173 }
174}
175
176void CyberTopologyMessage::ChangeState(const Screen* s, int key) {
177 switch (key) {
178 case 'f':
179 case 'F':
180 second_column_ = SecondColumnType::MessageFrameRatio;
181 break;
182
183 case 't':
184 case 'T':
185 second_column_ = SecondColumnType::MessageType;
186 break;
187
188 case ' ': {
189 auto iter = FindChild(*line_no());
190 if (!GeneralChannelMessage::IsErrorCode(iter->second)) {
191 GeneralChannelMessage* child = iter->second;
192 if (child->is_enabled()) {
193 child->CloseChannel();
194 } else {
195 GeneralChannelMessage* ret = child->OpenChannel(iter->first);
197 delete child;
198 all_channels_map_[iter->first] = ret;
199 } else {
200 child->add_reader(child->NodeName());
201 }
202 }
203 }
204 }
205
206 default: {
207 }
208 }
209}
210
211int CyberTopologyMessage::Render(const Screen* s, int key) {
212 page_item_count_ = s->Height() - 1;
213 pages_ = static_cast<int>(all_channels_map_.size()) / page_item_count_ + 1;
214 ChangeState(s, key);
215 SplitPages(key);
216
217 s->AddStr(0, 0, Screen::WHITE_BLACK, "Channels");
218 switch (second_column_) {
219 case SecondColumnType::MessageType:
220 s->AddStr(col1_width_ + SecondColumnOffset, 0, Screen::WHITE_BLACK,
221 "TypeName");
222 break;
223 case SecondColumnType::MessageFrameRatio:
224 s->AddStr(col1_width_ + SecondColumnOffset, 0, Screen::WHITE_BLACK,
225 "FrameRatio");
226 break;
227 }
228
229 auto iter = all_channels_map_.cbegin();
230 register int tmp = page_index_ * page_item_count_;
231 register int line = 0;
232 while (line < tmp) {
233 ++iter;
234 ++line;
235 }
236
237 Screen::ColorPair color;
238 std::ostringstream out_str;
239
240 tmp = page_item_count_ + 1;
241 for (line = 1; iter != all_channels_map_.cend() && line < tmp;
242 ++iter, ++line) {
243 color = Screen::RED_BLACK;
244
245 if (!GeneralChannelMessage::IsErrorCode(iter->second)) {
246 if (iter->second->has_message_come()) {
247 if (iter->second->is_enabled()) {
248 color = Screen::GREEN_BLACK;
249 } else {
250 color = Screen::YELLOW_BLACK;
251 }
252 }
253 }
254
255 s->SetCurrentColor(color);
256 s->AddStr(0, line, iter->first.c_str());
257
258 if (!GeneralChannelMessage::IsErrorCode(iter->second)) {
259 switch (second_column_) {
260 case SecondColumnType::MessageType:
261 s->AddStr(col1_width_ + SecondColumnOffset, line,
262 iter->second->message_type().c_str());
263 break;
264 case SecondColumnType::MessageFrameRatio: {
265 out_str.str("");
266 out_str << std::fixed << std::setprecision(FrameRatio_Precision)
267 << iter->second->frame_ratio();
268 s->AddStr(col1_width_ + SecondColumnOffset, line,
269 out_str.str().c_str());
270 } break;
271 }
272 } else {
275 s->AddStr(col1_width_ + SecondColumnOffset, line,
277 }
279 }
280
281 return line;
282}
void AddReaderWriter(const apollo::cyber::proto::RoleAttributes &role, bool isWriter)
int Render(const Screen *s, int key) override
CyberTopologyMessage(const std::string &channel)
RenderableMessage * Child(int index) const override
void TopologyChanged(const apollo::cyber::proto::ChangeMsg &change_msg)
static bool IsErrorCode(void *ptr)
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)
static GeneralChannelMessage * CastErrorCode2Ptr(ErrorCode errCode)
void add_reader(const std::string &reader)
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
void SetCurrentColor(ColorPair color) const
Definition screen.cc:124
void ClearCurrentColor(void) const
Definition screen.cc:145
ColorPair
Definition screen.h:34
@ WHITE_BLACK
Definition screen.h:39
@ GREEN_BLACK
Definition screen.h:36
@ YELLOW_BLACK
Definition screen.h:37
@ RED_BLACK
Definition screen.h:38
constexpr int SecondColumnOffset
std::string MessageType()
optional RoleAttributes role_attr