summaryrefslogtreecommitdiffstats
path: root/src/mqtt/qmqttclient.cpp
diff options
context:
space:
mode:
authorMaurice Kalinowski <maurice.kalinowski@qt.io>2017-10-16 13:01:29 +0200
committerMaurice Kalinowski <maurice.kalinowski@qt.io>2017-10-18 07:39:03 +0000
commitea0d792a44d7c6cbfdbeeed34b70c833a3157550 (patch)
tree61eef2657fef4b12087eda27c8ba6315209bdf85 /src/mqtt/qmqttclient.cpp
parent899cd2d31e4b3c71cc8614e5ee74532c670fb02c (diff)
Refactor state and error handling
Rename State to ClientState to avoid clashing names with other state management. Introduce ClientError to provide further information on occurring errors. Error values 1-5 are from the standard, followed by Qt specific error values. Change-Id: Ica1b703845e93e923fd1af4e3fb6ffb95de8390d Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
Diffstat (limited to 'src/mqtt/qmqttclient.cpp')
-rw-r--r--src/mqtt/qmqttclient.cpp72
1 files changed, 64 insertions, 8 deletions
diff --git a/src/mqtt/qmqttclient.cpp b/src/mqtt/qmqttclient.cpp
index 4d69e73..e76dbb2 100644
--- a/src/mqtt/qmqttclient.cpp
+++ b/src/mqtt/qmqttclient.cpp
@@ -111,6 +111,11 @@ QT_BEGIN_NAMESPACE
*/
/*!
+ \property QMqttClient::error
+ \brief Specifies the current error of the client.
+*/
+
+/*!
\property QMqttClient::username
\brief This property holds the user name for connecting to a broker.
*/
@@ -162,7 +167,7 @@ QT_BEGIN_NAMESPACE
*/
/*!
- \enum QMqttClient::State
+ \enum QMqttClient::ClientState
This enum type specifies the states a client can enter.
@@ -176,6 +181,31 @@ QT_BEGIN_NAMESPACE
*/
/*!
+ \enum QMqttClient::ClientError
+
+ This enum type specifies the error state of a client.
+
+ \value NoError
+ No Error occurred.
+ \value InvalidProtocolVersion
+ The broker does not accept a connection using the specified protocol version.
+ \value IdRejected
+ The client ID is malformed. This might be related to the length of the ID.
+ \value ServerUnavailable
+ The network connection has been establied, but the service is unavailable on the
+ broker side.
+ \value BadUsernameOrPassword
+ The data in the username or password is malformed.
+ \value NotAuthorized
+ The client is not authorized to connect
+ \value TransportInvalid
+ The underlying transport caused an error, eg. the connection has been interrupted
+ unexpectedly.
+ \value UnknownError
+ An unknown error occurred.
+*/
+
+/*!
\enum QMqttClient::ProtocolVersion
This enum specifies the protocol version of the MQTT standard to use during
@@ -240,7 +270,7 @@ QT_BEGIN_NAMESPACE
QMqttClient::QMqttClient(QObject *parent) : QObject(*(new QMqttClientPrivate), parent)
{
Q_D(QMqttClient);
- d->m_connection.setClient(this);
+ d->m_connection.setClient(this, d);
}
/*!
@@ -382,21 +412,21 @@ void QMqttClient::connectToHost(bool encrypted, const QString &sslPeerName)
if (!d->m_connection.ensureTransport(encrypted)) {
qWarning("Could not ensure connection");
- setState(Disconnected);
+ d->setStateAndError(Disconnected, TransportInvalid);
return;
}
- setState(Connecting);
+ d->setStateAndError(Connecting);
if (!d->m_connection.ensureTransportOpen(sslPeerName)) {
qWarning("Could not ensure that connection is open");
- setState(Disconnected);
+ d->setStateAndError(Disconnected, TransportInvalid);
return;
}
if (!d->m_connection.sendControlConnect()) {
qWarning("Could not send CONNECT to broker");
// ### Who disconnects now? Connection or client?
- setState(Disconnected);
+ d->setStateAndError(Disconnected, TransportInvalid);
return;
}
}
@@ -414,7 +444,7 @@ void QMqttClient::disconnectFromHost()
d->m_connection.sendControlDisconnect();
}
-QMqttClient::State QMqttClient::state() const
+QMqttClient::ClientState QMqttClient::state() const
{
Q_D(const QMqttClient);
return d->m_state;
@@ -462,6 +492,12 @@ bool QMqttClient::willRetain() const
return d->m_willRetain;
}
+QMqttClient::ClientError QMqttClient::error() const
+{
+ Q_D(const QMqttClient);
+ return d->m_error;
+}
+
QMqttClient::ProtocolVersion QMqttClient::protocolVersion() const
{
Q_D(const QMqttClient);
@@ -539,7 +575,7 @@ void QMqttClient::setProtocolVersion(ProtocolVersion protocolVersion)
emit protocolVersionChanged(protocolVersion);
}
-void QMqttClient::setState(QMqttClient::State state)
+void QMqttClient::setState(ClientState state)
{
Q_D(QMqttClient);
if (d->m_state == state)
@@ -623,6 +659,16 @@ void QMqttClient::setWillRetain(bool willRetain)
emit willRetainChanged(willRetain);
}
+void QMqttClient::setError(ClientError e)
+{
+ Q_D(QMqttClient);
+ if (d->m_error == e)
+ return;
+
+ d->m_error = e;
+ emit errorChanged(d->m_error);
+}
+
QMqttClientPrivate::QMqttClientPrivate()
: QObjectPrivate()
{
@@ -637,4 +683,14 @@ QMqttClientPrivate::~QMqttClientPrivate()
{
}
+void QMqttClientPrivate::setStateAndError(QMqttClient::ClientState s, QMqttClient::ClientError e)
+{
+ Q_Q(QMqttClient);
+
+ if (s != m_state)
+ q->setState(s);
+ if (m_error != QMqttClient::NoError && m_error != e)
+ q->setError(e);
+}
+
QT_END_NAMESPACE