#include "consoleworker.h" #include "copythread.h" #include "datasource/cpuload.h" #include "datasource/networkspeed.h" #include #include #include ConsoleWorker::ConsoleWorker(QString fileName) : _thread(nullptr), _fileName(fileName), _networkSource(nullptr) { _timer.setInterval(200); connect(&_timer, &QTimer::timeout, this, &ConsoleWorker::updateTimer); QTimer::singleShot(1, [this]() { QFile *file = new QFile(_fileName); if (!file->open(QIODevice::ReadOnly)) { puts(tr("Could not open %1 for reading.").arg(_fileName).toLocal8Bit().constData()); return; } _networkSource = new NetworkSpeed(); _thread = new CopyThread(file, this); connect(_thread, &CopyThread::logMessage, this, &ConsoleWorker::logMessage, Qt::QueuedConnection); connect(_thread, &CopyThread::finished, this, &ConsoleWorker::testFinished, Qt::QueuedConnection); _timer.start(); _thread->start(); }); } ConsoleWorker::~ConsoleWorker() { delete _networkSource; } void ConsoleWorker::logMessage(CopyThread::LogMessageId msgId, QString message) { if (msgId == CopyThread::TestRandomStart) { printf("+SEQ"); } else if (msgId == CopyThread::TestFinished) { printf("+RND"); } if (msgId == CopyThread::TestRandomStart || msgId == CopyThread::TestFinished) { for (int i : _series) { printf(",%i", i); } putchar('\n'); } _series.clear(); puts(message.toLocal8Bit().constData()); } void ConsoleWorker::testFinished() { _timer.stop(); delete _thread; _thread = NULL; QCoreApplication::instance()->quit(); } void ConsoleWorker::updateTimer() { int i = (int)_networkSource->read(); _series.append(i); }