• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

DNSSD

mdnsd-publicservice.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002  *
00003  * Copyright (C) 2004, 2005 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 <QtCore/QCoreApplication>
00022 #include <QtCore/QStringList>
00023 #include <netinet/in.h>
00024 #include "publicservice.h"
00025 #include "servicebase_p.h"
00026 #include "mdnsd-sdevent.h"
00027 #include "mdnsd-responder.h"
00028 #include "settings.h"
00029 
00030 #define K_D PublicServicePrivate* d=static_cast<PublicServicePrivate*>(this->d)
00031 
00032 namespace DNSSD
00033 {
00034 void publish_callback (DNSServiceRef, DNSServiceFlags, DNSServiceErrorType errorCode, const char *name,
00035                const char*, const char*, void *context);
00036 class PublicServicePrivate : public Responder, public ServiceBasePrivate
00037 {
00038 public:
00039     PublicServicePrivate(PublicService* parent, const QString& name, const QString& type, unsigned int port,
00040                   const QString& domain) : Responder(), ServiceBasePrivate(name, type, domain, QString(), port),
00041         m_published(false), m_parent(parent)
00042     {}
00043     bool m_published;
00044     PublicService* m_parent;
00045     QStringList m_subtypes;
00046     virtual void customEvent(QEvent* event);
00047 };
00048 
00049 PublicService::PublicService(const QString& name, const QString& type, unsigned int port,
00050                   const QString& domain, const QStringList& subtypes)
00051         : QObject(), ServiceBase(new PublicServicePrivate(this, name, type, port, domain))
00052 {
00053     K_D;
00054     if (domain.isNull())  d->m_domain="local.";
00055     d->m_subtypes=subtypes;
00056 }
00057 
00058 
00059 PublicService::~PublicService()
00060 {
00061     stop();
00062 }
00063 
00064 void PublicService::setServiceName(const QString& serviceName)
00065 {
00066     K_D;
00067     d->m_serviceName = serviceName;
00068     if (d->isRunning()) {
00069         stop();
00070         publishAsync();
00071     }
00072 }
00073 
00074 void PublicService::setDomain(const QString& domain)
00075 {
00076     K_D;
00077     d->m_domain = domain;
00078     if (d->isRunning()) {
00079     stop();
00080     publishAsync();
00081     }
00082 }
00083 
00084 QStringList PublicService::subtypes() const
00085 {
00086     K_D;
00087     return d->m_subtypes;
00088 }
00089 
00090 void PublicService::setType(const QString& type)
00091 {
00092     K_D;
00093     d->m_type = type;
00094     if (d->isRunning()) {
00095         stop();
00096         publishAsync();
00097     }
00098 }
00099 
00100 void PublicService::setSubTypes(const QStringList& subtypes)
00101 {
00102     K_D;
00103     d->m_subtypes = subtypes;
00104     if (d->isRunning()) {
00105         stop();
00106         publishAsync();
00107     }
00108 }
00109 
00110 void PublicService::setPort(unsigned short port)
00111 {
00112     K_D;
00113     d->m_port = port;
00114     if (d->isRunning()) {
00115         stop();
00116         publishAsync();
00117     }
00118 }
00119 
00120 bool PublicService::isPublished() const
00121 {
00122     K_D;
00123     return d->m_published;
00124 }
00125 
00126 void PublicService::setTextData(const QMap<QString,QByteArray>& textData)
00127 {
00128     K_D;
00129     d->m_textData = textData;
00130     if (d->isRunning()) {
00131         stop();
00132         publishAsync();
00133     }
00134 }
00135 
00136 bool PublicService::publish()
00137 {
00138     K_D;
00139     publishAsync();
00140     while (d->isRunning() && !d->m_published) d->process();
00141     return d->m_published;
00142 }
00143 
00144 void PublicService::stop()
00145 {
00146     K_D;
00147     d->stop();
00148     d->m_published = false;
00149 }
00150 
00151 void PublicService::publishAsync()
00152 {
00153     K_D;
00154     if (d->isRunning()) stop();
00155     TXTRecordRef txt;
00156     TXTRecordCreate(&txt,0,0);
00157     QMap<QString,QByteArray>::ConstIterator itEnd = d->m_textData.end();
00158     for (QMap<QString,QByteArray>::ConstIterator it = d->m_textData.begin(); it!=itEnd ; ++it) {
00159         if (TXTRecordSetValue(&txt,it.key().toUtf8(),it.value().length(),it.value())!=kDNSServiceErr_NoError) {
00160             TXTRecordDeallocate(&txt);
00161             emit published(false);
00162             return;
00163         }
00164     }
00165     DNSServiceRef ref;
00166     QString fullType=d->m_type;
00167     Q_FOREACH(const QString &subtype, d->m_subtypes) fullType+=','+subtype;
00168     if (DNSServiceRegister(&ref,0,0,d->m_serviceName.toUtf8(),fullType.toAscii().constData(),domainToDNS(d->m_domain),NULL,
00169         htons(d->m_port),TXTRecordGetLength(&txt),TXTRecordGetBytesPtr(&txt),publish_callback,
00170         reinterpret_cast<void*>(d)) == kDNSServiceErr_NoError) d->setRef(ref);
00171     TXTRecordDeallocate(&txt);
00172     if (!d->isRunning()) emit published(false);
00173 }
00174 
00175 void publish_callback (DNSServiceRef, DNSServiceFlags, DNSServiceErrorType errorCode, const char *name,
00176                const char*, const char*, void *context)
00177 {
00178     QObject *obj = reinterpret_cast<QObject*>(context);
00179     if (errorCode != kDNSServiceErr_NoError) {
00180         ErrorEvent err;
00181         QCoreApplication::sendEvent(obj, &err);
00182     } else {
00183         PublishEvent pev(QString::fromUtf8(name));
00184         QCoreApplication::sendEvent(obj, &pev);
00185     }
00186 }
00187 
00188 void PublicServicePrivate::customEvent(QEvent* event)
00189 {
00190     if (event->type()==QEvent::User+SD_ERROR) {
00191         m_parent->stop();
00192         emit m_parent->published(false);
00193     }
00194     if (event->type()==QEvent::User+SD_PUBLISH) {
00195         m_published=true;
00196         emit m_parent->published(true);
00197         m_serviceName = static_cast<PublishEvent*>(event)->m_name;
00198     }
00199 }
00200 
00201 void PublicService::virtual_hook(int, void*)
00202 {
00203 }
00204 
00205 }
00206 
00207 #include "publicservice.moc"

DNSSD

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal