Commits

Anonymous committed 59bed20

zeroconf: redid the qxtmdns implementation so that it's more sane, pimpled, and doesn't including external libraries from the public headers

  • Participants
  • Parent commits 9eab9f0

Comments (0)

Files changed (5)

src/zeroconf/qxtmdns_avahi.cpp

 #include "qxtmdns_avahi.h"
+#include "qxtmdns_avahi_p.h"
 #include <qxtmetaobject.h>
 #include <QDebug>
 #include <QNetworkInterface>
 #include <avahi-common/error.h>
 
 QxtMDNS::QxtMDNS(int id, QObject* parent)
-		: QObject(parent),
-		info(id),
-		client(NULL),
-		recordbrowser(NULL),
-		sent(false)
+		: QObject(parent)
 {
+	QXT_INIT_PRIVATE(QxtMDNS);
+	qxt_d().info = id;
+	qxt_d().client = NULL;
+	qxt_d().recordbrowser = NULL;
+	qxt_d().sent = false;
 }
 
 void QxtMDNS::doLookup(QString n, QObject * r, const char * m)
 {
-	name = n;
-	receiver = r;
-	member = QxtMetaObject::methodName(m);
+	qxt_d().name = n;
+	qxt_d().receiver = r;
+	qxt_d().member = QxtMetaObject::methodName(m);
 	int error;
-	client = avahi_client_new(
-	             avahi_qt_poll_get(), // use the qt event loop
-	             (AvahiClientFlags)0,
-	             QxtMDNS::avahiClientCallback,
-	             this,
-	             &error
-	         );
+	qxt_d().client = avahi_client_new(
+	                     qxtAvahiPoll(), // use the qt event loop
+	                     (AvahiClientFlags)0,
+	                     QxtMDNSPrivate::avahiClientCallback,
+	                     &(qxt_d()),
+	                     &error
+	                 );
 }
 
-void QxtMDNS::avahiClientCallback(AvahiClient *s, AvahiClientState state, void *userdata)
+void QxtMDNSPrivate::avahiClientCallback(AvahiClient *s, AvahiClientState state, void *userdata)
 {
-	QxtMDNS* md = static_cast<QxtMDNS*>(userdata);
-	md->client = s;
+	QxtMDNSPrivate* self = static_cast<QxtMDNSPrivate*>(userdata);
+	self->client = s;
 	if (state == AVAHI_CLIENT_S_RUNNING)
 	{
-		md->recordbrowser = avahi_record_browser_new(
-		                        s,
-		                        AVAHI_IF_UNSPEC,
-		                        AVAHI_PROTO_INET,
-		                        md->name.toAscii().constData(),
-		                        AVAHI_DNS_CLASS_IN,
-		                        AVAHI_DNS_TYPE_A,
-		                        (AvahiLookupFlags)0,
-		                        QxtMDNS::avahiRecordBrowserCallback,
-		                        md);
-		if(md->recordbrowser == NULL)
+		self->recordbrowser = avahi_record_browser_new(
+		                          s,
+		                          AVAHI_IF_UNSPEC,
+		                          AVAHI_PROTO_INET,
+		                          self->name.toAscii().constData(),
+		                          AVAHI_DNS_CLASS_IN,
+		                          AVAHI_DNS_TYPE_A,
+		                          (AvahiLookupFlags)0,
+					   QxtMDNSPrivate::avahiRecordBrowserCallback,
+		                          self);
+		if (self->recordbrowser == NULL)
 		{
-			md->info.setError(QHostInfo::UnknownError);
-			md->info.setErrorString(avahi_strerror(avahi_client_errno(md->client)));
-			QMetaObject::invokeMethod(md->receiver, md->member, Q_ARG(QHostInfo, md->info));
-			md->sent = true;
-			md->cancelLookup();
+			self->info.setError(QHostInfo::UnknownError);
+			self->info.setErrorString(avahi_strerror(avahi_client_errno(self->client)));
+			QMetaObject::invokeMethod(self->receiver, self->member, Q_ARG(QHostInfo, self->info));
+			self->sent = true;
+			self->qxt_p().cancelLookup();
 		}
 	}
 }
 
