Crazy commit
[agianapa.git] / qt / sphere-orbit-camera-gui / workerthread.cpp
blobd1776d7c04ccf9d27854472c6529b65bbf8c0d1b
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 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 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 (!abort) {
44 m_mutex.lock();
45 if (m_data.size() >= 6667) {
46 qDebug("BLOCKED");
47 condition.wait(&m_mutex);
48 m_mutex.unlock();
50 else {
51 qDebug("Reading from file");
52 in >> r;
53 m_data.enqueue(r);
54 m_mutex.unlock();
59 void WorkerThread::setFileName(QString fileName)
61 qDebug("WorkerThread::setFileName()");
63 // Close old file
64 m_file.close();
66 // Caution: discard all old data in QQueue
67 // Worst case scenario if you don't: frame read corruption
68 m_data.clear();
70 // Open new one
71 m_file.setFileName(fileName);
72 m_file.open(QIODevice::ReadOnly);
73 in.setDevice(&m_file);
74 extractFileHeader();
77 quint32 WorkerThread::getMagicVersion(void) const
79 qDebug("WorkerThread::getMagicVersion()");
81 return m_magicVersion;
84 quint32 WorkerThread::getProtocolVersion(void) const
86 qDebug("WorkerThread::getProtocolVersion()");
88 return m_protocolVersion;
91 float WorkerThread::getDuration(void) const
93 qDebug("WorkerThread::getDuration()");
95 return m_duration;
98 quint32 WorkerThread::getNumOfRecords(void) const
100 qDebug("WorkerThread::getNumOfRecords()");
102 return m_numOfRecords;
105 void WorkerThread::extractFileHeader(void)
107 qDebug("WorkerThread::extractFileHeader()");
109 in >> m_magicVersion;
110 //Q_ASSERT(m_magicVersion == 0xA0B0C0D0);
112 in >> m_protocolVersion;
113 //Q_ASSERT(m_protocolVersion == 0x1);
115 in >> m_duration;
116 in >> m_numOfRecords;
118 // qDebug() adds an extra space between items,
119 // plus a new line in the last one.
120 qDebug() << "Magic version =" << hex << m_magicVersion;
121 qDebug() << "Protocol version =" << m_protocolVersion;
122 qDebug() << "Duration in msec = " << m_duration;
123 qDebug() << "Number of records =" << m_numOfRecords;