From 7ff39b3e6d01479af299a4c73f56fa07a6eddc78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Sun, 25 Jul 2010 13:09:05 +0200 Subject: [PATCH] Move service watching into avahi lib --- libqcp/qcp.cpp | 33 ------------------------------- libqcp/qcp.h | 31 ----------------------------- libqtavahi/qtavahi.cpp | 19 ++++++++++++++---- libqtavahi/qtavahi.h | 11 +++++++++-- libqtavahi/qtavahi_p.h | 41 +++++++++++++++++++++++++++++++++++++-- libqtavahi/qtavahimodel.h | 1 - 6 files changed, 63 insertions(+), 73 deletions(-) diff --git a/libqcp/qcp.cpp b/libqcp/qcp.cpp index 6fbdf56..cf06f6d 100644 --- a/libqcp/qcp.cpp +++ b/libqcp/qcp.cpp @@ -11,24 +11,6 @@ namespace QCP { connect(m_av_browse, SIGNAL(stopped()), this, SIGNAL(stopped())); } - WatchShare *Browse::watch(const Avahi::Service &service, QObject *parent) { - ShareData *sd = shareData(service); - WatchShare *ws; - if (!sd) return 0; - - sd->watcherList.append(ws = new WatchShare(parent, Share(this, service))); - ws->m_list_it = sd->watcherList.end()-1; - - return ws; - } - - void Browse::removeWatch(WatcherList::iterator it, const Avahi::Service &service) { - ShareData *sd = shareData(service); - if (!sd || WatcherList::iterator() == it) return; - - sd->watcherList.erase(it); - } - void Browse::foundService(Avahi::Service s) { ShareData *sd = new ShareData(); s.setData(sd); @@ -39,23 +21,8 @@ namespace QCP { emit lost(Share(this, s)); ShareData *sd = shareData(s); if (0 != sd) { - while (!sd->watcherList.empty()) { - WatchShare *ws = sd->watcherList.first(); - emit ws->lost(); - if (ws == sd->watcherList.first()) { - ws->m_list_it = WatcherList::iterator(); - sd->watcherList.pop_front(); - } - } delete sd; } sd->data = 0; } - - - WatchShare *Share::watch(QObject *parent) { - return m_browse->watch(m_service, parent); - } - } - diff --git a/libqcp/qcp.h b/libqcp/qcp.h index 4b6725c..2aef16b 100644 --- a/libqcp/qcp.h +++ b/libqcp/qcp.h @@ -21,11 +21,8 @@ namespace QCP { Q_OBJECT private: friend class QCP::Share; - friend class QCP::WatchShare; - typedef QLinkedList WatcherList; struct ShareData { - WatcherList watcherList; void *data; }; @@ -49,9 +46,6 @@ namespace QCP { void lostService(Avahi::Service s); private: - WatchShare *watch(const Avahi::Service &service, QObject *parent); - void removeWatch(WatcherList::iterator it, const Avahi::Service &service); - static inline ShareData* shareData(const Avahi::Service &service) { return static_cast(service.data()); } @@ -70,8 +64,6 @@ namespace QCP { public: Avahi::Service avahiService() const { return m_service; } - WatchShare *watch(QObject *parent = 0); - void* data() const { Browse::ShareData *sd = Browse::shareData(m_service); return sd ? sd->data : 0; } void setData(void *data) const { Browse::ShareData *sd = Browse::shareData(m_service); if (sd) sd->data = data; } @@ -79,29 +71,6 @@ namespace QCP { QCP::Browse *m_browse; Avahi::Service m_service; }; - - class LIBQCPSHARED_EXPORT WatchShare : public QObject { - Q_OBJECT - - friend class QCP::Share; - friend class QCP::Browse; - private: - inline WatchShare(QObject *parent, Share share) : QObject(parent), m_share(share) { } - - public: - virtual ~WatchShare() { - m_share.m_browse->removeWatch(m_list_it, m_share.m_service); - } - - inline Share share() { return m_share; } - - signals: - void lost(); - - private: - Share m_share; - Browse::WatcherList::iterator m_list_it; - }; } #endif // QCP_H diff --git a/libqtavahi/qtavahi.cpp b/libqtavahi/qtavahi.cpp index eaa2e38..09f9ea7 100644 --- a/libqtavahi/qtavahi.cpp +++ b/libqtavahi/qtavahi.cpp @@ -93,7 +93,7 @@ namespace Avahi { QList l = m_list.values(); m_list.clear(); for (int i = 0; i < l.size(); ++i) { - emit m_parent->lost(l.at(i)); + m_parent->lostService(l.at(i)); } } @@ -164,7 +164,7 @@ namespace Avahi { Service s = m_list.value(k); if (s.valid()) { m_list.remove(k); - emit m_parent->lost(s); + m_parent->lostService(s); } } break; @@ -213,12 +213,12 @@ namespace Avahi { Service s = m_list.value(k); if (s.valid()) { m_list.remove(k); - emit m_parent->lost(s); + m_parent->lostService(s); } s = Service(QString::fromUtf8(name), QString::fromUtf8(host_name), QString::fromUtf8(domain), fromAvahiAddress(address, interface), interface, protocol, port, qtxt); m_list.insert(k, s); - emit m_parent->found(s); + m_parent->foundService(s); } } @@ -525,6 +525,17 @@ namespace Avahi { return m_priv->m_list.values(); } + void Browse::foundService(Avahi::Service s) { + emit found(s); + } + + void Browse::lostService(Avahi::Service s) { + emit lost(s); + if (s.m_priv && s.m_priv->m_watcher) { + emit s.m_priv->m_watcher->lost(s); + } + } + Announce::Announce(QString serviceType, QString name, quint16 port, QHash txt, QObject *parent) : QObject(parent), m_priv(new priv::AnnounceP(serviceType, name, port, txt, this)) { diff --git a/libqtavahi/qtavahi.h b/libqtavahi/qtavahi.h index 84baab9..aca8981 100644 --- a/libqtavahi/qtavahi.h +++ b/libqtavahi/qtavahi.h @@ -24,6 +24,7 @@ inline uint qHash(const Avahi::ServiceKey& k); #include "qtavahi_p.h" namespace Avahi { + typedef Avahi::priv::ServiceWatch ServiceWatch; class LIBQTAVAHISHARED_EXPORT Service { public: @@ -50,8 +51,11 @@ namespace Avahi { inline void* data() const { return m_priv ? m_priv->m_data : 0; } inline void setData(void *data) const { if (m_priv) m_priv->m_data = data; } + inline Avahi::ServiceWatch* watcher() const { return m_priv ? m_priv->watcher() : 0; } + private: friend class Avahi::ServiceKey; + friend class Avahi::Browse; inline void assign(Avahi::priv::ServiceP *p) { if (p == m_priv) return; @@ -128,9 +132,12 @@ namespace Avahi { private: void clearList(); + void foundService(Avahi::Service s); + void lostService(Avahi::Service s); + signals: - void found(Avahi::Service i); - void lost(Avahi::Service i); + void found(Avahi::Service s); + void lost(Avahi::Service s); void started(); void stopped(); diff --git a/libqtavahi/qtavahi_p.h b/libqtavahi/qtavahi_p.h index f0cf808..4e6f8d8 100644 --- a/libqtavahi/qtavahi_p.h +++ b/libqtavahi/qtavahi_p.h @@ -1,30 +1,64 @@ #ifndef QTAVAHI_P_H #define QTAVAHI_P_H +#include "libqtavahi_global.h" + namespace Avahi { + class LIBQTAVAHISHARED_EXPORT Service; + class LIBQTAVAHISHARED_EXPORT ServiceKey; + class LIBQTAVAHISHARED_EXPORT Browse; + class LIBQTAVAHISHARED_EXPORT Announce; namespace priv { + class LIBQTAVAHISHARED_EXPORT ServiceWatch; class LIBQTAVAHISHARED_EXPORT ServiceP; class BrowseP; class AnnounceP; }; } +#include +#include + namespace Avahi { namespace priv { + class LIBQTAVAHISHARED_EXPORT ServiceWatch : public QObject { + Q_OBJECT + private: + friend class Avahi::priv::ServiceP; + friend class Avahi::Browse; + + inline ServiceWatch() { } + + signals: + void lost(Avahi::Service s); + }; + class LIBQTAVAHISHARED_EXPORT ServiceP { private: friend class Avahi::Service; friend class Avahi::ServiceKey; friend class Avahi::Browse; inline ServiceP(QString name, QString hostname, QString domain, QHostAddress address, int interface, int protocol, quint16 port, QHash txt) - : m_ref(1), m_name(name), m_hostname(hostname), m_domain(domain), m_address(address), m_interface(interface), m_protocol(protocol), m_port(port), m_txt(txt), m_data(0) { } + : m_ref(1), m_name(name), m_hostname(hostname), m_domain(domain), m_address(address), m_interface(interface), m_protocol(protocol), m_port(port), m_txt(txt), + m_data(0), m_watcher(0) { } + + ~ServiceP() { + delete m_watcher; + } Q_DISABLE_COPY(ServiceP); inline void ref() { m_ref.ref(); } inline void deref() { if (!m_ref.deref()) { delete this; } } + inline Avahi::priv::ServiceWatch* watcher() { + if (!m_watcher) { + m_watcher = new Avahi::priv::ServiceWatch(); + } + return m_watcher; + } + QAtomicInt m_ref; QString m_name, m_hostname, m_domain; @@ -33,7 +67,8 @@ namespace Avahi { quint16 m_port; QHash m_txt; - void* m_data; + void *m_data; + Avahi::priv::ServiceWatch *m_watcher; }; typedef enum { TCP, UDP } Protocol; @@ -47,4 +82,6 @@ namespace Avahi { } } +#include "qtavahi.h" + #endif // QTAVAHI_P_H diff --git a/libqtavahi/qtavahimodel.h b/libqtavahi/qtavahimodel.h index 9bf4096..8c10712 100644 --- a/libqtavahi/qtavahimodel.h +++ b/libqtavahi/qtavahimodel.h @@ -110,7 +110,6 @@ namespace Avahi { void remove(const Avahi::Service &s); private: - Avahi::Browse *m_browse; QMap m_services; }; }