source: pacpusframework/branches/2.0-beta1/src/DBITEPlayerLib/DbtPlyFileManager.cpp@ 96

Last change on this file since 96 was 89, checked in by morasjul, 12 years ago

PACPUS 2.0 Beta deployed in new branch

Major changes:
-Add communication interface between components
-Add examples for communications interface (TestComponents)
-Move to Qt5 support

  • Property svn:executable set to *
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/// @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
18using namespace pacpus;
19using namespace std;
20
21DECLARE_STATIC_LOGGER("pacpus.core.DbtPlyFileManager");
22
23const string kPropertyVerbose = "verbose";
24const int kPropertyVerboseDefaultValue = 1;
25
26const string kPropertyDbiteFileName = "dbt";
27
28// It is the maximum time elapsed between the computation of the tDbt and the data replay in microseconds
29static const int kMaxPendingTimeFromEngineMicrosecs = 10000;
30
31////////////////////////////////////////////////////////////////////////////////
32/// Constructor.
33DbtPlyFileManager::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.
45DbtPlyFileManager::~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.
59ComponentBase::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 LOG_ERROR("cannot open DBT file \"" << mDbtFilenameList.at(i) << "\"");
123 LOG_DEBUG("error: " << e.what());
124 return CONFIGURED_FAILED;
125 }
126 if (!dbt.pfile->isOpen()) {
127 LOG_ERROR("cannot open DBT file \"" << mDbtFilenameList.at(i) << "\"");
128 return CONFIGURED_FAILED;
129 }
130 cout << "Header: " << "\n"
131 << (string) *dbt.pfile << "\n";
132
133 // alloc buffer with the Data Size given in the header
134 dbt.cur.buffer = new char[dbt.pfile->getRecordSize()];
135 dbt.toProcess.buffer = new char[dbt.pfile->getRecordSize()];
136
137 dbt_.insert(i, dbt);
138
139 // Reading of the first data to put in the buffer_
140 dbt_[i].pfile->readRecord(dbt_[i].cur.t, dbt_[i].cur.tr, reinterpret_cast<char *>(dbt_[i].cur.buffer));
141
142 if (dbt_.at(i).pfile->getRecordCount() > 0) {
143 if (dbt_.at(i).pfile->getTimeMin() < tMin_) {
144 tMin_ = dbt_.at(i).pfile->getTimeMin();
145 }
146 if (dbt_.at(i).pfile->getTimeMax() > tMax_ ) {
147 tMax_ = dbt_.at(i).pfile->getTimeMax();
148 }
149 Q_EMIT tMinMaxIs(tMin_, tMax_);
150 }
151 }
152
153 return CONFIGURED_OK;
154}
155
156////////////////////////////////////////////////////////////////////////////////
157/// Starts activity of the file manager
158void DbtPlyFileManager::startActivity()
159{
160 // graphical user interface
161 if (mShowGui) {
162 displayUI();
163 }
164
165 deltaTDbtTabLoop_ = 0;
166
167 mode_ = mEngine->replayMode();
168 LOG_INFO("replay mode " << mode_);
169 start();
170}
171
172////////////////////////////////////////////////////////////////////////////////
173/// Stops activity of the file manager
174void DbtPlyFileManager::stopActivity()
175{
176 deltaTDbtTabLoop_ = 0;
177}
178
179void DbtPlyFileManager::displayUI()
180{
181 //LOG_WARN("component '" << componentName << "' has no user interface. Please set ui property to 0 in your XML configuration");
182}
183
184////////////////////////////////////////////////////////////////////////////////
185/// Goes back to the beginning of the file.
186/// Places the descriptor to the first data.
187void DbtPlyFileManager::beginfile()
188{
189 LOG_DEBUG("called beginfile()");
190 for (QList<dbtStructFile>::iterator it = dbt_.begin(), itend = dbt_.end(); it != itend; ++it) {
191 dbtStructFile & dbtFile = *it;
192
193 dbtFile.pfile->goToDataBegin();
194 // reinitialize buffer and times to the 1st value
195 dbtFile.pfile->readRecord(dbtFile.cur.t, dbtFile.cur.tr, reinterpret_cast<char *>(dbtFile.cur.buffer));
196 }
197}
198
199bool DbtPlyFileManager::processDbtFileHdfile(dbtStructFile & dbtFile, DbiteFile::ReadDirection direction)
200{
201 dbtFile.toProcess.t = dbtFile.cur.t;
202 dbtFile.toProcess.tr = dbtFile.cur.tr;
203 memcpy(dbtFile.toProcess.buffer, dbtFile.cur.buffer, dbtFile.pfile->getRecordSize());
204
205 if (!dbtFile.pfile->readRecord(dbtFile.cur.t, dbtFile.cur.tr, reinterpret_cast<char *>(dbtFile.cur.buffer), direction)) {
206 LOG_WARN("EOF or there was a problem in read function, no data read");
207 dbtFile.cur.t = tMax_;
208 return false;
209 }
210 return true;
211}
212
213////////////////////////////////////////////////////////////////////////////////
214/// play mode 1
215/// the player replays only the last data that has not been yet replayed
216void DbtPlyFileManager::playMode1(road_time_t tDbt, bool reverse)
217{
218 DbiteFile::ReadDirection readDirection = DbiteFile::ReadForward;
219 if (reverse) {
220 readDirection = DbiteFile::ReadBackward;
221 }
222
223 dbtIndex_ = 0;
224 for (QList<dbtStructFile>::iterator it = dbt_.begin(), itend = dbt_.end(); it != itend; ++it) {
225 dbtStructFile & dbtFile = *it;
226
227 bool mustProcess = false;
228 if (reverse) {
229 // backward
230 while (dbtFile.cur.t > tDbt) {
231 mustProcess = true;
232 if (!processDbtFileHdfile(dbtFile, readDirection)) {
233 break;
234 }
235 }
236 } else {
237 // forward
238 while (dbtFile.cur.t < tDbt) {
239 mustProcess = true;
240 if (!processDbtFileHdfile(dbtFile, readDirection)) {
241 break;
242 }
243 }
244 }
245
246 if (mustProcess) {
247 processData(dbtFile.cur.t, dbtFile.toProcess.tr, dbtFile.cur.buffer);
248 }
249 ++dbtIndex_;
250 }
251}
252
253////////////////////////////////////////////////////////////////////////////////
254// play mode 2
255// the player replays all the data that have not been yet replayed
256void DbtPlyFileManager::playMode2(road_time_t tDbt, bool reverse)
257{
258 DbiteFile::ReadDirection readDirection = DbiteFile::ReadForward;
259 if (reverse) {
260 readDirection = DbiteFile::ReadBackward;
261 }
262
263 dbtIndex_ = 0;
264 for (QList<dbtStructFile>::iterator it = dbt_.begin(), itend = dbt_.end(); it != itend; ++it) {
265 dbtStructFile & dbtFile = *it;
266
267 if (reverse) {
268 // backward
269 while (dbtFile.cur.t > tDbt) {
270 processDbtFileHdfile(dbtFile, readDirection);
271 processData(dbtFile.cur.t, dbtFile.toProcess.tr, dbtFile.cur.buffer);
272 }
273 } else {
274 // forward
275 while (dbtFile.cur.t < tDbt) {
276 processDbtFileHdfile(dbtFile, readDirection);
277 processData(dbtFile.cur.t, dbtFile.toProcess.tr, dbtFile.cur.buffer);
278 }
279 }
280 ++dbtIndex_;
281 }
282}
283
284////////////////////////////////////////////////////////////////////////////////
285// slot
286// Called by the player engine
287// Before replaying data, this funtion verifies that the replayed time is
288// included in the time interval of the data and that the time elapsed between
289// the estimation of this replayed time is not too big
290void DbtPlyFileManager::playData(road_time_t tDbt,road_time_t tNow, bool reverse)
291{
292 // If tDbt is not included in the time interval of the data, we don't have any data to play
293 if ((tDbt>tMax_) || (tDbt<tMin_)) {
294 return;
295 }
296
297 // measure the difference between the current time and the tNow used to calculate the tDbt
298 int deltaT = road_time() - tNow;
299 deltaTDbtTab_[(deltaTDbtTabLoop_++)%1000] = deltaT;
300 if (deltaT > kMaxPendingTimeFromEngineMicrosecs) {
301 LOG_WARN(componentName << ": data not replayed: elapsed time since engine notification too big:" << deltaT << "us");
302 return;
303 }
304
305 timeToRead_ = tDbt;
306 reverse_ = reverse;
307 sync_->release();
308}
309
310////////////////////////////////////////////////////////////////////////////////
311/// Main loop of the thread.
312/// Only synchronize on the player engine and when activated call the correct
313/// replay method.
314void DbtPlyFileManager::run()
315{
316 for (;;) {
317 sync_->acquire();
318
319 switch (mode_) {
320 case 1:
321 playMode1(timeToRead_, reverse_);
322 break;
323
324 case 2:
325 playMode2(timeToRead_, reverse_);
326 break;
327
328 default:
329 break;
330 }
331 }
332}
Note: See TracBrowser for help on using the repository browser.