Browse Source

Greatly improved peer host name resolution with caching

adaptive-webui-19844
Christophe Dumez 14 years ago
parent
commit
1eb26bd78b
  1. 19
      src/peerlistwidget.cpp
  2. 22
      src/peerlistwidget.h
  3. 53
      src/reverseresolution.h

19
src/peerlistwidget.cpp

@ -308,8 +308,14 @@ void PeerListWidget::loadPeers(const QTorrentHandle &h, bool force_hostname_reso @@ -308,8 +308,14 @@ void PeerListWidget::loadPeers(const QTorrentHandle &h, bool force_hostname_reso
updatePeer(peer_ip, peer);
old_peers_set.remove(peer_ip);
if(force_hostname_resolution) {
if(resolver)
if(resolver) {
QString host = resolver->getHostFromCache(peer.ip);
if(host.isNull()) {
resolver->resolve(peer.ip);
} else {
peerItems.value(peer_ip)->setData(host);
}
}
}
} else {
// Add new peer
@ -332,10 +338,17 @@ QStandardItem* PeerListWidget::addPeer(QString ip, peer_info peer) { @@ -332,10 +338,17 @@ QStandardItem* PeerListWidget::addPeer(QString ip, peer_info peer) {
int row = listModel->rowCount();
// Adding Peer to peer list
listModel->insertRow(row);
QString host;
if(resolver) {
host = resolver->getHostFromCache(peer.ip);
}
if(host.isNull())
listModel->setData(listModel->index(row, IP), ip);
else
listModel->setData(listModel->index(row, IP), host);
listModel->setData(listModel->index(row, IP_HIDDEN), ip);
// Resolve peer host name is asked
if(resolver)
if(resolver && host.isNull())
resolver->resolve(peer.ip);
if(display_flags) {
QString country_name;
@ -381,6 +394,6 @@ void PeerListWidget::updatePeer(QString ip, peer_info peer) { @@ -381,6 +394,6 @@ void PeerListWidget::updatePeer(QString ip, peer_info peer) {
void PeerListWidget::handleResolved(QString ip, QString hostname) {
QStandardItem *item = peerItems.value(ip, 0);
if(item) {
listModel->setData(listModel->indexFromItem(item), hostname);
item->setData(hostname);
}
}

22
src/peerlistwidget.h

@ -55,17 +55,6 @@ class PropertiesWidget; @@ -55,17 +55,6 @@ class PropertiesWidget;
class PeerListWidget : public QTreeView {
Q_OBJECT
private:
QStandardItemModel *listModel;
PeerListDelegate *listDelegate;
QSortFilterProxyModel * proxyModel;
QHash<QString, QStandardItem*> peerItems;
QHash<QString, libtorrent::asio::ip::tcp::endpoint> peerEndpoints;
QSet<QString> missingFlags;
QPointer<ReverseResolution> resolver;
PropertiesWidget* properties;
bool display_flags;
public:
PeerListWidget(PropertiesWidget *parent);
~PeerListWidget();
@ -86,6 +75,17 @@ protected slots: @@ -86,6 +75,17 @@ protected slots:
void limitUpRateSelectedPeers(QStringList peer_ips);
void limitDlRateSelectedPeers(QStringList peer_ips);
void banSelectedPeers(QStringList peer_ips);
private:
QStandardItemModel *listModel;
PeerListDelegate *listDelegate;
QSortFilterProxyModel * proxyModel;
QHash<QString, QStandardItem*> peerItems;
QHash<QString, libtorrent::asio::ip::tcp::endpoint> peerEndpoints;
QSet<QString> missingFlags;
QPointer<ReverseResolution> resolver;
PropertiesWidget* properties;
bool display_flags;
};
#endif // PEERLISTWIDGET_H

53
src/reverseresolution.h

@ -36,6 +36,7 @@ @@ -36,6 +36,7 @@
#include <QWaitCondition>
#include <QMutex>
#include <QList>
#include <QCache>
#include "misc.h"
#include <boost/version.hpp>
@ -47,7 +48,8 @@ @@ -47,7 +48,8 @@
using namespace libtorrent;
#define MAX_THREADS 20
const int MAX_THREADS = 20;
const int CACHE_SIZE = 500;
class ReverseResolutionST: public QThread {
Q_OBJECT
@ -58,7 +60,8 @@ private: @@ -58,7 +60,8 @@ private:
bool stopped;
public:
ReverseResolutionST(libtorrent::asio::io_service &ios, QObject *parent=0): QThread(parent), resolver(ios), stopped(false) {
ReverseResolutionST(libtorrent::asio::io_service &ios, QObject *parent=0):
QThread(parent), resolver(ios), stopped(false) {
}
@ -93,18 +96,11 @@ protected: @@ -93,18 +96,11 @@ protected:
class ReverseResolution: public QThread {
Q_OBJECT
private:
QQueue<libtorrent::asio::ip::tcp::endpoint> ips;
QMutex mut;
QWaitCondition cond;
bool stopped;
libtorrent::asio::io_service ios;
QList<ReverseResolutionST*> subThreads;
Q_DISABLE_COPY(ReverseResolution)
public:
ReverseResolution(QObject* parent): QThread(parent), stopped(false) {
explicit ReverseResolution(QObject* parent): QThread(parent), stopped(false) {
cache = new QCache<QString, QString>(CACHE_SIZE);
}
~ReverseResolution() {
@ -113,6 +109,7 @@ public: @@ -113,6 +109,7 @@ public:
stopped = true;
cond.wakeOne();
}
delete cache;
wait();
qDebug("Host name resolver was deleted");
}
@ -124,8 +121,29 @@ public: @@ -124,8 +121,29 @@ public:
cond.wakeOne();
}
QString getHostFromCache(libtorrent::asio::ip::tcp::endpoint ip) {
mut.lock();
QString ip_str = misc::toQString(ip.address().to_string());
QString ret;
if(cache->contains(ip_str)) {
qDebug("Got host name from cache");
ret = *cache->object(ip_str);
} else {
ret = QString::null;
}
mut.unlock();
return ret;
}
void resolve(libtorrent::asio::ip::tcp::endpoint ip) {
mut.lock();
QString ip_str = misc::toQString(ip.address().to_string());
if(cache->contains(ip_str)) {
qDebug("Resolved host name using cache");
emit ip_resolved(ip_str, *cache->object(ip_str));
mut.unlock();
return;
}
ips.enqueue(ip);
if(subThreads.size() < MAX_THREADS)
cond.wakeOne();
@ -139,6 +157,7 @@ protected slots: @@ -139,6 +157,7 @@ protected slots:
void forwardSignal(QString ip, QString hostname) {
emit ip_resolved(ip, hostname);
mut.lock();
cache->insert(ip, new QString(hostname));
subThreads.removeOne(static_cast<ReverseResolutionST*>(sender()));
if(!ips.empty())
cond.wakeOne();
@ -168,6 +187,16 @@ protected: @@ -168,6 +187,16 @@ protected:
qDeleteAll(subThreads);
mut.unlock();
}
private:
QQueue<libtorrent::asio::ip::tcp::endpoint> ips;
QMutex mut;
QWaitCondition cond;
bool stopped;
libtorrent::asio::io_service ios;
QCache<QString, QString> *cache;
QList<ReverseResolutionST*> subThreads;
};
#endif // REVERSERESOLUTION_H

Loading…
Cancel
Save