|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtNetwork module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
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 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
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 #if !defined QT_NO_THREAD |
|
65 #include "QtCore/qthread.h" |
|
66 # define QHostInfoAgentBase QThread |
|
67 #else |
|
68 # define QHostInfoAgentBase QObject |
|
69 #endif |
|
70 |
|
71 QT_BEGIN_NAMESPACE |
|
72 |
|
73 static const int QHOSTINFO_THREAD_WAIT = 250; // ms |
|
74 |
|
75 class QHostInfoResult : public QObject |
|
76 { |
|
77 Q_OBJECT |
|
78 public Q_SLOTS: |
|
79 inline void emitResultsReady(const QHostInfo &info) |
|
80 { |
|
81 emit resultsReady(info); |
|
82 if (autoDelete) |
|
83 delete this; |
|
84 } |
|
85 |
|
86 Q_SIGNALS: |
|
87 void resultsReady(const QHostInfo &info); |
|
88 |
|
89 public: |
|
90 int lookupId; |
|
91 bool autoDelete; |
|
92 }; |
|
93 |
|
94 struct QHostInfoQuery |
|
95 { |
|
96 inline QHostInfoQuery() : object(0) {} |
|
97 inline ~QHostInfoQuery() { delete object; } |
|
98 inline QHostInfoQuery(const QString &name, QHostInfoResult *result) |
|
99 : hostName(name), object(result) {} |
|
100 |
|
101 QString hostName; |
|
102 QHostInfoResult *object; |
|
103 }; |
|
104 |
|
105 class QHostInfoAgent : public QHostInfoAgentBase |
|
106 { |
|
107 Q_OBJECT |
|
108 public: |
|
109 inline QHostInfoAgent() |
|
110 { |
|
111 // There is a chance that there will be two instances of |
|
112 // QHostInfoAgent if two threads try to get Q_GLOBAL_STATIC |
|
113 // object at the same time. The second object will be deleted |
|
114 // immediately before anyone uses it, but we need to be |
|
115 // careful about what we do in the constructor. |
|
116 static QBasicAtomicInt done = Q_BASIC_ATOMIC_INITIALIZER(0); |
|
117 if (done.testAndSetRelaxed(0, 1)) |
|
118 qAddPostRoutine(staticCleanup); |
|
119 moveToThread(QCoreApplicationPrivate::mainThread()); |
|
120 quit = false; |
|
121 pendingQueryId = -1; |
|
122 } |
|
123 inline ~QHostInfoAgent() |
|
124 { cleanup(); } |
|
125 |
|
126 void run(); |
|
127 static QHostInfo fromName(const QString &hostName); |
|
128 |
|
129 inline void addHostName(const QString &name, QHostInfoResult *result) |
|
130 { |
|
131 QMutexLocker locker(&mutex); |
|
132 queries << new QHostInfoQuery(name, result); |
|
133 cond.wakeOne(); |
|
134 } |
|
135 |
|
136 inline void abortLookup(int id) |
|
137 { |
|
138 QMutexLocker locker(&mutex); |
|
139 for (int i = 0; i < queries.size(); ++i) { |
|
140 QHostInfoResult *result = queries.at(i)->object; |
|
141 if (result->lookupId == id) { |
|
142 result->disconnect(); |
|
143 delete queries.takeAt(i); |
|
144 return; |
|
145 } |
|
146 } |
|
147 if (pendingQueryId == id) |
|
148 pendingQueryId = -1; |
|
149 } |
|
150 |
|
151 static void staticCleanup(); |
|
152 |
|
153 public Q_SLOTS: |
|
154 inline void cleanup() |
|
155 { |
|
156 { |
|
157 QMutexLocker locker(&mutex); |
|
158 qDeleteAll(queries); |
|
159 queries.clear(); |
|
160 quit = true; |
|
161 cond.wakeOne(); |
|
162 } |
|
163 #ifndef QT_NO_THREAD |
|
164 if (!wait(QHOSTINFO_THREAD_WAIT)) |
|
165 terminate(); |
|
166 wait(); |
|
167 #endif |
|
168 } |
|
169 |
|
170 private: |
|
171 QList<QHostInfoQuery *> queries; |
|
172 QMutex mutex; |
|
173 QWaitCondition cond; |
|
174 volatile bool quit; |
|
175 int pendingQueryId; |
|
176 }; |
|
177 |
|
178 class QHostInfoPrivate |
|
179 { |
|
180 public: |
|
181 inline QHostInfoPrivate() |
|
182 : err(QHostInfo::NoError), |
|
183 errorStr(QLatin1String(QT_TRANSLATE_NOOP("QHostInfo", "Unknown error"))), |
|
184 lookupId(0) |
|
185 { |
|
186 } |
|
187 |
|
188 QHostInfo::HostInfoError err; |
|
189 QString errorStr; |
|
190 QList<QHostAddress> addrs; |
|
191 QString hostName; |
|
192 int lookupId; |
|
193 }; |
|
194 |
|
195 QT_END_NAMESPACE |
|
196 |
|
197 #endif // QHOSTINFO_P_H |