From 25c983a448de2fbb4bb912386d4ab1af5f699f80 Mon Sep 17 00:00:00 2001 From: Fawzi Mohamed Date: Thu, 27 Oct 2011 16:57:51 +0200 Subject: [PATCH] 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: Christian Kandeler --- src/libs/zeroconf/avahiLib.cpp | 11 ++-- .../zeroconf/{nativeLib.cpp => dnsSdLib.cpp} | 64 +++++++++---------- src/libs/zeroconf/embeddedLib.cpp | 8 +-- src/libs/zeroconf/servicebrowser.cpp | 45 +++++++------ src/libs/zeroconf/servicebrowser.h | 8 ++- src/libs/zeroconf/servicebrowser_p.h | 16 ++--- src/libs/zeroconf/zeroconf.pro | 7 +- 7 files changed, 83 insertions(+), 76 deletions(-) rename src/libs/zeroconf/{nativeLib.cpp => dnsSdLib.cpp} (85%) diff --git a/src/libs/zeroconf/avahiLib.cpp b/src/libs/zeroconf/avahiLib.cpp index bc4e0086630..bec4e3e595b 100644 --- a/src/libs/zeroconf/avahiLib.cpp +++ b/src/libs/zeroconf/avahiLib.cpp @@ -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(sg->serviceBrowser->mainConnection->lib); + ZConfLib::Ptr libBase = sg->serviceBrowser->mainConnection->lib; + AvahiZConfLib *lib = dynamic_cast(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; } diff --git a/src/libs/zeroconf/nativeLib.cpp b/src/libs/zeroconf/dnsSdLib.cpp similarity index 85% rename from src/libs/zeroconf/nativeLib.cpp rename to src/libs/zeroconf/dnsSdLib.cpp index c9fd4d5ad98..61ed14db6a8 100644 --- a/src/libs/zeroconf/nativeLib.cpp +++ b/src/libs/zeroconf/dnsSdLib.cpp @@ -43,7 +43,7 @@ #include -#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(nativeLib.resolve("DNSServiceRefDeallocate")); - m_resolve = reinterpret_cast(nativeLib.resolve("DNSServiceResolve")); - m_queryRecord = reinterpret_cast(nativeLib.resolve("DNSServiceQueryRecord")); - m_getAddrInfo = reinterpret_cast(nativeLib.resolve("DNSServiceGetAddrInfo")); - m_reconfirmRecord = reinterpret_cast(nativeLib.resolve("DNSServiceReconfirmRecord")); - m_browse = reinterpret_cast(nativeLib.resolve("DNSServiceBrowse")); - m_getProperty = reinterpret_cast(nativeLib.resolve("DNSServiceGetProperty")); - m_processResult = reinterpret_cast(nativeLib.resolve("DNSServiceProcessResult")) ; - m_createConnection = reinterpret_cast(nativeLib.resolve("DNSServiceCreateConnection")); - m_refSockFD = reinterpret_cast(nativeLib.resolve("DNSServiceRefSockFD")); + m_refDeallocate = reinterpret_cast(dnsSdLib.resolve("DNSServiceRefDeallocate")); + m_resolve = reinterpret_cast(dnsSdLib.resolve("DNSServiceResolve")); + m_queryRecord = reinterpret_cast(dnsSdLib.resolve("DNSServiceQueryRecord")); + m_getAddrInfo = reinterpret_cast(dnsSdLib.resolve("DNSServiceGetAddrInfo")); + m_reconfirmRecord = reinterpret_cast(dnsSdLib.resolve("DNSServiceReconfirmRecord")); + m_browse = reinterpret_cast(dnsSdLib.resolve("DNSServiceBrowse")); + m_getProperty = reinterpret_cast(dnsSdLib.resolve("DNSServiceGetProperty")); + m_processResult = reinterpret_cast(dnsSdLib.resolve("DNSServiceProcessResult")) ; + m_createConnection = reinterpret_cast(dnsSdLib.resolve("DNSServiceCreateConnection")); + m_refSockFD = reinterpret_cast(dnsSdLib.resolve("DNSServiceRefSockFD")); #else // static linking m_refDeallocate = reinterpret_cast(&DNSServiceRefDeallocate); @@ -143,24 +143,24 @@ public: m_refSockFD = reinterpret_cast(&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; } diff --git a/src/libs/zeroconf/embeddedLib.cpp b/src/libs/zeroconf/embeddedLib.cpp index aca6bf7eca9..6c72402f965 100644 --- a/src/libs/zeroconf/embeddedLib.cpp +++ b/src/libs/zeroconf/embeddedLib.cpp @@ -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; } diff --git a/src/libs/zeroconf/servicebrowser.cpp b/src/libs/zeroconf/servicebrowser.cpp index c59cf94831c..35b785b835f 100644 --- a/src/libs/zeroconf/servicebrowser.cpp +++ b/src/libs/zeroconf/servicebrowser.cpp @@ -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("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() diff --git a/src/libs/zeroconf/servicebrowser.h b/src/libs/zeroconf/servicebrowser.h index 80b15a19425..a7550d07a2b 100644 --- a/src/libs/zeroconf/servicebrowser.h +++ b/src/libs/zeroconf/servicebrowser.h @@ -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); } diff --git a/src/libs/zeroconf/servicebrowser_p.h b/src/libs/zeroconf/servicebrowser_p.h index 5825d3831bb..b6cc8a21b9b 100644 --- a/src/libs/zeroconf/servicebrowser_p.h +++ b/src/libs/zeroconf/servicebrowser_p.h @@ -58,6 +58,7 @@ class ServiceBrowserPrivate; class ZConfLib { Q_DECLARE_TR_FUNCTIONS(ZeroConf) public: + typedef QSharedPointer 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(); diff --git a/src/libs/zeroconf/zeroconf.pro b/src/libs/zeroconf/zeroconf.pro index 6dca3c066b5..b5be50fc833 100644 --- a/src/libs/zeroconf/zeroconf.pro +++ b/src/libs/zeroconf/zeroconf.pro @@ -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 } - - -