Move network related code to core/net.

This commit is contained in:
Vladimir Golovnev (Glassez)
2015-04-13 19:02:48 +03:00
parent 3eeed813d6
commit 4b5e7e6168
14 changed files with 154 additions and 70 deletions

View File

@@ -10,8 +10,6 @@ HEADERS += \
$$PWD/filesystemwatcher.h \
$$PWD/scannedfoldersmodel.h \
$$PWD/qinisettings.h \
$$PWD/smtp.h \
$$PWD/dnsupdater.h \
$$PWD/logger.h \
$$PWD/preferences.h \
$$PWD/qtracker.h \
@@ -21,7 +19,10 @@ HEADERS += \
$$PWD/http/responsegenerator.h \
$$PWD/http/server.h \
$$PWD/http/types.h \
$$PWD/http/responsebuilder.h
$$PWD/http/responsebuilder.h \
$$PWD/net/dnsupdater.h \
$$PWD/net/reverseresolution.h \
$$PWD/net/smtp.h
SOURCES += \
$$PWD/downloadthread.cpp \
@@ -29,8 +30,6 @@ SOURCES += \
$$PWD/torrentpersistentdata.cpp \
$$PWD/misc.cpp \
$$PWD/fs_utils.cpp \
$$PWD/smtp.cpp \
$$PWD/dnsupdater.cpp \
$$PWD/logger.cpp \
$$PWD/preferences.cpp \
$$PWD/qtracker.cpp \
@@ -38,4 +37,7 @@ SOURCES += \
$$PWD/http/requestparser.cpp \
$$PWD/http/responsegenerator.cpp \
$$PWD/http/server.cpp \
$$PWD/http/responsebuilder.cpp
$$PWD/http/responsebuilder.cpp \
$$PWD/net/dnsupdater.cpp \
$$PWD/net/reverseresolution.cpp \
$$PWD/net/smtp.cpp

View File

@@ -38,6 +38,8 @@
#include "dnsupdater.h"
#include "logger.h"
using namespace Net;
DNSUpdater::DNSUpdater(QObject *parent) :
QObject(parent), m_state(OK), m_service(DNS::NONE)
{

View File

@@ -38,6 +38,9 @@
#include <QTimer>
#include "preferences.h"
namespace Net
{
/*!
* Based on http://www.dyndns.com/developers/specs/
*/
@@ -78,4 +81,6 @@ private:
enum State { OK, INVALID_CREDS, FATAL };
};
}
#endif // DNSUPDATER_H

View File

@@ -0,0 +1,94 @@
/*
* Bittorrent Client using Qt 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
*/
#include <QDebug>
#include <QHostInfo>
#include <QString>
#include <boost/version.hpp>
#if BOOST_VERSION < 103500
#include <libtorrent/asio/ip/tcp.hpp>
#else
#include <boost/asio/ip/tcp.hpp>
#endif
#include "reverseresolution.h"
const int CACHE_SIZE = 500;
using namespace Net;
static inline bool isUsefulHostName(const QString &hostname, const QString &ip)
{
return (!hostname.isEmpty() && hostname != ip);
}
ReverseResolution::ReverseResolution(QObject *parent)
: QObject(parent)
{
m_cache.setMaxCost(CACHE_SIZE);
}
ReverseResolution::~ReverseResolution()
{
qDebug("Deleting host name resolver...");
}
void ReverseResolution::resolve(const QString &ip)
{
if (m_cache.contains(ip)) {
const QString &hostname = *m_cache.object(ip);
qDebug() << "Resolved host name using cache: " << ip << " -> " << hostname;
if (isUsefulHostName(hostname, ip))
emit ipResolved(ip, hostname);
}
else {
// Actually resolve the ip
m_lookups.insert(QHostInfo::lookupHost(ip, this, SLOT(hostResolved(QHostInfo))), ip);
}
}
void ReverseResolution::hostResolved(const QHostInfo &host)
{
const QString &ip = m_lookups.take(host.lookupId());
Q_ASSERT(!ip.isNull());
if (host.error() != QHostInfo::NoError) {
qDebug() << "DNS Reverse resolution error: " << host.errorString();
return;
}
const QString &hostname = host.hostName();
qDebug() << Q_FUNC_INFO << ip << QString("->") << hostname;
m_cache.insert(ip, new QString(hostname));
if (isUsefulHostName(hostname, ip))
emit ipResolved(ip, hostname);
}

View File

@@ -0,0 +1,67 @@
/*
* Bittorrent Client using Qt 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 NET_REVERSERESOLUTION_H
#define NET_REVERSERESOLUTION_H
#include <QCache>
#include <QObject>
QT_BEGIN_NAMESPACE
class QHostInfo;
class QString;
QT_END_NAMESPACE
namespace Net
{
class ReverseResolution : public QObject
{
Q_OBJECT
Q_DISABLE_COPY(ReverseResolution)
public:
explicit ReverseResolution(QObject *parent = 0);
~ReverseResolution();
void resolve(const QString &ip);
signals:
void ipResolved(const QString &ip, const QString &hostname);
private slots:
void hostResolved(const QHostInfo &host);
private:
QHash<int /* LookupID */, QString /* IP */> m_lookups;
QCache<QString /* IP */, QString /* HostName */> m_cache;
};
}
#endif // NET_REVERSERESOLUTION_H

View File

@@ -1,5 +1,5 @@
/*
* Bittorrent Client using Qt4 and libtorrent.
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2011 Christophe Dumez
*
* This program is free software; you can redistribute it and/or
@@ -89,6 +89,8 @@ QByteArray determineFQDN()
}
} // namespace
using namespace Net;
Smtp::Smtp(QObject *parent): QObject(parent),
state(Init), use_ssl(false) {
#ifndef QT_NO_OPENSSL

View File

@@ -1,5 +1,5 @@
/*
* Bittorrent Client using Qt4 and libtorrent.
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2011 Christophe Dumez
*
* This program is free software; you can redistribute it and/or
@@ -50,6 +50,9 @@ class QTcpSocket;
class QTextCodec;
QT_END_NAMESPACE
namespace Net
{
class Smtp : public QObject {
Q_OBJECT
@@ -96,4 +99,7 @@ private:
QString username;
QString password;
};
}
#endif

View File

@@ -38,7 +38,7 @@
#include <QProcess>
#include <QCoreApplication>
#include "smtp.h"
#include "core/net/smtp.h"
#include "filesystemwatcher.h"
#include "torrentspeedmonitor.h"
#include "torrentstatistics.h"
@@ -2046,7 +2046,7 @@ void QBtSession::sendNotificationEmail(const QTorrentHandle &h) {
content += tr("The torrent was downloaded in %1.", "The torrent was downloaded in 1 hour and 20 seconds").arg(misc::userFriendlyDuration(status.active_time)) + "\n\n\n";
content += tr("Thank you for using qBittorrent.") + "\n";
// Send the notification email
Smtp *sender = new Smtp(this);
Net::Smtp *sender = new Net::Smtp(this);
sender->sendMail("notification@qbittorrent.org", Preferences::instance()->getMailNotificationEmail(), tr("[qBittorrent] %1 has finished downloading").arg(h.name()), content);
}