Apollo  6.0
Open source self driving car software
general_channel_message.h
Go to the documentation of this file.
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 
30 class GeneralMessage;
31 
33  public:
34  enum class ErrorCode {
35  NewSubClassFailed = -1,
36  CreateNodeFailed = -2,
37  CreateReaderFailed = -3,
38  MessageTypeIsEmpty = -4,
40  NoCloseChannel = -6
41  };
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 
155  std::shared_ptr<apollo::cyber::message::RawMessage> CopyMsgPtr(void) const {
156  decltype(channel_message_) channel_msg;
157  {
158  std::lock_guard<std::mutex> g(inner_lock_);
159  channel_msg = channel_message_;
160  }
161  return channel_msg;
162  }
163 
164  GeneralChannelMessage* OpenChannel(const std::string& channel_name);
165 
166  void RenderDebugString(const Screen* s, int key, int* line_no);
167  void RenderInfo(const Screen* s, int key, int* line_no);
168 
169  void set_has_message_come(bool b) { has_message_come_ = b; }
170 
171  enum class State { ShowDebugString, ShowInfo } current_state_;
172 
173  bool has_message_come_;
174  std::string message_type_;
175  std::atomic<int> frame_counter_;
176  apollo::cyber::Time last_time_;
177  apollo::cyber::Time msg_time_;
179 
180  std::unique_ptr<apollo::cyber::Node> channel_node_;
181 
182  std::string node_name_;
183 
184  std::vector<std::string> readers_;
185  std::vector<std::string> writers_;
186 
187  std::shared_ptr<apollo::cyber::message::RawMessage> channel_message_;
188  std::shared_ptr<apollo::cyber::Reader<apollo::cyber::message::RawMessage>>
189  channel_reader_;
190  mutable std::mutex inner_lock_;
191 
192  google::protobuf::Message* raw_msg_class_;
193 
194  friend class CyberTopologyMessage;
195  friend class GeneralMessage;
196 }; // GeneralChannelMessage
197 
198 #endif // TOOLS_CVT_MONITOR_GENERAL_CHANNEL_MESSAGE_H_
Definition: cyber_topology_message.h:37
GeneralMessageBase(RenderableMessage *parent=nullptr)
Definition: general_message_base.h:54
bool has_message_come(void) const
Definition: general_channel_message.h:75
ErrorCode
Definition: general_channel_message.h:34
Definition: screen.h:30
Definition: renderable_message.h:24
static const char * ErrCode2Str(ErrorCode errCode)
bool is_enabled(void) const
Definition: general_channel_message.h:74
const std::string & message_type(void) const
Definition: general_channel_message.h:72
Definition: general_channel_message.h:32
void del_reader(const std::string &reader)
Definition: general_channel_message.h:82
int Render(const Screen *s, int key) override
static Time MonoTime()
static GeneralChannelMessage * CastErrorCode2Ptr(ErrorCode errCode)
Definition: general_channel_message.h:50
const std::string & NodeName(void) const
Definition: general_channel_message.h:79
void add_reader(const std::string &reader)
Definition: general_channel_message.h:81
Definition: general_message_base.h:28
~GeneralChannelMessage()
Definition: general_channel_message.h:55
std::string GetChannelName(void) const
Definition: general_channel_message.h:65
int * line_no(void)
Definition: renderable_message.h:55
Definition: general_message.h:26
RenderableMessage * parent(void) const
Definition: renderable_message.h:44
void CloseChannel(void)
Definition: general_channel_message.h:94
static ErrorCode CastPtr2ErrorCode(void *ptr)
Definition: general_channel_message.h:46
static bool IsErrorCode(void *ptr)
void set_message_type(const std::string &msgTypeName)
Definition: general_channel_message.h:69
void del_writer(const std::string &writer)
Definition: general_channel_message.h:85
double frame_ratio(void) override
Cyber has builtin time type Time.
Definition: time.h:31
void add_writer(const std::string &writer)
Definition: general_channel_message.h:84