1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-29 19:28:47 -05:00

Update threading model in Simple VOR localizer feature. Part of #1346

This commit is contained in:
f4exb 2022-09-24 12:41:05 +02:00
parent b1e91e9dd2
commit 31edca0a6b
4 changed files with 67 additions and 31 deletions

View File

@ -48,11 +48,12 @@ const char* const VORLocalizer::m_featureIdURI = "sdrangel.feature.vorlocalizer"
const char* const VORLocalizer::m_featureId = "VORLocalizer";
VORLocalizer::VORLocalizer(WebAPIAdapterInterface *webAPIAdapterInterface) :
Feature(m_featureIdURI, webAPIAdapterInterface)
Feature(m_featureIdURI, webAPIAdapterInterface),
m_thread(nullptr),
m_worker(nullptr),
m_running(false)
{
setObjectName(m_featureId);
m_worker = new VorLocalizerWorker(webAPIAdapterInterface);
m_worker->moveToThread(&m_thread);
m_state = StIdle;
m_errorMessage = "VORLocalizer error";
m_networkManager = new QNetworkAccessManager();
@ -85,35 +86,67 @@ VORLocalizer::~VORLocalizer()
&VORLocalizer::networkManagerFinished
);
delete m_networkManager;
if (m_worker->isRunning()) {
stop();
}
delete m_worker;
}
void VORLocalizer::start()
{
qDebug("VORLocalizer::start");
QMutexLocker m_lock(&m_mutex);
if (m_running) {
return;
}
qDebug("VORLocalizer::start");
m_thread = new QThread();
m_worker = new VorLocalizerWorker(getWebAPIAdapterInterface());
m_worker->moveToThread(m_thread);
QObject::connect(
m_thread,
&QThread::started,
m_worker,
&VorLocalizerWorker::startWork
);
QObject::connect(
m_thread,
&QThread::finished,
m_worker,
&QObject::deleteLater
);
QObject::connect(
m_thread,
&QThread::finished,
m_thread,
&QThread::deleteLater
);
m_worker->reset();
m_worker->setMessageQueueToFeature(getInputMessageQueue());
m_worker->setAvailableChannels(&m_availableChannels);
bool ok = m_worker->startWork();
m_state = ok ? StRunning : StError;
m_thread.start();
m_worker->startWork();
m_state = StRunning;
m_thread->start();
VorLocalizerWorker::MsgConfigureVORLocalizerWorker *msg = VorLocalizerWorker::MsgConfigureVORLocalizerWorker::create(m_settings, true);
m_worker->getInputMessageQueue()->push(msg);
m_running = true;
}
void VORLocalizer::stop()
{
QMutexLocker m_lock(&m_mutex);
if (!m_running) {
return;
}
qDebug("VORLocalizer::stop");
m_running = false;
m_worker->stopWork();
m_state = StIdle;
m_thread.quit();
m_thread.wait();
m_thread->quit();
m_thread->wait();
}
bool VORLocalizer::handleMessage(const Message& cmd)
@ -334,10 +367,13 @@ void VORLocalizer::applySettings(const VORLocalizerSettings& settings, bool forc
reverseAPIKeys.append("centerShift");
}
if (m_running)
{
VorLocalizerWorker::MsgConfigureVORLocalizerWorker *msg = VorLocalizerWorker::MsgConfigureVORLocalizerWorker::create(
settings, force
);
m_worker->getInputMessageQueue()->push(msg);
}
if (settings.m_useReverseAPI)
{
@ -723,6 +759,9 @@ void VORLocalizer::notifyUpdateChannels()
getMessageQueueToGUI()->push(msgToGUI);
}
if (m_running)
{
VorLocalizerWorker::MsgRefreshChannels *msgToWorker = VorLocalizerWorker::MsgRefreshChannels::create();
m_worker->getInputMessageQueue()->push(msgToWorker);
}
}

View File

@ -18,7 +18,7 @@
#ifndef INCLUDE_FEATURE_VORLOCALIZER_H_
#define INCLUDE_FEATURE_VORLOCALIZER_H_
#include <QThread>
#include <QRecursiveMutex>
#include <QNetworkRequest>
#include "feature/feature.h"
@ -31,6 +31,7 @@ class WebAPIAdapterInterface;
class VorLocalizerWorker;
class QNetworkAccessManager;
class QNetworkReply;
class QThread;
namespace SWGSDRangel {
class SWGDeviceState;
@ -199,8 +200,10 @@ private:
VORChannelReport& operator=(const VORChannelReport&) = default;
};
QThread m_thread;
QThread *m_thread;
VorLocalizerWorker *m_worker;
bool m_running;
QRecursiveMutex m_mutex;
VORLocalizerSettings m_settings;
QHash<ChannelAPI*, VORLocalizerSettings::AvailableChannel> m_availableChannels;
QHash<int, VORChannelReport> m_vorChannelReports;

View File

@ -45,7 +45,6 @@ VorLocalizerWorker::VorLocalizerWorker(WebAPIAdapterInterface *webAPIAdapterInte
m_webAPIAdapterInterface(webAPIAdapterInterface),
m_msgQueueToFeature(nullptr),
m_availableChannels(nullptr),
m_running(false),
m_updateTimer(this),
m_rrTimer(this)
{
@ -64,15 +63,13 @@ void VorLocalizerWorker::reset()
m_inputMessageQueue.clear();
}
bool VorLocalizerWorker::startWork()
void VorLocalizerWorker::startWork()
{
QMutexLocker mutexLocker(&m_mutex);
connect(&m_inputMessageQueue, SIGNAL(messageEnqueued()), this, SLOT(handleInputMessages()));
connect(&m_rrTimer, SIGNAL(timeout()), this, SLOT(rrNextTurn()));
connect(thread(), SIGNAL(started()), this, SLOT(started()));
connect(thread(), SIGNAL(finished()), this, SLOT(finished()));
m_running = true;
return m_running;
}
// startWork() is called from main thread. Timers/sockets need to be started on worker thread
@ -93,7 +90,6 @@ void VorLocalizerWorker::finished()
m_rrTimer.stop();
disconnect(&m_rrTimer, SIGNAL(timeout()), this, SLOT(rrNextTurn()));
disconnect(thread(), SIGNAL(finished()), this, SLOT(finished()));
m_running = false;
}
void VorLocalizerWorker::handleInputMessages()

View File

@ -74,9 +74,8 @@ public:
VorLocalizerWorker(WebAPIAdapterInterface *webAPIAdapterInterface);
~VorLocalizerWorker();
void reset();
bool startWork();
void startWork();
void stopWork();
bool isRunning() const { return m_running; }
MessageQueue *getInputMessageQueue() { return &m_inputMessageQueue; }
void setMessageQueueToFeature(MessageQueue *messageQueue) { m_msgQueueToFeature = messageQueue; }
void setAvailableChannels(QHash<ChannelAPI*, VORLocalizerSettings::AvailableChannel> *avaialbleChannels) {
@ -146,7 +145,6 @@ private:
QList<VORLocalizerSettings::VORChannel> m_vorChannels;
QHash<int, ChannelAllocation> m_channelAllocations;
QHash<ChannelAPI*, VORLocalizerSettings::AvailableChannel> *m_availableChannels;
bool m_running;
QTimer m_updateTimer;
QRecursiveMutex m_mutex;
QTimer m_rrTimer;