1 | // %pacpus:license{
|
---|
2 | // This file is part of the PACPUS framework distributed under the
|
---|
3 | // CECILL-C License, Version 1.0.
|
---|
4 | // %pacpus:license}
|
---|
5 | /// @version $Id: DbtPlyFileManager.cpp 76 2013-01-10 17:05:10Z kurdejma $
|
---|
6 |
|
---|
7 | #include <Pacpus/DbitePlayer/DbtPlyFileManager.h>
|
---|
8 | #include <Pacpus/kernel/ComponentManager.h>
|
---|
9 | #include <Pacpus/kernel/DbiteException.h>
|
---|
10 | #include <Pacpus/kernel/Log.h>
|
---|
11 |
|
---|
12 | #include <cassert>
|
---|
13 | #include <iostream>
|
---|
14 | #include <limits>
|
---|
15 | #include <QMetaType>
|
---|
16 | #include <QSemaphore>
|
---|
17 |
|
---|
18 | using namespace pacpus;
|
---|
19 | using namespace std;
|
---|
20 |
|
---|
21 | DECLARE_STATIC_LOGGER("pacpus.core.DbtPlyFileManager");
|
---|
22 |
|
---|
23 | const string kPropertyVerbose = "verbose";
|
---|
24 | const int kPropertyVerboseDefaultValue = 1;
|
---|
25 |
|
---|
26 | const string kPropertyDbiteFileName = "dbt";
|
---|
27 |
|
---|
28 | // It is the maximum time elapsed between the computation of the tDbt and the data replay in microseconds
|
---|
29 | static const int kMaxPendingTimeFromEngineMicrosecs = 10000;
|
---|
30 |
|
---|
31 | ////////////////////////////////////////////////////////////////////////////////
|
---|
32 | /// Constructor.
|
---|
33 | DbtPlyFileManager::DbtPlyFileManager(QString name):ComponentBase(name)
|
---|
34 | {
|
---|
35 | LOG_TRACE("constructor");
|
---|
36 |
|
---|
37 | sync_ = new QSemaphore(1);
|
---|
38 | sync_->acquire();
|
---|
39 | tMin_ = numeric_limits<road_time_t>::max();
|
---|
40 | tMax_ = numeric_limits<road_time_t>::min();
|
---|
41 | }
|
---|
42 |
|
---|
43 | ////////////////////////////////////////////////////////////////////////////////
|
---|
44 | /// Destructor.
|
---|
45 | DbtPlyFileManager::~DbtPlyFileManager()
|
---|
46 | {
|
---|
47 | LOG_TRACE("destructor");
|
---|
48 |
|
---|
49 | for (QList<dbtStructFile>::iterator it = dbt_.begin(), itend = dbt_.end(); it != itend; ++it) {
|
---|
50 | dbtStructFile & dbtFile = *it;
|
---|
51 |
|
---|
52 | delete[] dbtFile.cur.buffer;
|
---|
53 | delete[] dbtFile.toProcess.buffer;
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | ////////////////////////////////////////////////////////////////////////////////
|
---|
58 | /// Configures the file manager.
|
---|
59 | ComponentBase::COMPONENT_CONFIGURATION DbtPlyFileManager::configureComponent(XmlComponentConfig config)
|
---|
60 | {
|
---|
61 | ComponentManager * mgr = ComponentManager::getInstance();
|
---|
62 | mEngine = static_cast<DbtPlyEngine *>(mgr->getComponent("dbiteEngine"));
|
---|
63 | if (NULL == mEngine ) {
|
---|
64 | LOG_FATAL("cannot get a pointer of the 'dbiteEngine' component");
|
---|
65 | return CONFIGURED_FAILED;
|
---|
66 | }
|
---|
67 | if (!mEngine->isConfigured()) {
|
---|
68 | LOG_WARN("'dbiteEngine' component was not configured");
|
---|
69 | return CONFIGURATION_DELAYED;
|
---|
70 | }
|
---|
71 |
|
---|
72 | // Qt Signal-Slot connections
|
---|
73 | // register the road_time_t type for the connection
|
---|
74 | qRegisterMetaType<road_time_t>("road_time_t");
|
---|
75 | connect(mEngine, SIGNAL(play(road_time_t,road_time_t, bool)),
|
---|
76 | this, SLOT(playData(road_time_t,road_time_t, bool)),
|
---|
77 | Qt::DirectConnection);
|
---|
78 | connect(this, SIGNAL(tMinMaxIs(road_time_t,road_time_t )),
|
---|
79 | mEngine, SLOT(tMinMax(road_time_t, road_time_t)));
|
---|
80 | connect(mEngine, SIGNAL(stopfile()),
|
---|
81 | this, SLOT (beginfile()));
|
---|
82 |
|
---|
83 |
|
---|
84 | // get the properties in the XML node
|
---|
85 | /////////////////////////////////////////
|
---|
86 | {
|
---|
87 | QString verbose = param.getProperty(kPropertyVerbose.c_str());
|
---|
88 | if (verbose.isNull()) {
|
---|
89 | LOG_INFO("property " << kPropertyVerbose << " not set."
|
---|
90 | << " Set to default = " << kPropertyVerboseDefaultValue
|
---|
91 | );
|
---|
92 | // default value
|
---|
93 | mVerbose = kPropertyVerboseDefaultValue;
|
---|
94 | } else {
|
---|
95 | verbose = verbose.toLower();
|
---|
96 | LOG_DEBUG("property " << kPropertyVerbose << "=\""
|
---|
97 | << verbose.toStdString() << "\"");
|
---|
98 | mVerbose = verbose.toInt();
|
---|
99 | LOG_DEBUG("property interpreted as " << kPropertyVerbose << "=\""
|
---|
100 | << mVerbose << "\"");
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | /////////////////////////////////////////
|
---|
105 | dbtProperty_ = param.getProperty(kPropertyDbiteFileName.c_str());
|
---|
106 | mShowGui = param.getProperty("ui").toInt();
|
---|
107 | mDbtDataPath = mEngine->getDataDir();
|
---|
108 |
|
---|
109 | mDbtFilenameList = dbtProperty_.split("|");
|
---|
110 |
|
---|
111 | for (int i = 0; i < mDbtFilenameList.size(); ++i) {
|
---|
112 | mDbtFilenameList[i] = mDbtDataPath + mDbtFilenameList.at(i);
|
---|
113 | LOG_INFO("opening DBT file \"" << mDbtFilenameList.at(i) << "\"");
|
---|
114 |
|
---|
115 | // temporary dbt object
|
---|
116 | dbtStructFile dbt;
|
---|
117 | dbt.pfile = new DbiteFile();
|
---|
118 | assert(dbt.pfile);
|
---|
119 | try {
|
---|
120 | dbt.pfile->open(mDbtFilenameList.at(i).toStdString(), ReadMode);
|
---|
121 | } catch (DbiteException & e) {
|
---|
122 | (void) e; // unused
|
---|
123 | LOG_ERROR("cannot open DBT file \"" << mDbtFilenameList.at(i) << "\"");
|
---|
124 | LOG_DEBUG("error: " << e.what());
|
---|
125 | return CONFIGURED_FAILED;
|
---|
126 | }
|
---|
127 | if (!dbt.pfile->isOpen()) {
|
---|
128 | LOG_ERROR("cannot open DBT file \"" << mDbtFilenameList.at(i) << "\"");
|
---|
129 | return CONFIGURED_FAILED;
|
---|
130 | }
|
---|
131 | cout << "Header: " << "\n"
|
---|
132 | << (string) *dbt.pfile << "\n";
|
---|
133 |
|
---|
134 | // alloc buffer with the Data Size given in the header
|
---|
135 | dbt.cur.buffer = new char[dbt.pfile->getRecordSize()];
|
---|
136 | dbt.toProcess.buffer = new char[dbt.pfile->getRecordSize()];
|
---|
137 |
|
---|
138 | dbt_.insert(i, dbt);
|
---|
139 |
|
---|
140 | // Reading of the first data to put in the buffer_
|
---|
141 | dbt_[i].pfile->readRecord(dbt_[i].cur.t, dbt_[i].cur.tr, reinterpret_cast<char *>(dbt_[i].cur.buffer));
|
---|
142 |
|
---|
143 | if (dbt_.at(i).pfile->getRecordCount() > 0) {
|
---|
144 | if (dbt_.at(i).pfile->getTimeMin() < tMin_) {
|
---|
145 | tMin_ = dbt_.at(i).pfile->getTimeMin();
|
---|
146 | }
|
---|
147 | if (dbt_.at(i).pfile->getTimeMax() > tMax_ ) {
|
---|
148 | tMax_ = dbt_.at(i).pfile->getTimeMax();
|
---|
149 | }
|
---|
150 | Q_EMIT tMinMaxIs(tMin_, tMax_);
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | return CONFIGURED_OK;
|
---|
155 | }
|
---|
156 |
|
---|
157 | ////////////////////////////////////////////////////////////////////////////////
|
---|
158 | /// Starts activity of the file manager
|
---|
159 | void DbtPlyFileManager::startActivity()
|
---|
160 | {
|
---|
161 | // graphical user interface
|
---|
162 | if (mShowGui) {
|
---|
163 | displayUI();
|
---|
164 | }
|
---|
165 |
|
---|
166 | deltaTDbtTabLoop_ = 0;
|
---|
167 |
|
---|
168 | mode_ = mEngine->replayMode();
|
---|
169 | LOG_INFO("replay mode " << mode_);
|
---|
170 | start();
|
---|
171 | }
|
---|
172 |
|
---|
173 | ////////////////////////////////////////////////////////////////////////////////
|
---|
174 | /// Stops activity of the file manager
|
---|
175 | void DbtPlyFileManager::stopActivity()
|
---|
176 | {
|
---|
177 | deltaTDbtTabLoop_ = 0;
|
---|
178 | }
|
---|
179 |
|
---|
180 | void DbtPlyFileManager::displayUI()
|
---|
181 | {
|
---|
182 | LOG_WARN("component '" << componentName << "' has no user interface. Please set ui property to 0 in your XML configuration");
|
---|
183 | }
|
---|
184 |
|
---|
185 | ////////////////////////////////////////////////////////////////////////////////
|
---|
186 | /// Goes back to the beginning of the file.
|
---|
187 | /// Places the descriptor to the first data.
|
---|
188 | void DbtPlyFileManager::beginfile()
|
---|
189 | {
|
---|
190 | LOG_DEBUG("called beginfile()");
|
---|
191 | for (QList<dbtStructFile>::iterator it = dbt_.begin(), itend = dbt_.end(); it != itend; ++it) {
|
---|
192 | dbtStructFile & dbtFile = *it;
|
---|
193 |
|
---|
194 | dbtFile.pfile->goToDataBegin();
|
---|
195 | // reinitialize buffer and times to the 1st value
|
---|
196 | dbtFile.pfile->readRecord(dbtFile.cur.t, dbtFile.cur.tr, reinterpret_cast<char *>(dbtFile.cur.buffer));
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | bool DbtPlyFileManager::processDbtFileHdfile(dbtStructFile & dbtFile, DbiteFile::ReadDirection direction)
|
---|
201 | {
|
---|
202 | dbtFile.toProcess.t = dbtFile.cur.t;
|
---|
203 | dbtFile.toProcess.tr = dbtFile.cur.tr;
|
---|
204 | memcpy(dbtFile.toProcess.buffer, dbtFile.cur.buffer, dbtFile.pfile->getRecordSize());
|
---|
205 |
|
---|
206 | if (!dbtFile.pfile->readRecord(dbtFile.cur.t, dbtFile.cur.tr, reinterpret_cast<char *>(dbtFile.cur.buffer), direction)) {
|
---|
207 | LOG_WARN("EOF or there was a problem in read function, no data read");
|
---|
208 | dbtFile.cur.t = tMax_;
|
---|
209 | return false;
|
---|
210 | }
|
---|
211 | return true;
|
---|
212 | }
|
---|
213 |
|
---|
214 | ////////////////////////////////////////////////////////////////////////////////
|
---|
215 | /// play mode 1
|
---|
216 | /// the player replays only the last data that has not been yet replayed
|
---|
217 | void DbtPlyFileManager::playMode1(road_time_t tDbt, bool reverse)
|
---|
218 | {
|
---|
219 | DbiteFile::ReadDirection readDirection = DbiteFile::ReadForward;
|
---|
220 | if (reverse) {
|
---|
221 | readDirection = DbiteFile::ReadBackward;
|
---|
222 | }
|
---|
223 |
|
---|
224 | dbtIndex_ = 0;
|
---|
225 | for (QList<dbtStructFile>::iterator it = dbt_.begin(), itend = dbt_.end(); it != itend; ++it) {
|
---|
226 | dbtStructFile & dbtFile = *it;
|
---|
227 |
|
---|
228 | bool mustProcess = false;
|
---|
229 | if (reverse) {
|
---|
230 | // backward
|
---|
231 | while (dbtFile.cur.t > tDbt) {
|
---|
232 | mustProcess = true;
|
---|
233 | if (!processDbtFileHdfile(dbtFile, readDirection)) {
|
---|
234 | break;
|
---|
235 | }
|
---|
236 | }
|
---|
237 | } else {
|
---|
238 | // forward
|
---|
239 | while (dbtFile.cur.t < tDbt) {
|
---|
240 | mustProcess = true;
|
---|
241 | if (!processDbtFileHdfile(dbtFile, readDirection)) {
|
---|
242 | break;
|
---|
243 | }
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 | if (mustProcess) {
|
---|
248 | processData(dbtFile.cur.t, dbtFile.toProcess.tr, dbtFile.cur.buffer);
|
---|
249 | }
|
---|
250 | ++dbtIndex_;
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | ////////////////////////////////////////////////////////////////////////////////
|
---|
255 | // play mode 2
|
---|
256 | // the player replays all the data that have not been yet replayed
|
---|
257 | void DbtPlyFileManager::playMode2(road_time_t tDbt, bool reverse)
|
---|
258 | {
|
---|
259 | DbiteFile::ReadDirection readDirection = DbiteFile::ReadForward;
|
---|
260 | if (reverse) {
|
---|
261 | readDirection = DbiteFile::ReadBackward;
|
---|
262 | }
|
---|
263 |
|
---|
264 | dbtIndex_ = 0;
|
---|
265 | for (QList<dbtStructFile>::iterator it = dbt_.begin(), itend = dbt_.end(); it != itend; ++it) {
|
---|
266 | dbtStructFile & dbtFile = *it;
|
---|
267 |
|
---|
268 | if (reverse) {
|
---|
269 | // backward
|
---|
270 | while (dbtFile.cur.t > tDbt) {
|
---|
271 | processDbtFileHdfile(dbtFile, readDirection);
|
---|
272 | processData(dbtFile.cur.t, dbtFile.toProcess.tr, dbtFile.cur.buffer);
|
---|
273 | }
|
---|
274 | } else {
|
---|
275 | // forward
|
---|
276 | while (dbtFile.cur.t < tDbt) {
|
---|
277 | processDbtFileHdfile(dbtFile, readDirection);
|
---|
278 | processData(dbtFile.cur.t, dbtFile.toProcess.tr, dbtFile.cur.buffer);
|
---|
279 | }
|
---|
280 | }
|
---|
281 | ++dbtIndex_;
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 | ////////////////////////////////////////////////////////////////////////////////
|
---|
286 | // slot
|
---|
287 | // Called by the player engine
|
---|
288 | // Before replaying data, this funtion verifies that the replayed time is
|
---|
289 | // included in the time interval of the data and that the time elapsed between
|
---|
290 | // the estimation of this replayed time is not too big
|
---|
291 | void DbtPlyFileManager::playData(road_time_t tDbt,road_time_t tNow, bool reverse)
|
---|
292 | {
|
---|
293 | // If tDbt is not included in the time interval of the data, we don't have any data to play
|
---|
294 | if ((tDbt>tMax_) || (tDbt<tMin_)) {
|
---|
295 | return;
|
---|
296 | }
|
---|
297 |
|
---|
298 | // measure the difference between the current time and the tNow used to calculate the tDbt
|
---|
299 | int deltaT = road_time() - tNow;
|
---|
300 | deltaTDbtTab_[(deltaTDbtTabLoop_++)%1000] = deltaT;
|
---|
301 | if (deltaT > kMaxPendingTimeFromEngineMicrosecs) {
|
---|
302 | LOG_WARN(componentName << ": data not replayed: elapsed time since engine notification too big:" << deltaT << "us");
|
---|
303 | return;
|
---|
304 | }
|
---|
305 |
|
---|
306 | timeToRead_ = tDbt;
|
---|
307 | reverse_ = reverse;
|
---|
308 | sync_->release();
|
---|
309 | }
|
---|
310 |
|
---|
311 | ////////////////////////////////////////////////////////////////////////////////
|
---|
312 | /// Main loop of the thread.
|
---|
313 | /// Only synchronize on the player engine and when activated call the correct
|
---|
314 | /// replay method.
|
---|
315 | void DbtPlyFileManager::run()
|
---|
316 | {
|
---|
317 | for (;;) {
|
---|
318 | sync_->acquire();
|
---|
319 |
|
---|
320 | switch (mode_) {
|
---|
321 | case 1:
|
---|
322 | playMode1(timeToRead_, reverse_);
|
---|
323 | break;
|
---|
324 |
|
---|
325 | case 2:
|
---|
326 | playMode2(timeToRead_, reverse_);
|
---|
327 | break;
|
---|
328 |
|
---|
329 | default:
|
---|
330 | break;
|
---|
331 | }
|
---|
332 | }
|
---|
333 | }
|
---|