Revert "Use fabs() for floats, not abs()".
[agianapa.git] / socg / workerthread.cpp
blobe82cc13a0ab6675a937c833c811dd498e974a6e9
1 #include <cstdlib>
2 #include <iostream>
3 #include <QDebug>
5 #include "workerthread.h"
7 WorkerThread::WorkerThread(QObject *parent)
8 : QThread(parent)
10 qDebug("WorkerThread::WorkerThread()");
12 // Initialize random generator here
13 srand(time(NULL));
15 // Make thread work forever by default
16 m_abort = false;
18 // At this point the class hasn't been fully initialized.
19 // The user must call setFileName(), before run()'ing.
22 WorkerThread::~WorkerThread()
24 qDebug("WorkerThread::~WorkerThread()");
26 m_mutex.lock();
27 m_abort = true;
28 condition.wakeOne();
29 m_mutex.unlock();
31 m_file.close();
33 wait();
36 void WorkerThread::run(void)
38 float r;
39 qDebug("WorkerThread::run()");
41 // Loop
42 while (!m_abort) {
43 m_mutex.lock();
44 if (m_data.size() >= 2007) {
45 qDebug("BLOCKED");
46 condition.wait(&m_mutex);
47 m_mutex.unlock();
49 else {
50 qDebug("Reading from file");
51 in >> r;
52 if (in.status() == QDataStream::ReadPastEnd) {
53 qDebug("Datastream has read past end");
54 m_abort = true;
55 m_mutex.unlock();
56 return;
58 m_data.enqueue(r);
59 m_mutex.unlock();
64 void WorkerThread::setFileName(QString fileName)
66 qDebug("WorkerThread::setFileName()");
68 // Close old file
69 m_file.close();
71 // Caution: discard all old data in QQueue
72 // Worst case scenario if you don't: frame read corruption
73 m_data.clear();
75 // Open new file
76 // XXX: toLatin1() segfaults
77 m_file.setFileName(fileName);
78 if (!m_file.open(QIODevice::ReadOnly)) {
79 qDebug("Cannot open file name: %s", fileName.toLatin1());
80 return;
83 in.setDevice(&m_file);
84 extractFileHeader();
87 quint32 WorkerThread::getMagicVersion(void) const
89 qDebug("WorkerThread::getMagicVersion()");
91 return m_magicVersion;
94 quint32 WorkerThread::getProtocolVersion(void) const
96 qDebug("WorkerThread::getProtocolVersion()");
98 return m_protocolVersion;
101 float WorkerThread::getDurationInSec(void) const
103 qDebug("WorkerThread::getDurationInSec()");
105 return m_durationInSec;
108 quint32 WorkerThread::getNumOfRecords(void) const
110 qDebug("WorkerThread::getNumOfRecords()");
112 return m_numOfRecords;
115 void WorkerThread::extractFileHeader(void)
117 qDebug("WorkerThread::extractFileHeader()");
119 in >> m_magicVersion;
120 //Q_ASSERT(m_magicVersion == 0xA0B0C0D0);
122 in >> m_protocolVersion;
123 //Q_ASSERT(m_protocolVersion == 0x1);
125 in >> m_durationInSec;
126 in >> m_numOfRecords;
128 // qDebug() adds an extra space between items,
129 // plus a new line in the last one.
130 qDebug() << "Magic version =" << hex << m_magicVersion;
131 qDebug() << "Protocol version =" << m_protocolVersion;
132 qDebug() << "Duration in sec = " << m_durationInSec;
133 qDebug() << "Number of records =" << m_numOfRecords;