• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • dnssd
 

dnssd

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., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include "query.h"
00022 #include "responder.h"
00023 #include "remoteservice.h"
00024 #include "sdevent.h"
00025 #include <tqdatetime.h>
00026 #include <tqapplication.h>
00027 #include <tqtimer.h>
00028 
00029 #ifdef HAVE_DNSSD
00030 #include <avahi-client/client.h>
00031 #ifdef AVAHI_API_0_6
00032 #include <avahi-client/lookup.h>
00033 #endif
00034 #endif
00035 
00036 #define TIMEOUT_LAN 200
00037 
00038 namespace DNSSD
00039 {
00040 #ifdef HAVE_DNSSD
00041 #ifdef AVAHI_API_0_6
00042 
00043 void services_callback(AvahiServiceBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event, const char* name,
00044     const char* regtype, const char* domain, AvahiLookupResultFlags, void* context);
00045 void types_callback(AvahiServiceTypeBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event, const char* regtype,
00046     const char* replyDomain, AvahiLookupResultFlags, void* context);
00047 #else
00048 void services_callback(AvahiServiceBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event, const char* name,
00049     const char* regtype, const char* domain, void* context);
00050 void types_callback(AvahiServiceTypeBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event, const char* regtype,
00051     const char* replyDomain, void* context);
00052 void domains_callback(AvahiDomainBrowser*,  AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event, const char* replyDomain,
00053      void* context);
00054 #endif
00055 #endif
00056 
00057 enum BrowserType { Types, Services };
00058 
00059 class QueryPrivate
00060 {
00061 public:
00062     QueryPrivate(const TQString& type, const TQString& domain) : m_finished(false), m_browser(0),
00063     m_running(false), m_domain(domain), m_type(type) {}
00064 
00065     bool m_finished;
00066     BrowserType m_browserType;
00067     void* m_browser;
00068     bool m_running;
00069     TQString m_domain;
00070     TQTimer timeout;
00071     TQString m_type;
00072 };
00073 
00074 Query::Query(const TQString& type, const TQString& domain)
00075 {
00076     d = new QueryPrivate(type,domain);
00077     connect(&d->timeout,TQT_SIGNAL(timeout()),this,TQT_SLOT(timeout()));
00078 }
00079 
00080 
00081 Query::~Query()
00082 {
00083 #ifdef HAVE_DNSSD
00084     if (d->m_browser) {
00085         switch (d->m_browserType) {
00086         case Services: avahi_service_browser_free((AvahiServiceBrowser*)d->m_browser); break;
00087         case Types: avahi_service_type_browser_free((AvahiServiceTypeBrowser*)d->m_browser); break;
00088         }
00089     }
00090 #endif
00091     delete d;
00092 }
00093 
00094 bool Query::isRunning() const
00095 {
00096     return d->m_running;
00097 }
00098 
00099 bool Query::isFinished() const
00100 {
00101     return d->m_finished;
00102 }
00103 
00104 const TQString& Query::domain() const
00105 {
00106     return d->m_domain;
00107 }
00108 
00109 void Query::startQuery()
00110 {
00111     if (d->m_running) return;
00112     d->m_finished = false;
00113     if (d->m_type=="_services._dns-sd._udp") {
00114         d->m_browserType = Types;
00115 #ifdef HAVE_DNSSD
00116 #ifdef AVAHI_API_0_6
00117         d->m_browser = avahi_service_type_browser_new(Responder::self().client(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
00118         domainToDNS(d->m_domain), (AvahiLookupFlags)0, types_callback, this);
00119 #else
00120         d->m_browser = avahi_service_type_browser_new(Responder::self().client(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
00121         d->m_domain.utf8(), types_callback, this);
00122 #endif
00123 #endif
00124     } else {
00125         d->m_browserType = Services;
00126 #ifdef HAVE_DNSSD
00127 #ifdef AVAHI_API_0_6
00128         d->m_browser = avahi_service_browser_new(Responder::self().client(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
00129         d->m_type.ascii(),domainToDNS(d->m_domain),  (AvahiLookupFlags)0, services_callback,this);
00130 #else
00131         d->m_browser = avahi_service_browser_new(Responder::self().client(), AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
00132         d->m_type.ascii(),d->m_domain.utf8(),services_callback,this);
00133 #endif
00134 #endif
00135     }
00136     if (d->m_browser) {
00137         d->m_running=true;
00138         d->timeout.start(TIMEOUT_LAN,true);
00139     } else emit finished();
00140 }
00141 void Query::virtual_hook(int, void*)
00142 {
00143 }
00144 
00145 void Query::customEvent(TQCustomEvent* event)
00146 {
00147     if (event->type()==TQEvent::User+SD_ADDREMOVE) {
00148         d->timeout.start(TIMEOUT_LAN,true);
00149         d->m_finished=false;
00150         AddRemoveEvent *aev = static_cast<AddRemoveEvent*>(event);
00151         // m_type has useless trailing dot
00152         RemoteService*  svr = new RemoteService(aev->m_name,
00153                 aev->m_type,aev->m_domain);
00154         if (aev->m_op==AddRemoveEvent::Add) emit serviceAdded(svr);
00155             else emit serviceRemoved(svr);
00156     }
00157 }
00158 
00159 void Query::timeout()
00160 {
00161     d->m_finished=true;
00162     emit finished();
00163 }
00164 
00165 #ifdef HAVE_DNSSD
00166 #ifdef AVAHI_API_0_6
00167 void services_callback (AvahiServiceBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event,
00168     const char* serviceName, const char* regtype, const char* replyDomain, AvahiLookupResultFlags, void* context)
00169 #else
00170 void services_callback (AvahiServiceBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event,
00171     const char* serviceName, const char* regtype, const char* replyDomain, void* context)
00172 #endif
00173 {
00174     TQObject *obj = reinterpret_cast<TQObject*>(context);
00175     AddRemoveEvent* arev = new AddRemoveEvent((event==AVAHI_BROWSER_NEW) ? AddRemoveEvent::Add :
00176             AddRemoveEvent::Remove, TQString::fromUtf8(serviceName), regtype,
00177             DNSToDomain(replyDomain));
00178         TQApplication::postEvent(obj, arev);
00179 }
00180 
00181 #ifdef AVAHI_API_0_6
00182 void types_callback(AvahiServiceTypeBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event, const char* regtype,
00183     const char* replyDomain, AvahiLookupResultFlags, void* context)
00184 #else
00185 void types_callback(AvahiServiceTypeBrowser*, AvahiIfIndex, AvahiProtocol, AvahiBrowserEvent event, const char* regtype,
00186     const char* replyDomain, void* context)
00187 #endif
00188 {
00189     TQObject *obj = reinterpret_cast<TQObject*>(context);
00190     AddRemoveEvent* arev = new AddRemoveEvent((event==AVAHI_BROWSER_NEW) ? AddRemoveEvent::Add :
00191             AddRemoveEvent::Remove, TQString::null, regtype,
00192             DNSToDomain(replyDomain));
00193         TQApplication::postEvent(obj, arev);
00194 }
00195 #endif
00196 
00197 }
00198 #include "query.moc"

dnssd

Skip menu "dnssd"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

dnssd

Skip menu "dnssd"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for dnssd by doxygen 1.6.3
This website is maintained by Timothy Pearson.