duration -> durationInSec
[agianapa.git] / socg / workerthread.cpp
blobddcf1d46ff29e17de1337004fdba4089c966f0fd
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;
40 qDebug("WorkerThread::run()");
42 // Loop
43 while (!m_abort) {
44 m_mutex.lock();
45 if (m_data.size() >= 2007) {
46 qDebug("BLOCKED");
47 condition.wait(&m_mutex);
48 m_mutex.unlock();
50 else {
51 qDebug("Reading from file");
52 in >> r;
53 if (in.status() == QDataStream::ReadPastEnd) {
54 qDebug("Datastream has read past end");
55 m_abort = true;
56 m_mutex.unlock();
57 return;
59 m_data.enqueue(r);
60 m_mutex.unlock();
65 void WorkerThread::setFileName(QString fileName)
67 qDebug("WorkerThread::setFileName()");
69 // Close old file
70 m_file.close();
72 // Caution: discard all old data in QQueue
73 // Worst case scenario if you don't: frame read corruption
74 m_data.clear();
76 // Open new file
77 // XXX: toLatin1() segfaults
78 m_file.setFileName(fileName);
79 if (!m_file.open(QIODevice::ReadOnly)) {
80 qDebug("Cannot open file name: %s", fileName.toLatin1());
81 return;
84 in.setDevice(&m_file);
85 extractFileHeader();
88 quint32 WorkerThread::getMagicVersion(void) const
90 qDebug("WorkerThread::getMagicVersion()");
92 return m_magicVersion;
95 quint32 WorkerThread::getProtocolVersion(void) const
97 qDebug("WorkerThread::getProtocolVersion()");
99 return m_protocolVersion;
102 float WorkerThread::getDurationInSec(void) const
104 qDebug("WorkerThread::getDurationInSec()");
106 return m_durationInSec;
109 quint32 WorkerThread::getNumOfRecords(void) const
111 qDebug("WorkerThread::getNumOfRecords()");
113 return m_numOfRecords;
116 void WorkerThread::extractFileHeader(void)
118 qDebug("WorkerThread::extractFileHeader()");
120 in >> m_magicVersion;
121 //Q_ASSERT(m_magicVersion == 0xA0B0C0D0);
123 in >> m_protocolVersion;
124 //Q_ASSERT(m_protocolVersion == 0x1);
126 in >> m_durationInSec;
127 in >> m_numOfRecords;
129 // qDebug() adds an extra space between items,
130 // plus a new line in the last one.
131 qDebug() << "Magic version =" << hex << m_magicVersion;
132 qDebug() << "Protocol version =" << m_protocolVersion;
133 qDebug() << "Duration in sec = " << m_durationInSec;
134 qDebug() << "Number of records =" << m_numOfRecords;