~ubuntu-branches/debian/squeeze/kdelibs/squeeze

« back to all changes in this revision

Viewing changes to dnssd/remoteservice.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Modestas Vainius
  • Date: 2010-08-07 23:20:21 UTC
  • Revision ID: james.westby@ubuntu.com-20100807232021-owvkgp5wpc076s33
Tags: 4:3.5.10.dfsg.1-5
* Change by email address to @debian.org.
* Drop common HTML docs from kdelibs-data package. Instead suggest
  kdelibs5-data which ships them (Closes: #591609). What's more, whoever
  wants to view docs, will have to install khelpcenter4 which pulls in
  kdelibs5-data anyway.
* Switch to dpkg-source format 3.0 (quilt):
  - drop simple-patchsys.mk from debian/rules;
  - add debian/patches/series file.
* Fix corruption of zip files caused by wrong encoding of umlauts in kzip
  (patch 67_kio_zip_file_encoding.diff). (Closes: #563942) Thanks to Bjoern
  Ricks for the patch.
* Support opening of KDE 4 khelpcenter in Help -> Handbook. (Closes: #525621)
  Thanks to Ben Burton for the patch.
* Do not recurse into .pc subdirectory with doxygen 
  (patch debian/patches/02_exclude_pc_from_dox.diff).
* Urgency=medium due to multiple RC bug fixes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
#include <sys/types.h>
28
28
#endif
29
29
#include <netinet/in.h>
 
30
#include <avahi-client/client.h>
 
31
#include <avahi-common/strlst.h>
 
32
#ifdef AVAHI_API_0_6
 
33
#include <avahi-client/lookup.h>
 
34
#endif
30
35
#include "remoteservice.h"
31
36
#include "responder.h"
32
37
#include "sdevent.h"
33
 
#include <kdebug.h>
34
38
 
35
39
namespace DNSSD
36
40
{
37
 
#ifdef HAVE_DNSSD
38
 
void resolve_callback    (    DNSServiceRef,
39
 
                                DNSServiceFlags,
40
 
                                uint32_t,
41
 
                                DNSServiceErrorType                 errorCode,
42
 
                                const char*,
43
 
                                const char                          *hosttarget,
44
 
                                uint16_t                            port,
45
 
                                uint16_t                            txtLen,
46
 
                                const unsigned char                 *txtRecord,
47
 
                                void                                *context
48
 
                         );
 
41
#ifdef AVAHI_API_0_6
 
42
void resolve_callback(AvahiServiceResolver*, AvahiIfIndex, AvahiProtocol proto, AvahiResolverEvent e,
 
43
    const char* name, const char* type, const char* domain, const char* hostname, const AvahiAddress* a,
 
44
    uint16_t port, AvahiStringList* txt, AvahiLookupResultFlags, void* context);
 
45
#else
 
46
void resolve_callback(AvahiServiceResolver*, AvahiIfIndex, AvahiProtocol proto, AvahiResolverEvent e,
 
47
    const char* name, const char* type, const char* domain, const char* hostname, const AvahiAddress* a,
 
48
    uint16_t port, AvahiStringList* txt, void* context);
 
49
#endif
49
50
 
50
 
#endif
51
51
class RemoteServicePrivate : public Responder
52
52
{
53
53
public:
54
 
        RemoteServicePrivate() : Responder(), m_resolved(false)
55
 
        {};
 
54
        RemoteServicePrivate() :  m_resolved(false), m_running(false), m_resolver(0) {}
56
55
        bool m_resolved;
 
56
        bool m_running;
 
57
        AvahiServiceResolver* m_resolver;
 
58
        void stop() {
 
59
            m_running = false;
 
60
            if (m_resolver) avahi_service_resolver_free(m_resolver);
 
61
            m_resolver=0;
 
62
        }
57
63
};
58
64
 
59
65
RemoteService::RemoteService(const QString& label)
83
89
 
84
90
RemoteService::~RemoteService()
85
91
{
 
92
        if (d->m_resolver) avahi_service_resolver_free(d->m_resolver);
86
93
        delete d;
87
94
}
88
95
 
89
96
bool RemoteService::resolve()
90
97
{
91
98
        resolveAsync();
92
 
        while (d->isRunning() && !d->m_resolved) d->process();
 
99
        while (d->m_running && !d->m_resolved) Responder::self().process();
93
100
        d->stop();
94
101
        return d->m_resolved;
95
102
}
96
103
 
97
104
void RemoteService::resolveAsync()
98
105
{
99
 
        if (d->isRunning()) return;
 
106
        if (d->m_running) return;
100
107
        d->m_resolved = false;
101
 
        kdDebug() << this << ":Starting resolve of : " << m_serviceName << " " << m_type << " " << m_domain << "\n";
102
 
#ifdef HAVE_DNSSD
103
 
        DNSServiceRef ref;
104
 
        if (DNSServiceResolve(&ref,0,0,m_serviceName.utf8(), m_type.ascii(), 
105
 
                domainToDNS(m_domain),(DNSServiceResolveReply)resolve_callback,reinterpret_cast<void*>(this))
106
 
                == kDNSServiceErr_NoError) d->setRef(ref);
 
108
        // FIXME: first protocol should be set?
 
109
#ifdef AVAHI_API_0_6
 
110
        d->m_resolver = avahi_service_resolver_new(Responder::self().client(),AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
 
111
            m_serviceName.utf8(), m_type.ascii(), domainToDNS(m_domain), AVAHI_PROTO_UNSPEC, AVAHI_LOOKUP_NO_ADDRESS,
 
112
            resolve_callback, this);
 
113
#else
 
114
        d->m_resolver = avahi_service_resolver_new(Responder::self().client(),AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
 
115
            m_serviceName.utf8(), m_type.ascii(), m_domain.utf8(), AVAHI_PROTO_UNSPEC, resolve_callback, this);
107
116
#endif
108
 
        if (!d->isRunning()) emit resolved(false);
 
117
        if (d->m_resolver) d->m_running=true;
 
118
            else  emit resolved(false);
109
119
}
110
120
 
111
121
bool RemoteService::isResolved() const
154
164
        return s;
155
165
}
156
166
 
157
 
 
158
 
#ifdef HAVE_DNSSD
159
 
void resolve_callback    (    DNSServiceRef,
160
 
                              DNSServiceFlags,
161
 
                              uint32_t,
162
 
                              DNSServiceErrorType                 errorCode,
163
 
                              const char*,
164
 
                              const char                          *hosttarget,
165
 
                              uint16_t                            port,
166
 
                              uint16_t                            txtLen,
167
 
                              const unsigned char                 *txtRecord,
168
 
                              void                                *context
169
 
                         )
 
167
#ifdef AVAHI_API_0_6
 
168
void resolve_callback(AvahiServiceResolver*, AvahiIfIndex, AvahiProtocol, AvahiResolverEvent e,
 
169
    const char*, const char*, const char*, const char* hostname, const AvahiAddress*,
 
170
    uint16_t port, AvahiStringList* txt, AvahiLookupResultFlags, void* context)
 
171
#else
 
172
void resolve_callback(AvahiServiceResolver*, AvahiIfIndex, AvahiProtocol, AvahiResolverEvent e,
 
173
    const char*, const char*, const char*, const char* hostname, const AvahiAddress*,
 
174
    uint16_t port, AvahiStringList* txt, void* context)
 
175
#endif
170
176
{
171
177
        QObject *obj = reinterpret_cast<QObject*>(context);
172
 
        if (errorCode != kDNSServiceErr_NoError) {
 
178
        if (e != AVAHI_RESOLVER_FOUND) {
173
179
                ErrorEvent err;
174
180
                QApplication::sendEvent(obj, &err);     
175
181
                return;
176
182
        }
177
 
        char key[256];
178
 
        int index=0;
179
 
        unsigned char valueLen;
180
 
        kdDebug() << "Resolve callback\n";
181
183
        QMap<QString,QString> map;
182
 
        const void *voidValue = 0;
183
 
        while (TXTRecordGetItemAtIndex(txtLen,txtRecord,index++,256,key,&valueLen,
184
 
                &voidValue) == kDNSServiceErr_NoError)  
185
 
        {
186
 
                if (voidValue) map[QString::fromUtf8(key)]=QString::fromUtf8((const char*)voidValue,valueLen);
187
 
                        else map[QString::fromUtf8(key)]=QString::null;
188
 
        }
189
 
        ResolveEvent rev(DNSToDomain(hosttarget),ntohs(port),map);
 
184
        while (txt) {
 
185
            char *key, *value;
 
186
            size_t size;
 
187
            if (avahi_string_list_get_pair(txt,&key,&value,&size)) break;
 
188
            map[QString::fromUtf8(key)]=(value) ? QString::fromUtf8(value) : QString::null;
 
189
            txt = txt->next;
 
190
        }
 
191
        ResolveEvent rev(DNSToDomain(hostname),port,map);
190
192
        QApplication::sendEvent(obj, &rev);
191
193
}
192
 
#endif
193
194
 
194
195
 
195
196
}