KDNSSD

avahi-servicetypebrowser.cpp
1 /*
2  This file is part of the KDE project
3 
4  SPDX-FileCopyrightText: 2004, 2007 Jakub Stachowski <[email protected]>
5  SPDX-FileCopyrightText: 2018 Harald Sitter <[email protected]>
6 
7  SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9 
10 #include "avahi-servicetypebrowser_p.h"
11 #include <QSet>
12 #include "avahi_server_interface.h"
13 #include "servicetypebrowser.h"
14 #include "avahi_servicetypebrowser_interface.h"
15 
16 #define UNSPEC -1
17 namespace KDNSSD
18 {
19 
20 ServiceTypeBrowser::ServiceTypeBrowser(const QString &domain, QObject *parent) : QObject(parent), d(new ServiceTypeBrowserPrivate(this))
21 {
22  d->m_domain = domain;
23  d->m_timer.setSingleShot(true);
24 }
25 
26 ServiceTypeBrowser::~ServiceTypeBrowser()
27 {
28  delete d;
29 }
30 
32 {
33  if (d->m_started) {
34  return;
35  }
36  d->m_started = true;
37 
38  // Do not race!
39  // https://github.com/lathiat/avahi/issues/9
40  // Avahi's DBus API is incredibly racey with signals getting fired
41  // immediately after a request was made even though we may not yet be
42  // listening. In lieu of a proper upstream fix for this we'll unfortunately
43  // have to resort to this hack:
44  // We register to all signals regardless of path and then filter them once
45  // we know what "our" path is. This is much more fragile than a proper
46  // QDBusInterface assisted signal connection but unfortunately the only way
47  // we can reliably prevent signals getting lost in the race.
48  // This uses a fancy trick whereby using QDBusMessage as last argument will
49  // give us the correct signal argument types as well as the underlying
50  // message so that we may check the message path.
52  .connect("org.freedesktop.Avahi",
53  "",
54  "org.freedesktop.Avahi.ServiceTypeBrowser",
55  "ItemNew",
56  d,
57  SLOT(gotGlobalItemNew(int,int,QString,QString,uint,QDBusMessage)));
59  .connect("org.freedesktop.Avahi",
60  "",
61  "org.freedesktop.Avahi.ServiceTypeBrowser",
62  "ItemRemove",
63  d,
64  SLOT(gotGlobalItemRemove(int,int,QString,QString,uint,QDBusMessage)));
66  .connect("org.freedesktop.Avahi",
67  "",
68  "org.freedesktop.Avahi.ServiceTypeBrowser",
69  "AllForNow",
70  d,
71  SLOT(gotGlobalAllForNow(QDBusMessage)));
72  d->m_dbusObjectPath.clear();
73 
74  org::freedesktop::Avahi::Server s(QStringLiteral("org.freedesktop.Avahi"), QStringLiteral("/"), QDBusConnection::systemBus());
75 
76  QDBusReply<QDBusObjectPath> rep = s.ServiceTypeBrowserNew(-1, -1, d->m_domain, 0);
77  if (!rep.isValid()) {
78  return;
79  }
80 
81  d->m_dbusObjectPath = rep.value().path();
82 
83  // This is held because we need to explicitly Free it!
84  d->m_browser = new org::freedesktop::Avahi::ServiceTypeBrowser(
85  s.service(),
86  d->m_dbusObjectPath,
87  s.connection());
88 
89  connect(&d->m_timer, SIGNAL(timeout()), d, SLOT(finished()));
90  d->m_timer.start(domainIsLocal(d->m_domain) ? TIMEOUT_LAST_SERVICE : TIMEOUT_START_WAN);
91 }
92 
93 void ServiceTypeBrowserPrivate::finished()
94 {
95  m_timer.stop();
96  emit m_parent->finished();
97 }
98 
99 void ServiceTypeBrowserPrivate::gotGlobalItemNew(int interface,
100  int protocol,
101  const QString &type,
102  const QString &domain,
103  uint flags,
104  QDBusMessage msg)
105 {
106  if (!isOurMsg(msg)) {
107  return;
108  }
109  gotNewServiceType(interface, protocol, type, domain, flags);
110 }
111 
112 void ServiceTypeBrowserPrivate::gotGlobalItemRemove(int interface,
113  int protocol,
114  const QString &type,
115  const QString &domain,
116  uint flags,
117  QDBusMessage msg)
118 {
119  if (!isOurMsg(msg)) {
120  return;
121  }
122  gotRemoveServiceType(interface, protocol, type, domain, flags);
123 }
124 
125 void ServiceTypeBrowserPrivate::gotGlobalAllForNow(QDBusMessage msg)
126 {
127  if (!isOurMsg(msg)) {
128  return;
129  }
130  finished();
131 }
132 
133 void ServiceTypeBrowserPrivate::gotNewServiceType(int, int, const QString &type, const QString &, uint)
134 {
135  m_timer.start(TIMEOUT_LAST_SERVICE);
136  m_servicetypes += type;
137  emit m_parent->serviceTypeAdded(type);
138 }
139 
140 void ServiceTypeBrowserPrivate::gotRemoveServiceType(int, int, const QString &type, const QString &, uint)
141 {
142  m_timer.start(TIMEOUT_LAST_SERVICE);
143  m_servicetypes.removeAll(type);
144  emit m_parent->serviceTypeRemoved(type);
145 }
146 
148 {
149  return d->m_servicetypes;
150 }
151 
152 }
153 #include "moc_servicetypebrowser.cpp"
154 #include "moc_avahi-servicetypebrowser_p.cpp"
QDBusConnection systemBus()
bool isValid() const const
void finished()
Emitted when the list of published service types has settled.
ServiceTypeBrowser(const QString &domain=QString(), QObject *parent=nullptr)
Create a ServiceTypeBrowser for a domain.
QDBusReply::Type value() const const
void startBrowse()
Starts browsing.
bool connect(const QString &service, const QString &path, const QString &interface, const QString &name, QObject *receiver, const char *slot)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QStringList serviceTypes() const
All the service types currently being published.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Aug 10 2020 22:43:09 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.