Apollo 10.0
自动驾驶开放平台
recoverer.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 "cyber/base/for_each.h"
21
22namespace apollo {
23namespace cyber {
24namespace record {
25
29
30Recoverer::Recoverer(const std::string& input_file,
31 const std::string& output_file)
32 : input_file_(input_file), output_file_(output_file) {}
33
35
37 if (!reader_.Open(input_file_)) {
38 AERROR << "open input file failed, file: " << input_file_;
39 return false;
40 }
41
42 // open output file
44 if (!writer_.Open(output_file_)) {
45 AERROR << "open output file failed. file: " << output_file_;
46 return false;
47 }
48 if (!writer_.WriteHeader(new_hdr)) {
49 AERROR << "write header to output file failed. file: " << output_file_;
50 return false;
51 }
52
53 // write channel sections
54 if (reader_.ReadIndex()) {
55 proto::Index index = reader_.GetIndex();
56 FOR_EACH(i, 0, index.indexes_size()) {
57 proto::SingleIndex* single_index = index.mutable_indexes(i);
58 if (single_index->type() != SectionType::SECTION_CHANNEL) {
59 continue;
60 }
61 ChannelCache* chan_cache = single_index->mutable_channel_cache();
62 if (std::find(channel_vec_.begin(), channel_vec_.end(),
63 chan_cache->name()) == channel_vec_.end()) {
64 channel_vec_.push_back(chan_cache->name());
65 Channel chan;
66 chan.set_name(chan_cache->name());
67 chan.set_message_type(chan_cache->message_type());
68 chan.set_proto_desc(chan_cache->proto_desc());
69 writer_.WriteChannel(chan);
70 }
71 }
72 }
73
74 // read through record file
75 reader_.Reset();
76 while (!reader_.EndOfFile()) {
77 Section section;
78 if (!reader_.ReadSection(&section)) {
79 AINFO << "read section failed, try next.";
80 continue;
81 }
82 if (section.type == SectionType::SECTION_INDEX) {
83 break;
84 }
85 switch (section.type) {
86 case SectionType::SECTION_CHANNEL: {
87 Channel chan;
88 if (!reader_.ReadSection<Channel>(section.size, &chan)) {
89 AINFO << "one channel section broken, skip it.";
90 break;
91 }
92 if (std::find(channel_vec_.begin(), channel_vec_.end(), chan.name()) ==
93 channel_vec_.end()) {
94 channel_vec_.push_back(chan.name());
95 writer_.WriteChannel(chan);
96 }
97 break;
98 }
99 case SectionType::SECTION_CHUNK_HEADER: {
100 ChunkHeader chdr;
101 if (!reader_.ReadSection<ChunkHeader>(section.size, &chdr)) {
102 AINFO << "one chunk header section broken, skip it.";
103 }
104 break;
105 }
106 case SectionType::SECTION_CHUNK_BODY: {
107 ChunkBody cbd;
108 if (!reader_.ReadSection<ChunkBody>(section.size, &cbd)) {
109 AINFO << "one chunk body section broken, skip it";
110 break;
111 }
112 for (int idx = 0; idx < cbd.messages_size(); ++idx) {
113 if (!writer_.WriteMessage(cbd.messages(idx))) {
114 AERROR << "add new message failed.";
115 return false;
116 }
117 }
118 break;
119 }
120 default: {
121 AERROR << "this section should not be here, section type: "
122 << section.type;
123 return false;
124 }
125 } // end for switch
126 } // end for while
127 AINFO << "recover record file done.";
128 return true;
129} // end for Proc()
130
131} // namespace record
132} // namespace cyber
133} // namespace apollo
static proto::Header GetHeader()
Build a default record header.
const proto::Index & GetIndex() const
bool Open(const std::string &path) override
bool Open(const std::string &path) override
bool WriteHeader(const proto::Header &header)
bool WriteChannel(const proto::Channel &channel)
bool WriteMessage(const proto::SingleMessage &message)
Recoverer(const std::string &input_file, const std::string &output_file)
Definition recoverer.cc:30
#define FOR_EACH(i, begin, end)
Definition for_each.h:44
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37
repeated SingleMessage messages
Definition record.proto:96
optional SectionType type
Definition record.proto:20
proto::SectionType type
Definition section.h:25