34namespace external_command {
42 const std::shared_ptr<cyber::WriterBase>& writer,
43 const std::shared_ptr<cyber::WriterBase>& history_writer =
nullptr)
45 history_writer_(history_writer),
46 last_message_(nullptr) {}
53 bool Write(
const T& message);
60 bool Write(
const std::shared_ptr<T>& message);
68 void WriteHistoryMessage();
70 std::shared_ptr<cyber::WriterBase> writer_;
71 std::shared_ptr<cyber::WriterBase> history_writer_;
72 std::shared_ptr<google::protobuf::Message> last_message_;
73 std::function<void()> write_history_function_;
79 if (
nullptr == message_writer) {
82 message_writer->
Write(message);
83 if (
nullptr == last_message_) {
84 last_message_ = std::dynamic_pointer_cast<google::protobuf::Message>(
85 std::make_shared<T>());
86 write_history_function_ =
87 std::bind(&WriterHandle::WriteHistoryMessage<T>,
this);
89 last_message_->CopyFrom(message);
101 CHECK_NOTNULL(message);
102 return Write(*message);
106 if (write_history_function_) {
107 write_history_function_();
112void WriterHandle::WriteHistoryMessage() {
113 std::shared_ptr<cyber::Writer<T>> history_writer =
114 std::dynamic_pointer_cast<cyber::Writer<T>>(history_writer_);
115 if (
nullptr == history_writer) {
116 ADEBUG <<
"History Writer type is not correct!";
119 std::shared_ptr<T> message = std::dynamic_pointer_cast<T>(last_message_);
120 if (message ==
nullptr) {
121 ADEBUG <<
"History message type is not correct!";
125 message->mutable_header()->set_timestamp_sec(timestamp);
126 history_writer->Write(message);
137 template <
typename T>
139 const std::string& channel_name,
140 const std::string& history_channel_name =
"");
147 template <
typename T>
150 const std::string& history_channel_name =
"");
153 std::map<std::string, std::shared_ptr<WriterHandle>> writer_map_;
154 std::list<std::shared_ptr<WriterHandle>> history_writers_;
155 std::shared_ptr<cyber::Node> node_;
157 std::unique_ptr<::apollo::cyber::Timer> timer_;
164 const std::string& channel_name,
const std::string& history_channel_name) {
165 auto existing_writer_iter = writer_map_.find(channel_name);
167 if (existing_writer_iter == writer_map_.end()) {
168 auto writer = node_->CreateWriter<T>(channel_name);
169 if (history_channel_name !=
"") {
170 auto history_writer = node_->CreateWriter<T>(history_channel_name);
171 writer_map_[channel_name] =
172 std::make_shared<WriterHandle>(writer, history_writer);
173 history_writers_.emplace_back(writer_map_[channel_name]);
175 writer_map_[channel_name] = std::make_shared<WriterHandle>(writer);
177 return writer_map_[channel_name];
179 return existing_writer_iter->second;
185 const std::string& history_channel_name) {
187 auto existing_writer_iter = writer_map_.find(channel_name);
189 if (existing_writer_iter == writer_map_.end()) {
190 auto writer = node_->CreateWriter<T>(role_attr);
191 if (history_channel_name !=
"") {
192 auto history_writer = node_->CreateWriter<T>(history_channel_name);
193 writer_map_[channel_name] =
194 std::make_shared<WriterHandle>(writer, history_writer);
195 history_writers_.emplace_back(writer_map_[channel_name]);
197 writer_map_[channel_name] = std::make_shared<WriterHandle>(writer);
199 return writer_map_[channel_name];
201 return existing_writer_iter->second;
static double NowInSeconds()
gets the current time in second.
virtual bool Write(const MessageT &msg)
Write a MessageT instance
std::shared_ptr< WriterHandle > RegisterMessage(const std::string &channel_name, const std::string &history_channel_name="")
Register the message writer with the given channel name.
Wrapped Writer with last sent channel.
WriterHandle(const std::shared_ptr< cyber::WriterBase > &writer, const std::shared_ptr< cyber::WriterBase > &history_writer=nullptr)
void WriteLastMessage()
Write the last received message as history.
bool Write(const T &message)
Write the message with the channel name.
#define DECLARE_SINGLETON(classname)
optional string channel_name