Warning: That file was not part of the compilation database. It may have many parsing errors.

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 <netinet/in.h>
22#include <QtCore/QEventLoop>
23#include <QtCore/QCoreApplication>
24#include <kdebug.h>
25#include "remoteservice.h"
26#include "servicebase_p.h"
27#include "mdnsd-responder.h"
28#include "mdnsd-sdevent.h"
29
30namespace DNSSD
31{
32void resolve_callback ( DNSServiceRef,
33 DNSServiceFlags,
34 uint32_t,
35 DNSServiceErrorType errorCode,
36 const char*,
37 const char *hosttarget,
38 uint16_t port,
39 uint16_t txtLen,
40 const unsigned char *txtRecord,
41 void *context
42 );
43
44#define K_D RemoteServicePrivate* d=static_cast<RemoteServicePrivate*>(this->d)
45
46class RemoteServicePrivate : public Responder, public ServiceBasePrivate
47{
48public:
49 RemoteServicePrivate(RemoteService* parent, const QString& name,const QString& type,const QString& domain) :
50 Responder(), ServiceBasePrivate(name, type, domain, QString(), 0), m_resolved(false), m_parent(parent)
51 {}
52 bool m_resolved;
53 RemoteService* m_parent;
54 virtual void customEvent(QEvent* event);
55};
56
57RemoteService::RemoteService(const QString& name,const QString& type,const QString& domain)
58 : ServiceBase(new RemoteServicePrivate(this, name, type, domain))
59{
60}
61
62
63RemoteService::~RemoteService()
64{}
65
66bool RemoteService::resolve()
67{
68 K_D;
69 resolveAsync();
70 while (d->isRunning() && !d->m_resolved) d->process();
71 d->stop();
72 return d->m_resolved;
73}
74
75void RemoteService::resolveAsync()
76{
77 K_D;
78 if (d->isRunning()) return;
79 d->m_resolved = false;
80 kDebug() << this << ":Starting resolve of : " << d->m_serviceName << " " << d->m_type << " " << d->m_domain << "\n";
81 DNSServiceRef ref;
82 if (DNSServiceResolve(&ref,0,0,d->m_serviceName.toUtf8(), d->m_type.toLatin1().constData(),
83 domainToDNS(d->m_domain),(DNSServiceResolveReply)resolve_callback,reinterpret_cast<void*>(d))
84 == kDNSServiceErr_NoError) d->setRef(ref);
85 if (!d->isRunning()) emit resolved(false);
86}
87
88bool RemoteService::isResolved() const
89{
90 K_D;
91 return d->m_resolved;
92}
93
94void RemoteServicePrivate::customEvent(QEvent* event)
95{
96 if (event->type() == QEvent::User+SD_ERROR) {
97 stop();
98 m_resolved=false;
99 emit m_parent->resolved(false);
100 }
101 if (event->type() == QEvent::User+SD_RESOLVE) {
102 ResolveEvent* rev = static_cast<ResolveEvent*>(event);
103 m_hostName = rev->m_hostname;
104 m_port = rev->m_port;
105 m_textData = rev->m_txtdata;
106 m_resolved = true;
107 emit m_parent->resolved(true);
108 }
109}
110
111void RemoteService::virtual_hook(int, void*)
112{
113 // BASE::virtual_hook(int, void*);
114}
115
116
117void resolve_callback ( DNSServiceRef,
118 DNSServiceFlags,
119 uint32_t,
120 DNSServiceErrorType errorCode,
121 const char*,
122 const char *hosttarget,
123 uint16_t port,
124 uint16_t txtLen,
125 const unsigned char *txtRecord,
126 void *context
127 )
128{
129 QObject *obj = reinterpret_cast<QObject*>(context);
130 if (errorCode != kDNSServiceErr_NoError) {
131 ErrorEvent err;
132 QCoreApplication::sendEvent(obj, &err);
133 return;
134 }
135 char key[256];
136 int index=0;
137 unsigned char valueLen;
138 kDebug() << "Resolve callback\n";
139 QMap<QString,QByteArray> map;
140 const void *voidValue = 0;
141 while (TXTRecordGetItemAtIndex(txtLen,txtRecord,index++,256,key,&valueLen,
142 &voidValue) == kDNSServiceErr_NoError)
143 {
144 if (voidValue) map[QString::fromUtf8(key)]=QByteArray((const char*)voidValue,valueLen);
145 else map[QString::fromUtf8(key)].clear();
146 }
147 ResolveEvent rev(DNSToDomain(hosttarget),ntohs(port),map);
148 QCoreApplication::sendEvent(obj, &rev);
149}
150
151
152}
153
154#include "remoteservice.moc"
155

Warning: That file was not part of the compilation database. It may have many parsing errors.