Move service watching into avahi lib

This commit is contained in:
Stefan Bühler 2010-07-25 13:09:05 +02:00
parent b118d85e11
commit 7ff39b3e6d
6 changed files with 63 additions and 73 deletions

View File

@ -11,24 +11,6 @@ namespace QCP {
connect(m_av_browse, SIGNAL(stopped()), this, SIGNAL(stopped())); 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) { void Browse::foundService(Avahi::Service s) {
ShareData *sd = new ShareData(); ShareData *sd = new ShareData();
s.setData(sd); s.setData(sd);
@ -39,23 +21,8 @@ namespace QCP {
emit lost(Share(this, s)); emit lost(Share(this, s));
ShareData *sd = shareData(s); ShareData *sd = shareData(s);
if (0 != sd) { 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; delete sd;
} }
sd->data = 0; sd->data = 0;
} }
WatchShare *Share::watch(QObject *parent) {
return m_browse->watch(m_service, parent);
}
} }

View File

@ -21,11 +21,8 @@ namespace QCP {
Q_OBJECT Q_OBJECT
private: private:
friend class QCP::Share; friend class QCP::Share;
friend class QCP::WatchShare;
typedef QLinkedList<WatchShare*> WatcherList;
struct ShareData { struct ShareData {
WatcherList watcherList;
void *data; void *data;
}; };
@ -49,9 +46,6 @@ namespace QCP {
void lostService(Avahi::Service s); void lostService(Avahi::Service s);
private: 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) { static inline ShareData* shareData(const Avahi::Service &service) {
return static_cast<ShareData*>(service.data()); return static_cast<ShareData*>(service.data());
} }
@ -70,8 +64,6 @@ namespace QCP {
public: public:
Avahi::Service avahiService() const { return m_service; } 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* 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; } 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; QCP::Browse *m_browse;
Avahi::Service m_service; 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 #endif // QCP_H

View File

@ -93,7 +93,7 @@ namespace Avahi {
QList<Service> l = m_list.values(); QList<Service> l = m_list.values();
m_list.clear(); m_list.clear();
for (int i = 0; i < l.size(); ++i) { 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); Service s = m_list.value(k);
if (s.valid()) { if (s.valid()) {
m_list.remove(k); m_list.remove(k);
emit m_parent->lost(s); m_parent->lostService(s);
} }
} }
break; break;
@ -213,12 +213,12 @@ namespace Avahi {
Service s = m_list.value(k); Service s = m_list.value(k);
if (s.valid()) { if (s.valid()) {
m_list.remove(k); 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); s = Service(QString::fromUtf8(name), QString::fromUtf8(host_name), QString::fromUtf8(domain), fromAvahiAddress(address, interface), interface, protocol, port, qtxt);
m_list.insert(k, s); 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(); 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<QString,QString> txt, QObject *parent) Announce::Announce(QString serviceType, QString name, quint16 port, QHash<QString,QString> txt, QObject *parent)
: QObject(parent), m_priv(new priv::AnnounceP(serviceType, name, port, txt, this)) { : QObject(parent), m_priv(new priv::AnnounceP(serviceType, name, port, txt, this)) {

View File

@ -24,6 +24,7 @@ inline uint qHash(const Avahi::ServiceKey& k);
#include "qtavahi_p.h" #include "qtavahi_p.h"
namespace Avahi { namespace Avahi {
typedef Avahi::priv::ServiceWatch ServiceWatch;
class LIBQTAVAHISHARED_EXPORT Service { class LIBQTAVAHISHARED_EXPORT Service {
public: public:
@ -50,8 +51,11 @@ namespace Avahi {
inline void* data() const { return m_priv ? m_priv->m_data : 0; } 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 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: private:
friend class Avahi::ServiceKey; friend class Avahi::ServiceKey;
friend class Avahi::Browse;
inline void assign(Avahi::priv::ServiceP *p) { inline void assign(Avahi::priv::ServiceP *p) {
if (p == m_priv) return; if (p == m_priv) return;
@ -128,9 +132,12 @@ namespace Avahi {
private: private:
void clearList(); void clearList();
void foundService(Avahi::Service s);
void lostService(Avahi::Service s);
signals: signals:
void found(Avahi::Service i); void found(Avahi::Service s);
void lost(Avahi::Service i); void lost(Avahi::Service s);
void started(); void started();
void stopped(); void stopped();

View File

@ -1,30 +1,64 @@
#ifndef QTAVAHI_P_H #ifndef QTAVAHI_P_H
#define QTAVAHI_P_H #define QTAVAHI_P_H
#include "libqtavahi_global.h"
namespace Avahi { namespace Avahi {
class LIBQTAVAHISHARED_EXPORT Service;
class LIBQTAVAHISHARED_EXPORT ServiceKey;
class LIBQTAVAHISHARED_EXPORT Browse;
class LIBQTAVAHISHARED_EXPORT Announce;
namespace priv { namespace priv {
class LIBQTAVAHISHARED_EXPORT ServiceWatch;
class LIBQTAVAHISHARED_EXPORT ServiceP; class LIBQTAVAHISHARED_EXPORT ServiceP;
class BrowseP; class BrowseP;
class AnnounceP; class AnnounceP;
}; };
} }
#include <QObject>
#include <QHostAddress>
namespace Avahi { namespace Avahi {
namespace priv { 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 { class LIBQTAVAHISHARED_EXPORT ServiceP {
private: private:
friend class Avahi::Service; friend class Avahi::Service;
friend class Avahi::ServiceKey; friend class Avahi::ServiceKey;
friend class Avahi::Browse; friend class Avahi::Browse;
inline ServiceP(QString name, QString hostname, QString domain, QHostAddress address, int interface, int protocol, quint16 port, QHash<QString, QString> txt) inline ServiceP(QString name, QString hostname, QString domain, QHostAddress address, int interface, int protocol, quint16 port, QHash<QString, QString> 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); Q_DISABLE_COPY(ServiceP);
inline void ref() { m_ref.ref(); } inline void ref() { m_ref.ref(); }
inline void deref() { if (!m_ref.deref()) { delete this; } } 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; QAtomicInt m_ref;
QString m_name, m_hostname, m_domain; QString m_name, m_hostname, m_domain;
@ -33,7 +67,8 @@ namespace Avahi {
quint16 m_port; quint16 m_port;
QHash<QString, QString> m_txt; QHash<QString, QString> m_txt;
void* m_data; void *m_data;
Avahi::priv::ServiceWatch *m_watcher;
}; };
typedef enum { TCP, UDP } Protocol; typedef enum { TCP, UDP } Protocol;
@ -47,4 +82,6 @@ namespace Avahi {
} }
} }
#include "qtavahi.h"
#endif // QTAVAHI_P_H #endif // QTAVAHI_P_H

View File

@ -110,7 +110,6 @@ namespace Avahi {
void remove(const Avahi::Service &s); void remove(const Avahi::Service &s);
private: private:
Avahi::Browse *m_browse;
QMap<Avahi::ServiceKey, EntryList*> m_services; QMap<Avahi::ServiceKey, EntryList*> m_services;
}; };
} }