1 /* This file is part of the KDE project
3 * Copyright (C) 2004, 2005 Jakub Stachowski <qbast@go2.pl>
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.
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.
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.
21 #include <netinet/in.h>
22 #include <QtCore/QEventLoop>
23 #include <QtCore/QCoreApplication>
25 #include "remoteservice.h"
26 #include "servicebase_p.h"
27 #include "mdnsd-responder.h"
28 #include "mdnsd-sdevent.h"
32 void resolve_callback ( DNSServiceRef
,
35 DNSServiceErrorType errorCode
,
37 const char *hosttarget
,
40 const unsigned char *txtRecord
,
44 #define K_D RemoteServicePrivate* d=static_cast<RemoteServicePrivate*>(this->d)
46 class RemoteServicePrivate
: public Responder
, public ServiceBasePrivate
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
)
53 RemoteService
* m_parent
;
54 virtual void customEvent(QEvent
* event
);
57 RemoteService::RemoteService(const QString
& name
,const QString
& type
,const QString
& domain
)
58 : ServiceBase(new RemoteServicePrivate(this, name
, type
, domain
))
63 RemoteService::~RemoteService()
66 bool RemoteService::resolve()
70 while (d
->isRunning() && !d
->m_resolved
) d
->process();
75 void RemoteService::resolveAsync()
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";
82 if (DNSServiceResolve(&ref
,0,0,d
->m_serviceName
.toUtf8(), d
->m_type
.toAscii().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);
88 bool RemoteService::isResolved() const
94 void RemoteServicePrivate::customEvent(QEvent
* event
)
96 if (event
->type() == QEvent::User
+SD_ERROR
) {
99 emit m_parent
->resolved(false);
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
;
107 emit m_parent
->resolved(true);
111 void RemoteService::virtual_hook(int, void*)
113 // BASE::virtual_hook(int, void*);
117 void resolve_callback ( DNSServiceRef
,
120 DNSServiceErrorType errorCode
,
122 const char *hosttarget
,
125 const unsigned char *txtRecord
,
129 QObject
*obj
= reinterpret_cast<QObject
*>(context
);
130 if (errorCode
!= kDNSServiceErr_NoError
) {
132 QCoreApplication::sendEvent(obj
, &err
);
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
)
144 if (voidValue
) map
[QString::fromUtf8(key
)]=QByteArray((const char*)voidValue
,valueLen
);
145 else map
[QString::fromUtf8(key
)].clear();
147 ResolveEvent
rev(DNSToDomain(hosttarget
),ntohs(port
),map
);
148 QCoreApplication::sendEvent(obj
, &rev
);
154 #include "remoteservice.moc"