23 #include "lookuphost.h" 27 #include <QMutexLocker> 40 static const int MAX_LOOKUPS = 20;
42 QAtomicInt workCounter;
50 #if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)) && (QT_VERSION < QT_VERSION_CHECK(5, 3, 0)) 54 return workCounter.load();
66 d->runningLookups = 0;
69 LookupHost::~LookupHost()
73 void LookupHost::lookupHost(
const QString &name, QObject *receiver,
const char *receiverSlot)
75 instance()->lookupHost_(name, receiver, receiverSlot);
78 void LookupHost::lookupHost_(
const QString &name, QObject *receiver,
const char *receiverSlot)
80 QMutexLocker locker(&d->lock);
87 QTimer *timer =
new QTimer();
90 QObject::connect(worker, SIGNAL(hostFound(QHostInfo)), receiver, receiverSlot);
91 worker->connect(timer, SIGNAL(timeout()), SLOT(work()));
92 timer->connect(timer, SIGNAL(timeout()), SLOT(deleteLater()));
96 worker->moveToThread(&d->thread);
97 timer->moveToThread(&d->thread);
100 void LookupHost::finalizeAndJoin()
104 gLog << tr(
"Finalizing LookupHost thread");
105 instance()->finalizeAndJoin_();
106 gLog << tr(
"Finalized LookupHost thread");
110 void LookupHost::finalizeAndJoin_()
113 QMutexLocker locker(&d->lock);
114 d->accepting =
false;
116 while (d->workCount() > 0)
118 d->thread.wait(1000);
123 void LookupHost::derefWorkCounter()
125 d->workCounter.deref();
133 inst->d->thread.start();
138 LookupHostWorker::LookupHostWorker(
const QString &hostName)
140 this->hostName = hostName;
143 void LookupHostWorker::work()
145 QMutexLocker locker(&LookupHost::instance()->d->lock);
146 if (LookupHost::instance()->d->accepting)
150 ++LookupHost::instance()->d->runningLookups;
151 QHostInfo::lookupHost(hostName,
this, SLOT(hostFoundReceived(QHostInfo)));
156 QTimer::singleShot(1000,
this, SLOT(work()));
161 LookupHost::instance()->derefWorkCounter();
166 void LookupHostWorker::hostFoundReceived(
const QHostInfo &hostInfo)
168 QMutexLocker locker(&LookupHost::instance()->d->lock);
169 --LookupHost::instance()->d->runningLookups;
170 LookupHost::instance()->derefWorkCounter();
171 emit hostFound(hostInfo);
175 void LookupHostConsumerThread::run()
177 qDebug() <<
"LookupHostConsumerThread:" << QThread::currentThreadId();