source: pacpusframework/trunk/src/DBITEPlayerLib/DbtPlyEngine.cpp@ 337

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

Using boost::shared_ptr for storing components.

  • Property svn:executable set to *
File size: 9.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: DbtPlyEngine.cpp 76 2013-01-10 17:05:10Z kurdejma $
6
7#include <Pacpus/DbitePlayer/DbtPlyEngine.h>
8
9#include <Pacpus/kernel/ComponentManager.h>
10#include <Pacpus/kernel/Log.h>
11
12#include <boost/assert.hpp>
13#include <limits>
14#include <QDir>
15
16using namespace std;
17
18// Construction de la fabrique de composant DbtPlyEngine
19//static ComponentFactory<DbtPlyEngine> factory("DbtPlyEngine");
20
21namespace pacpus {
22
23DECLARE_STATIC_LOGGER("pacpus.core.DbtPlyEngine");
24
25typedef float SpeedType;
26static const SpeedType kInitialSpeed = 1.0;
27static const SpeedType kMinSpeed = 1.0 / 32;
28static const SpeedType kMaxSpeed = 32;
29
30static const PlayMode kDefaultPlayMode = PlayModeLastData;
31
32////////////////////////////////////////////////////////////////////////////////
33/// Constructor
34DbtPlyEngine::DbtPlyEngine(QString name)
35 : ComponentBase(name)
36 , mCurrentState(StoppedState::getInstance())
37 , mSpeed(kInitialSpeed)
38 , mIsReverse(false)
39{
40 LOG_TRACE("constructor");
41
42 sync_ = new QSemaphore(1);
43 sync_->acquire();
44 direction_ = 1;
45 tMinMaxSem_ = new QSemaphore(1);
46 tMinMaxSem_->release();
47 tDbtMin_ = numeric_limits<road_time_t>::max();
48 tDbtMax_ = 0;
49
50 namespace po = boost::program_options;
51 addParameters()
52 ("datadir", po::value<string>(&mDataDir)->required(), "set root data directory")
53 ("replay_mode", po::value<unsigned>(&reinterpret_cast<unsigned&>(replayMode_))->default_value(kDefaultPlayMode), "set data replay mode")
54 ("log-config-file", po::value<string>(&mLoggerConfigFile), "set logger configuration file path")
55 ;
56}
57
58////////////////////////////////////////////////////////////////////////////////
59/// Destructor.
60DbtPlyEngine::~DbtPlyEngine()
61{
62 LOG_TRACE("destructor");
63}
64
65void DbtPlyEngine::addInputs()
66{
67 // empty: no inputs
68}
69
70void DbtPlyEngine::addOutputs()
71{
72 // empty: no outputs
73}
74
75////////////////////////////////////////////////////////////////////////////////
76/// Returns the directory where the data are stored.
77/// @return QString::null if directory is invalid.
78QString DbtPlyEngine::getDataDir()
79{
80 return mDataDir.c_str();
81}
82
83////////////////////////////////////////////////////////////////////////////////
84/// Slot.
85/// called when trigger reaches a new timer event
86void DbtPlyEngine::engReceiver()
87{
88 sync_->release();
89}
90
91////////////////////////////////////////////////////////////////////////////////
92/// @returns @b true if the engine is in the PLAY_STATE, @b false otherwise
93bool DbtPlyEngine::isPlaying()
94{
95 return mCurrentState->isPlaying();
96}
97
98////////////////////////////////////////////////////////////////////////////////
99/// main loop of the thread
100/// entry point for a new thread after the invocation of QThread::start()
101void DbtPlyEngine::run()
102{
103 PACPUS_LOG_FUNCTION();
104
105 // We are waiting the first trigger signal to init some values
106 // before entering in the while loop
107 sync_->acquire();
108 lastTDbt_ = tDbtMin_;
109
110 // For statistics purpose
111 static const size_t kDeltaArraySize = 1000;
112 int deltaTDbtTab[kDeltaArraySize];
113 int i = 0;
114
115 while (isActive()) {
116 tNow_ = road_time();
117 float elapsedTime = tNow_ - lastTNow_;
118 int deltaTDbt = direction_ * elapsedTime * mSpeed;
119 tDbt_ = lastTDbt_ + deltaTDbt;
120 deltaTDbtTab[(i++) % kDeltaArraySize] = elapsedTime;
121
122 if ((tDbt_ <= tDbtMax_) && (tDbt_ >= tDbtMin_)) {
123 Q_EMIT play(tDbt_, tNow_, mIsReverse);
124 Q_EMIT curReplayTime(tDbt_);
125 lastTNow_ = tNow_ ;
126 lastTDbt_ = tDbt_ ;
127 } else {
128 lastTNow_ = tNow_ ;
129 lastTDbt_ = tDbt_ ;
130 LOG_INFO("Reading is finished.");
131
132 mCurrentState->stop(*this);
133 }
134
135 // The waiting is placed at the end due to the init before the while loop
136 sync_->acquire();
137 }
138}
139
140////////////////////////////////////////////////////////////////////////////////
141/// Slot.
142/// change the direction of replaying : normal or reverse
143void DbtPlyEngine::changeDirection(bool reverse)
144{
145 if (reverse) {
146 direction_ = -1;
147 mIsReverse = true;
148 } else {
149 direction_ = +1;
150 mIsReverse = false;
151 }
152}
153
154////////////////////////////////////////////////////////////////////////////////
155/// Configuration method of the engine
156/// called automatically by the component manager
157ComponentBase::COMPONENT_CONFIGURATION DbtPlyEngine::configureComponent(XmlComponentConfig config)
158{
159 {
160 // data directory: add path separator at end if needed
161 QString dataDir = mDataDir.c_str();
162 dataDir = QDir::toNativeSeparators(dataDir);
163 if (!dataDir.endsWith(QDir::separator())) {
164 dataDir += QDir::separator();
165 mDataDir = dataDir.toStdString();
166 }
167 }
168
169 ////////////////////////////////////////////////////////////////////////////////
170 // logger configuration
171 if (!mLoggerConfigFile.empty()) {
172 LOG_INFO("configuring logger with file '" << mLoggerConfigFile << "'");
173 LogConfigurator::configureLoggerWithFile(mLoggerConfigFile.c_str());
174 }
175
176 return ComponentBase::CONFIGURED_OK;
177}
178
179////////////////////////////////////////////////////////////////////////////////
180/// The start function of the engine
181void DbtPlyEngine::startActivity()
182{
183 LOG_INFO("Starting...");
184
185 setActive(true);
186 start();
187}
188
189////////////////////////////////////////////////////////////////////////////////
190/// The stop function of the engine
191void DbtPlyEngine::stopActivity()
192{
193 LOG_TRACE("stopping activity...");
194
195 setActive(false);
196}
197
198////////////////////////////////////////////////////////////////////////////////
199/// Slot.
200/// called by each file manager to provide min and max time of their data
201void DbtPlyEngine::tMinMax(road_time_t tMin, road_time_t tMax)
202{
203 tMinMaxSem_->acquire();
204 if (tDbtMin_>tMin) {
205 tDbtMin_= tMin;
206 }
207 if (tDbtMax_<tMax) {
208 tDbtMax_ = tMax;
209 }
210 Q_EMIT timeMinMax(tDbtMin_, tDbtMax_);
211 tMinMaxSem_->release();
212}
213
214////////////////////////////////////////////////////////////////////////////////
215/// Slot.
216/// called when the time slider is pressed
217void DbtPlyEngine::pauseEvent()
218{
219 LOG_DEBUG("Clicked: pause");
220 mCurrentState->pause(*this);
221}
222
223////////////////////////////////////////////////////////////////////////////////
224/// Slot.
225/// called when the time slider is released
226void DbtPlyEngine::playEvent()
227{
228 using boost::dynamic_pointer_cast;
229
230 LOG_DEBUG("Clicked: play");
231
232 // get user interface
233 ComponentManager * mgr = ComponentManager::getInstance();
234 boost::shared_ptr<DbtPlyUserInterface> ui = dynamic_pointer_cast<DbtPlyUserInterface>(mgr->getComponent("dbiteUserInterface"));
235 BOOST_ASSERT(ui);
236 // get time value from slider
237 lastTDbt_ = ui->getTime() + tDbtMin_;
238
239 mCurrentState->play(*this);
240}
241
242////////////////////////////////////////////////////////////////////////////////
243void DbtPlyEngine::stopEvent()
244{
245 LOG_DEBUG("Clicked: stop");
246 mCurrentState->stop(*this);
247}
248
249////////////////////////////////////////////////////////////////////////////////
250void DbtPlyEngine::speedUpEvent()
251{
252 LOG_DEBUG("Clicked: speed Up");
253 mCurrentState->speedUp(*this);
254}
255
256////////////////////////////////////////////////////////////////////////////////
257void DbtPlyEngine::speedDownEvent()
258{
259 LOG_DEBUG("Clicked: speed down");
260 mCurrentState->speedDown(*this);
261}
262
263////////////////////////////////////////////////////////////////////////////////
264const DbtPlyEngineState * DbtPlyEngine::getState()
265{
266 return mCurrentState;
267}
268
269////////////////////////////////////////////////////////////////////////////////
270void DbtPlyEngine::setState(DbtPlyEngineState * newState)
271{
272 BOOST_ASSERT(newState);
273 LOG_DEBUG(mCurrentState->toString() << " => " << newState->toString());
274 mCurrentState = newState;
275
276 Q_EMIT displayStateSig(mCurrentState, mSpeed);
277
278 LOG_DEBUG("tDbt = " << tDbt_ << "\tlastTDbt = " << lastTDbt_);
279 LOG_DEBUG("tNow = " << tNow_ << "\tlastTNow = " << lastTNow_);
280}
281
282void DbtPlyEngine::speedUp()
283{
284 mSpeed *= 2;
285 if (mSpeed > kMaxSpeed) {
286 mSpeed = kMaxSpeed;
287 }
288 BOOST_ASSERT(kMinSpeed <= mSpeed);
289 BOOST_ASSERT(mSpeed <= kMaxSpeed);
290 LOG_INFO("event: Speed Up, speed = " << mSpeed);
291 Q_EMIT displayStateSig(mCurrentState, mSpeed);
292}
293
294void DbtPlyEngine::speedDown()
295{
296 mSpeed /= 2;
297 if (mSpeed < kMinSpeed) {
298 mSpeed = kMinSpeed;
299 }
300 BOOST_ASSERT(kMinSpeed <= mSpeed);
301 BOOST_ASSERT(mSpeed <= kMaxSpeed);
302 LOG_INFO("event: Speed Up, speed = " << mSpeed);
303 Q_EMIT displayStateSig(mCurrentState, mSpeed);
304}
305
306void DbtPlyEngine::reset()
307{
308 using boost::dynamic_pointer_cast;
309
310 lastTNow_ = road_time();
311 lastTDbt_ = tDbtMin_;
312 mSpeed = kInitialSpeed;
313 Q_EMIT stopfile();
314
315 // get user interface
316 ComponentManager * mgr = ComponentManager::getInstance();
317 boost::shared_ptr<DbtPlyUserInterface> ui = dynamic_pointer_cast<DbtPlyUserInterface>(mgr->getComponent("dbiteUserInterface"));
318 BOOST_ASSERT(ui);
319 // reset time value of the slider
320 ui->resetTime();
321}
322
323} // namespace pacpus
Note: See TracBrowser for help on using the repository browser.