Qt 4.8
qhostinfo_p.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 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 #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 #include "QtCore/qthread.h"
64 #include "QtCore/qthreadpool.h"
65 #include "QtCore/qmutex.h"
66 #include "QtCore/qrunnable.h"
67 #include "QtCore/qlist.h"
68 #include "QtCore/qqueue.h"
69 #include <QElapsedTimer>
70 #include <QCache>
71 
72 #include <QNetworkSession>
73 #include <QSharedPointer>
74 
75 #ifdef Q_OS_SYMBIAN
76 // Symbian Headers
77 #include <es_sock.h>
78 #include <in_sock.h>
79 #endif
80 
81 
83 
84 
85 class QHostInfoResult : public QObject
86 {
87  Q_OBJECT
88 public Q_SLOTS:
89  inline void emitResultsReady(const QHostInfo &info)
90  {
91  emit resultsReady(info);
92  }
93 
94 Q_SIGNALS:
95  void resultsReady(const QHostInfo &info);
96 };
97 
98 // needs to be QObject because fromName calls tr()
99 class QHostInfoAgent : public QObject
100 {
101  Q_OBJECT
102 public:
103  static QHostInfo fromName(const QString &hostName);
104 #ifndef QT_NO_BEARERMANAGEMENT
105  static QHostInfo fromName(const QString &hostName, QSharedPointer<QNetworkSession> networkSession);
106 #endif
107 
108 #ifdef Q_OS_SYMBIAN
109  static int lookupHost(const QString &name, QObject *receiver, const char *member);
110  static void abortHostLookup(int lookupId);
111 #endif
112 };
113 
115 {
116 public:
118  : err(QHostInfo::NoError),
119  errorStr(QLatin1String(QT_TRANSLATE_NOOP("QHostInfo", "Unknown error"))),
120  lookupId(0)
121  {
122  }
123 #ifndef QT_NO_BEARERMANAGEMENT
124  //not a public API yet
125  static QHostInfo fromName(const QString &hostName, QSharedPointer<QNetworkSession> networkSession);
126 #endif
127 
132  int lookupId;
133 };
134 
135 // These functions are outside of the QHostInfo class and strictly internal.
136 // Do NOT use them outside of QAbstractSocket.
137 QHostInfo Q_NETWORK_EXPORT qt_qhostinfo_lookup(const QString &name, QObject *receiver, const char *member, bool *valid, int *id);
140 
142 {
143 public:
144  QHostInfoCache();
145  const int max_age; // seconds
146 
147  QHostInfo get(const QString &name, bool *valid);
148  void put(const QString &name, const QHostInfo &info);
149  void clear();
150 
151  bool isEnabled();
152  void setEnabled(bool e);
153 private:
154  bool enabled;
158  };
161 };
162 
163 // the following classes are used for the (normal) case: We use multiple threads to lookup DNS
164 
166 {
167 public:
168  QHostInfoRunnable (QString hn, int i);
169  void run();
170 
172  int id;
174 };
175 
176 
178 {
179  Q_OBJECT
180 
181 public:
183  virtual void clear() = 0;
184 
186 
187 protected:
189  static QAbstractHostInfoLookupManager* globalInstance();
190 
191 };
192 
193 #ifndef Q_OS_SYMBIAN
195 {
196  Q_OBJECT
197 public:
200 
201  void clear();
202  void work();
203 
204  // called from QHostInfo
205  void scheduleLookup(QHostInfoRunnable *r);
206  void abortLookup(int id);
207 
208  // called from QHostInfoRunnable
209  void lookupFinished(QHostInfoRunnable *r);
210  bool wasAborted(int id);
211 
212  friend class QHostInfoRunnable;
213 protected:
215  QList<QHostInfoRunnable*> postponedLookups; // postponed because in progress for same host
218  QList<int> abortedLookups; // ids of aborted lookups
219 
221 
223 
225 
226 private slots:
227  void waitForThreadPoolDone() { threadPool.waitForDone(); }
228 };
229 
230 #else
231 
232 class QSymbianHostResolver : public CActive
233 {
234 public:
235  QSymbianHostResolver(const QString &hostName, int id, QSharedPointer<QNetworkSession> networkSession);
236  ~QSymbianHostResolver();
237 
238  void requestHostLookup();
239  void abortHostLookup();
240  int id();
241 
242  void returnResults();
243 
244  QHostInfoResult resultEmitter;
245 
246 private:
247  void DoCancel();
248  void RunL();
249  void run();
250  TInt RunError(TInt aError);
251 
252  void processNameResult();
253  void nextNameResult();
254  void processAddressResult();
255 
256 private:
257  int iId;
258 
259  const QString iHostName;
260  QString iEncodedHostName;
261  TPtrC iHostNamePtr;
262 
263  RSocketServ& iSocketServ;
264  RHostResolver iHostResolver;
265  QSharedPointer<QNetworkSession> iNetworkSession;
266 
267  TNameEntry iNameResult;
268  TInetAddr IpAdd;
269 
270  QHostAddress iAddress;
271 
272  QHostInfo iResults;
273 
274  QList<QHostAddress> iHostAddresses;
275 
276  enum {
277  EIdle,
278  EGetByName,
279  EGetByAddress,
280  ECompleteFromCache,
281  EError
282  } iState;
283 };
284 
285 class QSymbianHostInfoLookupManager : public QAbstractHostInfoLookupManager
286 {
287  Q_OBJECT
288 public:
289  QSymbianHostInfoLookupManager();
290  ~QSymbianHostInfoLookupManager();
291 
292  static QSymbianHostInfoLookupManager* globalInstance();
293 
294  int id();
295  void clear();
296 
297  // called from QHostInfo
298  void scheduleLookup(QSymbianHostResolver *r);
299  void abortLookup(int id);
300 
301  // called from QSymbianHostResolver
302  void lookupFinished(QSymbianHostResolver *r);
303 
304 private:
305  void runNextLookup();
306 
307  // this is true for single threaded use, with multiple threads the max is ((number of threads) + KMaxConcurrentLookups - 1)
308  static const int KMaxConcurrentLookups = 5;
309 
310  QList<QSymbianHostResolver*> iCurrentLookups;
311  QList<QSymbianHostResolver*> iScheduledLookups;
312 
313  QMutex mutex;
314 };
315 #endif
316 
317 
318 
320 
321 #endif // QHOSTINFO_P_H
static mach_timebase_info_data_t info
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void emitResultsReady(const QHostInfo &info)
Definition: qhostinfo_p.h:89
The QQueue class is a generic container that provides a queue.
Definition: qcontainerfwd.h:61
The QMutex class provides access serialization between threads.
Definition: qmutex.h:60
QList< QHostAddress > addrs
Definition: qhostinfo_p.h:130
void Q_AUTOTEST_EXPORT qt_qhostinfo_enable_cache(bool e)
Definition: qhostinfo.cpp:755
Definition: qcache.h:54
QHostInfo Q_NETWORK_EXPORT qt_qhostinfo_lookup(const QString &name, QObject *receiver, const char *member, bool *valid, int *id)
Definition: qhostinfo.cpp:726
static void clear(QVariant::Private *d)
Definition: qvariant.cpp:197
QThreadPool threadPool
Definition: qhostinfo_p.h:220
#define QT_TRANSLATE_NOOP(scope, x)
Marks the string literal sourceText for dynamic translation in the given context; i...
Definition: qglobal.h:2487
#define Q_SLOTS
Definition: qobjectdefs.h:71
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_SIGNALS
Definition: qobjectdefs.h:72
The QElapsedTimer class provides a fast way to calculate elapsed times.
Definition: qelapsedtimer.h:53
The QRunnable class is the base class for all runnable objects.
Definition: qrunnable.h:52
The QThreadPool class manages a collection of QThreads.
Definition: qthreadpool.h:58
QCache< QString, QHostInfoCacheElement > cache
Definition: qhostinfo_p.h:159
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QList< int > abortedLookups
Definition: qhostinfo_p.h:218
QString toBeLookedUp
Definition: qhostinfo_p.h:171
void waitForDone()
Waits for each thread to exit and removes all threads from the thread pool.
const char * name
#define emit
Definition: qobjectdefs.h:76
QHostInfo::HostInfoError err
Definition: qhostinfo_p.h:128
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
The QHostInfo class provides static functions for host name lookups.
Definition: qhostinfo.h:58
void resultsReady(const QHostInfo &info)
QList< QHostInfoRunnable * > currentLookups
Definition: qhostinfo_p.h:214
#define Q_OBJECT
Definition: qobjectdefs.h:157
const int max_age
Definition: qhostinfo_p.h:145
#define Q_NETWORK_EXPORT
Definition: qglobal.h:1452
#define Q_AUTOTEST_EXPORT
Definition: qglobal.h:1510
QHostInfoResult resultEmitter
Definition: qhostinfo_p.h:173
QFuture< T > run(Function function,...)
HostInfoError
This enum describes the various errors that can occur when trying to resolve a host name...
Definition: qhostinfo.h:61
The QHostAddress class provides an IP address.
Definition: qhostaddress.h:70
QList< QHostInfoRunnable * > finishedLookups
Definition: qhostinfo_p.h:217
#define slots
Definition: qobjectdefs.h:68
QQueue< QHostInfoRunnable * > scheduledLookups
Definition: qhostinfo_p.h:216
QList< QHostInfoRunnable * > postponedLookups
Definition: qhostinfo_p.h:215
void Q_AUTOTEST_EXPORT qt_qhostinfo_clear_cache()
Definition: qhostinfo.cpp:747