src/network/kernel/qhostinfo_p.h
changeset 19 fcece45ef507
parent 18 2f34d5167611
child 30 5dc02b23752f
--- a/src/network/kernel/qhostinfo_p.h	Fri Apr 16 15:50:13 2010 +0300
+++ b/src/network/kernel/qhostinfo_p.h	Mon May 03 13:17:34 2010 +0300
@@ -68,6 +68,8 @@
 #include "QtCore/qrunnable.h"
 #include "QtCore/qlist.h"
 #include "QtCore/qqueue.h"
+#include <QTime>
+#include <QCache>
 #endif
 
 QT_BEGIN_NAMESPACE
@@ -111,6 +113,34 @@
 };
 
 #ifndef QT_NO_THREAD
+// These functions are outside of the QHostInfo class and strictly internal.
+// Do NOT use them outside of QAbstractSocket.
+QHostInfo Q_NETWORK_EXPORT qt_qhostinfo_lookup(const QString &name, QObject *receiver, const char *member, bool *valid, int *id);
+void Q_NETWORK_EXPORT qt_qhostinfo_clear_cache();
+void Q_AUTOTEST_EXPORT qt_qhostinfo_enable_cache(bool e);
+
+class QHostInfoCache
+{
+public:
+    QHostInfoCache();
+    const int max_age; // seconds
+
+    QHostInfo get(const QString &name, bool *valid);
+    void put(const QString &name, const QHostInfo &info);
+    void clear();
+
+    bool isEnabled();
+    void setEnabled(bool e);
+private:
+    bool enabled;
+    struct QHostInfoCacheElement {
+        QHostInfo info;
+        QTime age;
+    };
+    QCache<QString,QHostInfoCacheElement> cache;
+    QMutex mutex;
+};
+
 // the following classes are used for the (normal) case: We use multiple threads to lookup DNS
 
 class QHostInfoRunnable : public QRunnable
@@ -141,6 +171,7 @@
     void lookupFinished(QHostInfoRunnable *r);
     bool wasAborted(int id);
 
+    QHostInfoCache cache;
 protected:
     QList<QHostInfoRunnable*> currentLookups; // in progress
     QList<QHostInfoRunnable*> postponedLookups; // postponed because in progress for same host
@@ -153,7 +184,11 @@
     QMutex mutex;
 
     bool wasDeleted;
+
+private slots:
+    void waitForThreadPoolDone() { threadPool.waitForDone(); }
 };
+
 #endif
 
 QT_END_NAMESPACE