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

Last change on this file since 172 was 152, checked in by Marek Kurdej, 11 years ago

Major update.
Renamed: addInput -> addInputs, addOutput -> addOutputs and made pure virtual (=0).
Transformed macro definitions into template methods: ADD_INPUT -> ComponentBase::addInput, ADD_OUTPUT -> ComponentBase::addOutput, GET_INPUT -> ComponentBase::getTypedInput, GET_OUTPUT -> ComponentBase::getTypedOutput.
Fixed: added public/protected set/get methods in ComponentBase, made member fields private.

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