From 49ea560e4e2fec110bb4c2e29376a4fc1b4aae21 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 21 Dec 2015 16:33:40 -0700 Subject: [PATCH 1/4] make QTimers in ThreadedAssignment not be raw pointers --- .../networking/src/ThreadedAssignment.cpp | 63 ++++++------------- libraries/networking/src/ThreadedAssignment.h | 8 +-- 2 files changed, 22 insertions(+), 49 deletions(-) diff --git a/libraries/networking/src/ThreadedAssignment.cpp b/libraries/networking/src/ThreadedAssignment.cpp index 2f5ab7a015..bdf2518de8 100644 --- a/libraries/networking/src/ThreadedAssignment.cpp +++ b/libraries/networking/src/ThreadedAssignment.cpp @@ -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,10 @@ 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; - } + // stop our owned timers - if (_statsTimer) { - _statsTimer->deleteLater(); - _statsTimer = nullptr; - } + _domainServerTimer.stop(); + _statsTimer.stop(); // call our virtual aboutToFinish method - this gives the ThreadedAssignment subclass a chance to cleanup aboutToFinish(); @@ -66,30 +66,21 @@ 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->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 checkInWithDomainServerOrExit(); - - // move the domain server time to the NL so check-ins fire from there - _domainServerTimer->moveToThread(nodeList->thread()); - 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()), &_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()->getNumNoReplyDomainCheckIns() == MAX_SILENT_DOMAIN_SERVER_CHECK_INS) { setFinished(true); } else { - DependencyManager::get()->sendDomainServerCheckIn(); + auto nodeList = DependencyManager::get(); + QMetaObject::invokeMethod(nodeList.data(), "sendDomainServerCheckIn"); } } diff --git a/libraries/networking/src/ThreadedAssignment.h b/libraries/networking/src/ThreadedAssignment.h index 87d503d2bf..42d4903c2f 100644 --- a/libraries/networking/src/ThreadedAssignment.h +++ b/libraries/networking/src/ThreadedAssignment.h @@ -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(); }; From 9e2d06710feb773a06bf86cd869340edeedc1380 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 21 Dec 2015 16:37:22 -0700 Subject: [PATCH 2/4] fix for connectedToDomain signal signature --- libraries/networking/src/ThreadedAssignment.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/networking/src/ThreadedAssignment.cpp b/libraries/networking/src/ThreadedAssignment.cpp index bdf2518de8..b8f8f5697f 100644 --- a/libraries/networking/src/ThreadedAssignment.cpp +++ b/libraries/networking/src/ThreadedAssignment.cpp @@ -77,7 +77,7 @@ void ThreadedAssignment::commonInit(const QString& targetName, NodeType_t nodeTy checkInWithDomainServerOrExit(); // start sending stats packet once we connect to the domain - connect(&nodeList->getDomainHandler(), SIGNAL(connectedToDomain()), &_statsTimer, SLOT(start())); + connect(&nodeList->getDomainHandler(), SIGNAL(connectedToDomain(const QString&)), &_statsTimer, SLOT(start())); // stop sending stats if we disconnect connect(&nodeList->getDomainHandler(), &DomainHandler::disconnectedFromDomain, &_statsTimer, &QTimer::stop); From 74d168561749e38cb59635ae8fe4ceb400ec9f06 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 21 Dec 2015 16:45:33 -0700 Subject: [PATCH 3/4] remove an extra space --- libraries/networking/src/ThreadedAssignment.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/libraries/networking/src/ThreadedAssignment.cpp b/libraries/networking/src/ThreadedAssignment.cpp index b8f8f5697f..9ec07bc5d6 100644 --- a/libraries/networking/src/ThreadedAssignment.cpp +++ b/libraries/networking/src/ThreadedAssignment.cpp @@ -54,7 +54,6 @@ void ThreadedAssignment::setFinished(bool isFinished) { nodeList->getDomainHandler().disconnect(); // stop our owned timers - _domainServerTimer.stop(); _statsTimer.stop(); From a0d6001fa506c1cd78113befab5e6f08e728da10 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 28 Dec 2015 10:54:42 -0800 Subject: [PATCH 4/4] actually start domain-server check in timer --- libraries/networking/src/ThreadedAssignment.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libraries/networking/src/ThreadedAssignment.cpp b/libraries/networking/src/ThreadedAssignment.cpp index 9ec07bc5d6..303755c8f3 100644 --- a/libraries/networking/src/ThreadedAssignment.cpp +++ b/libraries/networking/src/ThreadedAssignment.cpp @@ -72,8 +72,9 @@ void ThreadedAssignment::commonInit(const QString& targetName, NodeType_t nodeTy auto nodeList = DependencyManager::get(); nodeList->setOwnerType(nodeType); - // 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(); + _domainServerTimer.start(); // start sending stats packet once we connect to the domain connect(&nodeList->getDomainHandler(), SIGNAL(connectedToDomain(const QString&)), &_statsTimer, SLOT(start()));