1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtNetwork module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #ifndef QHOSTINFO_P_H
|
---|
43 | #define QHOSTINFO_P_H
|
---|
44 |
|
---|
45 | //
|
---|
46 | // W A R N I N G
|
---|
47 | // -------------
|
---|
48 | //
|
---|
49 | // This file is not part of the Qt API. It exists for the convenience
|
---|
50 | // of the QHostInfo class. This header file may change from
|
---|
51 | // version to version without notice, or even be removed.
|
---|
52 | //
|
---|
53 | // We mean it.
|
---|
54 | //
|
---|
55 |
|
---|
56 | #include "QtCore/qcoreapplication.h"
|
---|
57 | #include "private/qcoreapplication_p.h"
|
---|
58 | #include "QtNetwork/qhostinfo.h"
|
---|
59 | #include "QtCore/qmutex.h"
|
---|
60 | #include "QtCore/qwaitcondition.h"
|
---|
61 | #include "QtCore/qobject.h"
|
---|
62 | #include "QtCore/qpointer.h"
|
---|
63 |
|
---|
64 | #ifndef QT_NO_THREAD
|
---|
65 | #include "QtCore/qthread.h"
|
---|
66 | #include "QtCore/qthreadpool.h"
|
---|
67 | #include "QtCore/qmutex.h"
|
---|
68 | #include "QtCore/qrunnable.h"
|
---|
69 | #include "QtCore/qlist.h"
|
---|
70 | #include "QtCore/qqueue.h"
|
---|
71 | #include <QTime>
|
---|
72 | #include <QCache>
|
---|
73 | #endif
|
---|
74 |
|
---|
75 | QT_BEGIN_NAMESPACE
|
---|
76 |
|
---|
77 | class QHostInfoResult : public QObject
|
---|
78 | {
|
---|
79 | Q_OBJECT
|
---|
80 | public Q_SLOTS:
|
---|
81 | inline void emitResultsReady(const QHostInfo &info)
|
---|
82 | {
|
---|
83 | emit resultsReady(info);
|
---|
84 | }
|
---|
85 |
|
---|
86 | Q_SIGNALS:
|
---|
87 | void resultsReady(const QHostInfo &info);
|
---|
88 | };
|
---|
89 |
|
---|
90 | // needs to be QObject because fromName calls tr()
|
---|
91 | class QHostInfoAgent : public QObject
|
---|
92 | {
|
---|
93 | Q_OBJECT
|
---|
94 | public:
|
---|
95 | static QHostInfo fromName(const QString &hostName);
|
---|
96 | };
|
---|
97 |
|
---|
98 | class QHostInfoPrivate
|
---|
99 | {
|
---|
100 | public:
|
---|
101 | inline QHostInfoPrivate()
|
---|
102 | : err(QHostInfo::NoError),
|
---|
103 | errorStr(QLatin1String(QT_TRANSLATE_NOOP("QHostInfo", "Unknown error"))),
|
---|
104 | lookupId(0)
|
---|
105 | {
|
---|
106 | }
|
---|
107 |
|
---|
108 | QHostInfo::HostInfoError err;
|
---|
109 | QString errorStr;
|
---|
110 | QList<QHostAddress> addrs;
|
---|
111 | QString hostName;
|
---|
112 | int lookupId;
|
---|
113 | };
|
---|
114 |
|
---|
115 | #ifndef QT_NO_THREAD
|
---|
116 | // These functions are outside of the QHostInfo class and strictly internal.
|
---|
117 | // Do NOT use them outside of QAbstractSocket.
|
---|
118 | QHostInfo Q_NETWORK_EXPORT qt_qhostinfo_lookup(const QString &name, QObject *receiver, const char *member, bool *valid, int *id);
|
---|
119 | void Q_AUTOTEST_EXPORT qt_qhostinfo_clear_cache();
|
---|
120 | void Q_AUTOTEST_EXPORT qt_qhostinfo_enable_cache(bool e);
|
---|
121 |
|
---|
122 | class QHostInfoCache
|
---|
123 | {
|
---|
124 | public:
|
---|
125 | QHostInfoCache();
|
---|
126 | const int max_age; // seconds
|
---|
127 |
|
---|
128 | QHostInfo get(const QString &name, bool *valid);
|
---|
129 | void put(const QString &name, const QHostInfo &info);
|
---|
130 | void clear();
|
---|
131 |
|
---|
132 | bool isEnabled();
|
---|
133 | void setEnabled(bool e);
|
---|
134 | private:
|
---|
135 | bool enabled;
|
---|
136 | struct QHostInfoCacheElement {
|
---|
137 | QHostInfo info;
|
---|
138 | QTime age;
|
---|
139 | };
|
---|
140 | QCache<QString,QHostInfoCacheElement> cache;
|
---|
141 | QMutex mutex;
|
---|
142 | };
|
---|
143 |
|
---|
144 | // the following classes are used for the (normal) case: We use multiple threads to lookup DNS
|
---|
145 |
|
---|
146 | class QHostInfoRunnable : public QRunnable
|
---|
147 | {
|
---|
148 | public:
|
---|
149 | QHostInfoRunnable (QString hn, int i);
|
---|
150 | void run();
|
---|
151 |
|
---|
152 | QString toBeLookedUp;
|
---|
153 | int id;
|
---|
154 | QHostInfoResult resultEmitter;
|
---|
155 | };
|
---|
156 |
|
---|
157 | class QHostInfoLookupManager : public QObject
|
---|
158 | {
|
---|
159 | Q_OBJECT
|
---|
160 | public:
|
---|
161 | QHostInfoLookupManager();
|
---|
162 | ~QHostInfoLookupManager();
|
---|
163 |
|
---|
164 | void clear();
|
---|
165 | void work();
|
---|
166 |
|
---|
167 | // called from QHostInfo
|
---|
168 | void scheduleLookup(QHostInfoRunnable *r);
|
---|
169 | void abortLookup(int id);
|
---|
170 |
|
---|
171 | // called from QHostInfoRunnable
|
---|
172 | void lookupFinished(QHostInfoRunnable *r);
|
---|
173 | bool wasAborted(int id);
|
---|
174 |
|
---|
175 | QHostInfoCache cache;
|
---|
176 |
|
---|
177 | friend class QHostInfoRunnable;
|
---|
178 | protected:
|
---|
179 | QList<QHostInfoRunnable*> currentLookups; // in progress
|
---|
180 | QList<QHostInfoRunnable*> postponedLookups; // postponed because in progress for same host
|
---|
181 | QQueue<QHostInfoRunnable*> scheduledLookups; // not yet started
|
---|
182 | QList<QHostInfoRunnable*> finishedLookups; // recently finished
|
---|
183 | QList<int> abortedLookups; // ids of aborted lookups
|
---|
184 |
|
---|
185 | QThreadPool threadPool;
|
---|
186 |
|
---|
187 | QMutex mutex;
|
---|
188 |
|
---|
189 | bool wasDeleted;
|
---|
190 |
|
---|
191 | private slots:
|
---|
192 | void waitForThreadPoolDone() { threadPool.waitForDone(); }
|
---|
193 | };
|
---|
194 |
|
---|
195 | #endif
|
---|
196 |
|
---|
197 | QT_END_NAMESPACE
|
---|
198 |
|
---|
199 | #endif // QHOSTINFO_P_H
|
---|