publicservice.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "publicservice.h"
00022 #include <netinet/in.h>
00023 #include <qapplication.h>
00024 #include "sdevent.h"
00025 #include "responder.h"
00026 #include "settings.h"
00027
00028 namespace DNSSD
00029 {
00030 #ifdef HAVE_DNSSD
00031 void publish_callback (DNSServiceRef, DNSServiceFlags, DNSServiceErrorType errorCode, const char *name,
00032 const char*, const char*, void *context);
00033 #endif
00034 class PublicServicePrivate : public Responder
00035 {
00036 public:
00037 PublicServicePrivate() : m_published(false)
00038 {}
00039 bool m_published;
00040 };
00041
00042 PublicService::PublicService(const QString& name, const QString& type, unsigned int port,
00043 const QString& domain)
00044 : QObject(), ServiceBase(name, type, QString::null, domain, port)
00045 {
00046 d = new PublicServicePrivate;
00047 if (domain.isNull())
00048 if (Configuration::publishType()==Configuration::EnumPublishType::LAN) m_domain="local.";
00049 else m_domain=Configuration::publishDomain();
00050 }
00051
00052
00053 PublicService::~PublicService()
00054 {
00055 stop();
00056 delete d;
00057 }
00058
00059 void PublicService::setServiceName(const QString& serviceName)
00060 {
00061 m_serviceName = serviceName;
00062 if (d->isRunning()) {
00063 stop();
00064 publishAsync();
00065 }
00066 }
00067
00068 void PublicService::setDomain(const QString& domain)
00069 {
00070 m_domain = domain;
00071 if (d->isRunning()) {
00072 stop();
00073 publishAsync();
00074 }
00075 }
00076
00077
00078 void PublicService::setType(const QString& type)
00079 {
00080 m_type = type;
00081 if (d->isRunning()) {
00082 stop();
00083 publishAsync();
00084 }
00085 }
00086
00087 void PublicService::setPort(unsigned short port)
00088 {
00089 m_port = port;
00090 if (d->isRunning()) {
00091 stop();
00092 publishAsync();
00093 }
00094 }
00095
00096 bool PublicService::isPublished() const
00097 {
00098 return d->m_published;
00099 }
00100
00101 void PublicService::setTextData(const QMap<QString,QString>& textData)
00102 {
00103 m_textData = textData;
00104 if (d->isRunning()) {
00105 stop();
00106 publishAsync();
00107 }
00108 }
00109
00110 bool PublicService::publish()
00111 {
00112 publishAsync();
00113 while (d->isRunning() && !d->m_published) d->process();
00114 return d->m_published;
00115 }
00116
00117 void PublicService::stop()
00118 {
00119 d->stop();
00120 d->m_published = false;
00121 }
00122
00123 void PublicService::publishAsync()
00124 {
00125 if (d->isRunning()) stop();
00126 #ifdef HAVE_DNSSD
00127 TXTRecordRef txt;
00128 TXTRecordCreate(&txt,0,0);
00129 QMap<QString,QString>::ConstIterator itEnd = m_textData.end();
00130 for (QMap<QString,QString>::ConstIterator it = m_textData.begin(); it!=itEnd ; ++it) {
00131 QCString value = it.data().utf8();
00132 if (TXTRecordSetValue(&txt,it.key().utf8(),value.length(),value)!=kDNSServiceErr_NoError) {
00133 TXTRecordDeallocate(&txt);
00134 emit published(false);
00135 return;
00136 }
00137 }
00138 DNSServiceRef ref;
00139 if (DNSServiceRegister(&ref,0,0,m_serviceName.utf8(),m_type.ascii(),domainToDNS(m_domain),NULL,
00140 htons(m_port),TXTRecordGetLength(&txt),TXTRecordGetBytesPtr(&txt),publish_callback,
00141 reinterpret_cast<void*>(this)) == kDNSServiceErr_NoError) d->setRef(ref);
00142 TXTRecordDeallocate(&txt);
00143 #endif
00144 if (!d->isRunning()) emit published(false);
00145 }
00146
00147 #ifdef HAVE_DNSSD
00148 void publish_callback (DNSServiceRef, DNSServiceFlags, DNSServiceErrorType errorCode, const char *name,
00149 const char*, const char*, void *context)
00150 {
00151 QObject *obj = reinterpret_cast<QObject*>(context);
00152 if (errorCode != kDNSServiceErr_NoError) {
00153 ErrorEvent err;
00154 QApplication::sendEvent(obj, &err);
00155 } else {
00156 PublishEvent pev(QString::fromUtf8(name));
00157 QApplication::sendEvent(obj, &pev);
00158 }
00159 }
00160 #endif
00161
00162
00163 void PublicService::customEvent(QCustomEvent* event)
00164 {
00165 if (event->type()==QEvent::User+SD_ERROR) {
00166 stop();
00167 emit published(false);
00168 }
00169 if (event->type()==QEvent::User+SD_PUBLISH) {
00170 d->m_published=true;
00171 emit published(true);
00172 m_serviceName = static_cast<PublishEvent*>(event)->m_name;
00173 }
00174 }
00175
00176 void PublicService::virtual_hook(int, void*)
00177 {
00178 }
00179
00180 }
00181
00182 #include "publicservice.moc"
This file is part of the documentation for dnssd Library Version 3.4.0.