mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
populate zeroconf/avahi/bonjour records via json api (#419)
* start of integrating a bonkour service browser * some experiments * blub * bonjour browser via jsonrpc ... * fix indention * - make leddevice as component - extend sysinfo with domain - add more data for bonjour browser (e.g. split domain and hostname) * code cleanup * add translation * use component names instead of ids * fix compile
This commit is contained in:
@@ -6,6 +6,8 @@ set(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/libsrc/bonjour)
|
||||
# Group the headers that go through the MOC compiler
|
||||
set(Bonjour_QT_HEADERS
|
||||
${CURRENT_HEADER_DIR}/bonjourserviceregister.h
|
||||
${CURRENT_HEADER_DIR}/bonjourservicebrowser.h
|
||||
${CURRENT_HEADER_DIR}/bonjourserviceresolver.h
|
||||
)
|
||||
|
||||
set(Bonjour_HEADERS
|
||||
@@ -13,13 +15,15 @@ set(Bonjour_HEADERS
|
||||
|
||||
set(Bonjour_SOURCES
|
||||
${CURRENT_SOURCE_DIR}/bonjourserviceregister.cpp
|
||||
${CURRENT_SOURCE_DIR}/bonjourservicebrowser.cpp
|
||||
${CURRENT_SOURCE_DIR}/bonjourserviceresolver.cpp
|
||||
)
|
||||
|
||||
set(Bonjour_RESOURCES
|
||||
)
|
||||
#set(Bonjour_RESOURCES
|
||||
#)
|
||||
|
||||
qt5_wrap_cpp(Bonjour_HEADERS_MOC ${Bonjour_QT_HEADERS})
|
||||
qt5_add_resources(Bonjour_RESOURCES_RCC ${Bonjour_RESOURCES} OPTIONS "-no-compress")
|
||||
#qt5_add_resources(Bonjour_RESOURCES_RCC ${Bonjour_RESOURCES} OPTIONS "-no-compress")
|
||||
|
||||
add_library(bonjour
|
||||
${Bonjour_HEADERS}
|
||||
|
109
libsrc/bonjour/bonjourservicebrowser.cpp
Normal file
109
libsrc/bonjour/bonjourservicebrowser.cpp
Normal file
@@ -0,0 +1,109 @@
|
||||
/*
|
||||
Copyright (c) 2007, Trenton Schulz
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
3. The name of the author may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "bonjour/bonjourservicebrowser.h"
|
||||
|
||||
#include <QtCore/QSocketNotifier>
|
||||
|
||||
BonjourServiceBrowser::BonjourServiceBrowser(QObject *parent)
|
||||
: QObject(parent)
|
||||
, dnssref(0)
|
||||
, bonjourSocket(0)
|
||||
{
|
||||
}
|
||||
|
||||
BonjourServiceBrowser::~BonjourServiceBrowser()
|
||||
{
|
||||
if (dnssref)
|
||||
{
|
||||
DNSServiceRefDeallocate(dnssref);
|
||||
dnssref = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void BonjourServiceBrowser::browseForServiceType(const QString &serviceType)
|
||||
{
|
||||
DNSServiceErrorType err = DNSServiceBrowse(&dnssref, 0, 0, serviceType.toUtf8().constData(), 0, bonjourBrowseReply, this);
|
||||
if (err != kDNSServiceErr_NoError)
|
||||
{
|
||||
emit error(err);
|
||||
}
|
||||
else
|
||||
{
|
||||
int sockfd = DNSServiceRefSockFD(dnssref);
|
||||
if (sockfd == -1)
|
||||
{
|
||||
emit error(kDNSServiceErr_Invalid);
|
||||
}
|
||||
else
|
||||
{
|
||||
bonjourSocket = new QSocketNotifier(sockfd, QSocketNotifier::Read, this);
|
||||
connect(bonjourSocket, SIGNAL(activated(int)), this, SLOT(bonjourSocketReadyRead()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BonjourServiceBrowser::bonjourSocketReadyRead()
|
||||
{
|
||||
DNSServiceErrorType err = DNSServiceProcessResult(dnssref);
|
||||
if (err != kDNSServiceErr_NoError)
|
||||
{
|
||||
emit error(err);
|
||||
}
|
||||
}
|
||||
|
||||
void BonjourServiceBrowser::bonjourBrowseReply(DNSServiceRef , DNSServiceFlags flags,
|
||||
quint32 , DNSServiceErrorType errorCode,
|
||||
const char *serviceName, const char *regType,
|
||||
const char *replyDomain, void *context)
|
||||
{
|
||||
BonjourServiceBrowser *serviceBrowser = static_cast<BonjourServiceBrowser *>(context);
|
||||
if (errorCode != kDNSServiceErr_NoError)
|
||||
{
|
||||
emit serviceBrowser->error(errorCode);
|
||||
}
|
||||
else
|
||||
{
|
||||
BonjourRecord bonjourRecord(serviceName, regType, replyDomain);
|
||||
if (flags & kDNSServiceFlagsAdd)
|
||||
{
|
||||
if (!serviceBrowser->bonjourRecords.contains(bonjourRecord))
|
||||
{
|
||||
serviceBrowser->bonjourRecords.append(bonjourRecord);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
serviceBrowser->bonjourRecords.removeAll(bonjourRecord);
|
||||
}
|
||||
if (!(flags & kDNSServiceFlagsMoreComing))
|
||||
{
|
||||
emit serviceBrowser->currentBonjourRecordsChanged(serviceBrowser->bonjourRecords);
|
||||
}
|
||||
}
|
||||
}
|
122
libsrc/bonjour/bonjourserviceresolver.cpp
Normal file
122
libsrc/bonjour/bonjourserviceresolver.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
Copyright (c) 2007, Trenton Schulz
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
3. The name of the author may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <QtCore/QSocketNotifier>
|
||||
#include <QtNetwork/QHostInfo>
|
||||
|
||||
#include "bonjour/bonjourrecord.h"
|
||||
#include "bonjour/bonjourserviceresolver.h"
|
||||
|
||||
BonjourServiceResolver::BonjourServiceResolver(QObject *parent)
|
||||
: QObject(parent)
|
||||
, dnssref(0)
|
||||
, bonjourSocket(0)
|
||||
, bonjourPort(-1)
|
||||
{
|
||||
}
|
||||
|
||||
BonjourServiceResolver::~BonjourServiceResolver()
|
||||
{
|
||||
cleanupResolve();
|
||||
}
|
||||
|
||||
void BonjourServiceResolver::cleanupResolve()
|
||||
{
|
||||
if (dnssref)
|
||||
{
|
||||
DNSServiceRefDeallocate(dnssref);
|
||||
dnssref = 0;
|
||||
delete bonjourSocket;
|
||||
bonjourPort = -1;
|
||||
}
|
||||
}
|
||||
|
||||
bool BonjourServiceResolver::resolveBonjourRecord(const BonjourRecord &record)
|
||||
{
|
||||
if (dnssref)
|
||||
{
|
||||
//qWarning("resolve in process, aborting");
|
||||
return false;
|
||||
}
|
||||
DNSServiceErrorType err = DNSServiceResolve(&dnssref, 0, 0,
|
||||
record.serviceName.toUtf8().constData(),
|
||||
record.registeredType.toUtf8().constData(),
|
||||
record.replyDomain.toUtf8().constData(),
|
||||
(DNSServiceResolveReply)bonjourResolveReply, this);
|
||||
if (err != kDNSServiceErr_NoError)
|
||||
{
|
||||
emit error(err);
|
||||
}
|
||||
else
|
||||
{
|
||||
int sockfd = DNSServiceRefSockFD(dnssref);
|
||||
if (sockfd == -1)
|
||||
{
|
||||
emit error(kDNSServiceErr_Invalid);
|
||||
}
|
||||
else
|
||||
{
|
||||
bonjourSocket = new QSocketNotifier(sockfd, QSocketNotifier::Read, this);
|
||||
connect(bonjourSocket, SIGNAL(activated(int)), this, SLOT(bonjourSocketReadyRead()));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void BonjourServiceResolver::bonjourSocketReadyRead()
|
||||
{
|
||||
DNSServiceErrorType err = DNSServiceProcessResult(dnssref);
|
||||
if (err != kDNSServiceErr_NoError)
|
||||
emit error(err);
|
||||
}
|
||||
|
||||
void BonjourServiceResolver::bonjourResolveReply(DNSServiceRef sdRef, DNSServiceFlags ,
|
||||
quint32 , DNSServiceErrorType errorCode,
|
||||
const char *, const char *hosttarget, quint16 port,
|
||||
quint16 , const char *, void *context)
|
||||
{
|
||||
BonjourServiceResolver *serviceResolver = static_cast<BonjourServiceResolver *>(context);
|
||||
if (errorCode != kDNSServiceErr_NoError) {
|
||||
emit serviceResolver->error(errorCode);
|
||||
return;
|
||||
}
|
||||
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
|
||||
{
|
||||
port = 0 | ((port & 0x00ff) << 8) | ((port & 0xff00) >> 8);
|
||||
}
|
||||
#endif
|
||||
serviceResolver->bonjourPort = port;
|
||||
|
||||
QHostInfo::lookupHost(QString::fromUtf8(hosttarget), serviceResolver, SLOT(finishConnect(const QHostInfo &)));
|
||||
}
|
||||
|
||||
void BonjourServiceResolver::finishConnect(const QHostInfo &hostInfo)
|
||||
{
|
||||
emit bonjourRecordResolved(hostInfo, bonjourPort);
|
||||
QMetaObject::invokeMethod(this, "cleanupResolve", Qt::QueuedConnection);
|
||||
}
|
Reference in New Issue
Block a user