source: trunk/src/network/kernel/qhostinfo_p.h@ 67

Last change on this file since 67 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtNetwork module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department 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 QLibrary 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
71QT_BEGIN_NAMESPACE
72
73static const int QHOSTINFO_THREAD_WAIT = 250; // ms
74
75class QHostInfoResult : public QObject
76{
77 Q_OBJECT
78public Q_SLOTS:
79 inline void emitResultsReady(const QHostInfo &info)
80 {
81 emit resultsReady(info);
82 if (autoDelete)
83 delete this;
84 }
85
86Q_SIGNALS:
87 void resultsReady(const QHostInfo &info);
88
89public:
90 int lookupId;
91 bool autoDelete;
92};
93
94struct 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
105class QHostInfoAgent : public QHostInfoAgentBase
106{
107 Q_OBJECT
108public:
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
153public 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
170private:
171 QList<QHostInfoQuery *> queries;
172 QMutex mutex;
173 QWaitCondition cond;
174 volatile bool quit;
175 int pendingQueryId;
176};
177
178class QHostInfoPrivate
179{
180public:
181 inline QHostInfoPrivate()
182 : err(QHostInfo::NoError),
183 errorStr(QLatin1String(QT_TRANSLATE_NOOP("QHostInfo", "Unknown error")))
184 {
185 }
186
187 QHostInfo::HostInfoError err;
188 QString errorStr;
189 QList<QHostAddress> addrs;
190 QString hostName;
191 int lookupId;
192};
193
194QT_END_NAMESPACE
195
196#endif // QHOSTINFO_P_H
Note: See TracBrowser for help on using the repository browser.