DNSSD
mdnsd-responder.cpp
Go to the documentation of this file.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 "mdnsd-responder.h" 00022 #include "servicebase.h" 00023 #include <kurl.h> 00024 #include <QtCore/QCoreApplication> 00025 00026 namespace DNSSD 00027 { 00028 00029 Responder::Responder(DNSServiceRef ref,QObject *parent) 00030 : QObject(parent), m_ref(0), m_socket(0) 00031 { 00032 setRef(ref); 00033 } 00034 00035 void Responder::setRef(DNSServiceRef ref) 00036 { 00037 if (m_socket || m_ref) stop(); 00038 m_running = false; 00039 m_ref = ref; 00040 if (m_ref == 0 ) return; 00041 int fd = DNSServiceRefSockFD(ref); 00042 if (fd == -1) return; 00043 m_socket = new QSocketNotifier(fd,QSocketNotifier::Read,this); 00044 connect(m_socket,SIGNAL(activated(int)),this,SLOT(process())); 00045 m_running = true; 00046 } 00047 Responder::~Responder() 00048 { 00049 stop(); 00050 } 00051 00052 void Responder::stop() 00053 { 00054 if (m_socket) delete m_socket; 00055 m_socket = 0; 00056 if (m_ref) DNSServiceRefDeallocate(m_ref); 00057 m_ref = 0; 00058 m_running = false; 00059 } 00060 00061 00062 void Responder::process() 00063 { 00064 if ( DNSServiceProcessResult(m_ref) != kDNSServiceErr_NoError) stop(); 00065 } 00066 00067 bool Responder::isRunning() const 00068 { 00069 return m_running; 00070 } 00071 00072 QByteArray domainToDNS(const QString &domain) 00073 { 00074 if (domainIsLocal(domain)) return domain.toUtf8(); 00075 else return KUrl::toAce(domain); 00076 } 00077 00078 QString DNSToDomain(const char* domain) 00079 { 00080 if (domainIsLocal(domain)) return QString::fromUtf8(domain); 00081 else return KUrl::fromAce(domain); 00082 } 00083 00084 } 00085 #include "mdnsd-responder.moc"