2009-11-14 16:33:55 +03:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
|
|
|
* Copyright (C) 2006 Christophe Dumez
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
|
|
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
|
|
|
* and distribute the linked executables. You must obey the GNU General Public
|
|
|
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
|
|
|
* modify file(s), you may extend this exception to your version of the file(s),
|
|
|
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
|
|
|
* exception statement from your version.
|
|
|
|
*
|
|
|
|
* Contact : chris@qbittorrent.org
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef REVERSERESOLUTION_H
|
|
|
|
#define REVERSERESOLUTION_H
|
|
|
|
|
|
|
|
#include <QQueue>
|
|
|
|
#include <QThread>
|
|
|
|
#include <QWaitCondition>
|
|
|
|
#include <QMutex>
|
|
|
|
#include <QList>
|
2009-11-29 00:46:46 +03:00
|
|
|
#include <libtorrent/asio/ip/tcp.hpp>
|
2009-11-14 16:33:55 +03:00
|
|
|
#include "misc.h"
|
|
|
|
|
|
|
|
#define MAX_THREADS 20
|
|
|
|
|
|
|
|
class ReverseResolutionST: public QThread {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private:
|
2009-11-29 00:46:46 +03:00
|
|
|
libtorrent::asio::ip::tcp::endpoint ip;
|
|
|
|
libtorrent::asio::ip::tcp::resolver resolver;
|
2009-11-15 13:17:21 +03:00
|
|
|
bool stopped;
|
2009-11-14 16:33:55 +03:00
|
|
|
|
|
|
|
public:
|
2009-11-29 00:46:46 +03:00
|
|
|
ReverseResolutionST(libtorrent::asio::io_service &ios, QObject *parent=0): QThread(parent), resolver(ios), stopped(false) {
|
2009-11-14 16:33:55 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
~ReverseResolutionST() {
|
2009-11-15 13:17:21 +03:00
|
|
|
stopped = true;
|
2009-11-28 16:50:16 +03:00
|
|
|
if(isRunning()) {
|
2009-11-15 13:17:21 +03:00
|
|
|
resolver.cancel();
|
2009-11-28 16:50:16 +03:00
|
|
|
wait();
|
|
|
|
}
|
2009-11-14 16:33:55 +03:00
|
|
|
}
|
|
|
|
|
2009-11-29 00:46:46 +03:00
|
|
|
void setIP(libtorrent::asio::ip::tcp::endpoint &_ip) {
|
2009-11-14 16:33:55 +03:00
|
|
|
ip = _ip;
|
|
|
|
}
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void ip_resolved(QString ip, QString hostname);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void run() {
|
2009-11-21 11:37:09 +03:00
|
|
|
try {
|
2009-11-29 00:46:46 +03:00
|
|
|
libtorrent::asio::ip::tcp::resolver::iterator it = resolver.resolve(ip);
|
2009-11-21 11:37:09 +03:00
|
|
|
if(stopped) return;
|
2009-11-29 00:46:46 +03:00
|
|
|
libtorrent::asio::ip::tcp::endpoint endpoint = *it;
|
2009-11-21 11:37:09 +03:00
|
|
|
emit ip_resolved(misc::toQString(endpoint.address().to_string()), misc::toQString((*it).host_name()));
|
|
|
|
} catch(std::exception &e) {
|
|
|
|
std::cerr << "Hostname resolution failed, reason: " << e.what() << std::endl;
|
|
|
|
}
|
2009-11-14 16:33:55 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ReverseResolution: public QThread {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private:
|
2009-11-29 00:46:46 +03:00
|
|
|
QQueue<libtorrent::asio::ip::tcp::endpoint> ips;
|
2009-11-14 16:33:55 +03:00
|
|
|
QMutex mut;
|
|
|
|
QWaitCondition cond;
|
|
|
|
bool stopped;
|
2009-11-29 00:46:46 +03:00
|
|
|
libtorrent::asio::io_service ios;
|
2009-11-14 16:33:55 +03:00
|
|
|
QList<ReverseResolutionST*> subThreads;
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
ReverseResolution(QObject* parent): QThread(parent), stopped(false) {
|
|
|
|
}
|
|
|
|
|
|
|
|
~ReverseResolution() {
|
2009-11-15 13:17:21 +03:00
|
|
|
qDebug("Deleting host name resolver...");
|
|
|
|
if(!stopped) {
|
|
|
|
stopped = true;
|
|
|
|
cond.wakeOne();
|
|
|
|
}
|
|
|
|
wait();
|
|
|
|
qDebug("Host name resolver was deleted");
|
|
|
|
}
|
|
|
|
|
|
|
|
void asyncDelete() {
|
|
|
|
connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
|
|
|
|
qDebug("Deleting async host name resolver...");
|
2009-11-14 16:33:55 +03:00
|
|
|
stopped = true;
|
|
|
|
cond.wakeOne();
|
|
|
|
}
|
|
|
|
|
2009-11-29 00:46:46 +03:00
|
|
|
void resolve(libtorrent::asio::ip::tcp::endpoint ip) {
|
2009-11-14 16:33:55 +03:00
|
|
|
mut.lock();
|
|
|
|
ips.enqueue(ip);
|
|
|
|
if(subThreads.size() < MAX_THREADS)
|
|
|
|
cond.wakeOne();
|
|
|
|
mut.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void ip_resolved(QString ip, QString hostname);
|
|
|
|
|
|
|
|
protected slots:
|
|
|
|
void forwardSignal(QString ip, QString hostname) {
|
|
|
|
emit ip_resolved(ip, hostname);
|
|
|
|
mut.lock();
|
|
|
|
subThreads.removeOne(static_cast<ReverseResolutionST*>(sender()));
|
|
|
|
if(!ips.empty())
|
|
|
|
cond.wakeOne();
|
|
|
|
mut.unlock();
|
2009-11-14 17:18:32 +03:00
|
|
|
delete sender();
|
|
|
|
//sender()->deleteLater();
|
2009-11-14 16:33:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void run() {
|
|
|
|
do {
|
|
|
|
mut.lock();
|
|
|
|
cond.wait(&mut);
|
2009-11-15 13:17:21 +03:00
|
|
|
if(stopped) {
|
|
|
|
mut.unlock();
|
|
|
|
break;
|
|
|
|
}
|
2009-11-29 00:46:46 +03:00
|
|
|
libtorrent::asio::ip::tcp::endpoint ip = ips.dequeue();
|
2009-11-14 16:33:55 +03:00
|
|
|
ReverseResolutionST *st = new ReverseResolutionST(ios);
|
|
|
|
subThreads.append(st);
|
|
|
|
mut.unlock();
|
|
|
|
connect(st, SIGNAL(ip_resolved(QString,QString)), this, SLOT(forwardSignal(QString,QString)));
|
|
|
|
st->setIP(ip);
|
|
|
|
st->start();
|
|
|
|
}while(!stopped);
|
2009-11-15 13:17:21 +03:00
|
|
|
mut.lock();
|
|
|
|
qDeleteAll(subThreads);
|
|
|
|
mut.unlock();
|
2009-11-14 16:33:55 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // REVERSERESOLUTION_H
|