Commit 25c983a4 authored by Fawzi Mohamed's avatar Fawzi Mohamed
Browse files

zeroconf: initLib/setDefaultZConfLib related cleanups



Renamed initLib to setDefaultZConfLib.
Use shared pointers for libs: avoid small leak for each call to initLib.
Renamed nativeLib to more logical dnsSdLib.
Print in ~ServiceBrowserPrivate only if DEBUG_ZEROCONF.

Change-Id: Icf76bba490c2ece46769253555b28c1220b44c23
Reviewed-by: default avatarChristian Kandeler <christian.kandeler@nokia.com>
parent e4f28995
......@@ -114,7 +114,7 @@ private:
QLibrary nativeLib;
public:
AvahiZConfLib(QString libName = QLatin1String("avahi"), ZConfLib *fallBack = 0) : ZConfLib(fallBack), nativeLib(libName)
AvahiZConfLib(QString libName = QLatin1String("avahi"), ZConfLib::Ptr fallBack = ZConfLib::Ptr(0)) : ZConfLib(fallBack), nativeLib(libName)
{
#ifndef ZCONF_AVAHI_STATIC_LINKING
// dynamic linking
......@@ -321,8 +321,8 @@ public:
}
};
ZConfLib *ZConfLib::createAvahiLib(const QString &libName, ZConfLib *fallback) {
return new AvahiZConfLib(libName, fallback);
ZConfLib::Ptr ZConfLib::createAvahiLib(const QString &libName, ZConfLib::Ptr fallback) {
return ZConfLib::Ptr(new AvahiZConfLib(libName, fallback));
}
extern "C" void cAvahiResolveReply( AvahiServiceResolver * r, AvahiIfIndex interface, AvahiProtocol /*protocol*/,
......@@ -381,7 +381,8 @@ extern "C" void cAvahiResolveReply( AvahiServiceResolver * r, AvahiIfIndex inter
qDebug() << "Error: unexpected avahi event " << event << " in cAvahiResolveReply";
break;
}
AvahiZConfLib *lib = dynamic_cast<AvahiZConfLib *>(sg->serviceBrowser->mainConnection->lib);
ZConfLib::Ptr libBase = sg->serviceBrowser->mainConnection->lib;
AvahiZConfLib *lib = dynamic_cast<AvahiZConfLib *>(libBase.data());
if (lib)
lib->serviceResolverFree(r);
}
......@@ -467,7 +468,7 @@ extern "C" void cAvahiBrowseReply(AvahiServiceBrowser * /*b*/, AvahiIfIndex inte
namespace ZeroConf {
namespace Internal {
ZConfLib *ZConfLib::createAvahiLib(const QString &/*extraPaths*/, ZConfLib * fallback) {
ZConfLib::Ptr ZConfLib::createAvahiLib(const QString &/*extraPaths*/, ZConfLib::Ptr fallback) {
return fallback;
}
......
......@@ -43,7 +43,7 @@
#include <errno.h>
#ifndef NO_NATIVE_LIB
#ifndef NO_DNS_SD_LIB
#ifdef Q_OS_MACX
#define ZCONF_MDNS_STATIC_LINKING
......@@ -91,7 +91,7 @@ typedef int (DNSSD_API *RefSockFDPtr)(DNSServiceRef sdRef);
}
// represents a zero conf library exposing the dns-sd interface
class NativeZConfLib : public ZConfLib{
class DnsSdZConfLib : public ZConfLib{
Q_DECLARE_TR_FUNCTIONS(ZeroConf)
private:
RefDeallocatePtr m_refDeallocate;
......@@ -104,31 +104,31 @@ private:
ProcessResultPtr m_processResult;
CreateConnectionPtr m_createConnection;
RefSockFDPtr m_refSockFD;
QLibrary nativeLib;
QLibrary dnsSdLib;
public:
enum {
// Note: the select() implementation on Windows (Winsock2) fails with any timeout much larger than this
LONG_TIME = 100000000
};
NativeZConfLib(QString libName = QLatin1String("dns_sd"), ZConfLib *fallBack = 0) : ZConfLib(fallBack), nativeLib(libName)
DnsSdZConfLib(QString libName = QLatin1String("dns_sd"), ZConfLib::Ptr fallBack = ZConfLib::Ptr(0)) : ZConfLib(fallBack), dnsSdLib(libName)
{
#ifndef ZCONF_MDNS_STATIC_LINKING
// dynamic linking
if (!nativeLib.load()) {
if (!dnsSdLib.load()) {
m_isOk = false;
m_errorMsg=tr("NativeZConfLib could not load native library");
m_errorMsg=tr("DnsSdZConfLib could not load native library");
}
m_refDeallocate = reinterpret_cast<RefDeallocatePtr>(nativeLib.resolve("DNSServiceRefDeallocate"));
m_resolve = reinterpret_cast<ResolvePtr>(nativeLib.resolve("DNSServiceResolve"));
m_queryRecord = reinterpret_cast<QueryRecordPtr>(nativeLib.resolve("DNSServiceQueryRecord"));
m_getAddrInfo = reinterpret_cast<GetAddrInfoPtr>(nativeLib.resolve("DNSServiceGetAddrInfo"));
m_reconfirmRecord = reinterpret_cast<ReconfirmRecordPtr>(nativeLib.resolve("DNSServiceReconfirmRecord"));
m_browse = reinterpret_cast<BrowsePtr>(nativeLib.resolve("DNSServiceBrowse"));
m_getProperty = reinterpret_cast<GetPropertyPtr>(nativeLib.resolve("DNSServiceGetProperty"));
m_processResult = reinterpret_cast<ProcessResultPtr>(nativeLib.resolve("DNSServiceProcessResult")) ;
m_createConnection = reinterpret_cast<CreateConnectionPtr>(nativeLib.resolve("DNSServiceCreateConnection"));
m_refSockFD = reinterpret_cast<RefSockFDPtr>(nativeLib.resolve("DNSServiceRefSockFD"));
m_refDeallocate = reinterpret_cast<RefDeallocatePtr>(dnsSdLib.resolve("DNSServiceRefDeallocate"));
m_resolve = reinterpret_cast<ResolvePtr>(dnsSdLib.resolve("DNSServiceResolve"));
m_queryRecord = reinterpret_cast<QueryRecordPtr>(dnsSdLib.resolve("DNSServiceQueryRecord"));
m_getAddrInfo = reinterpret_cast<GetAddrInfoPtr>(dnsSdLib.resolve("DNSServiceGetAddrInfo"));
m_reconfirmRecord = reinterpret_cast<ReconfirmRecordPtr>(dnsSdLib.resolve("DNSServiceReconfirmRecord"));
m_browse = reinterpret_cast<BrowsePtr>(dnsSdLib.resolve("DNSServiceBrowse"));
m_getProperty = reinterpret_cast<GetPropertyPtr>(dnsSdLib.resolve("DNSServiceGetProperty"));
m_processResult = reinterpret_cast<ProcessResultPtr>(dnsSdLib.resolve("DNSServiceProcessResult")) ;
m_createConnection = reinterpret_cast<CreateConnectionPtr>(dnsSdLib.resolve("DNSServiceCreateConnection"));
m_refSockFD = reinterpret_cast<RefSockFDPtr>(dnsSdLib.resolve("DNSServiceRefSockFD"));
#else
// static linking
m_refDeallocate = reinterpret_cast<RefDeallocatePtr>(&DNSServiceRefDeallocate);
......@@ -143,24 +143,24 @@ public:
m_refSockFD = reinterpret_cast<RefSockFDPtr>(&DNSServiceRefSockFD);
#endif
if (DEBUG_ZEROCONF){
if (m_refDeallocate == 0) qDebug() << QLatin1String("NativeZConfLib.m_refDeallocate == 0");
if (m_resolve == 0) qDebug() << QLatin1String("NativeZConfLib.m_resolve == 0");
if (m_queryRecord == 0) qDebug() << QLatin1String("NativeZConfLib.m_queryRecord == 0");
if (m_getAddrInfo == 0) qDebug() << QLatin1String("NativeZConfLib.m_getAddrInfo == 0");
if (m_reconfirmRecord == 0) qDebug() << QLatin1String("NativeZConfLib.m_reconfirmRecord == 0");
if (m_browse == 0) qDebug() << QLatin1String("NativeZConfLib.m_browse == 0");
if (m_getProperty == 0) qDebug() << QLatin1String("NativeZConfLib.m_getProperty == 0");
if (m_processResult == 0) qDebug() << QLatin1String("NativeZConfLib.m_processResult == 0");
if (m_createConnection == 0) qDebug() << QLatin1String("NativeZConfLib.m_createConnection == 0");
if (m_refSockFD == 0) qDebug() << QLatin1String("NativeZConfLib.m_refSockFD == 0");
if (m_refDeallocate == 0) qDebug() << QLatin1String("DnsSdZConfLib.m_refDeallocate == 0");
if (m_resolve == 0) qDebug() << QLatin1String("DnsSdZConfLib.m_resolve == 0");
if (m_queryRecord == 0) qDebug() << QLatin1String("DnsSdZConfLib.m_queryRecord == 0");
if (m_getAddrInfo == 0) qDebug() << QLatin1String("DnsSdZConfLib.m_getAddrInfo == 0");
if (m_reconfirmRecord == 0) qDebug() << QLatin1String("DnsSdZConfLib.m_reconfirmRecord == 0");
if (m_browse == 0) qDebug() << QLatin1String("DnsSdZConfLib.m_browse == 0");
if (m_getProperty == 0) qDebug() << QLatin1String("DnsSdZConfLib.m_getProperty == 0");
if (m_processResult == 0) qDebug() << QLatin1String("DnsSdZConfLib.m_processResult == 0");
if (m_createConnection == 0) qDebug() << QLatin1String("DnsSdZConfLib.m_createConnection == 0");
if (m_refSockFD == 0) qDebug() << QLatin1String("DnsSdZConfLib.m_refSockFD == 0");
}
}
~NativeZConfLib() {
~DnsSdZConfLib() {
}
QString name(){
return QString::fromUtf8("NativeZeroConfLib@%1").arg(size_t(this),0,16);
return QString::fromUtf8("DnsSdZeroConfLib@%1").arg(size_t(this),0,16);
}
// bool tryStartDaemon();
......@@ -344,19 +344,19 @@ public:
}
};
ZConfLib *ZConfLib::createNativeLib(const QString &libName, ZConfLib *fallback) {
return new NativeZConfLib(libName, fallback);
ZConfLib::Ptr ZConfLib::createDnsSdLib(const QString &libName, ZConfLib::Ptr fallback) {
return ZConfLib::Ptr(new DnsSdZConfLib(libName, fallback));
return fallback;
}
} // namespace Internal
} // namespace ZeroConf
#else // no native lib
#else // NO_DNS_SD_LIB
namespace ZeroConf {
namespace Internal {
ZConfLib *ZConfLib::createNativeLib(const QString &/*extraPaths*/, ZConfLib * fallback) {
ZConfLib::Ptr ZConfLib::createDnsSdLib(const QString &/*extraPaths*/, ZConfLib::Ptr fallback) {
return fallback;
}
......
......@@ -60,7 +60,7 @@ class EmbeddedZConfLib : public ZConfLib
public:
QString daemonPath;
EmbeddedZConfLib(const QString &daemonPath, ZConfLib *fallBack = 0) : ZConfLib(fallBack), daemonPath(daemonPath)
EmbeddedZConfLib(const QString &daemonPath, ZConfLib::Ptr fallBack) : ZConfLib(fallBack), daemonPath(daemonPath)
{
if (!daemonPath.isEmpty() && daemonPath.at(0) != '/' && daemonPath.at(0) != '.')
this->daemonPath = QCoreApplication::applicationDirPath() + QChar('/') + daemonPath;
......@@ -204,9 +204,9 @@ public:
}
};
ZConfLib *ZConfLib::createEmbeddedLib(const QString &daemonPath, ZConfLib *fallback)
ZConfLib::Ptr ZConfLib::createEmbeddedLib(const QString &daemonPath, ZConfLib::Ptr fallback)
{
return new EmbeddedZConfLib(daemonPath, fallback);
return ZConfLib::Ptr(new EmbeddedZConfLib(daemonPath, fallback));
}
} // namespace Internal
} // namespace ZeroConf
......@@ -216,7 +216,7 @@ ZConfLib *ZConfLib::createEmbeddedLib(const QString &daemonPath, ZConfLib *fallb
namespace ZeroConf {
namespace Internal {
ZConfLib *ZConfLib::createEmbeddedLib(const QString &, ZConfLib * fallback)
ZConfLib::Ptr ZConfLib::createEmbeddedLib(const QString &, ZConfLib::Ptr fallback)
{
return fallback;
}
......
......@@ -114,40 +114,46 @@ class ZeroConfLib
{
public:
ZeroConfLib();
ZConfLib *defaultLib();
void setDefaultLib(LibUsage usage, const QString &libName, const QString &daemonPath);
ZConfLib::Ptr defaultLib();
void setDefaultLib(LibUsage usage, const QString &avahiLibName, const QString &dnsSdLibName, const QString &dnsSdDaemonPath);
private:
QMutex m_lock;
ZConfLib *m_defaultLib;
ZConfLib::Ptr m_defaultLib;
};
Q_GLOBAL_STATIC(ZeroConfLib, zeroConfLibInstance)
ZeroConfLib::ZeroConfLib(): m_lock(QMutex::Recursive),
m_defaultLib(ZConfLib::createAvahiLib(QLatin1String("avahi-client"),
ZConfLib::createNativeLib(QLatin1String("dns_sd"),
ZConfLib::createEmbeddedLib(QString("mdnssd"), 0))))
ZConfLib::createDnsSdLib(QLatin1String("dns_sd"),
ZConfLib::createEmbeddedLib(QLatin1String("mdnssd")))))
{
qRegisterMetaType<Service::ConstPtr>("ZeroConf::Service::ConstPtr");
}
ZConfLib *ZeroConfLib::defaultLib(){
ZConfLib::Ptr ZeroConfLib::defaultLib(){
QMutexLocker l(&m_lock);
return m_defaultLib;
}
void ZeroConfLib::setDefaultLib(LibUsage usage, const QString &libName, const QString &daemonPath){ // leaks... should be ok, switch to shared pointers???
void ZeroConfLib::setDefaultLib(LibUsage usage, const QString &avahiLibName, const QString &dnsSdLibName, const QString &dnsSdDaemonPath){
QMutexLocker l(&m_lock);
switch (usage){
case (UseNativeOnly):
m_defaultLib = ZConfLib::createNativeLib(libName, 0);
case (UseDnsSdOnly):
m_defaultLib = ZConfLib::createDnsSdLib(dnsSdLibName);
break;
case (UseEmbeddedOnly):
m_defaultLib = ZConfLib::createEmbeddedLib(daemonPath, 0);
m_defaultLib = ZConfLib::createEmbeddedLib(dnsSdDaemonPath);
break;
case (UseNativeOrEmbedded):
m_defaultLib = ZConfLib::createNativeLib(libName, ZConfLib::createEmbeddedLib(daemonPath, 0));
case (UseAvahiOnly):
m_defaultLib = ZConfLib::createAvahiLib(avahiLibName);
break;
case (UseAvahiOrDnsSd):
m_defaultLib = ZConfLib::createAvahiLib(avahiLibName, ZConfLib::createDnsSdLib(dnsSdLibName));
break;
case (UseAvahiOrDnsSdOrEmbedded):
m_defaultLib = ZConfLib::createAvahiLib(avahiLibName,ZConfLib::createDnsSdLib(dnsSdLibName, ZConfLib::createEmbeddedLib(dnsSdDaemonPath)));
break;
default:
qDebug() << "invalid usage " << usage;
......@@ -411,7 +417,7 @@ void ServiceBrowser::reconfirmService(Service::ConstPtr service)
// ----------------- library initialization impl -----------------
/*!
Intializes the library used for the mdns queries.
Sets the library used by future Service Browsers to preform the mdns queries.
This changes the default library used by the next MainConnection, it does not change the already instantiated
connections.
\a usage can decide which libraries are tried,
......@@ -421,9 +427,9 @@ void ServiceBrowser::reconfirmService(Service::ConstPtr service)
\threadsafe
*/
void initLib(LibUsage usage, const QString &libName, const QString &daemonPath)
void setDefaultZConfLib(LibUsage usage, const QString &avahiLibName, const QString &dnsSdLibName, const QString &dnsSdDaemonPath)
{
zeroConfLibInstance()->setDefaultLib(usage, libName, daemonPath);
zeroConfLibInstance()->setDefaultLib(usage, avahiLibName, dnsSdLibName, dnsSdDaemonPath);
}
namespace Internal {
......@@ -536,7 +542,7 @@ ConnectionThread::ConnectionThread(MainConnection &mc, QObject *parent):
// ----------------- ServiceGatherer impl -----------------
ZConfLib *ServiceGatherer::lib()
ZConfLib::Ptr ServiceGatherer::lib()
{
return serviceBrowser->mainConnection->lib;
}
......@@ -971,7 +977,8 @@ ServiceBrowserPrivate::ServiceBrowserPrivate(const QString &serviceType, const Q
ServiceBrowserPrivate::~ServiceBrowserPrivate()
{
qDebug() << "destroying ServiceBrowserPrivate " << serviceType;
if (DEBUG_ZEROCONF)
qDebug() << "destroying ServiceBrowserPrivate " << serviceType;
if (browsing){
stopBrowsing();
}
......@@ -1172,7 +1179,7 @@ void MainConnection::stop(bool wait)
MainConnection::MainConnection():
lib(zeroConfLibInstance()->defaultLib()), m_lock(QMutex::Recursive), m_mainRef(0), m_failed(false), m_status(Starting), m_nErrs(0)
{
if (lib == 0){
if (lib.isNull()){
qDebug() << "could not load a valid library for ZeroConf::MainConnection, failing";
} else {
m_thread = new ConnectionThread(*this);
......@@ -1465,7 +1472,7 @@ QString ZConfLib::name(){
return QString::fromUtf8("ZeroConfLib@%1").arg(size_t(this),0,16);
}
ZConfLib::ZConfLib(ZConfLib * f) : fallbackLib(f), m_isOk(true)
ZConfLib::ZConfLib(ZConfLib::Ptr f) : fallbackLib(f), m_isOk(true)
{ }
ZConfLib::~ZConfLib()
......
......@@ -135,12 +135,14 @@ private:
};
enum LibUsage {
UseNativeOnly = 1,
UseDnsSdOnly = 1,
UseEmbeddedOnly,
UseNativeOrEmbedded
UseAvahiOnly,
UseAvahiOrDnsSd,
UseAvahiOrDnsSdOrEmbedded
};
void initLib(LibUsage usage, const QString &libName, const QString & daemonPaths);
void setDefaultZConfLib(LibUsage usage, const QString &avahiLibName, const QString &dnsSdLibName, const QString & dnsSdDaemonPath);
}
......
......@@ -58,6 +58,7 @@ class ServiceBrowserPrivate;
class ZConfLib {
Q_DECLARE_TR_FUNCTIONS(ZeroConf)
public:
typedef QSharedPointer<ZConfLib> Ptr;
typedef void *ConnectionRef;
typedef void *BrowserRef;
enum ProcessStatus {
......@@ -66,9 +67,9 @@ public:
ProcessedError,
ProcessedFailure
};
ZConfLib *fallbackLib;
Ptr fallbackLib;
ZConfLib(ZConfLib *fallBack);
ZConfLib(Ptr fallBack);
virtual ~ZConfLib();
virtual QString name();
......@@ -100,10 +101,9 @@ public:
QString errorMsg();
void setError(bool failure, const QString &eMsg);
static ZConfLib *createEmbeddedLib(const QString &daemonPath, ZConfLib *fallback=0);
static ZConfLib *createNativeLib(const QString &libName, ZConfLib *fallback=0);
static ZConfLib *createAvahiLib(const QString &libName, ZConfLib *fallback=0);
static ZConfLib *defaultLib();
static Ptr createEmbeddedLib(const QString &daemonPath, Ptr fallback = Ptr(0));
static Ptr createDnsSdLib(const QString &libName, Ptr fallback = Ptr(0));
static Ptr createAvahiLib(const QString &libName, Ptr fallback = Ptr(0));
protected:
bool m_isOk;
QString m_errorMsg;
......@@ -166,7 +166,7 @@ public:
void reload(qint32 interfaceIndex=0);
void remove();
void reconfirm();
ZConfLib *lib();
ZConfLib::Ptr lib();
private:
ServiceGatherer(const QString &newService, const QString &newType, const QString &newDomain,
const QString &fullName, uint32_t interfaceIndex, ServiceBrowserPrivate *serviceBrowser);
......@@ -201,7 +201,7 @@ public:
Stopping,
Stopped
};
ZConfLib *lib;
ZConfLib::Ptr lib;
MainConnection();
~MainConnection();
......
......@@ -8,9 +8,9 @@ DEFINES += ZEROCONF_LIBRARY
SOURCES += servicebrowser.cpp \
embeddedLib.cpp \
nativeLib.cpp \
mdnsderived.cpp \
avahiLib.cpp
avahiLib.cpp \
dnsSdLib.cpp
HEADERS += servicebrowser.h \
zeroconf_global.h \
......@@ -24,6 +24,3 @@ include(../../qtcreatorlibrary.pri)
win32{
LIBS += -lws2_32
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment