23 #include "lookuphost.h"
27 #include <QMutexLocker>
40 static const int MAX_LOOKUPS = 20;
42 QAtomicInt workCounter;
54 d->runningLookups = 0;
57 LookupHost::~LookupHost()
61 void LookupHost::lookupHost(
const QString &name, QObject *receiver,
const char *receiverSlot)
63 instance()->lookupHost_(name, receiver, receiverSlot);
66 void LookupHost::lookupHost_(
const QString &name, QObject *receiver,
const char *receiverSlot)
68 QMutexLocker locker(&d->lock);
75 QTimer *timer =
new QTimer();
78 QObject::connect(worker, SIGNAL(hostFound(QHostInfo)), receiver, receiverSlot);
79 worker->connect(timer, SIGNAL(timeout()), SLOT(work()));
80 timer->connect(timer, SIGNAL(timeout()), SLOT(deleteLater()));
84 worker->moveToThread(&d->thread);
85 timer->moveToThread(&d->thread);
88 void LookupHost::finalizeAndJoin()
92 gLog << tr(
"Finalizing LookupHost thread");
93 instance()->finalizeAndJoin_();
94 gLog << tr(
"Finalized LookupHost thread");
98 void LookupHost::finalizeAndJoin_()
101 QMutexLocker locker(&d->lock);
102 d->accepting =
false;
104 while (d->workCounter > 0)
106 d->thread.wait(1000);
111 void LookupHost::derefWorkCounter()
113 d->workCounter.deref();
121 inst->d->thread.start();
126 LookupHostWorker::LookupHostWorker(
const QString &hostName)
128 this->hostName = hostName;
131 void LookupHostWorker::work()
133 QMutexLocker locker(&LookupHost::instance()->d->lock);
134 if (LookupHost::instance()->d->accepting)
138 ++LookupHost::instance()->d->runningLookups;
139 QHostInfo::lookupHost(hostName,
this, SLOT(hostFoundReceived(QHostInfo)));
144 QTimer::singleShot(1000,
this, SLOT(work()));
149 LookupHost::instance()->derefWorkCounter();
154 void LookupHostWorker::hostFoundReceived(
const QHostInfo &hostInfo)
156 QMutexLocker locker(&LookupHost::instance()->d->lock);
157 --LookupHost::instance()->d->runningLookups;
158 LookupHost::instance()->derefWorkCounter();
159 emit hostFound(hostInfo);
163 void LookupHostConsumerThread::run()
165 qDebug() <<
"LookupHostConsumerThread:" << QThread::currentThreadId();