00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "avahi-remoteservice_p.h"
00022 #include <netinet/in.h>
00023 #include <QtCore/QEventLoop>
00024 #include <QtGui/QApplication>
00025 #include <kdebug.h>
00026 #include "remoteservice.h"
00027 #include "avahi_server_interface.h"
00028 #include "avahi_serviceresolver_interface.h"
00029 #ifndef KDE_USE_FINAL
00030 Q_DECLARE_METATYPE(QList<QByteArray>)
00031 #endif
00032 namespace DNSSD
00033 {
00034
00035 RemoteService::RemoteService(const QString& name,const QString& type,const QString& domain)
00036 : ServiceBase(new RemoteServicePrivate(this, name,type,domain))
00037 {
00038 }
00039
00040
00041 RemoteService::~RemoteService()
00042 {
00043 }
00044
00045 bool RemoteService::resolve()
00046 {
00047 K_D;
00048 resolveAsync();
00049 while (d->m_running && !d->m_resolved) QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
00050 return d->m_resolved;
00051 }
00052
00053 void RemoteService::resolveAsync()
00054 {
00055 K_D;
00056 if (d->m_running) return;
00057 d->m_resolved = false;
00058 registerTypes();
00059 kDebug() << this << ":Starting resolve of : " << d->m_serviceName << " " << d->m_type << " " << d->m_domain << "\n";
00060 org::freedesktop::Avahi::Server s("org.freedesktop.Avahi","/",QDBusConnection::systemBus());
00061
00062 QDBusReply<QDBusObjectPath> rep=s.ServiceResolverNew(-1, -1, d->m_serviceName, d->m_type,
00063 domainToDNS(d->m_domain), -1, 8 );
00064 if (!rep.isValid()) {
00065 emit resolved(false);
00066 return;
00067 }
00068
00069 org::freedesktop::Avahi::ServiceResolver *b=new org::freedesktop::Avahi::ServiceResolver("org.freedesktop.Avahi",rep.value().path(),
00070 QDBusConnection::systemBus());
00071 connect(b,SIGNAL(Found(int,int,const QString &,const QString &,const QString &,const QString &, int, const QString &,ushort,
00072 const QList<QByteArray>&, uint)),d, SLOT(gotFound(int,int,const QString &,const QString &,const QString &,const QString &,
00073 int, const QString &,ushort , const QList<QByteArray>&, uint)));
00074 connect(b,SIGNAL(Failure(const QString&)),d, SLOT(gotError()));
00075 d->m_running=true;
00076 }
00077
00078 bool RemoteService::isResolved() const
00079 {
00080 K_D;
00081 return d->m_resolved;
00082 }
00083
00084 void RemoteServicePrivate::gotError()
00085 {
00086 m_resolved=false;
00087 stop();
00088
00089 emit m_parent->resolved(false);
00090 }
00091
00092 void RemoteServicePrivate::gotFound(int, int, const QString &name, const QString &, const QString &domain, const QString &host, int, const QString &, ushort port, const QList<QByteArray> &txt, uint)
00093 {
00094 m_serviceName = name;
00095 m_hostName = host;
00096 m_port = port;
00097 m_domain=DNSToDomain(domain);
00098 Q_FOREACH(const QByteArray &x, txt) {
00099 int pos=x.indexOf("=");
00100 if (pos==-1) m_textData[x]=QByteArray();
00101 else m_textData[x.mid(0,pos)]=x.mid(pos+1,x.size()-pos);
00102 }
00103 m_resolved = true;
00104 emit m_parent->resolved(true);
00105 }
00106
00107 void RemoteServicePrivate::stop()
00108 {
00109 if (m_resolver) m_resolver->Free();
00110 delete m_resolver;
00111 m_resolver=0;
00112 m_running=false;
00113 }
00114
00115 void RemoteService::virtual_hook(int, void*)
00116 {
00117
00118 }
00119
00120
00121 }
00122
00123 #include "remoteservice.moc"
00124 #include "avahi-remoteservice_p.moc"