22 #include "responder.h"
23 #include "remoteservice.h"
25 #include <tqdatetime.h>
26 #include <tqapplication.h>
30 #include <avahi-client/client.h>
32 #include <avahi-client/lookup.h>
36 #define TIMEOUT_LAN 200
43 void services_callback(AvahiServiceBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event,
const char* name,
44 const char* regtype,
const char* domain, AvahiLookupResultFlags,
void* context);
45 void types_callback(AvahiServiceTypeBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event,
const char* regtype,
46 const char* replyDomain, AvahiLookupResultFlags,
void* context);
48 void services_callback(AvahiServiceBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event,
const char* name,
49 const char* regtype,
const char* domain,
void* context);
50 void types_callback(AvahiServiceTypeBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event,
const char* regtype,
51 const char* replyDomain,
void* context);
52 void domains_callback(AvahiDomainBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event,
const char* replyDomain,
57 enum BrowserType { Types, Services };
62 QueryPrivate(
const TQString& type,
const TQString& domain) : m_finished(false), m_browser(0),
63 m_running(false), m_domain(domain), m_type(type) {}
66 BrowserType m_browserType;
76 d =
new QueryPrivate(type,
domain);
77 connect(&d->timeout,TQ_SIGNAL(timeout()),
this,TQ_SLOT(timeout()));
85 switch (d->m_browserType) {
86 case Services: avahi_service_browser_free((AvahiServiceBrowser*)d->m_browser);
break;
87 case Types: avahi_service_type_browser_free((AvahiServiceTypeBrowser*)d->m_browser);
break;
101 return d->m_finished;
111 if (d->m_running)
return;
112 d->m_finished =
false;
113 if (d->m_type==
"_services._dns-sd._udp") {
114 d->m_browserType = Types;
117 d->m_browser = avahi_service_type_browser_new(Responder::self().client(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
118 domainToDNS(d->m_domain), (AvahiLookupFlags)0, types_callback,
this);
120 d->m_browser = avahi_service_type_browser_new(Responder::self().client(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
121 d->m_domain.utf8(), types_callback,
this);
125 d->m_browserType = Services;
128 d->m_browser = avahi_service_browser_new(Responder::self().client(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
129 d->m_type.ascii(),domainToDNS(d->m_domain), (AvahiLookupFlags)0, services_callback,
this);
131 d->m_browser = avahi_service_browser_new(Responder::self().client(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
132 d->m_type.ascii(),d->m_domain.utf8(),services_callback,
this);
138 d->timeout.start(TIMEOUT_LAN,
true);
141 void Query::virtual_hook(
int,
void*)
145 void Query::customEvent(TQCustomEvent* event)
147 if (event->type()==TQEvent::User+SD_ADDREMOVE) {
148 d->timeout.start(TIMEOUT_LAN,
true);
150 AddRemoveEvent *aev =
static_cast<AddRemoveEvent*
>(event);
152 RemoteService* svr =
new RemoteService(aev->m_name,
153 aev->m_type,aev->m_domain);
154 if (aev->m_op==AddRemoveEvent::Add) emit
serviceAdded(svr);
159 void Query::timeout()
167 void services_callback (AvahiServiceBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event,
168 const char* serviceName,
const char* regtype,
const char* replyDomain, AvahiLookupResultFlags,
void* context)
170 void services_callback (AvahiServiceBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event,
171 const char* serviceName,
const char* regtype,
const char* replyDomain,
void* context)
174 TQObject *obj =
reinterpret_cast<TQObject*
>(context);
175 AddRemoveEvent* arev =
new AddRemoveEvent((event==AVAHI_BROWSER_NEW) ? AddRemoveEvent::Add :
176 AddRemoveEvent::Remove, TQString::fromUtf8(serviceName), regtype,
177 DNSToDomain(replyDomain));
178 TQApplication::postEvent(obj, arev);
182 void types_callback(AvahiServiceTypeBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event,
const char* regtype,
183 const char* replyDomain, AvahiLookupResultFlags,
void* context)
185 void types_callback(AvahiServiceTypeBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event,
const char* regtype,
186 const char* replyDomain,
void* context)
189 TQObject *obj =
reinterpret_cast<TQObject*
>(context);
190 AddRemoveEvent* arev =
new AddRemoveEvent((event==AVAHI_BROWSER_NEW) ? AddRemoveEvent::Add :
191 AddRemoveEvent::Remove, TQString::null, regtype,
192 DNSToDomain(replyDomain));
193 TQApplication::postEvent(obj, arev);
virtual void startQuery()
Starts query.
bool isFinished() const
Returns TRUE if all currently announced services has been reported.
bool isRunning() const
Returns TRUE if query is already running.
void finished()
Emitted when all announced services has been reported.
Query(const TQString &type, const TQString &domain)
Creates new query.
void serviceAdded(DNSSD::RemoteService::Ptr)
Emitted when new service has been discovered.
const TQString & domain() const
Returns queried domain.
void serviceRemoved(DNSSD::RemoteService::Ptr)
Emitted when previously discovered service is not longer published.