From 69de953d5e64d8bf4dec90bf34504e8bc96b014c Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Tue, 8 Jan 2019 18:41:55 +0100 Subject: [PATCH] make it more solid --- libnymea-core/cloud/awsconnector.cpp | 29 +++++++++++++++++++++++++--- libnymea-core/cloud/awsconnector.h | 2 ++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/libnymea-core/cloud/awsconnector.cpp b/libnymea-core/cloud/awsconnector.cpp index 0834e73b..605550a7 100644 --- a/libnymea-core/cloud/awsconnector.cpp +++ b/libnymea-core/cloud/awsconnector.cpp @@ -37,6 +37,9 @@ AWSConnector::AWSConnector(QObject *parent) : QObject(parent) { qRegisterMetaType(); m_clientName = readSyncedNameCache(); + + m_reconnectTimer.setSingleShot(true); + connect(&m_reconnectTimer, &QTimer::timeout, this, &AWSConnector::doConnect); } AWSConnector::~AWSConnector() @@ -69,6 +72,14 @@ void AWSConnector::connect2AWS(const QString &endpoint, const QString &clientId, void AWSConnector::doConnect() { + if (m_setupInProgress) { + qCWarning(dcAWS()) << "Connection attempt already in progress..."; + return; + } + if (isConnected()) { + qCWarning(dcAWS()) << "Already connected. Not connecting again..."; + return; + } m_setupInProgress = true; m_subscriptionCache.clear(); @@ -99,8 +110,12 @@ void AWSConnector::doConnect() connect(m_client, &MqttClient::connected, this, &AWSConnector::onConnected); connect(m_client, &MqttClient::disconnected, this, &AWSConnector::onDisconnected); - connect(m_client, &MqttClient::error, this, [](const QAbstractSocket::SocketError error){ + connect(m_client, &MqttClient::error, this, [this](const QAbstractSocket::SocketError error){ qCWarning(dcAWS()) << "An error happened in the MQTT transport" << error; + // In order to also call onDisconnected (and start the reconnect timer) even when we have never been connected + // we'll call it here. However, that might cause onDisconnected to be called twice. Let's prevent that. + disconnect(m_client, &MqttClient::disconnected, this, &AWSConnector::onDisconnected); + onDisconnected(); }); connect(m_client, &MqttClient::subscribed, this, &AWSConnector::onSubscribed); @@ -310,6 +325,7 @@ void AWSConnector::onDisconnected() bool needReRegistering = false; if (m_setupInProgress) { qCWarning(dcAWS()) << "Setup process interrupted by disconnect."; + m_setupInProgress = false; needReRegistering = true; } else { if (m_lastConnectionDrop.addSecs(60) > QDateTime::currentDateTime()) { @@ -331,8 +347,8 @@ void AWSConnector::onDisconnected() } if (m_shouldReconnect) { - qCDebug(dcAWS()) << "Reconnecting to AWS..."; - QTimer::singleShot(1000, this, &AWSConnector::doConnect); + qCDebug(dcAWS()) << "Reconnecting to AWS in 5 seconds..."; + m_reconnectTimer.start(5000); } } @@ -361,6 +377,13 @@ void AWSConnector::setName() void AWSConnector::subscribe(const QStringList &topics) { + // Note: Do not check for isConnected here because subscribing is part of the connection + // flow and it needs to work before we are actually connected. + if (!m_client) { + qCWarning(dcAWS()) << "Not connected. Cannot subscribe."; + return; + } + foreach (const QString &topic, topics) { if (m_subscriptionCache.contains(topic)) { qCDebug(dcAWS()) << "Already subscribed to topic:" << topic << ". Not resubscribing"; diff --git a/libnymea-core/cloud/awsconnector.h b/libnymea-core/cloud/awsconnector.h index fea3daea..c57bf55e 100644 --- a/libnymea-core/cloud/awsconnector.h +++ b/libnymea-core/cloud/awsconnector.h @@ -24,6 +24,7 @@ #include #include #include +#include #include @@ -107,6 +108,7 @@ private: QFuture m_connectingFuture; bool m_isCleanSession = true; bool m_shouldReconnect = false; + QTimer m_reconnectTimer; quint8 m_transactionId = 0; QString m_createDeviceId;