mirror of
https://github.com/overte-org/overte.git
synced 2025-04-20 04:44:11 +02:00
Merge pull request #6718 from birarda/master
cleanup QTimer management in ThreadedAssignment
This commit is contained in:
commit
0df920d9a3
2 changed files with 24 additions and 51 deletions
|
@ -20,10 +20,16 @@
|
|||
|
||||
ThreadedAssignment::ThreadedAssignment(ReceivedMessage& message) :
|
||||
Assignment(message),
|
||||
_isFinished(false)
|
||||
|
||||
_isFinished(false),
|
||||
_domainServerTimer(this),
|
||||
_statsTimer(this)
|
||||
{
|
||||
static const int STATS_TIMEOUT_MS = 1000;
|
||||
_statsTimer.setInterval(STATS_TIMEOUT_MS);
|
||||
connect(&_statsTimer, &QTimer::timeout, this, &ThreadedAssignment::sendStatsPacket);
|
||||
|
||||
connect(&_domainServerTimer, &QTimer::timeout, this, &ThreadedAssignment::checkInWithDomainServerOrExit);
|
||||
_domainServerTimer.setInterval(DOMAIN_SERVER_CHECK_IN_MSECS);
|
||||
}
|
||||
|
||||
void ThreadedAssignment::setFinished(bool isFinished) {
|
||||
|
@ -47,16 +53,9 @@ void ThreadedAssignment::setFinished(bool isFinished) {
|
|||
// send a disconnect packet to the domain
|
||||
nodeList->getDomainHandler().disconnect();
|
||||
|
||||
if (_domainServerTimer) {
|
||||
// stop the domain-server check in timer by calling deleteLater so it gets cleaned up on NL thread
|
||||
_domainServerTimer->deleteLater();
|
||||
_domainServerTimer = nullptr;
|
||||
}
|
||||
|
||||
if (_statsTimer) {
|
||||
_statsTimer->deleteLater();
|
||||
_statsTimer = nullptr;
|
||||
}
|
||||
// stop our owned timers
|
||||
_domainServerTimer.stop();
|
||||
_statsTimer.stop();
|
||||
|
||||
// call our virtual aboutToFinish method - this gives the ThreadedAssignment subclass a chance to cleanup
|
||||
aboutToFinish();
|
||||
|
@ -66,30 +65,22 @@ void ThreadedAssignment::setFinished(bool isFinished) {
|
|||
}
|
||||
}
|
||||
|
||||
void ThreadedAssignment::commonInit(const QString& targetName, NodeType_t nodeType, bool shouldSendStats) {
|
||||
void ThreadedAssignment::commonInit(const QString& targetName, NodeType_t nodeType) {
|
||||
// change the logging target name while the assignment is running
|
||||
LogHandler::getInstance().setTargetName(targetName);
|
||||
|
||||
auto nodeList = DependencyManager::get<NodeList>();
|
||||
nodeList->setOwnerType(nodeType);
|
||||
|
||||
_domainServerTimer = new QTimer;
|
||||
connect(_domainServerTimer, SIGNAL(timeout()), this, SLOT(checkInWithDomainServerOrExit()));
|
||||
_domainServerTimer->start(DOMAIN_SERVER_CHECK_IN_MSECS);
|
||||
|
||||
// send a domain-server check in immediately
|
||||
// send a domain-server check in immediately and start the timer to fire them every DOMAIN_SERVER_CHECK_IN_MSECS
|
||||
checkInWithDomainServerOrExit();
|
||||
|
||||
// move the domain server time to the NL so check-ins fire from there
|
||||
_domainServerTimer->moveToThread(nodeList->thread());
|
||||
_domainServerTimer.start();
|
||||
|
||||
if (shouldSendStats) {
|
||||
// start sending stats packet once we connect to the domain
|
||||
connect(&nodeList->getDomainHandler(), &DomainHandler::connectedToDomain, this, &ThreadedAssignment::startSendingStats);
|
||||
|
||||
// stop sending stats if we disconnect
|
||||
connect(&nodeList->getDomainHandler(), &DomainHandler::disconnectedFromDomain, this, &ThreadedAssignment::stopSendingStats);
|
||||
}
|
||||
// start sending stats packet once we connect to the domain
|
||||
connect(&nodeList->getDomainHandler(), SIGNAL(connectedToDomain(const QString&)), &_statsTimer, SLOT(start()));
|
||||
|
||||
// stop sending stats if we disconnect
|
||||
connect(&nodeList->getDomainHandler(), &DomainHandler::disconnectedFromDomain, &_statsTimer, &QTimer::stop);
|
||||
}
|
||||
|
||||
void ThreadedAssignment::addPacketStatsAndSendStatsPacket(QJsonObject &statsObject) {
|
||||
|
@ -111,28 +102,12 @@ void ThreadedAssignment::sendStatsPacket() {
|
|||
addPacketStatsAndSendStatsPacket(statsObject);
|
||||
}
|
||||
|
||||
void ThreadedAssignment::startSendingStats() {
|
||||
// send the stats packet every 1s
|
||||
if (!_statsTimer) {
|
||||
_statsTimer = new QTimer;
|
||||
connect(_statsTimer, &QTimer::timeout, this, &ThreadedAssignment::sendStatsPacket);
|
||||
}
|
||||
|
||||
_statsTimer->start(1000);
|
||||
}
|
||||
|
||||
void ThreadedAssignment::stopSendingStats() {
|
||||
if (_statsTimer) {
|
||||
// stop sending stats, we just disconnected from domain
|
||||
_statsTimer->stop();
|
||||
}
|
||||
}
|
||||
|
||||
void ThreadedAssignment::checkInWithDomainServerOrExit() {
|
||||
if (DependencyManager::get<NodeList>()->getNumNoReplyDomainCheckIns() == MAX_SILENT_DOMAIN_SERVER_CHECK_INS) {
|
||||
setFinished(true);
|
||||
} else {
|
||||
DependencyManager::get<NodeList>()->sendDomainServerCheckIn();
|
||||
auto nodeList = DependencyManager::get<NodeList>();
|
||||
QMetaObject::invokeMethod(nodeList.data(), "sendDomainServerCheckIn");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -38,17 +38,15 @@ signals:
|
|||
void finished();
|
||||
|
||||
protected:
|
||||
void commonInit(const QString& targetName, NodeType_t nodeType, bool shouldSendStats = true);
|
||||
void commonInit(const QString& targetName, NodeType_t nodeType);
|
||||
bool _isFinished;
|
||||
QTimer* _domainServerTimer = nullptr;
|
||||
QTimer* _statsTimer = nullptr;
|
||||
QTimer _domainServerTimer;
|
||||
QTimer _statsTimer;
|
||||
|
||||
protected slots:
|
||||
void domainSettingsRequestFailed();
|
||||
|
||||
private slots:
|
||||
void startSendingStats();
|
||||
void stopSendingStats();
|
||||
void checkInWithDomainServerOrExit();
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue