forked from commaai/openpilot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileReader.cpp
139 lines (110 loc) · 3.74 KB
/
FileReader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "FileReader.hpp"
#include "FrameReader.hpp"
#include <QtNetwork>
FileReader::FileReader(const QString& file_) : file(file_) {
}
void FileReader::process() {
timer.start();
QString str = file.simplified();
str.replace(" ", "");
startRequest(QUrl(str));
}
void FileReader::startRequest(const QUrl &url) {
qnam = new QNetworkAccessManager;
reply = qnam->get(QNetworkRequest(url));
connect(reply, &QNetworkReply::finished, this, &FileReader::httpFinished);
connect(reply, &QIODevice::readyRead, this, &FileReader::readyRead);
qDebug() << "requesting" << url;
}
void FileReader::httpFinished() {
if (reply->error()) {
qWarning() << reply->errorString();
}
const QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (!redirectionTarget.isNull()) {
const QUrl redirectedUrl = redirectionTarget.toUrl();
//qDebug() << "redirected to" << redirectedUrl;
startRequest(redirectedUrl);
} else {
qDebug() << "done in" << timer.elapsed() << "ms";
done();
}
}
void FileReader::readyRead() {
QByteArray dat = reply->readAll();
printf("got http ready read: %d\n", dat.size());
}
FileReader::~FileReader() {
}
LogReader::LogReader(const QString& file, Events *events_, QReadWriteLock* events_lock_, QMap<int, QPair<int, int> > *eidx_) :
FileReader(file), events(events_), events_lock(events_lock_), eidx(eidx_) {
bStream.next_in = NULL;
bStream.avail_in = 0;
bStream.bzalloc = NULL;
bStream.bzfree = NULL;
bStream.opaque = NULL;
int ret = BZ2_bzDecompressInit(&bStream, 0, 0);
if (ret != BZ_OK) qWarning() << "bz2 init failed";
// start with 64MB buffer
raw.resize(1024*1024*64);
// auto increment?
bStream.next_out = raw.data();
bStream.avail_out = raw.size();
// parsed no events yet
event_offset = 0;
parser = new std::thread([&]() {
while (1) {
mergeEvents(cdled.get());
}
});
}
void LogReader::mergeEvents(int dled) {
auto amsg = kj::arrayPtr((const capnp::word*)(raw.data() + event_offset), (dled-event_offset)/sizeof(capnp::word));
Events events_local;
QMap<int, QPair<int, int> > eidx_local;
while (amsg.size() > 0) {
try {
capnp::FlatArrayMessageReader cmsg = capnp::FlatArrayMessageReader(amsg);
// this needed? it is
capnp::FlatArrayMessageReader *tmsg =
new capnp::FlatArrayMessageReader(kj::arrayPtr(amsg.begin(), cmsg.getEnd()));
amsg = kj::arrayPtr(cmsg.getEnd(), amsg.end());
cereal::Event::Reader event = tmsg->getRoot<cereal::Event>();
events_local.insert(event.getLogMonoTime(), event);
// hack
// TODO: rewrite with callback
if (event.which() == cereal::Event::ENCODE_IDX) {
auto ee = event.getEncodeIdx();
eidx_local.insert(ee.getFrameId(), qMakePair(ee.getSegmentNum(), ee.getSegmentId()));
}
// increment
event_offset = (char*)cmsg.getEnd() - raw.data();
} catch (const kj::Exception& e) {
// partial messages trigger this
//qDebug() << e.getDescription().cStr();
break;
}
}
// merge in events
// TODO: add lock
events_lock->lockForWrite();
*events += events_local;
eidx->unite(eidx_local);
events_lock->unlock();
printf("parsed %d into %d events with offset %d\n", dled, events->size(), event_offset);
}
void LogReader::readyRead() {
QByteArray dat = reply->readAll();
bStream.next_in = dat.data();
bStream.avail_in = dat.size();
while (bStream.avail_in > 0) {
int ret = BZ2_bzDecompress(&bStream);
if (ret != BZ_OK && ret != BZ_STREAM_END) {
qWarning() << "bz2 decompress failed";
break;
}
qDebug() << "got" << dat.size() << "with" << bStream.avail_out << "size" << raw.size();
}
int dled = raw.size() - bStream.avail_out;
cdled.put(dled);
}