From 19d8f8d4d7c2c1afc5db359522142c08d28e5ce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Mon, 13 Nov 2017 21:14:02 +0100 Subject: [PATCH] migrate avahi broser resource --- libguh/devicemanager.h | 3 -- libguh/hardwaremanager.cpp | 5 +++- libguh/hardwareresource.h | 3 +- libguh/network/avahi/qtavahiclient.cpp | 22 +++++++++----- libguh/network/avahi/qtavahiclient.h | 5 ++-- libguh/network/avahi/qtavahiservice.cpp | 8 ++--- .../network/avahi/qtavahiservicebrowser.cpp | 27 ++++++++++++----- libguh/network/avahi/qtavahiservicebrowser.h | 9 ++++-- .../network/avahi/qtavahiservicebrowser_p.cpp | 8 ++--- libguh/network/upnp/upnpdiscovery.cpp | 29 +++++++++++++++++-- libguh/network/upnp/upnpdiscovery.h | 2 +- 11 files changed, 85 insertions(+), 36 deletions(-) diff --git a/libguh/devicemanager.h b/libguh/devicemanager.h index 02a6760e..a3aa0ba6 100644 --- a/libguh/devicemanager.h +++ b/libguh/devicemanager.h @@ -166,12 +166,9 @@ private slots: void slotDeviceStateValueChanged(const QUuid &stateTypeId, const QVariant &value); // void radio433SignalReceived(QList rawData); - // void replyReady(const PluginId &pluginId, QNetworkReply *reply); - // void upnpDiscoveryFinished(const QList &deviceDescriptorList, const PluginId &pluginId); // void upnpNotifyReceived(const QByteArray ¬ifyData); - // void bluetoothDiscoveryFinished(const PluginId &pluginId, const QList &deviceInfos); void timerEvent(); diff --git a/libguh/hardwaremanager.cpp b/libguh/hardwaremanager.cpp index 05c64fbf..a767c6af 100644 --- a/libguh/hardwaremanager.cpp +++ b/libguh/hardwaremanager.cpp @@ -19,8 +19,8 @@ HardwareManager::HardwareManager(QObject *parent) : QObject(parent) m_radio433->enable(); // Create network access manager for all resources, centralized - m_networkAccessManager = new QNetworkAccessManager(this); // Note: configuration and proxy settings could be implemented here + m_networkAccessManager = new QNetworkAccessManager(this); // Network manager m_networkManager = new NetworkAccessManager(m_networkAccessManager, this); @@ -29,9 +29,12 @@ HardwareManager::HardwareManager(QObject *parent) : QObject(parent) // UPnP discovery m_upnpDiscovery = new UpnpDiscovery(m_networkAccessManager, this); + m_hardwareResources.append(m_upnpDiscovery); + m_upnpDiscovery->enable(); // Avahi Browser m_avahiBrowser = new QtAvahiServiceBrowser(this); + m_hardwareResources.append(m_avahiBrowser); m_avahiBrowser->enable(); // Bluetooth LE diff --git a/libguh/hardwareresource.h b/libguh/hardwareresource.h index 4acba91f..c04d08eb 100644 --- a/libguh/hardwareresource.h +++ b/libguh/hardwareresource.h @@ -35,7 +35,8 @@ public: TypeTimer = 2, TypeNetworkManager = 4, TypeUpnpDisovery = 8, - TypeBluetoothLE = 16 + TypeBluetoothLE = 16, + TypeAvahiBrowser = 32 }; Q_ENUM(Type) Q_DECLARE_FLAGS(Types, Type) diff --git a/libguh/network/avahi/qtavahiclient.cpp b/libguh/network/avahi/qtavahiclient.cpp index 1ad5eaaa..d7dbdfde 100644 --- a/libguh/network/avahi/qtavahiclient.cpp +++ b/libguh/network/avahi/qtavahiclient.cpp @@ -28,8 +28,8 @@ QtAvahiClient::QtAvahiClient(QObject *parent) : QObject(parent), - poll(avahi_qt_poll_get()), - client(0), + m_poll(avahi_qt_poll_get()), + m_client(0), error(0), m_state(QtAvahiClientStateNone) { @@ -38,8 +38,8 @@ QtAvahiClient::QtAvahiClient(QObject *parent) : QtAvahiClient::~QtAvahiClient() { - if (client) - avahi_client_free(client); + if (m_client) + avahi_client_free(m_client); } @@ -50,10 +50,18 @@ QtAvahiClient::QtAvahiClientState QtAvahiClient::state() const void QtAvahiClient::start() { - if (client) + if (m_client) return; - avahi_client_new(poll, (AvahiClientFlags) 0, QtAvahiClient::callback, this, &error); + avahi_client_new(m_poll, (AvahiClientFlags) 0, QtAvahiClient::callback, this, &error); +} + +void QtAvahiClient::stop() +{ + if (m_client) + avahi_client_free(m_client); + + m_client = nullptr; } QString QtAvahiClient::errorString() const @@ -67,7 +75,7 @@ void QtAvahiClient::callback(AvahiClient *client, AvahiClientState state, void * if (!serviceClient) return; - serviceClient->client = client; + serviceClient->m_client = client; switch (state) { case AVAHI_CLIENT_S_RUNNING: diff --git a/libguh/network/avahi/qtavahiclient.h b/libguh/network/avahi/qtavahiclient.h index 128c2c3f..05d43e95 100644 --- a/libguh/network/avahi/qtavahiclient.h +++ b/libguh/network/avahi/qtavahiclient.h @@ -53,12 +53,13 @@ private: friend class QtAvahiServiceBrowser; friend class QtAvahiServiceBrowserPrivate; - const AvahiPoll *poll; - AvahiClient *client; + const AvahiPoll *m_poll; + AvahiClient *m_client; int error; QtAvahiClientState m_state; void start(); + void stop(); QString errorString() const; static void callback(AvahiClient *client, AvahiClientState state, void *userdata); diff --git a/libguh/network/avahi/qtavahiservice.cpp b/libguh/network/avahi/qtavahiservice.cpp index 09f290e4..d177a9dc 100644 --- a/libguh/network/avahi/qtavahiservice.cpp +++ b/libguh/network/avahi/qtavahiservice.cpp @@ -110,7 +110,7 @@ QtAvahiService::QtAvahiServiceState QtAvahiService::state() const bool QtAvahiService::registerService(const QString &name, const quint16 &port, const QString &serviceType, const QHash &txtRecords) { // Check if the client is running - if (!d_ptr->client->client || AVAHI_CLIENT_S_RUNNING != avahi_client_get_state(d_ptr->client->client)) { + if (!d_ptr->client->m_client || AVAHI_CLIENT_S_RUNNING != avahi_client_get_state(d_ptr->client->m_client)) { qCWarning(dcAvahi()) << "Could not register service" << name << port << serviceType << ". The client is not available."; return false; } @@ -122,7 +122,7 @@ bool QtAvahiService::registerService(const QString &name, const quint16 &port, c // If the group is not set yet, create it if (!d_ptr->group) - d_ptr->group = avahi_entry_group_new(d_ptr->client->client, QtAvahiServicePrivate::callback, this); + d_ptr->group = avahi_entry_group_new(d_ptr->client->m_client, QtAvahiServicePrivate::callback, this); // If the group is empty if (avahi_entry_group_is_empty(d_ptr->group)) { @@ -219,10 +219,10 @@ bool QtAvahiService::isValid() const /*! Returns the error string of this \l{QtAvahiService}. */ QString QtAvahiService::errorString() const { - if (!d_ptr->client->client) + if (!d_ptr->client->m_client) return "Invalid client."; - return avahi_strerror(avahi_client_errno(d_ptr->client->client)); + return avahi_strerror(avahi_client_errno(d_ptr->client->m_client)); } bool QtAvahiService::handlCollision() diff --git a/libguh/network/avahi/qtavahiservicebrowser.cpp b/libguh/network/avahi/qtavahiservicebrowser.cpp index 2c31ce22..e87d021f 100644 --- a/libguh/network/avahi/qtavahiservicebrowser.cpp +++ b/libguh/network/avahi/qtavahiservicebrowser.cpp @@ -44,7 +44,7 @@ /*! Constructs a new \l{QtAvahiServiceBrowser} with the given \a parent. */ QtAvahiServiceBrowser::QtAvahiServiceBrowser(QObject *parent) : - QObject(parent), + HardwareResource(HardwareResource::TypeAvahiBrowser, "Avahi browser", parent), d_ptr(new QtAvahiServiceBrowserPrivate(new QtAvahiClient)) { connect(d_ptr->client, &QtAvahiClient::clientStateChanged, this, &QtAvahiServiceBrowser::onClientStateChanged); @@ -68,11 +68,7 @@ QtAvahiServiceBrowser::~QtAvahiServiceBrowser() delete d_ptr; } -/*! Enables this \l{QtAvahiServiceBrowser} and starts the service browsing. */ -void QtAvahiServiceBrowser::enable() -{ - d_ptr->client->start(); -} + /*! Returns the current \l{AvahiServiceEntry} list of this \l{QtAvahiServiceBrowser}. */ QList QtAvahiServiceBrowser::serviceEntries() const @@ -88,16 +84,31 @@ void QtAvahiServiceBrowser::onClientStateChanged(const QtAvahiClient::QtAvahiCli if (d_ptr->serviceTypeBrowser) return; - d_ptr->serviceTypeBrowser = avahi_service_type_browser_new(d_ptr->client->client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, (AvahiLookupFlags) 0, QtAvahiServiceBrowserPrivate::callbackServiceTypeBrowser, this); + d_ptr->serviceTypeBrowser = avahi_service_type_browser_new(d_ptr->client->m_client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, (AvahiLookupFlags) 0, QtAvahiServiceBrowserPrivate::callbackServiceTypeBrowser, this); } else if (state == QtAvahiClient::QtAvahiClientStateFailure) { qCWarning(dcAvahi()) << "Service browser client failure:" << d_ptr->client->errorString(); } } +/*! Enables this \l{QtAvahiServiceBrowser} and starts the service browsing. */ +bool QtAvahiServiceBrowser::enable() +{ + d_ptr->client->start(); + setEnabled(true); + return true; +} + +bool QtAvahiServiceBrowser::disable() +{ + d_ptr->client->stop(); + setEnabled(false); + return true; +} + void QtAvahiServiceBrowser::createServiceBrowser(const char *serviceType) { // create a new service browser for the given serviceType - AvahiServiceBrowser *browser = avahi_service_browser_new(d_ptr->client->client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, serviceType, NULL, (AvahiLookupFlags) 0, QtAvahiServiceBrowserPrivate::callbackServiceBrowser, this); + AvahiServiceBrowser *browser = avahi_service_browser_new(d_ptr->client->m_client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, serviceType, NULL, (AvahiLookupFlags) 0, QtAvahiServiceBrowserPrivate::callbackServiceBrowser, this); d_ptr->serviceBrowserTable.insert(serviceType, browser); } diff --git a/libguh/network/avahi/qtavahiservicebrowser.h b/libguh/network/avahi/qtavahiservicebrowser.h index a83c5df7..1d928354 100644 --- a/libguh/network/avahi/qtavahiservicebrowser.h +++ b/libguh/network/avahi/qtavahiservicebrowser.h @@ -27,20 +27,19 @@ #include #include "libguh.h" +#include "hardwareresource.h" #include "qtavahiclient.h" #include "avahiserviceentry.h" class QtAvahiServiceBrowserPrivate; -class LIBGUH_EXPORT QtAvahiServiceBrowser : public QObject +class LIBGUH_EXPORT QtAvahiServiceBrowser : public HardwareResource { Q_OBJECT public: explicit QtAvahiServiceBrowser(QObject *parent = 0); ~QtAvahiServiceBrowser(); - void enable(); - QList serviceEntries() const; signals: @@ -50,6 +49,10 @@ signals: private slots: void onClientStateChanged(const QtAvahiClient::QtAvahiClientState &state); +public slots: + bool enable(); + bool disable(); + private: QtAvahiServiceBrowserPrivate *d_ptr; diff --git a/libguh/network/avahi/qtavahiservicebrowser_p.cpp b/libguh/network/avahi/qtavahiservicebrowser_p.cpp index 17c53c6e..23b41da3 100644 --- a/libguh/network/avahi/qtavahiservicebrowser_p.cpp +++ b/libguh/network/avahi/qtavahiservicebrowser_p.cpp @@ -71,7 +71,7 @@ void QtAvahiServiceBrowserPrivate::callbackServiceTypeBrowser(AvahiServiceTypeBr case AVAHI_BROWSER_ALL_FOR_NOW: break; case AVAHI_BROWSER_FAILURE: - qCWarning(dcAvahi()) << "Service type browser error:" << QString(avahi_strerror(avahi_client_errno(serviceBrowser->d_ptr->client->client))); + qCWarning(dcAvahi()) << "Service type browser error:" << QString(avahi_strerror(avahi_client_errno(serviceBrowser->d_ptr->client->m_client))); break; } } @@ -88,7 +88,7 @@ void QtAvahiServiceBrowserPrivate::callbackServiceBrowser(AvahiServiceBrowser *b switch (event) { case AVAHI_BROWSER_NEW: { // Start resolving new service - AvahiServiceResolver *resolver = avahi_service_resolver_new(serviceBrowser->d_ptr->client->client, + AvahiServiceResolver *resolver = avahi_service_resolver_new(serviceBrowser->d_ptr->client->m_client, interface, protocol, name, @@ -101,7 +101,7 @@ void QtAvahiServiceBrowserPrivate::callbackServiceBrowser(AvahiServiceBrowser *b if (resolver) { serviceBrowser->d_ptr->m_serviceResolvers.append(resolver); } else { - qCWarning(dcAvahi()) << "Failed to resolve service" << QString(name) << ":" << avahi_strerror(avahi_client_errno(serviceBrowser->d_ptr->client->client)); + qCWarning(dcAvahi()) << "Failed to resolve service" << QString(name) << ":" << avahi_strerror(avahi_client_errno(serviceBrowser->d_ptr->client->m_client)); } break; } @@ -134,7 +134,7 @@ void QtAvahiServiceBrowserPrivate::callbackServiceBrowser(AvahiServiceBrowser *b case AVAHI_BROWSER_CACHE_EXHAUSTED: break; case AVAHI_BROWSER_FAILURE: - qCWarning(dcAvahi()) << "Service browser error:" << QString(avahi_strerror(avahi_client_errno(serviceBrowser->d_ptr->client->client))); + qCWarning(dcAvahi()) << "Service browser error:" << QString(avahi_strerror(avahi_client_errno(serviceBrowser->d_ptr->client->m_client))); break; } diff --git a/libguh/network/upnp/upnpdiscovery.cpp b/libguh/network/upnp/upnpdiscovery.cpp index b869e42b..69e2215c 100644 --- a/libguh/network/upnp/upnpdiscovery.cpp +++ b/libguh/network/upnp/upnpdiscovery.cpp @@ -83,6 +83,19 @@ UpnpDiscovery::~UpnpDiscovery() * the given \a searchTarget, \a userAgent and \a pluginId can be discovered.*/ bool UpnpDiscovery::discoverDevices(const QString &searchTarget, const QString &userAgent, const PluginId &pluginId) { + if (!available()) { + qCWarning(dcHardware()) << name() << "not available."; + return false; + } + + if (!enabled()) { + qCWarning(dcHardware()) << name() << "disabled."; + return false; + } + + if (!m_socket) + return false; + if(m_socket->state() != QUdpSocket::BoundState){ qCWarning(dcHardware) << "UPnP not bound to port 1900"; return false; @@ -103,6 +116,7 @@ bool UpnpDiscovery::discoverDevices(const QString &searchTarget, const QString & void UpnpDiscovery::requestDeviceInformation(const QNetworkRequest &networkRequest, const UpnpDeviceDescriptor &upnpDeviceDescriptor) { QNetworkReply *replay = m_networkAccessManager->get(networkRequest); + connect(replay, &QNetworkReply::finished, this, &UpnpDiscovery::replyFinished); m_informationRequestList.insert(replay, upnpDeviceDescriptor); } @@ -169,6 +183,9 @@ void UpnpDiscovery::respondToSearchRequest(QHostAddress host, int port) /*! This method will be called to send the SSDP message \a data to the UPnP multicast.*/ void UpnpDiscovery::sendToMulticast(const QByteArray &data) { + if (!m_socket) + return; + m_socket->writeDatagram(data, m_host, m_port); } @@ -226,8 +243,9 @@ void UpnpDiscovery::readData() } } -void UpnpDiscovery::replyFinished(QNetworkReply *reply) +void UpnpDiscovery::replyFinished() { + QNetworkReply *reply = static_cast(sender()); int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); switch (status) { @@ -451,7 +469,14 @@ bool UpnpDiscovery::enable() bool UpnpDiscovery::disable() { - // TODO: + sendByeByeMessage(); + m_socket->waitForBytesWritten(); + m_socket->close(); + delete m_socket; + m_socket = nullptr; + + m_notificationTimer->stop(); + setEnabled(false); return true; } diff --git a/libguh/network/upnp/upnpdiscovery.h b/libguh/network/upnp/upnpdiscovery.h index 273b6c12..66df5544 100644 --- a/libguh/network/upnp/upnpdiscovery.h +++ b/libguh/network/upnp/upnpdiscovery.h @@ -77,7 +77,7 @@ signals: private slots: void error(QAbstractSocket::SocketError error); void readData(); - void replyFinished(QNetworkReply *reply); + void replyFinished(); void notificationTimeout(); void sendByeByeMessage(); void sendAliveMessage();