• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • dnssd
 

dnssd

  • dnssd
remoteservice.cpp
1/* This file is part of the KDE project
2 *
3 * Copyright (C) 2004, 2005 Jakub Stachowski <qbast@go2.pl>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include <config.h>
22
23#include <tqeventloop.h>
24#include <tqapplication.h>
25#include <kurl.h>
26#ifdef HAVE_SYS_TYPES_H
27#include <sys/types.h>
28#endif
29#include <netinet/in.h>
30#ifdef HAVE_DNSSD
31#include <avahi-client/client.h>
32#include <avahi-common/strlst.h>
33#ifdef AVAHI_API_0_6
34#include <avahi-client/lookup.h>
35#endif
36#endif
37#include "remoteservice.h"
38#include "responder.h"
39#include "sdevent.h"
40
41namespace DNSSD
42{
43#ifdef HAVE_DNSSD
44#ifdef AVAHI_API_0_6
45void 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);
48#else
49void 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);
52#endif
53#endif
54
55class RemoteServicePrivate : public Responder
56{
57public:
58 RemoteServicePrivate() : m_resolved(false), m_running(false)
59#ifdef HAVE_DNSSD
60 , m_resolver(0)
61#endif
62 {}
63 bool m_resolved;
64 bool m_running;
65#ifdef HAVE_DNSSD
66 AvahiServiceResolver* m_resolver;
67 void stop() {
68 m_running = false;
69 if (m_resolver) avahi_service_resolver_free(m_resolver);
70 m_resolver=0;
71 }
72#endif
73};
74
75RemoteService::RemoteService(const TQString& label)
76{
77 decode(label);
78 d = new RemoteServicePrivate();
79}
80RemoteService::RemoteService(const TQString& name,const TQString& type,const TQString& domain)
81 : ServiceBase(name, type, domain)
82{
83 d = new RemoteServicePrivate();
84}
85
86RemoteService::RemoteService(const KURL& url)
87{
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();
93 m_port = url.port();
94 m_type = url.path().section('/',1,1);
95 m_serviceName = url.path().section('/',2);
96 m_textData = url.queryItems();
97 d->m_resolved=true;
98}
99
100RemoteService::~RemoteService()
101{
102#ifdef HAVE_DNSSD
103 if (d->m_resolver) avahi_service_resolver_free(d->m_resolver);
104#endif
105 delete d;
106}
107
108bool RemoteService::resolve()
109{
110 resolveAsync();
111 while (d->m_running && !d->m_resolved) Responder::self().process();
112#ifdef HAVE_DNSSD
113 d->stop();
114#endif
115 return d->m_resolved;
116}
117
118void RemoteService::resolveAsync()
119{
120 if (d->m_running) return;
121 d->m_resolved = false;
122 // FIXME: first protocol should be set?
123#ifdef HAVE_DNSSD
124#ifdef AVAHI_API_0_6
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);
128#else
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);
131#endif
132 if (d->m_resolver) d->m_running=true;
133 else emit resolved(false);
134#endif
135}
136
137bool RemoteService::isResolved() const
138{
139 return d->m_resolved;
140}
141
142void RemoteService::customEvent(TQCustomEvent* event)
143{
144 if (event->type() == TQEvent::User+SD_ERROR) {
145#ifdef HAVE_DNSSD
146 d->stop();
147#endif
148 d->m_resolved=false;
149 emit resolved(false);
150 }
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;
155 m_textData = rev->m_txtdata;
156 d->m_resolved = true;
157 emit resolved(true);
158 }
159}
160
161void RemoteService::virtual_hook(int, void*)
162{
163 // BASE::virtual_hook(int, void*);
164}
165
166TQDataStream & operator<< (TQDataStream & s, const RemoteService & a)
167{
168 s << (static_cast<ServiceBase>(a));
169 TQ_INT8 resolved = a.d->m_resolved ? 1:0;
170 s << resolved;
171 return s;
172}
173
174TQDataStream & operator>> (TQDataStream & s, RemoteService & a)
175{
176 // stop any possible resolve going on
177#ifdef HAVE_DNSSD
178 a.d->stop();
179#endif
180 TQ_INT8 resolved;
181 operator>>(s,(static_cast<ServiceBase&>(a)));
182 s >> resolved;
183 a.d->m_resolved = (resolved == 1);
184 return s;
185}
186
187#ifdef HAVE_DNSSD
188#ifdef AVAHI_API_0_6
189void 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)
192#else
193void 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)
196#endif
197{
198 TQObject *obj = reinterpret_cast<TQObject*>(context);
199 if (e != AVAHI_RESOLVER_FOUND) {
200 ErrorEvent err;
201 TQApplication::sendEvent(obj, &err);
202 return;
203 }
204 TQMap<TQString,TQString> map;
205 while (txt) {
206 char *key, *value;
207 size_t size;
208 if (avahi_string_list_get_pair(txt,&key,&value,&size)) break;
209 map[TQString::fromUtf8(key)]=(value) ? TQString::fromUtf8(value) : TQString::null;
210 txt = txt->next;
211 }
212 ResolveEvent rev(DNSToDomain(hostname),port,map);
213 TQApplication::sendEvent(obj, &rev);
214}
215#endif
216
217
218}
219
220#include "remoteservice.moc"
DNSSD::RemoteService::resolved
void resolved(bool)
Emitted when resolving is complete.
DNSSD::RemoteService::resolveAsync
void resolveAsync()
Resolves host name and port of service.
Definition: remoteservice.cpp:118
DNSSD::RemoteService::RemoteService
RemoteService(const TQString &label)
Creates unresolved service from given DNS label.
Definition: remoteservice.cpp:75
DNSSD::RemoteService::resolve
bool resolve()
Synchronous version of resolveAsync().
Definition: remoteservice.cpp:108
DNSSD::RemoteService::isResolved
bool isResolved() const
Returns true if service has been successfully resolved.
Definition: remoteservice.cpp:137
DNSSD::ServiceBase
This class is used to carry information about service.
Definition: servicebase.h:41
DNSSD::ServiceBase::m_textData
TQMap< TQString, TQString > m_textData
Map of TXT properties.
Definition: servicebase.h:99
DNSSD::ServiceBase::decode
void decode(const TQString &name)
Decode PTR label returned by DNS resolver into service name, type and domain.
Definition: servicebase.cpp:45

dnssd

Skip menu "dnssd"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

dnssd

Skip menu "dnssd"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for dnssd by doxygen 1.9.4
This website is maintained by Timothy Pearson.