-void QxtMDNS::avahiRecordBrowserCallback(AvahiRecordBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, uint16_t clazz, uint16_t type, const void *rdata, size_t size, AvahiLookupResultFlags flags, void *userdata)
+void QxtMDNSPrivate::avahiRecordBrowserCallback(AvahiRecordBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, uint16_t clazz, uint16_t type, const void *rdata, size_t size, AvahiLookupResultFlags flags, void *userdata)
 {
 	///@TODO Support IPv6
 	Q_UNUSED(interface);
 	Q_UNUSED(type);
 	Q_UNUSED(size);
 	Q_UNUSED(flags);
-	QxtMDNS* md = static_cast<QxtMDNS*>(userdata);
-	md->recordbrowser = b;
+	QxtMDNSPrivate* self = static_cast<QxtMDNSPrivate*>(userdata);
+	self->recordbrowser = b;
 // 	qDebug() << "Return thing" << md->name << name << size;
 	switch (event)
 	{
 		{
 			//Found an entry!
 			uint32_t ip = qFromBigEndian(*static_cast<const uint32_t*>(rdata));
-			if (md->sent)
+			if (self->sent)
 			{
-				QHostInfo info(md->info.lookupId());
+				QHostInfo info(self->info.lookupId());
 				info.setAddresses(QList<QHostAddress>() << QHostAddress(ip));
-				QMetaObject::invokeMethod(md->receiver, md->member, Q_ARG(QHostInfo, info));
+				QMetaObject::invokeMethod(self->receiver, self->member, Q_ARG(QHostInfo, info));
 			}
 			else
 			{
-				md->addresses << QHostAddress(ip);
+				self->addresses << QHostAddress(ip);
 			}
 			break;
 		}
 		case AVAHI_BROWSER_REMOVE:
 		{
 			uint32_t ip = qFromBigEndian(*static_cast<const uint32_t*>(rdata));
-			md->addresses.removeAll(QHostAddress(ip));
+			self->addresses.removeAll(QHostAddress(ip));
 			break;
 		}
 		case AVAHI_BROWSER_CACHE_EXHAUSTED:
 			break;
 		case AVAHI_BROWSER_ALL_FOR_NOW:
-			if (md->addresses.count() == 0)
+			if (self->addresses.count() == 0)
 			{
-				md->info.setError(QHostInfo::HostNotFound);
-				md->info.setErrorString("The host was not found.");
+				self->info.setError(QHostInfo::HostNotFound);
+				self->info.setErrorString("The host was not found.");
 			}
 			else
 			{
-				md->info.setAddresses(md->addresses);
-				md->addresses.clear();
+				self->info.setAddresses(self->addresses);
+				self->addresses.clear();
 			}
-			QMetaObject::invokeMethod(md->receiver, md->member, Q_ARG(QHostInfo, md->info));
-			md->sent = true;
+			QMetaObject::invokeMethod(self->receiver, self->member, Q_ARG(QHostInfo, self->info));
+			self->sent = true;
 			break;
 		case AVAHI_BROWSER_FAILURE:
 
-			if (md->sent)
+			if (self->sent)
 			{
-				QHostInfo info(md->info.lookupId());
+				QHostInfo info(self->info.lookupId());
 				info.setError(QHostInfo::UnknownError);
-				info.setErrorString(avahi_strerror(avahi_client_errno(md->client)));
-				info.setAddresses(md->addresses);
-				QMetaObject::invokeMethod(md->receiver, md->member, Q_ARG(QHostInfo, info));
+				info.setErrorString(avahi_strerror(avahi_client_errno(self->client)));
+				info.setAddresses(self->addresses);
+				QMetaObject::invokeMethod(self->receiver, self->member, Q_ARG(QHostInfo, info));
 			}
 			else
 			{
-				md->info.setError(QHostInfo::UnknownError);
-				md->info.setErrorString(avahi_strerror(avahi_client_errno(md->client)));
-				md->info.setAddresses(md->addresses);
-				md->addresses.clear();
-				QMetaObject::invokeMethod(md->receiver, md->member, Q_ARG(QHostInfo, md->info));
-				md->sent = true;
+				self->info.setError(QHostInfo::UnknownError);
+				self->info.setErrorString(avahi_strerror(avahi_client_errno(self->client)));
+				self->info.setAddresses(self->addresses);
+				self->addresses.clear();
+				QMetaObject::invokeMethod(self->receiver, self->member, Q_ARG(QHostInfo, self->info));
+				self->sent = true;
 			}
 			break;
 	}
 
 void QxtMDNS::cancelLookup()
 {
-	if (recordbrowser != NULL)
-		avahi_record_browser_free(recordbrowser);
+	if (qxt_d().recordbrowser != NULL)
+		avahi_record_browser_free(qxt_d().recordbrowser);
 
-	if (client != NULL)
-		avahi_client_free(client);
+	if (qxt_d().client != NULL)
+		avahi_client_free(qxt_d().client);
 
 	deleteLater();
 }

src/zeroconf/qxtmdns_avahi.h

 #ifndef QXTMDNS_H
 #define QXTMDNS_H
 
+#include "qxtglobal.h"
 #include <QObject>
-#include <QHostAddress>
-#include <QHostInfo>
-#include <avahi-client/client.h>
-#include <avahi-client/lookup.h>
-#include <avahi-qt4/qt-watch.h>
+#include <QxtPimpl>
 
-// This is basically a struct of data with functions in it for organization :P
-class QxtMDNS : public QObject
+class QxtMDNSPrivate;
+
+// This class just throws QHostInfo objects at the receiver...
+// The receiver slot should take 1 parameter, a QHostInfo...
+class QXT_ZEROCONF_EXPORT QxtMDNS : public QObject
 {
+	QXT_DECLARE_PRIVATE(QxtMDNS);
 	Q_OBJECT
 public:
 	QxtMDNS(int id = -1, QObject* parent = 0);
 
 	void doLookup(QString name, QObject * receiver, const char * member);
 	void cancelLookup();
-	
-	static void avahiClientCallback(AvahiClient *s, AvahiClientState state, void *userdata);
-	static void avahiRecordBrowserCallback(AvahiRecordBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, uint16_t clazz, uint16_t type, const void *rdata, size_t size, AvahiLookupResultFlags flags, void *userdata);
-
-	QHostInfo info;
-	QList<QHostAddress> addresses;
-	QString name;
-	QObject* receiver;
-	QByteArray member;
-	AvahiClient* client;
-	AvahiRecordBrowser* recordbrowser;
-	bool sent;
-	int id;
 };
 
 #endif //#ifndef QXTMDNS_H

src/zeroconf/qxtmdns_avahi_p.h

+#ifndef QXTMDNS_P_H
+#define QXTMDNS_P_H
+
+#include "qxtmdns_avahi.h"
+#include <QHostAddress>
+#include <QHostInfo>
+#include <avahi-client/client.h>
+#include <avahi-client/lookup.h>
+#include <qxtavahipoll.h>
+
+class QxtMDNSPrivate : public QxtPrivate<QxtMDNS>
+{
+public:
+	QxtMDNSPrivate()
+	{
+	}
+
+	static void avahiClientCallback(AvahiClient *s, AvahiClientState state, void *userdata);
+	static void avahiRecordBrowserCallback(AvahiRecordBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, uint16_t clazz, uint16_t type, const void *rdata, size_t size, AvahiLookupResultFlags flags, void *userdata);
+
+	QHostInfo info;
+	QList<QHostAddress> addresses;
+
+	QString name;
+	QObject* receiver;
+	QByteArray member;
+	AvahiClient* client;
+	AvahiRecordBrowser* recordbrowser;
+	bool sent;
+	int id;
+};
+
+
+#endif //#ifndef QXTMDNS_H

src/zeroconf/zeroconf.pri

 SOURCES += qxtservicebrowser.cpp
 SOURCES += qxtdiscoverableservicename.cpp
 
-unix:!macx {
-DEFINES += USE_AVAHI
-SOURCES += qxtmdns_avahi.cpp
-HEADERS += qxtmdns_avahi.h
+unix : !macx {
+        DEFINES += USE_AVAHI
+        SOURCES += qxtmdns_avahi.cpp
+        SOURCES += qxtavahipoll.cpp
+        HEADERS += qxtmdns_avahi.h
+        HEADERS += qxtmdns_avahi_p.h
+        HEADERS += qxtavahipoll.h
+        HEADERS += qxtavahipoll_p.h
 } else {
-SOURCES += qxtmdns_bonjour.cpp
-HEADERS += qxtmdns_bonjour.h
+        SOURCES += qxtmdns_bonjour.cpp
+        HEADERS += qxtmdns_bonjour.h
 }

src/zeroconf/zeroconf.pro

 include(zeroconf.pri)
 include(../qxtbase.pri)
 
-unix:!macx:LIBS += -ldns_sd -lavahi-client -lavahi-qt4 -lavahi-common
+unix:!macx:LIBS += -ldns_sd -lavahi-client -lavahi-common
 win32:LIBS        += -L"c:\\PROGRA~1\\BONJOU~1\\lib\\win32" -ldnssd
 win32:INCLUDEPATH += "c:\\program files\\bonjour sdk\\include"