dnssd Library: query.cpp Source File (dnssd Library)

| # dnssd Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# query.cpp

    00001 /* This file is part of the KDE project
    00002  *
    00003  * Copyright (C) 2004 Jakub Stachowski <qbast@go2.pl>
    00004  *
    00005  * This library is free software; you can redistribute it and/or
    00006  * modify it under the terms of the GNU Library General Public
    00007  * License as published by the Free Software Foundation; either
    00008  * version 2 of the License, or (at your option) any later version.
    00009  *
    00010  * This library is distributed in the hope that it will be useful,
    00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013  * Library General Public License for more details.
    00014  *
    00015  * You should have received a copy of the GNU Library General Public License
    00016  * along with this library; see the file COPYING.LIB.  If not, write to
    00017  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00018  * Boston, MA 02111-1307, USA.
    00019  */
    00020
    00021 #include "query.h"
    00022 #include "responder.h"
    00023 #include "remoteservice.h"
    00024 #include "sdevent.h"
    00025 #include <qapplication.h>
    00026 #include <qtimer.h>
    00027
    00028 #define TIMEOUT_WAN 2000
    00029 #define TIMEOUT_LAN 200
    00030
    00031 namespace DNSSD
    00032 {
    00033 #ifdef HAVE_DNSSD
    00034 void query_callback (DNSServiceRef, DNSServiceFlags flags, uint32_t, DNSServiceErrorType errorCode,
    00035              const char *serviceName, const char *regtype, const char *replyDomain, void *context);
    00036 #endif
    00037 class QueryPrivate : public Responder
    00038 {
    00039 public:
    00040     QueryPrivate(const QString& type, const QString& domain) : Responder(), m_finished(false),
    00041     m_domain(domain), m_type(type)
    00042     {};
    00043     bool m_finished;
    00044     QString m_domain;
    00045     QTimer timeout;
    00046     QString m_type;
    00047 };
    00048
    00049 Query::Query(const QString& type, const QString& domain)
    00050 {
    00051     d = new QueryPrivate(type,domain);
    00052     connect(&d->timeout,SIGNAL(timeout()),this,SLOT(timeout()));
    00053 }
    00054
    00055
    00056 Query::~Query()
    00057 {
    00058     delete d;
    00059 }
    00060
    00061 bool Query::isRunning() const
    00062 {
    00063     return d->isRunning();
    00064 }
    00065
    00066 bool Query::isFinished() const
    00067 {
    00068     return d->m_finished;
    00069 }
    00070
    00071 const QString& Query::domain() const
    00072 {
    00073     return d->m_domain;
    00074 }
    00075
    00076 void Query::startQuery()
    00077 {
    00078     if (d->isRunning()) return;
    00079     d->m_finished = false;
    00080 #ifdef HAVE_DNSSD
    00081     DNSServiceRef ref;
    00082     if (DNSServiceBrowse(&ref,0,0, d->m_type.ascii(),
    00083         domainToDNS(d->m_domain),query_callback,reinterpret_cast<void*>(this))
    00084            == kDNSServiceErr_NoError) d->setRef(ref);
    00085 #endif
    00086     if (!d->isRunning()) emit finished();
    00087         else d->timeout.start(domainIsLocal(d->m_domain) ? TIMEOUT_LAN : TIMEOUT_WAN,true);
    00088 }
    00089 void Query::virtual_hook(int, void*)
    00090 {
    00091 }
    00092
    00093 void Query::customEvent(QCustomEvent* event)
    00094 {
    00095     if (event->type()==QEvent::User+SD_ERROR) {
    00096         d->stop();
    00097         d->m_finished=false;
    00098         emit finished();
    00099     }
    00100     if (event->type()==QEvent::User+SD_ADDREMOVE) {
    00101         AddRemoveEvent *aev = static_cast<AddRemoveEvent*>(event);
    00102         // m_type has useless trailing dot
    00103         RemoteService*  svr = new RemoteService(aev->m_name+"."+
    00104                 aev->m_type.left(aev->m_type.length()-1)+"."+aev->m_domain);
    00105         if (aev->m_op==AddRemoveEvent::Add) emit serviceAdded(svr);
    00106             else emit serviceRemoved(svr);
    00107         d->m_finished = aev->m_last;
    00108         if (d->m_finished) emit finished();
    00109     }
    00110 }
    00111
    00112 void Query::timeout()
    00113 {
    00114     d->m_finished=true;
    00115     emit finished();
    00116 }
    00117 #ifdef HAVE_DNSSD
    00118 void query_callback (DNSServiceRef, DNSServiceFlags flags, uint32_t, DNSServiceErrorType errorCode,
    00119              const char *serviceName, const char *regtype, const char *replyDomain,
    00120              void *context)
    00121 {
    00122     QObject *obj = reinterpret_cast<QObject*>(context);
    00123     if (errorCode != kDNSServiceErr_NoError) {
    00124         ErrorEvent err;
    00125         QApplication::sendEvent(obj, &err);
    00126     } else {
    00127         AddRemoveEvent arev((flags & kDNSServiceFlagsAdd) ? AddRemoveEvent::Add :
    00128             AddRemoveEvent::Remove, QString::fromUtf8(serviceName), regtype,
    00129             DNSToDomain(replyDomain), !(flags & kDNSServiceFlagsMoreComing));
    00130         QApplication::sendEvent(obj, &arev);
    00131     }
    00132 }
    00133 #endif
    00134 }
    00135 #include "query.moc"

This file is part of the documentation for dnssd Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:28:18 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003