kernel/qhostinfo.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtNetwork module of the Qt Toolkit. -
7** -
8** $QT_BEGIN_LICENSE:LGPL$ -
9** Commercial License Usage -
10** Licensees holding valid commercial Qt licenses may use this file in -
11** accordance with the commercial license agreement provided with the -
12** Software or, alternatively, in accordance with the terms contained in -
13** a written agreement between you and Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/contact-us. -
16** -
17** GNU Lesser General Public License Usage -
18** Alternatively, this file may be used under the terms of the GNU Lesser -
19** General Public License version 2.1 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42#include "qhostinfo.h" -
43#include "qhostinfo_p.h" -
44 -
45#include "QtCore/qscopedpointer.h" -
46#include <qabstracteventdispatcher.h> -
47#include <qcoreapplication.h> -
48#include <qmetaobject.h> -
49#include <qstringlist.h> -
50#include <qthread.h> -
51#include <qurl.h> -
52#include <private/qnetworksession_p.h> -
53 -
54#ifdef Q_OS_UNIX -
55# include <unistd.h> -
56#endif -
57 -
58QT_BEGIN_NAMESPACE -
59 -
60//#define QHOSTINFO_DEBUG -
61 -
62Q_GLOBAL_STATIC(QHostInfoLookupManager, theHostInfoLookupManager)
never executed: delete x;
executed: return thisGlobalStatic.pointer.load();
Execution Count:2483
partially evaluated: !thisGlobalStatic.pointer.testAndSetOrdered(0, x)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:13
evaluated: !thisGlobalStatic.pointer.load()
TRUEFALSE
yes
Evaluation Count:13
yes
Evaluation Count:2470
partially evaluated: !thisGlobalStatic.destroyed
TRUEFALSE
yes
Evaluation Count:13
no
Evaluation Count:0
0-2483
63 -
64/*! -
65 \class QHostInfo -
66 \brief The QHostInfo class provides static functions for host name lookups. -
67 -
68 \reentrant -
69 \inmodule QtNetwork -
70 \ingroup network -
71 -
72 QHostInfo uses the lookup mechanisms provided by the operating -
73 system to find the IP address(es) associated with a host name, -
74 or the host name associated with an IP address. -
75 The class provides two static convenience functions: one that -
76 works asynchronously and emits a signal once the host is found, -
77 and one that blocks and returns a QHostInfo object. -
78 -
79 To look up a host's IP addresses asynchronously, call lookupHost(), -
80 which takes the host name or IP address, a receiver object, and a slot -
81 signature as arguments and returns an ID. You can abort the -
82 lookup by calling abortHostLookup() with the lookup ID. -
83 -
84 Example: -
85 -
86 \snippet code/src_network_kernel_qhostinfo.cpp 0 -
87 -
88 -
89 The slot is invoked when the results are ready. The results are -
90 stored in a QHostInfo object. Call -
91 addresses() to get the list of IP addresses for the host, and -
92 hostName() to get the host name that was looked up. -
93 -
94 If the lookup failed, error() returns the type of error that -
95 occurred. errorString() gives a human-readable description of the -
96 lookup error. -
97 -
98 If you want a blocking lookup, use the QHostInfo::fromName() function: -
99 -
100 \snippet code/src_network_kernel_qhostinfo.cpp 1 -
101 -
102 QHostInfo supports Internationalized Domain Names (IDNs) through the -
103 IDNA and Punycode standards. -
104 -
105 To retrieve the name of the local host, use the static -
106 QHostInfo::localHostName() function. -
107 -
108 \note Since Qt 4.6.1 QHostInfo is using multiple threads for DNS lookup -
109 instead of one dedicated DNS thread. This improves performance, -
110 but also changes the order of signal emissions when using lookupHost() -
111 compared to previous versions of Qt. -
112 \note Since Qt 4.6.3 QHostInfo is using a small internal 60 second DNS cache -
113 for performance improvements. -
114 -
115 \sa QAbstractSocket, {http://www.rfc-editor.org/rfc/rfc3492.txt}{RFC 3492} -
116*/ -
117 -
118static QBasicAtomicInt theIdCounter = Q_BASIC_ATOMIC_INITIALIZER(1); -
119 -
120/*! -
121 Looks up the IP address(es) associated with host name \a name, and -
122 returns an ID for the lookup. When the result of the lookup is -
123 ready, the slot or signal \a member in \a receiver is called with -
124 a QHostInfo argument. The QHostInfo object can then be inspected -
125 to get the results of the lookup. -
126 -
127 The lookup is performed by a single function call, for example: -
128 -
129 \snippet code/src_network_kernel_qhostinfo.cpp 2 -
130 -
131 The implementation of the slot prints basic information about the -
132 addresses returned by the lookup, or reports an error if it failed: -
133 -
134 \snippet code/src_network_kernel_qhostinfo.cpp 3 -
135 -
136 If you pass a literal IP address to \a name instead of a host name, -
137 QHostInfo will search for the domain name for the IP (i.e., QHostInfo will -
138 perform a \e reverse lookup). On success, the resulting QHostInfo will -
139 contain both the resolved domain name and IP addresses for the host -
140 name. Example: -
141 -
142 \snippet code/src_network_kernel_qhostinfo.cpp 4 -
143 -
144 \note There is no guarantee on the order the signals will be emitted -
145 if you start multiple requests with lookupHost(). -
146 -
147 \sa abortHostLookup(), addresses(), error(), fromName() -
148*/ -
149int QHostInfo::lookupHost(const QString &name, QObject *receiver, -
150 const char *member) -
151{ -
152#if defined QHOSTINFO_DEBUG -
153 qDebug("QHostInfo::lookupHost(\"%s\", %p, %s)", -
154 name.toLatin1().constData(), receiver, member ? member + 1 : 0); -
155#endif -
156 -
157 if (!QAbstractEventDispatcher::instance(QThread::currentThread())) {
partially evaluated: !QAbstractEventDispatcher::instance(QThread::currentThread())
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:132
0-132
158 qWarning("QHostInfo::lookupHost() called with no event dispatcher");
never executed (the execution status of this line is deduced): QMessageLogger("kernel/qhostinfo.cpp", 158, __PRETTY_FUNCTION__).warning("QHostInfo::lookupHost() called with no event dispatcher");
-
159 return -1;
never executed: return -1;
0
160 } -
161 -
162 qRegisterMetaType<QHostInfo>();
executed (the execution status of this line is deduced): qRegisterMetaType<QHostInfo>();
-
163 -
164 int id = theIdCounter.fetchAndAddRelaxed(1); // generate unique ID
executed (the execution status of this line is deduced): int id = theIdCounter.fetchAndAddRelaxed(1);
-
165 -
166 if (name.isEmpty()) {
partially evaluated: name.isEmpty()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:132
0-132
167 QHostInfo hostInfo(id);
never executed (the execution status of this line is deduced): QHostInfo hostInfo(id);
-
168 hostInfo.setError(QHostInfo::HostNotFound);
never executed (the execution status of this line is deduced): hostInfo.setError(QHostInfo::HostNotFound);
-
169 hostInfo.setErrorString(QCoreApplication::translate("QHostInfo", "No host name given"));
never executed (the execution status of this line is deduced): hostInfo.setErrorString(QCoreApplication::translate("QHostInfo", "No host name given"));
-
170 QScopedPointer<QHostInfoResult> result(new QHostInfoResult);
never executed (the execution status of this line is deduced): QScopedPointer<QHostInfoResult> result(new QHostInfoResult);
-
171 QObject::connect(result.data(), SIGNAL(resultsReady(QHostInfo)),
never executed (the execution status of this line is deduced): QObject::connect(result.data(), "2""resultsReady(QHostInfo)",
-
172 receiver, member, Qt::QueuedConnection);
never executed (the execution status of this line is deduced): receiver, member, Qt::QueuedConnection);
-
173 result.data()->emitResultsReady(hostInfo);
never executed (the execution status of this line is deduced): result.data()->emitResultsReady(hostInfo);
-
174 return id;
never executed: return id;
0
175 } -
176 -
177 QHostInfoLookupManager *manager = theHostInfoLookupManager();
executed (the execution status of this line is deduced): QHostInfoLookupManager *manager = theHostInfoLookupManager();
-
178 -
179 if (manager) {
partially evaluated: manager
TRUEFALSE
yes
Evaluation Count:132
no
Evaluation Count:0
0-132
180 // the application is still alive -
181 if (manager->cache.isEnabled()) {
partially evaluated: manager->cache.isEnabled()
TRUEFALSE
yes
Evaluation Count:132
no
Evaluation Count:0
0-132
182 // check cache first -
183 bool valid = false;
executed (the execution status of this line is deduced): bool valid = false;
-
184 QHostInfo info = manager->cache.get(name, &valid);
executed (the execution status of this line is deduced): QHostInfo info = manager->cache.get(name, &valid);
-
185 if (valid) {
evaluated: valid
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:131
1-131
186 info.setLookupId(id);
executed (the execution status of this line is deduced): info.setLookupId(id);
-
187 QHostInfoResult result;
executed (the execution status of this line is deduced): QHostInfoResult result;
-
188 QObject::connect(&result, SIGNAL(resultsReady(QHostInfo)), receiver, member, Qt::QueuedConnection);
executed (the execution status of this line is deduced): QObject::connect(&result, "2""resultsReady(QHostInfo)", receiver, member, Qt::QueuedConnection);
-
189 result.emitResultsReady(info);
executed (the execution status of this line is deduced): result.emitResultsReady(info);
-
190 return id;
executed: return id;
Execution Count:1
1
191 } -
192 }
executed: }
Execution Count:131
131
193 -
194 // cache is not enabled or it was not in the cache, do normal lookup -
195 QHostInfoRunnable* runnable = new QHostInfoRunnable(name, id);
executed (the execution status of this line is deduced): QHostInfoRunnable* runnable = new QHostInfoRunnable(name, id);
-
196 QObject::connect(&runnable->resultEmitter, SIGNAL(resultsReady(QHostInfo)), receiver, member, Qt::QueuedConnection);
executed (the execution status of this line is deduced): QObject::connect(&runnable->resultEmitter, "2""resultsReady(QHostInfo)", receiver, member, Qt::QueuedConnection);
-
197 manager->scheduleLookup(runnable);
executed (the execution status of this line is deduced): manager->scheduleLookup(runnable);
-
198 }
executed: }
Execution Count:131
131
199 return id;
executed: return id;
Execution Count:131
131
200} -
201 -
202/*! -
203 Aborts the host lookup with the ID \a id, as returned by lookupHost(). -
204 -
205 \sa lookupHost(), lookupId() -
206*/ -
207void QHostInfo::abortHostLookup(int id) -
208{ -
209 theHostInfoLookupManager()->abortLookup(id);
executed (the execution status of this line is deduced): theHostInfoLookupManager()->abortLookup(id);
-
210}
executed: }
Execution Count:120
120
211 -
212/*! -
213 Looks up the IP address(es) for the given host \a name. The -
214 function blocks during the lookup which means that execution of -
215 the program is suspended until the results of the lookup are -
216 ready. Returns the result of the lookup in a QHostInfo object. -
217 -
218 If you pass a literal IP address to \a name instead of a host name, -
219 QHostInfo will search for the domain name for the IP (i.e., QHostInfo will -
220 perform a \e reverse lookup). On success, the returned QHostInfo will -
221 contain both the resolved domain name and IP addresses for the host name. -
222 -
223 \sa lookupHost() -
224*/ -
225QHostInfo QHostInfo::fromName(const QString &name) -
226{ -
227#if defined QHOSTINFO_DEBUG -
228 qDebug("QHostInfo::fromName(\"%s\")",name.toLatin1().constData()); -
229#endif -
230 -
231 QHostInfo hostInfo = QHostInfoAgent::fromName(name);
executed (the execution status of this line is deduced): QHostInfo hostInfo = QHostInfoAgent::fromName(name);
-
232 QAbstractHostInfoLookupManager* manager = theHostInfoLookupManager();
executed (the execution status of this line is deduced): QAbstractHostInfoLookupManager* manager = theHostInfoLookupManager();
-
233 manager->cache.put(name, hostInfo);
executed (the execution status of this line is deduced): manager->cache.put(name, hostInfo);
-
234 return hostInfo;
executed: return hostInfo;
Execution Count:23
23
235} -
236 -
237#ifndef QT_NO_BEARERMANAGEMENT -
238QHostInfo QHostInfoPrivate::fromName(const QString &name, QSharedPointer<QNetworkSession> session) -
239{ -
240#if defined QHOSTINFO_DEBUG -
241 qDebug("QHostInfoPrivate::fromName(\"%s\") with session %p",name.toLatin1().constData(), session.data()); -
242#endif -
243 -
244 QHostInfo hostInfo = QHostInfoAgent::fromName(name, session);
never executed (the execution status of this line is deduced): QHostInfo hostInfo = QHostInfoAgent::fromName(name, session);
-
245 QAbstractHostInfoLookupManager* manager = theHostInfoLookupManager();
never executed (the execution status of this line is deduced): QAbstractHostInfoLookupManager* manager = theHostInfoLookupManager();
-
246 manager->cache.put(name, hostInfo);
never executed (the execution status of this line is deduced): manager->cache.put(name, hostInfo);
-
247 return hostInfo;
never executed: return hostInfo;
0
248} -
249#endif -
250 -
251#ifndef QT_NO_BEARERMANAGEMENT -
252QHostInfo QHostInfoAgent::fromName(const QString &hostName, QSharedPointer<QNetworkSession>) -
253{ -
254 return QHostInfoAgent::fromName(hostName);
never executed: return QHostInfoAgent::fromName(hostName);
0
255} -
256#endif -
257 -
258 -
259/*! -
260 \enum QHostInfo::HostInfoError -
261 -
262 This enum describes the various errors that can occur when trying -
263 to resolve a host name. -
264 -
265 \value NoError The lookup was successful. -
266 \value HostNotFound No IP addresses were found for the host. -
267 \value UnknownError An unknown error occurred. -
268 -
269 \sa error(), setError() -
270*/ -
271 -
272/*! -
273 Constructs an empty host info object with lookup ID \a id. -
274 -
275 \sa lookupId() -
276*/ -
277QHostInfo::QHostInfo(int id) -
278 : d(new QHostInfoPrivate) -
279{ -
280 d->lookupId = id;
executed (the execution status of this line is deduced): d->lookupId = id;
-
281}
executed: }
Execution Count:990
990
282 -
283/*! -
284 Constructs a copy of \a other. -
285*/ -
286QHostInfo::QHostInfo(const QHostInfo &other) -
287 : d(new QHostInfoPrivate(*other.d.data())) -
288{ -
289}
executed: }
Execution Count:4389
4389
290 -
291/*! -
292 Assigns the data of the \a other object to this host info object, -
293 and returns a reference to it. -
294*/ -
295QHostInfo &QHostInfo::operator=(const QHostInfo &other) -
296{ -
297 *d.data() = *other.d.data();
executed (the execution status of this line is deduced): *d.data() = *other.d.data();
-
298 return *this;
executed: return *this;
Execution Count:57
57
299} -
300 -
301/*! -
302 Destroys the host info object. -
303*/ -
304QHostInfo::~QHostInfo() -
305{ -
306} -
307 -
308/*! -
309 Returns the list of IP addresses associated with hostName(). This -
310 list may be empty. -
311 -
312 Example: -
313 -
314 \snippet code/src_network_kernel_qhostinfo.cpp 5 -
315 -
316 \sa hostName(), error() -
317*/ -
318QList<QHostAddress> QHostInfo::addresses() const -
319{ -
320 return d->addrs;
executed: return d->addrs;
Execution Count:2526
2526
321} -
322 -
323/*! -
324 Sets the list of addresses in this QHostInfo to \a addresses. -
325 -
326 \sa addresses() -
327*/ -
328void QHostInfo::setAddresses(const QList<QHostAddress> &addresses) -
329{ -
330 d->addrs = addresses;
executed (the execution status of this line is deduced): d->addrs = addresses;
-
331}
executed: }
Execution Count:550
550
332 -
333/*! -
334 Returns the name of the host whose IP addresses were looked up. -
335 -
336 \sa localHostName() -
337*/ -
338QString QHostInfo::hostName() const -
339{ -
340 return d->hostName;
never executed: return d->hostName;
0
341} -
342 -
343/*! -
344 Sets the host name of this QHostInfo to \a hostName. -
345 -
346 \sa hostName() -
347*/ -
348void QHostInfo::setHostName(const QString &hostName) -
349{ -
350 d->hostName = hostName;
executed (the execution status of this line is deduced): d->hostName = hostName;
-
351}
executed: }
Execution Count:36
36
352 -
353/*! -
354 Returns the type of error that occurred if the host name lookup -
355 failed; otherwise returns NoError. -
356 -
357 \sa setError(), errorString() -
358*/ -
359QHostInfo::HostInfoError QHostInfo::error() const -
360{ -
361 return d->err;
executed: return d->err;
Execution Count:50
50
362} -
363 -
364/*! -
365 Sets the error type of this QHostInfo to \a error. -
366 -
367 \sa error(), errorString() -
368*/ -
369void QHostInfo::setError(HostInfoError error) -
370{ -
371 d->err = error;
executed (the execution status of this line is deduced): d->err = error;
-
372}
executed: }
Execution Count:5
5
373 -
374/*! -
375 Returns the ID of this lookup. -
376 -
377 \sa setLookupId(), abortHostLookup(), hostName() -
378*/ -
379int QHostInfo::lookupId() const -
380{ -
381 return d->lookupId;
executed: return d->lookupId;
Execution Count:2
2
382} -
383 -
384/*! -
385 Sets the ID of this lookup to \a id. -
386 -
387 \sa lookupId(), lookupHost() -
388*/ -
389void QHostInfo::setLookupId(int id) -
390{ -
391 d->lookupId = id;
executed (the execution status of this line is deduced): d->lookupId = id;
-
392}
executed: }
Execution Count:13
13
393 -
394/*! -
395 If the lookup failed, this function returns a human readable -
396 description of the error; otherwise "Unknown error" is returned. -
397 -
398 \sa setErrorString(), error() -
399*/ -
400QString QHostInfo::errorString() const -
401{ -
402 return d->errorStr;
executed: return d->errorStr;
Execution Count:2
2
403} -
404 -
405/*! -
406 Sets the human readable description of the error that occurred to \a str -
407 if the lookup failed. -
408 -
409 \sa errorString(), setError() -
410*/ -
411void QHostInfo::setErrorString(const QString &str) -
412{ -
413 d->errorStr = str;
executed (the execution status of this line is deduced): d->errorStr = str;
-
414}
executed: }
Execution Count:5
5
415 -
416/*! -
417 \fn QString QHostInfo::localHostName() -
418 -
419 Returns the host name of this machine. -
420 -
421 \sa hostName() -
422*/ -
423 -
424/*! -
425 \fn QString QHostInfo::localDomainName() -
426 -
427 Returns the DNS domain of this machine. -
428 -
429 Note: DNS domains are not related to domain names found in -
430 Windows networks. -
431 -
432 \sa hostName() -
433*/ -
434 -
435QHostInfoRunnable::QHostInfoRunnable(QString hn, int i) : toBeLookedUp(hn), id(i) -
436{ -
437 setAutoDelete(true);
executed (the execution status of this line is deduced): setAutoDelete(true);
-
438}
executed: }
Execution Count:131
131
439 -
440// the QHostInfoLookupManager will at some point call this via a QThreadPool -
441void QHostInfoRunnable::run() -
442{ -
443 QHostInfoLookupManager *manager = theHostInfoLookupManager();
executed (the execution status of this line is deduced): QHostInfoLookupManager *manager = theHostInfoLookupManager();
-
444 // check aborted -
445 if (manager->wasAborted(id)) {
evaluated: manager->wasAborted(id)
TRUEFALSE
yes
Evaluation Count:88
yes
Evaluation Count:13
13-88
446 manager->lookupFinished(this);
executed (the execution status of this line is deduced): manager->lookupFinished(this);
-
447 return;
executed: return;
Execution Count:88
88
448 } -
449 -
450 QHostInfo hostInfo;
executed (the execution status of this line is deduced): QHostInfo hostInfo;
-
451 -
452 // QHostInfo::lookupHost already checks the cache. However we need to check -
453 // it here too because it might have been cache saved by another QHostInfoRunnable -
454 // in the meanwhile while this QHostInfoRunnable was scheduled but not running -
455 if (manager->cache.isEnabled()) {
partially evaluated: manager->cache.isEnabled()
TRUEFALSE
yes
Evaluation Count:13
no
Evaluation Count:0
0-13
456 // check the cache first -
457 bool valid = false;
executed (the execution status of this line is deduced): bool valid = false;
-
458 hostInfo = manager->cache.get(toBeLookedUp, &valid);
executed (the execution status of this line is deduced): hostInfo = manager->cache.get(toBeLookedUp, &valid);
-
459 if (!valid) {
partially evaluated: !valid
TRUEFALSE
yes
Evaluation Count:13
no
Evaluation Count:0
0-13
460 // not in cache, we need to do the lookup and store the result in the cache -
461 hostInfo = QHostInfoAgent::fromName(toBeLookedUp);
executed (the execution status of this line is deduced): hostInfo = QHostInfoAgent::fromName(toBeLookedUp);
-
462 manager->cache.put(toBeLookedUp, hostInfo);
executed (the execution status of this line is deduced): manager->cache.put(toBeLookedUp, hostInfo);
-
463 }
executed: }
Execution Count:13
13
464 } else {
executed: }
Execution Count:13
13
465 // cache is not enabled, just do the lookup and continue -
466 hostInfo = QHostInfoAgent::fromName(toBeLookedUp);
never executed (the execution status of this line is deduced): hostInfo = QHostInfoAgent::fromName(toBeLookedUp);
-
467 }
never executed: }
0
468 -
469 // check aborted again -
470 if (manager->wasAborted(id)) {
evaluated: manager->wasAborted(id)
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:12
1-12
471 manager->lookupFinished(this);
executed (the execution status of this line is deduced): manager->lookupFinished(this);
-
472 return;
executed: return;
Execution Count:1
1
473 } -
474 -
475 // signal emission -
476 hostInfo.setLookupId(id);
executed (the execution status of this line is deduced): hostInfo.setLookupId(id);
-
477 resultEmitter.emitResultsReady(hostInfo);
executed (the execution status of this line is deduced): resultEmitter.emitResultsReady(hostInfo);
-
478 -
479 // now also iterate through the postponed ones -
480 { -
481 QMutexLocker locker(&manager->mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&manager->mutex);
-
482 QMutableListIterator<QHostInfoRunnable*> iterator(manager->postponedLookups);
executed (the execution status of this line is deduced): QMutableListIterator<QHostInfoRunnable*> iterator(manager->postponedLookups);
-
483 while (iterator.hasNext()) {
partially evaluated: iterator.hasNext()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:12
0-12
484 QHostInfoRunnable* postponed = iterator.next();
never executed (the execution status of this line is deduced): QHostInfoRunnable* postponed = iterator.next();
-
485 if (toBeLookedUp == postponed->toBeLookedUp) {
never evaluated: toBeLookedUp == postponed->toBeLookedUp
0
486 // we can now emit -
487 iterator.remove();
never executed (the execution status of this line is deduced): iterator.remove();
-
488 hostInfo.setLookupId(postponed->id);
never executed (the execution status of this line is deduced): hostInfo.setLookupId(postponed->id);
-
489 postponed->resultEmitter.emitResultsReady(hostInfo);
never executed (the execution status of this line is deduced): postponed->resultEmitter.emitResultsReady(hostInfo);
-
490 delete postponed;
never executed (the execution status of this line is deduced): delete postponed;
-
491 }
never executed: }
0
492 }
never executed: }
0
493 } -
494 -
495 manager->lookupFinished(this);
executed (the execution status of this line is deduced): manager->lookupFinished(this);
-
496 -
497 // thread goes back to QThreadPool -
498}
executed: }
Execution Count:12
12
499 -
500QHostInfoLookupManager::QHostInfoLookupManager() : mutex(QMutex::Recursive), wasDeleted(false) -
501{ -
502 moveToThread(QCoreApplicationPrivate::mainThread());
executed (the execution status of this line is deduced): moveToThread(QCoreApplicationPrivate::mainThread());
-
503 connect(QCoreApplication::instance(), SIGNAL(destroyed()), SLOT(waitForThreadPoolDone()), Qt::DirectConnection);
executed (the execution status of this line is deduced): connect(QCoreApplication::instance(), "2""destroyed()", "1""waitForThreadPoolDone()", Qt::DirectConnection);
-
504 threadPool.setMaxThreadCount(5); // do 5 DNS lookups in parallel
executed (the execution status of this line is deduced): threadPool.setMaxThreadCount(5);
-
505}
executed: }
Execution Count:13
13
506 -
507QHostInfoLookupManager::~QHostInfoLookupManager() -
508{ -
509 wasDeleted = true;
executed (the execution status of this line is deduced): wasDeleted = true;
-
510 -
511 // don't qDeleteAll currentLookups, the QThreadPool has ownership -
512 clear();
executed (the execution status of this line is deduced): clear();
-
513}
executed: }
Execution Count:13
13
514 -
515void QHostInfoLookupManager::clear() -
516{ -
517 { -
518 QMutexLocker locker(&mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&mutex);
-
519 qDeleteAll(postponedLookups);
executed (the execution status of this line is deduced): qDeleteAll(postponedLookups);
-
520 qDeleteAll(scheduledLookups);
executed (the execution status of this line is deduced): qDeleteAll(scheduledLookups);
-
521 qDeleteAll(finishedLookups);
executed (the execution status of this line is deduced): qDeleteAll(finishedLookups);
-
522 postponedLookups.clear();
executed (the execution status of this line is deduced): postponedLookups.clear();
-
523 scheduledLookups.clear();
executed (the execution status of this line is deduced): scheduledLookups.clear();
-
524 finishedLookups.clear();
executed (the execution status of this line is deduced): finishedLookups.clear();
-
525 } -
526 -
527 threadPool.waitForDone();
executed (the execution status of this line is deduced): threadPool.waitForDone();
-
528 cache.clear();
executed (the execution status of this line is deduced): cache.clear();
-
529}
executed: }
Execution Count:13
13
530 -
531void QHostInfoLookupManager::work() -
532{ -
533 if (wasDeleted)
partially evaluated: wasDeleted
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:232
0-232
534 return;
never executed: return;
0
535 -
536 // goals of this function: -
537 // - launch new lookups via the thread pool -
538 // - make sure only one lookup per host/IP is in progress -
539 -
540 QMutexLocker locker(&mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&mutex);
-
541 -
542 if (!finishedLookups.isEmpty()) {
evaluated: !finishedLookups.isEmpty()
TRUEFALSE
yes
Evaluation Count:101
yes
Evaluation Count:131
101-131
543 // remove ID from aborted if it is in there -
544 for (int i = 0; i < finishedLookups.length(); i++) {
evaluated: i < finishedLookups.length()
TRUEFALSE
yes
Evaluation Count:101
yes
Evaluation Count:101
101
545 abortedLookups.removeAll(finishedLookups.at(i)->id);
executed (the execution status of this line is deduced): abortedLookups.removeAll(finishedLookups.at(i)->id);
-
546 }
executed: }
Execution Count:101
101
547 -
548 finishedLookups.clear();
executed (the execution status of this line is deduced): finishedLookups.clear();
-
549 }
executed: }
Execution Count:101
101
550 -
551 if (!postponedLookups.isEmpty()) {
partially evaluated: !postponedLookups.isEmpty()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:232
0-232
552 // try to start the postponed ones -
553 -
554 QMutableListIterator<QHostInfoRunnable*> iterator(postponedLookups);
never executed (the execution status of this line is deduced): QMutableListIterator<QHostInfoRunnable*> iterator(postponedLookups);
-
555 while (iterator.hasNext()) {
never evaluated: iterator.hasNext()
0
556 QHostInfoRunnable* postponed = iterator.next();
never executed (the execution status of this line is deduced): QHostInfoRunnable* postponed = iterator.next();
-
557 -
558 // check if none of the postponed hostnames is currently running -
559 bool alreadyRunning = false;
never executed (the execution status of this line is deduced): bool alreadyRunning = false;
-
560 for (int i = 0; i < currentLookups.length(); i++) {
never evaluated: i < currentLookups.length()
0
561 if (currentLookups.at(i)->toBeLookedUp == postponed->toBeLookedUp) {
never evaluated: currentLookups.at(i)->toBeLookedUp == postponed->toBeLookedUp
0
562 alreadyRunning = true;
never executed (the execution status of this line is deduced): alreadyRunning = true;
-
563 break;
never executed: break;
0
564 } -
565 }
never executed: }
0
566 if (!alreadyRunning) {
never evaluated: !alreadyRunning
0
567 iterator.remove();
never executed (the execution status of this line is deduced): iterator.remove();
-
568 scheduledLookups.prepend(postponed); // prepend! we want to finish it ASAP
never executed (the execution status of this line is deduced): scheduledLookups.prepend(postponed);
-
569 }
never executed: }
0
570 }
never executed: }
0
571 }
never executed: }
0
572 -
573 if (!scheduledLookups.isEmpty()) {
evaluated: !scheduledLookups.isEmpty()
TRUEFALSE
yes
Evaluation Count:131
yes
Evaluation Count:101
101-131
574 // try to start the new ones -
575 QMutableListIterator<QHostInfoRunnable*> iterator(scheduledLookups);
executed (the execution status of this line is deduced): QMutableListIterator<QHostInfoRunnable*> iterator(scheduledLookups);
-
576 while (iterator.hasNext()) {
evaluated: iterator.hasNext()
TRUEFALSE
yes
Evaluation Count:131
yes
Evaluation Count:131
131
577 QHostInfoRunnable *scheduled = iterator.next();
executed (the execution status of this line is deduced): QHostInfoRunnable *scheduled = iterator.next();
-
578 -
579 // check if a lookup for this host is already running, then postpone -
580 for (int i = 0; i < currentLookups.size(); i++) {
evaluated: i < currentLookups.size()
TRUEFALSE
yes
Evaluation Count:30
yes
Evaluation Count:101
30-101
581 if (currentLookups.at(i)->toBeLookedUp == scheduled->toBeLookedUp) {
partially evaluated: currentLookups.at(i)->toBeLookedUp == scheduled->toBeLookedUp
TRUEFALSE
yes
Evaluation Count:30
no
Evaluation Count:0
0-30
582 iterator.remove();
executed (the execution status of this line is deduced): iterator.remove();
-
583 postponedLookups.append(scheduled);
executed (the execution status of this line is deduced): postponedLookups.append(scheduled);
-
584 scheduled = 0;
executed (the execution status of this line is deduced): scheduled = 0;
-
585 break;
executed: break;
Execution Count:30
30
586 } -
587 }
never executed: }
0
588 -
589 if (scheduled && currentLookups.size() < threadPool.maxThreadCount()) {
evaluated: scheduled
TRUEFALSE
yes
Evaluation Count:101
yes
Evaluation Count:30
partially evaluated: currentLookups.size() < threadPool.maxThreadCount()
TRUEFALSE
yes
Evaluation Count:101
no
Evaluation Count:0
0-101
590 // runnable now running in new thread, track this in currentLookups -
591 threadPool.start(scheduled);
executed (the execution status of this line is deduced): threadPool.start(scheduled);
-
592 iterator.remove();
executed (the execution status of this line is deduced): iterator.remove();
-
593 currentLookups.append(scheduled);
executed (the execution status of this line is deduced): currentLookups.append(scheduled);
-
594 } else {
executed: }
Execution Count:101
101
595 // was postponed, continue iterating -
596 continue;
executed: continue;
Execution Count:30
30
597 } -
598 }; -
599 }
executed: }
Execution Count:131
131
600}
executed: }
Execution Count:232
232
601 -
602// called by QHostInfo -
603void QHostInfoLookupManager::scheduleLookup(QHostInfoRunnable *r) -
604{ -
605 if (wasDeleted)
partially evaluated: wasDeleted
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:131
0-131
606 return;
never executed: return;
0
607 -
608 QMutexLocker locker(&this->mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&this->mutex);
-
609 scheduledLookups.enqueue(r);
executed (the execution status of this line is deduced): scheduledLookups.enqueue(r);
-
610 work();
executed (the execution status of this line is deduced): work();
-
611}
executed: }
Execution Count:131
131
612 -
613// called by QHostInfo -
614void QHostInfoLookupManager::abortLookup(int id) -
615{ -
616 if (wasDeleted)
partially evaluated: wasDeleted
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:120
0-120
617 return;
never executed: return;
0
618 -
619 QMutexLocker locker(&this->mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&this->mutex);
-
620 -
621 // is postponed? delete and return -
622 for (int i = 0; i < postponedLookups.length(); i++) {
evaluated: i < postponedLookups.length()
TRUEFALSE
yes
Evaluation Count:30
yes
Evaluation Count:90
30-90
623 if (postponedLookups.at(i)->id == id) {
partially evaluated: postponedLookups.at(i)->id == id
TRUEFALSE
yes
Evaluation Count:30
no
Evaluation Count:0
0-30
624 delete postponedLookups.takeAt(i);
executed (the execution status of this line is deduced): delete postponedLookups.takeAt(i);
-
625 return;
executed: return;
Execution Count:30
30
626 } -
627 }
never executed: }
0
628 -
629 // is scheduled? delete and return -
630 for (int i = 0; i < scheduledLookups.length(); i++) {
partially evaluated: i < scheduledLookups.length()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:90
0-90
631 if (scheduledLookups.at(i)->id == id) {
never evaluated: scheduledLookups.at(i)->id == id
0
632 delete scheduledLookups.takeAt(i);
never executed (the execution status of this line is deduced): delete scheduledLookups.takeAt(i);
-
633 return;
never executed: return;
0
634 } -
635 }
never executed: }
0
636 -
637 if (!abortedLookups.contains(id))
partially evaluated: !abortedLookups.contains(id)
TRUEFALSE
yes
Evaluation Count:90
no
Evaluation Count:0
0-90
638 abortedLookups.append(id);
executed: abortedLookups.append(id);
Execution Count:90
90
639}
executed: }
Execution Count:90
90
640 -
641// called from QHostInfoRunnable -
642bool QHostInfoLookupManager::wasAborted(int id) -
643{ -
644 if (wasDeleted)
partially evaluated: wasDeleted
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:114
0-114
645 return true;
never executed: return true;
0
646 -
647 QMutexLocker locker(&this->mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&this->mutex);
-
648 return abortedLookups.contains(id);
executed: return abortedLookups.contains(id);
Execution Count:114
114
649} -
650 -
651// called from QHostInfoRunnable -
652void QHostInfoLookupManager::lookupFinished(QHostInfoRunnable *r) -
653{ -
654 if (wasDeleted)
partially evaluated: wasDeleted
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:101
0-101
655 return;
never executed: return;
0
656 -
657 QMutexLocker locker(&this->mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&this->mutex);
-
658 currentLookups.removeOne(r);
executed (the execution status of this line is deduced): currentLookups.removeOne(r);
-
659 finishedLookups.append(r);
executed (the execution status of this line is deduced): finishedLookups.append(r);
-
660 work();
executed (the execution status of this line is deduced): work();
-
661}
executed: }
Execution Count:101
101
662 -
663// This function returns immediately when we had a result in the cache, else it will later emit a signal -
664QHostInfo qt_qhostinfo_lookup(const QString &name, QObject *receiver, const char *member, bool *valid, int *id) -
665{ -
666 *valid = false;
executed (the execution status of this line is deduced): *valid = false;
-
667 *id = -1;
executed (the execution status of this line is deduced): *id = -1;
-
668 -
669 // check cache -
670 QAbstractHostInfoLookupManager* manager = theHostInfoLookupManager();
executed (the execution status of this line is deduced): QAbstractHostInfoLookupManager* manager = theHostInfoLookupManager();
-
671 if (manager && manager->cache.isEnabled()) {
partially evaluated: manager
TRUEFALSE
yes
Evaluation Count:2107
no
Evaluation Count:0
partially evaluated: manager->cache.isEnabled()
TRUEFALSE
yes
Evaluation Count:2107
no
Evaluation Count:0
0-2107
672 QHostInfo info = manager->cache.get(name, valid);
executed (the execution status of this line is deduced): QHostInfo info = manager->cache.get(name, valid);
-
673 if (*valid) {
evaluated: *valid
TRUEFALSE
yes
Evaluation Count:1976
yes
Evaluation Count:131
131-1976
674 return info;
executed: return info;
Execution Count:1976
1976
675 } -
676 }
executed: }
Execution Count:131
131
677 -
678 // was not in cache, trigger lookup -
679 *id = QHostInfo::lookupHost(name, receiver, member);
executed (the execution status of this line is deduced): *id = QHostInfo::lookupHost(name, receiver, member);
-
680 -
681 // return empty response, valid==false -
682 return QHostInfo();
executed: return QHostInfo();
Execution Count:131
131
683} -
684 -
685void qt_qhostinfo_clear_cache() -
686{ -
687 QAbstractHostInfoLookupManager* manager = theHostInfoLookupManager();
never executed (the execution status of this line is deduced): QAbstractHostInfoLookupManager* manager = theHostInfoLookupManager();
-
688 if (manager) {
never evaluated: manager
0
689 manager->clear();
never executed (the execution status of this line is deduced): manager->clear();
-
690 }
never executed: }
0
691}
never executed: }
0
692 -
693void Q_AUTOTEST_EXPORT qt_qhostinfo_enable_cache(bool e) -
694{ -
695 QAbstractHostInfoLookupManager* manager = theHostInfoLookupManager();
never executed (the execution status of this line is deduced): QAbstractHostInfoLookupManager* manager = theHostInfoLookupManager();
-
696 if (manager) {
never evaluated: manager
0
697 manager->cache.setEnabled(e);
never executed (the execution status of this line is deduced): manager->cache.setEnabled(e);
-
698 }
never executed: }
0
699}
never executed: }
0
700 -
701// cache for 60 seconds -
702// cache 64 items -
703QHostInfoCache::QHostInfoCache() : max_age(60), enabled(true), cache(64) -
704{ -
705#ifdef QT_QHOSTINFO_CACHE_DISABLED_BY_DEFAULT -
706 enabled = false; -
707#endif -
708}
executed: }
Execution Count:13
13
709 -
710bool QHostInfoCache::isEnabled() -
711{ -
712 return enabled;
executed: return enabled;
Execution Count:2252
2252
713} -
714 -
715// this function is currently only used for the auto tests -
716// and not usable by public API -
717void QHostInfoCache::setEnabled(bool e) -
718{ -
719 enabled = e;
never executed (the execution status of this line is deduced): enabled = e;
-
720}
never executed: }
0
721 -
722 -
723QHostInfo QHostInfoCache::get(const QString &name, bool *valid) -
724{ -
725 QMutexLocker locker(&this->mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&this->mutex);
-
726 -
727 *valid = false;
executed (the execution status of this line is deduced): *valid = false;
-
728 if (cache.contains(name)) {
evaluated: cache.contains(name)
TRUEFALSE
yes
Evaluation Count:1992
yes
Evaluation Count:260
260-1992
729 QHostInfoCacheElement *element = cache.object(name);
executed (the execution status of this line is deduced): QHostInfoCacheElement *element = cache.object(name);
-
730 if (element->age.elapsed() < max_age*1000)
evaluated: element->age.elapsed() < max_age*1000
TRUEFALSE
yes
Evaluation Count:1977
yes
Evaluation Count:15
15-1977
731 *valid = true;
executed: *valid = true;
Execution Count:1977
1977
732 return element->info;
executed: return element->info;
Execution Count:1992
1992
733 -
734 // FIXME idea: -
735 // if too old but not expired, trigger a new lookup -
736 // to freshen our cache -
737 } -
738 -
739 return QHostInfo();
executed: return QHostInfo();
Execution Count:260
260
740} -
741 -
742void QHostInfoCache::put(const QString &name, const QHostInfo &info) -
743{ -
744 // if the lookup failed, don't cache -
745 if (info.error() != QHostInfo::NoError)
evaluated: info.error() != QHostInfo::NoError
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:31
5-31
746 return;
executed: return;
Execution Count:5
5
747 -
748 QHostInfoCacheElement* element = new QHostInfoCacheElement();
executed (the execution status of this line is deduced): QHostInfoCacheElement* element = new QHostInfoCacheElement();
-
749 element->info = info;
executed (the execution status of this line is deduced): element->info = info;
-
750 element->age = QElapsedTimer();
executed (the execution status of this line is deduced): element->age = QElapsedTimer();
-
751 element->age.start();
executed (the execution status of this line is deduced): element->age.start();
-
752 -
753 QMutexLocker locker(&this->mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&this->mutex);
-
754 cache.insert(name, element); // cache will take ownership
executed (the execution status of this line is deduced): cache.insert(name, element);
-
755}
executed: }
Execution Count:31
31
756 -
757void QHostInfoCache::clear() -
758{ -
759 QMutexLocker locker(&this->mutex);
executed (the execution status of this line is deduced): QMutexLocker locker(&this->mutex);
-
760 cache.clear();
executed (the execution status of this line is deduced): cache.clear();
-
761}
executed: }
Execution Count:13
13
762 -
763QAbstractHostInfoLookupManager* QAbstractHostInfoLookupManager::globalInstance() -
764{ -
765 return theHostInfoLookupManager();
never executed: return theHostInfoLookupManager();
0
766} -
767 -
768QT_END_NAMESPACE -
769 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial