mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2025-03-01 10:33:28 +00:00
Details coming soon.
This commit is contained in:
81
libsrc/bonjour/bonjourbrowserwrapper.cpp
Normal file
81
libsrc/bonjour/bonjourbrowserwrapper.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
#include <bonjour/bonjourbrowserwrapper.h>
|
||||
|
||||
//qt incl
|
||||
#include <QTimer>
|
||||
|
||||
// bonjour
|
||||
#include <bonjour/bonjourservicebrowser.h>
|
||||
#include <bonjour/bonjourserviceresolver.h>
|
||||
|
||||
BonjourBrowserWrapper* BonjourBrowserWrapper::instance = nullptr;
|
||||
|
||||
BonjourBrowserWrapper::BonjourBrowserWrapper(QObject * parent)
|
||||
: QObject(parent)
|
||||
, _bonjourResolver(new BonjourServiceResolver(this))
|
||||
, _timerBonjourResolver( new QTimer(this))
|
||||
{
|
||||
BonjourBrowserWrapper::instance = this;
|
||||
connect(_bonjourResolver, &BonjourServiceResolver::bonjourRecordResolved, this, &BonjourBrowserWrapper::bonjourRecordResolved);
|
||||
|
||||
connect(_timerBonjourResolver, &QTimer::timeout, this, &BonjourBrowserWrapper::bonjourResolve);
|
||||
_timerBonjourResolver->setInterval(1000);
|
||||
_timerBonjourResolver->start();
|
||||
|
||||
// browse for _hyperiond-http._tcp
|
||||
browseForServiceType(QLatin1String("_hyperiond-http._tcp"));
|
||||
}
|
||||
|
||||
bool BonjourBrowserWrapper::browseForServiceType(const QString &serviceType)
|
||||
{
|
||||
if(!_browsedServices.contains(serviceType))
|
||||
{
|
||||
BonjourServiceBrowser* newBrowser = new BonjourServiceBrowser(this);
|
||||
connect(newBrowser, &BonjourServiceBrowser::currentBonjourRecordsChanged, this, &BonjourBrowserWrapper::currentBonjourRecordsChanged);
|
||||
newBrowser->browseForServiceType(serviceType);
|
||||
_browsedServices.insert(serviceType, newBrowser);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void BonjourBrowserWrapper::currentBonjourRecordsChanged(const QList<BonjourRecord> &list)
|
||||
{
|
||||
_hyperionSessions.clear();
|
||||
for ( auto rec : list )
|
||||
{
|
||||
_hyperionSessions.insert(rec.serviceName, rec);
|
||||
}
|
||||
}
|
||||
|
||||
void BonjourBrowserWrapper::bonjourRecordResolved(const QHostInfo &hostInfo, int port)
|
||||
{
|
||||
if ( _hyperionSessions.contains(_bonjourCurrentServiceToResolve))
|
||||
{
|
||||
QString host = hostInfo.hostName();
|
||||
QString domain = _hyperionSessions[_bonjourCurrentServiceToResolve].replyDomain;
|
||||
if (host.endsWith("."+domain))
|
||||
{
|
||||
host.remove(host.length()-domain.length()-1,domain.length()+1);
|
||||
}
|
||||
_hyperionSessions[_bonjourCurrentServiceToResolve].hostName = host;
|
||||
_hyperionSessions[_bonjourCurrentServiceToResolve].port = port;
|
||||
_hyperionSessions[_bonjourCurrentServiceToResolve].address = hostInfo.addresses().isEmpty() ? "" : hostInfo.addresses().first().toString();
|
||||
//Debug(_log, "found hyperion session: %s:%d",QSTRING_CSTR(hostInfo.hostName()), port);
|
||||
|
||||
//emit change
|
||||
emit browserChange(_hyperionSessions);
|
||||
}
|
||||
}
|
||||
|
||||
void BonjourBrowserWrapper::bonjourResolve()
|
||||
{
|
||||
for(auto key : _hyperionSessions.keys())
|
||||
{
|
||||
if (_hyperionSessions[key].port < 0)
|
||||
{
|
||||
_bonjourCurrentServiceToResolve = key;
|
||||
_bonjourResolver->resolveBonjourRecord(_hyperionSessions[key]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
@@ -30,8 +30,12 @@ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <QtCore/QSocketNotifier>
|
||||
#include <QHostInfo>
|
||||
|
||||
#include <utils/Logger.h>
|
||||
#include <HyperionConfig.h>
|
||||
#include <hyperion/Hyperion.h>
|
||||
|
||||
|
||||
BonjourServiceRegister::BonjourServiceRegister(QObject *parent)
|
||||
: QObject(parent), dnssref(0), bonjourSocket(0)
|
||||
@@ -48,6 +52,19 @@ BonjourServiceRegister::~BonjourServiceRegister()
|
||||
}
|
||||
}
|
||||
|
||||
void BonjourServiceRegister::registerService(const QString& service, const int& port)
|
||||
{
|
||||
// zeroconf $configname@$hostname:port
|
||||
QString prettyName = Hyperion::getInstance()->getQJsonConfig()["general"].toObject()["name"].toString();
|
||||
registerService(
|
||||
BonjourRecord(prettyName+"@"+QHostInfo::localHostName()+ ":" + QString::number(port),
|
||||
service,
|
||||
QString()
|
||||
),
|
||||
port
|
||||
);
|
||||
}
|
||||
|
||||
void BonjourServiceRegister::registerService(const BonjourRecord &record, quint16 servicePort, std::vector<std::pair<std::string, std::string>> txt)
|
||||
{
|
||||
if (dnssref)
|
||||
@@ -61,22 +78,25 @@ void BonjourServiceRegister::registerService(const BonjourRecord &record, quint1
|
||||
bigEndianPort = 0 | ((servicePort & 0x00ff) << 8) | ((servicePort & 0xff00) >> 8);
|
||||
}
|
||||
#endif
|
||||
|
||||
// base txtRec
|
||||
std::vector<std::pair<std::string, std::string> > txtBase = {{"id",Hyperion::getInstance()->getId().toStdString()},{"version",HYPERION_VERSION}};
|
||||
// create txt record
|
||||
TXTRecordRef txtRec;
|
||||
TXTRecordCreate(&txtRec,0,NULL);
|
||||
|
||||
// add txt records
|
||||
if(!txt.empty())
|
||||
if(!txt.empty())
|
||||
{
|
||||
for(std::vector<std::pair<std::string, std::string> >::const_iterator it = txt.begin(); it != txt.end(); ++it)
|
||||
{
|
||||
//Debug(Logger::getInstance("BonJour"), "TXTRecord: key:%s, value:%s",it->first.c_str(),it->second.c_str());
|
||||
uint8_t txtLen = (uint8_t)strlen(it->second.c_str());
|
||||
TXTRecordSetValue(&txtRec, it->first.c_str(), txtLen, it->second.c_str());
|
||||
}
|
||||
txtBase.insert(txtBase.end(), txt.begin(), txt.end());
|
||||
}
|
||||
// add txt records
|
||||
for(std::vector<std::pair<std::string, std::string> >::const_iterator it = txtBase.begin(); it != txtBase.end(); ++it)
|
||||
{
|
||||
//Debug(Logger::getInstance("BonJour"), "TXTRecord: key:%s, value:%s",it->first.c_str(),it->second.c_str());
|
||||
uint8_t txtLen = (uint8_t)strlen(it->second.c_str());
|
||||
TXTRecordSetValue(&txtRec, it->first.c_str(), txtLen, it->second.c_str());
|
||||
}
|
||||
|
||||
|
||||
DNSServiceErrorType err = DNSServiceRegister(&dnssref, 0, 0, record.serviceName.toUtf8().constData(),
|
||||
record.registeredType.toUtf8().constData(),
|
||||
(record.replyDomain.isEmpty() ? 0 : record.replyDomain.toUtf8().constData()),
|
||||
|
Reference in New Issue
Block a user