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

Last change on this file since 73 was 73, checked in by Marek Kurdej, 12 years ago

Minor: line-endings.

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