Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a simple Bonjour/Avahi service registration class.
This should work out of the box on OS X but requires the Avahi Bonjour compatability layer on *nix (libdns_sd). Fairly straightforward service registration and removal that will be used by a couple of upcoming features (and could easily be used as an extra layer of 'advertising' by both mythfrontend and mythbackend).
- Loading branch information
Mark Kendall
committed
Jan 22, 2012
1 parent
1222fa2
commit b1f1fc8
Showing
4 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#include <QSocketNotifier> | ||
#include <QtEndian> | ||
#include "mythlogging.h" | ||
#include "bonjourregister.h" | ||
|
||
#define LOC QString("Bonjour: ") | ||
|
||
BonjourRegister::BonjourRegister(QObject *parent) | ||
: QObject(parent), m_dnssref(0), m_socket(NULL) | ||
{ | ||
} | ||
|
||
BonjourRegister::~BonjourRegister() | ||
{ | ||
if (m_socket) | ||
m_socket->setEnabled(false); | ||
|
||
if (m_dnssref) | ||
{ | ||
LOG(VB_GENERAL, LOG_INFO, LOC + "De-registering service."); | ||
DNSServiceRefDeallocate(m_dnssref); | ||
} | ||
m_dnssref = 0; | ||
|
||
m_socket->deleteLater(); | ||
m_socket = NULL; | ||
} | ||
|
||
bool BonjourRegister::Register(uint16_t port, const QByteArray &type, | ||
const QByteArray &name, const QByteArray &txt) | ||
{ | ||
if (m_dnssref) | ||
{ | ||
LOG(VB_GENERAL, LOG_WARNING, LOC + "Service already registered."); | ||
return true; | ||
} | ||
|
||
uint16_t qport = qToBigEndian(port); | ||
DNSServiceErrorType res = | ||
DNSServiceRegister(&m_dnssref, 0, 0, (const char*)name.data(), | ||
(const char*)type.data(), | ||
NULL, 0, qport, txt.size(), (void*)txt.data(), | ||
BonjourCallback, this); | ||
|
||
if (kDNSServiceErr_NoError != res) | ||
{ | ||
LOG(VB_GENERAL, LOG_ERR, LOC + QString("Error: %1").arg(res)); | ||
} | ||
else | ||
{ | ||
int fd = DNSServiceRefSockFD(m_dnssref); | ||
if (fd != -1) | ||
{ | ||
m_socket = new QSocketNotifier(fd, QSocketNotifier::Read, this); | ||
m_socket->setEnabled(true); | ||
connect(m_socket, SIGNAL(activated(int)), | ||
this, SLOT(socketReadyRead())); | ||
return true; | ||
} | ||
} | ||
|
||
LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to register service."); | ||
return false; | ||
} | ||
|
||
|
||
void BonjourRegister::socketReadyRead() | ||
{ | ||
DNSServiceErrorType res = DNSServiceProcessResult(m_dnssref); | ||
if (kDNSServiceErr_NoError != res) | ||
LOG(VB_GENERAL, LOG_ERR, LOC + QString("Read Error: %1").arg(res)); | ||
} | ||
|
||
|
||
void BonjourRegister::BonjourCallback(DNSServiceRef ref, DNSServiceFlags flags, | ||
DNSServiceErrorType errorcode, | ||
const char *name, const char *type, | ||
const char *domain, void *object) | ||
{ | ||
(void)ref; | ||
(void)flags; | ||
|
||
BonjourRegister *bonjour = static_cast<BonjourRegister *>(object); | ||
if (kDNSServiceErr_NoError != errorcode) | ||
{ | ||
LOG(VB_GENERAL, LOG_ERR, LOC + QString("Callback Error: %1") | ||
.arg(errorcode)); | ||
} | ||
else if (bonjour) | ||
{ | ||
LOG(VB_GENERAL, LOG_INFO, LOC + | ||
QString("Service registration complete: name '%1' type '%2' domain: '%3'") | ||
.arg(QString::fromUtf8(name)).arg(QString::fromUtf8(type)) | ||
.arg(QString::fromUtf8(domain))); | ||
} | ||
else | ||
{ | ||
LOG(VB_GENERAL, LOG_ERR, LOC + | ||
QString("BonjourCallback for unknown object.")); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#ifndef BONJOURREGISTER_H | ||
#define BONJOURREGISTER_H | ||
|
||
#include <QObject> | ||
#include <dns_sd.h> | ||
#include "mythbaseexp.h" | ||
|
||
class QSocketNotifier; | ||
|
||
class MBASE_PUBLIC BonjourRegister : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
BonjourRegister(QObject *parent = 0); | ||
~BonjourRegister(); | ||
|
||
bool Register(uint16_t port, const QByteArray &type, const QByteArray &name, | ||
const QByteArray &txt); | ||
|
||
private slots: | ||
void socketReadyRead(); | ||
|
||
private: | ||
static void DNSSD_API BonjourCallback(DNSServiceRef ref, | ||
DNSServiceFlags flags, | ||
DNSServiceErrorType errorcode, | ||
const char *name, const char *type, | ||
const char *domain, void *object); | ||
DNSServiceRef m_dnssref; | ||
QSocketNotifier *m_socket; | ||
}; | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters