source: pacpusframework/branches/0.0.x/src/DBITEPlayerLib/DbtPlyFileManager.cpp@ 218

Last change on this file since 218 was 91, checked in by DHERBOMEZ Gérald, 11 years ago

Improvement of the build system to avoid some workarounds

  • Property svn:keywords set to Id
File size: 11.4 KB
Line 
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/// @author Gerald Dherbomez <firstname.surname@utc.fr>
6/// @version $Id: DbtPlyFileManager.cpp 91 2013-05-19 10:32:48Z gdherbom $
7
8#include <Pacpus/DbitePlayer/DbtPlyFileManager.h>
9#include <Pacpus/kernel/ComponentManager.h>
10#include <Pacpus/kernel/DbiteException.h>
11#include <Pacpus/kernel/Log.h>
12
13#include <cassert>
14#include <iostream>
15#include <limits>
16#include <QMetaType>
17#include <QSemaphore>
18
19using namespace pacpus;
20using namespace std;
21
22DECLARE_STATIC_LOGGER("pacpus.core.DbtPlyFileManager");
23
24const string kPropertyVerbose = "verbose";
25const int kPropertyVerboseDefaultValue = 1;
26
27const string kPropertyDbiteFileName = "dbt";
28
29// It is the maximum time elapsed between the computation of the tDbt and the data replay in microseconds
30static const int kMaxPendingTimeFromEngineMicrosecs = 10000;
31
32////////////////////////////////////////////////////////////////////////////////
33/// Constructor.
34DbtPlyFileManager::DbtPlyFileManager(QString name):ComponentBase(name)
35{
36 LOG_TRACE("constructor");
37
38 sync_ = new QSemaphore(1);
39 sync_->acquire();
40 tMin_ = numeric_limits<road_time_t>::max();
41 tMax_ = numeric_limits<road_time_t>::min();
42}
43
44////////////////////////////////////////////////////////////////////////////////
45/// Destructor.
46DbtPlyFileManager::~DbtPlyFileManager()
47{
48 LOG_TRACE("destructor");
49
50 for (QList<dbtStructFile>::iterator it = dbt_.begin(), itend = dbt_.end(); it != itend; ++it) {
51 dbtStructFile & dbtFile = *it;
52
53 delete[] dbtFile.cur.buffer;
54 delete[] dbtFile.toProcess.buffer;
55 }
56}
57
58////////////////////////////////////////////////////////////////////////////////
59/// Configures the file manager.
60ComponentBase::COMPONENT_CONFIGURATION DbtPlyFileManager::configureComponent(XmlComponentConfig config)
61{
62 ComponentManager * mgr = ComponentManager::getInstance();
63 mEngine = static_cast<DbtPlyEngine *>(mgr->getComponent("dbiteEngine"));
64 if (NULL == mEngine ) {
65 LOG_FATAL("cannot get a pointer of the 'dbiteEngine' component");
66 return CONFIGURED_FAILED;
67 }
68 if (!mEngine->isConfigured()) {
69 LOG_WARN("'dbiteEngine' component was not configured");
70 return CONFIGURATION_DELAYED;
71 }
72
73 // Qt Signal-Slot connections
74 // register the road_time_t type for the connection
75 qRegisterMetaType<road_time_t>("road_time_t");
76 connect(mEngine, SIGNAL(play(road_time_t,road_time_t, bool)),
77 this, SLOT(playData(road_time_t,road_time_t, bool)),
78 Qt::DirectConnection);
79 connect(this, SIGNAL(tMinMaxIs(road_time_t,road_time_t )),
80 mEngine, SLOT(tMinMax(road_time_t, road_time_t)));
81 connect(mEngine, SIGNAL(stopfile()),
82 this, SLOT (beginfile()));
83
84
85 // get the properties in the XML node
86 /////////////////////////////////////////
87 {
88 QString verbose = param.getProperty(kPropertyVerbose.c_str());
89 if (verbose.isNull()) {
90 LOG_INFO("property " << kPropertyVerbose << " not set."
91 << " Set to default = " << kPropertyVerboseDefaultValue
92 );
93 // default value
94 mVerbose = kPropertyVerboseDefaultValue;
95 } else {
96 verbose = verbose.toLower();
97 LOG_DEBUG("property " << kPropertyVerbose << "=\""
98 << verbose.toStdString() << "\"");
99 mVerbose = verbose.toInt();
100 LOG_DEBUG("property interpreted as " << kPropertyVerbose << "=\""
101 << mVerbose << "\"");
102 }
103 }
104
105 /////////////////////////////////////////
106 dbtProperty_ = param.getProperty(kPropertyDbiteFileName.c_str());
107 mShowGui = param.getProperty("ui").toInt();
108 mDbtDataPath = mEngine->getDataDir();
109
110 mDbtFilenameList = dbtProperty_.split("|");
111
112 for (int i = 0; i < mDbtFilenameList.size(); ++i) {
113 mDbtFilenameList[i] = mDbtDataPath + mDbtFilenameList.at(i);
114 LOG_INFO("opening DBT file \"" << mDbtFilenameList.at(i) << "\"");
115
116 // temporary dbt object
117 dbtStructFile dbt;
118 dbt.pfile = new DbiteFile();
119 assert(dbt.pfile);
120 try {
121 dbt.pfile->open(mDbtFilenameList.at(i).toStdString(), ReadMode);
122 } catch (DbiteException & e) {
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
159void 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
175void DbtPlyFileManager::stopActivity()
176{
177 deltaTDbtTabLoop_ = 0;
178}
179
180void 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.
188void 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
200bool 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
217void 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
257void 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
291void 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.
315void 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}
Note: See TracBrowser for help on using the repository browser.