source: pacpusframework/trunk/src/DBITEPlayerLib/DbtPlyFileManager.cpp@ 34

Last change on this file since 34 was 31, checked in by sgosseli, 12 years ago

Huge commit: use the new includes style in all the files, add the license header in all the headers, and in some cpp.

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