src/network/kernel/qhostinfo_p.h
changeset 19 fcece45ef507
parent 18 2f34d5167611
child 30 5dc02b23752f
equal deleted inserted replaced
18:2f34d5167611 19:fcece45ef507
    66 #include "QtCore/qthreadpool.h"
    66 #include "QtCore/qthreadpool.h"
    67 #include "QtCore/qmutex.h"
    67 #include "QtCore/qmutex.h"
    68 #include "QtCore/qrunnable.h"
    68 #include "QtCore/qrunnable.h"
    69 #include "QtCore/qlist.h"
    69 #include "QtCore/qlist.h"
    70 #include "QtCore/qqueue.h"
    70 #include "QtCore/qqueue.h"
       
    71 #include <QTime>
       
    72 #include <QCache>
    71 #endif
    73 #endif
    72 
    74 
    73 QT_BEGIN_NAMESPACE
    75 QT_BEGIN_NAMESPACE
    74 
    76 
    75 class QHostInfoResult : public QObject
    77 class QHostInfoResult : public QObject
   109     QString hostName;
   111     QString hostName;
   110     int lookupId;
   112     int lookupId;
   111 };
   113 };
   112 
   114 
   113 #ifndef QT_NO_THREAD
   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_NETWORK_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 
   114 // the following classes are used for the (normal) case: We use multiple threads to lookup DNS
   144 // the following classes are used for the (normal) case: We use multiple threads to lookup DNS
   115 
   145 
   116 class QHostInfoRunnable : public QRunnable
   146 class QHostInfoRunnable : public QRunnable
   117 {
   147 {
   118 public:
   148 public:
   139 
   169 
   140     // called from QHostInfoRunnable
   170     // called from QHostInfoRunnable
   141     void lookupFinished(QHostInfoRunnable *r);
   171     void lookupFinished(QHostInfoRunnable *r);
   142     bool wasAborted(int id);
   172     bool wasAborted(int id);
   143 
   173 
       
   174     QHostInfoCache cache;
   144 protected:
   175 protected:
   145     QList<QHostInfoRunnable*> currentLookups; // in progress
   176     QList<QHostInfoRunnable*> currentLookups; // in progress
   146     QList<QHostInfoRunnable*> postponedLookups; // postponed because in progress for same host
   177     QList<QHostInfoRunnable*> postponedLookups; // postponed because in progress for same host
   147     QQueue<QHostInfoRunnable*> scheduledLookups; // not yet started
   178     QQueue<QHostInfoRunnable*> scheduledLookups; // not yet started
   148     QList<QHostInfoRunnable*> finishedLookups; // recently finished
   179     QList<QHostInfoRunnable*> finishedLookups; // recently finished
   151     QThreadPool threadPool;
   182     QThreadPool threadPool;
   152 
   183 
   153     QMutex mutex;
   184     QMutex mutex;
   154 
   185 
   155     bool wasDeleted;
   186     bool wasDeleted;
       
   187 
       
   188 private slots:
       
   189     void waitForThreadPoolDone() { threadPool.waitForDone(); }
   156 };
   190 };
       
   191 
   157 #endif
   192 #endif
   158 
   193 
   159 QT_END_NAMESPACE
   194 QT_END_NAMESPACE
   160 
   195 
   161 #endif // QHOSTINFO_P_H
   196 #endif // QHOSTINFO_P_H