diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2023-06-15 13:04:30 +0200 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2023-06-21 10:31:38 +0200 |
commit | ccad38f304f5e3b8d25585330ec14527d39e290d (patch) | |
tree | e8ebf3056a10e144478641cf354423e3c379bf48 /examples | |
parent | f2d50cef7ef84cf8b3b319143183100f61dd584b (diff) |
CoAP Multicast Discovery example: allow to stop discovery and clear the discovered resources
Multicast discovery takes several minutes by default, and so far the
example did not provide any means to stop it.
Fix it by introducing a new 'isDiscovering' property which allows to
check the current state of the client from QML, and a new invokable
'stopDiscovery()' method, which aborts the current request.
While on it, also delete the finished replies explicitly by calling
deleteLater(). Previously the replies were deleted only when the
parent QmlCoapMulticastClient was destroyed.
On QML side, adjust the discoverButton logic, so that it can start or
stop the discovery, depending on the current state.
Also introduce a new button which allows to clear all the discovered
resources.
Task-number: QTBUG-113858
Pick-to: 6.6 6.5
Change-Id: I7ec3bc2fd5fee1b3c589de74b00862ffb9d6983e
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Diffstat (limited to 'examples')
3 files changed, 71 insertions, 22 deletions
diff --git a/examples/coap/quickmulticastclient/Main.qml b/examples/coap/quickmulticastclient/Main.qml index 86dbd48..129978a 100644 --- a/examples/coap/quickmulticastclient/Main.qml +++ b/examples/coap/quickmulticastclient/Main.qml @@ -123,26 +123,39 @@ Window { Button { id: discoverButton - text: qsTr("Discover") - Layout.columnSpan: 2 + text: client.isDiscovering ? qsTr("Stop Discovery") : qsTr("Discover") + Layout.preferredWidth: 100 onClicked: { - var currentGroup = groupComboBox.model.get(groupComboBox.currentIndex).value; - - var path = ""; - if (currentGroup !== - 1) { - client.discover(currentGroup, parseInt(portField.text), - discoveryPathField.text); - path = groupComboBox.currentText; + if (client.isDiscovering) { + client.stopDiscovery() } else { - client.discover(customGroupField.text, parseInt(portField.text), - discoveryPathField.text); - path = customGroupField.text + discoveryPathField.text; + var currentGroup = groupComboBox.model.get(groupComboBox.currentIndex).value; + + var path = ""; + if (currentGroup !== - 1) { + client.discover(currentGroup, parseInt(portField.text), + discoveryPathField.text); + path = groupComboBox.currentText; + } else { + client.discover(customGroupField.text, parseInt(portField.text), + discoveryPathField.text); + path = customGroupField.text + discoveryPathField.text; + } + statusLabel.text = qsTr("Discovering resources at %1...").arg(path); } - statusLabel.text = qsTr("Discovering resources at %1...").arg(path); } } + Button { + id: clearButton + text: qsTr("Clear") + enabled: resourceModel.count !== 0 + Layout.preferredWidth: 100 + + onClicked: resourceModel.clear() + } + ListModel { id: resourceModel } diff --git a/examples/coap/quickmulticastclient/qmlcoapmulticastclient.cpp b/examples/coap/quickmulticastclient/qmlcoapmulticastclient.cpp index 5121b40..5669fb4 100644 --- a/examples/coap/quickmulticastclient/qmlcoapmulticastclient.cpp +++ b/examples/coap/quickmulticastclient/qmlcoapmulticastclient.cpp @@ -3,6 +3,7 @@ #include "qmlcoapmulticastclient.h" +#include <QCoapResourceDiscoveryReply> #include <QLoggingCategory> Q_LOGGING_CATEGORY(lcCoapClient, "qt.coap.client") @@ -12,10 +13,16 @@ QmlCoapMulticastClient::QmlCoapMulticastClient(QObject *parent) { connect(this, &QCoapClient::finished, this, [this](QCoapReply *reply) { - if (reply) + if (reply) { emit finished(static_cast<int>(reply->errorReceived())); - else + reply->deleteLater(); + if (m_reply == reply) { + m_reply = nullptr; + emit isDiscoveringChanged(); + } + } else { qCWarning(lcCoapClient, "Something went wrong, received a null reply"); + } }); connect(this, &QCoapClient::error, this, @@ -30,10 +37,11 @@ void QmlCoapMulticastClient::discover(const QString &host, int port, const QStri url.setHost(host); url.setPort(port); - QCoapResourceDiscoveryReply *discoverReply = QCoapClient::discover(url, discoveryPath); - if (discoverReply) { - connect(discoverReply, &QCoapResourceDiscoveryReply::discovered, + m_reply = QCoapClient::discover(url, discoveryPath); + if (m_reply) { + connect(m_reply, &QCoapResourceDiscoveryReply::discovered, this, &QmlCoapMulticastClient::onDiscovered); + emit isDiscoveringChanged(); } else { qCWarning(lcCoapClient, "Discovery request failed."); } @@ -42,15 +50,27 @@ void QmlCoapMulticastClient::discover(const QString &host, int port, const QStri void QmlCoapMulticastClient::discover(QtCoap::MulticastGroup group, int port, const QString &discoveryPath) { - QCoapResourceDiscoveryReply *discoverReply = QCoapClient::discover(group, port, discoveryPath); - if (discoverReply) { - connect(discoverReply, &QCoapResourceDiscoveryReply::discovered, + m_reply = QCoapClient::discover(group, port, discoveryPath); + if (m_reply) { + connect(m_reply, &QCoapResourceDiscoveryReply::discovered, this, &QmlCoapMulticastClient::onDiscovered); + emit isDiscoveringChanged(); } else { qCWarning(lcCoapClient, "Discovery request failed."); } } +void QmlCoapMulticastClient::stopDiscovery() +{ + if (m_reply) + m_reply->abortRequest(); +} + +bool QmlCoapMulticastClient::isDiscovering() const +{ + return m_reply && !m_reply->isFinished(); +} + void QmlCoapMulticastClient::onDiscovered(QCoapResourceDiscoveryReply *reply, const QList<QCoapResource> &resources) { @@ -58,3 +78,5 @@ void QmlCoapMulticastClient::onDiscovered(QCoapResourceDiscoveryReply *reply, for (const auto &resource : resources) emit discovered(resource); } + +#include "moc_qmlcoapmulticastclient.cpp" diff --git a/examples/coap/quickmulticastclient/qmlcoapmulticastclient.h b/examples/coap/quickmulticastclient/qmlcoapmulticastclient.h index 5d4943e..79c33ee 100644 --- a/examples/coap/quickmulticastclient/qmlcoapmulticastclient.h +++ b/examples/coap/quickmulticastclient/qmlcoapmulticastclient.h @@ -7,10 +7,13 @@ #include <QtCoap/qcoapnamespace.h> #include <QCoapClient> #include <QCoapResource> -#include <QCoapResourceDiscoveryReply> #include <QtQml/qqmlregistration.h> +QT_BEGIN_NAMESPACE +class QCoapResourceDiscoveryReply; +QT_END_NAMESPACE + class QmlCoapResource : public QCoapResource { Q_GADGET @@ -32,19 +35,30 @@ class QmlCoapMulticastClient : public QCoapClient { Q_OBJECT + Q_PROPERTY(bool isDiscovering READ isDiscovering NOTIFY isDiscoveringChanged) + QML_NAMED_ELEMENT(CoapMulticastClient) public: QmlCoapMulticastClient(QObject *parent = nullptr); Q_INVOKABLE void discover(const QString &host, int port, const QString &discoveryPath); Q_INVOKABLE void discover(QtCoap::MulticastGroup group, int port, const QString &discoveryPath); + Q_INVOKABLE void stopDiscovery(); + + bool isDiscovering() const; Q_SIGNALS: void discovered(const QmlCoapResource &resource); void finished(int error); + // The bool parameter is not provided, because the signal is only used by + // the QML property system, and it does not use the passed value anyway. + void isDiscoveringChanged(); public slots: void onDiscovered(QCoapResourceDiscoveryReply *reply, const QList<QCoapResource> &resources); + +private: + QCoapResourceDiscoveryReply *m_reply = nullptr; }; namespace QCoapForeignNamespace |