23 #include <tqeventloop.h>
24 #include <tqapplication.h>
26 #ifdef HAVE_SYS_TYPES_H
27 #include <sys/types.h>
29 #include <netinet/in.h>
31 #include <avahi-client/client.h>
32 #include <avahi-common/strlst.h>
34 #include <avahi-client/lookup.h>
37 #include "remoteservice.h"
38 #include "responder.h"
45 void resolve_callback(AvahiServiceResolver*, AvahiIfIndex, AvahiProtocol proto, AvahiResolverEvent e,
46 const char* name,
const char* type,
const char* domain,
const char* hostname,
const AvahiAddress* a,
47 uint16_t port, AvahiStringList* txt, AvahiLookupResultFlags,
void* context);
49 void resolve_callback(AvahiServiceResolver*, AvahiIfIndex, AvahiProtocol proto, AvahiResolverEvent e,
50 const char* name,
const char* type,
const char* domain,
const char* hostname,
const AvahiAddress* a,
51 uint16_t port, AvahiStringList* txt,
void* context);
55 class RemoteServicePrivate :
public Responder
58 RemoteServicePrivate() : m_resolved(false), m_running(false)
66 AvahiServiceResolver* m_resolver;
69 if (m_resolver) avahi_service_resolver_free(m_resolver);
78 d =
new RemoteServicePrivate();
83 d =
new RemoteServicePrivate();
88 d =
new RemoteServicePrivate();
89 if (!url.isValid())
return;
90 if (url.protocol()!=
"invitation")
return;
91 if (!url.hasPath())
return;
92 m_hostName = url.host();
94 m_type = url.path().section(
'/',1,1);
95 m_serviceName = url.path().section(
'/',2);
100 RemoteService::~RemoteService()
103 if (d->m_resolver) avahi_service_resolver_free(d->m_resolver);
111 while (d->m_running && !d->m_resolved) Responder::self().process();
115 return d->m_resolved;
120 if (d->m_running)
return;
121 d->m_resolved =
false;
125 d->m_resolver = avahi_service_resolver_new(Responder::self().client(),AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
126 m_serviceName.utf8(), m_type.ascii(), domainToDNS(m_domain), AVAHI_PROTO_UNSPEC, AVAHI_LOOKUP_NO_ADDRESS,
127 resolve_callback,
this);
129 d->m_resolver = avahi_service_resolver_new(Responder::self().client(),AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
130 m_serviceName.utf8(), m_type.ascii(), m_domain.utf8(), AVAHI_PROTO_UNSPEC, resolve_callback,
this);
132 if (d->m_resolver) d->m_running=
true;
139 return d->m_resolved;
142 void RemoteService::customEvent(TQCustomEvent* event)
144 if (event->type() == TQEvent::User+SD_ERROR) {
151 if (event->type() == TQEvent::User+SD_RESOLVE) {
152 ResolveEvent* rev =
static_cast<ResolveEvent*
>(event);
153 m_hostName = rev->m_hostname;
154 m_port = rev->m_port;
156 d->m_resolved =
true;
161 void RemoteService::virtual_hook(
int,
void*)
166 TQDataStream & operator<< (TQDataStream & s,
const RemoteService & a)
168 s << (static_cast<ServiceBase>(a));
169 TQ_INT8 resolved = a.d->m_resolved ? 1:0;
174 TQDataStream & operator>> (TQDataStream & s, RemoteService & a)
181 operator>>(s,(
static_cast<ServiceBase&
>(a)));
183 a.d->m_resolved = (resolved == 1);
189 void resolve_callback(AvahiServiceResolver*, AvahiIfIndex, AvahiProtocol, AvahiResolverEvent e,
190 const char*,
const char*,
const char*,
const char* hostname,
const AvahiAddress*,
191 uint16_t port, AvahiStringList* txt, AvahiLookupResultFlags,
void* context)
193 void resolve_callback(AvahiServiceResolver*, AvahiIfIndex, AvahiProtocol, AvahiResolverEvent e,
194 const char*,
const char*,
const char*,
const char* hostname,
const AvahiAddress*,
195 uint16_t port, AvahiStringList* txt,
void* context)
198 TQObject *obj =
reinterpret_cast<TQObject*
>(context);
199 if (e != AVAHI_RESOLVER_FOUND) {
201 TQApplication::sendEvent(obj, &err);
204 TQMap<TQString,TQString> map;
208 if (avahi_string_list_get_pair(txt,&key,&value,&size))
break;
209 map[TQString::fromUtf8(key)]=(value) ? TQString::fromUtf8(value) : TQString::null;
212 ResolveEvent rev(DNSToDomain(hostname),port,map);
213 TQApplication::sendEvent(obj, &rev);
220 #include "remoteservice.moc"
void resolved(bool)
Emitted when resolving is complete.
void resolveAsync()
Resolves host name and port of service.
RemoteService(const TQString &label)
Creates unresolved service from given DNS label.
bool resolve()
Synchronous version of resolveAsync().
bool isResolved() const
Returns true if service has been successfully resolved.
This class is used to carry information about service.
TQMap< TQString, TQString > m_textData
Map of TXT properties.
void decode(const TQString &name)
Decode PTR label returned by DNS resolver into service name, type and domain.