2011-01-11 19:05:24 +00:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
|
|
|
* Copyright (C) 2011 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 <QList>
|
2014-01-20 22:10:45 +00:00
|
|
|
#include <QDateTime>
|
2011-01-11 19:05:24 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2010-12-18 15:34:38 +00:00
|
|
|
#include "qbtsession.h"
|
|
|
|
#include "misc.h"
|
2011-01-11 19:05:24 +00:00
|
|
|
#include "torrentspeedmonitor.h"
|
2010-12-18 15:34:38 +00:00
|
|
|
|
|
|
|
using namespace libtorrent;
|
|
|
|
|
2013-03-10 18:04:05 +00:00
|
|
|
template<class T> struct Sample {
|
|
|
|
Sample(const T down = 0, const T up = 0) : download(down), upload(up) {}
|
|
|
|
T download;
|
|
|
|
T upload;
|
|
|
|
};
|
|
|
|
|
2011-01-11 19:05:24 +00:00
|
|
|
class SpeedSample {
|
|
|
|
|
|
|
|
public:
|
2012-02-20 17:56:07 +00:00
|
|
|
SpeedSample() {}
|
2013-03-10 18:04:05 +00:00
|
|
|
void addSample(int speedDL, int speedUL);
|
|
|
|
Sample<qreal> average() const;
|
2011-01-11 19:05:24 +00:00
|
|
|
void clear();
|
|
|
|
|
|
|
|
private:
|
|
|
|
static const int max_samples = 30;
|
|
|
|
|
|
|
|
private:
|
2013-03-10 18:04:05 +00:00
|
|
|
QList<Sample<int> > m_speedSamples;
|
2011-01-11 19:05:24 +00:00
|
|
|
};
|
|
|
|
|
2014-05-24 20:30:30 +00:00
|
|
|
TorrentSpeedMonitor::TorrentSpeedMonitor(QBtSession* session)
|
|
|
|
: m_session(session)
|
2010-12-18 15:34:38 +00:00
|
|
|
{
|
|
|
|
connect(m_session, SIGNAL(deletedTorrent(QString)), SLOT(removeSamples(QString)));
|
|
|
|
connect(m_session, SIGNAL(pausedTorrent(QTorrentHandle)), SLOT(removeSamples(QTorrentHandle)));
|
2014-05-24 20:30:30 +00:00
|
|
|
connect(m_session, SIGNAL(statsReceived(libtorrent::stats_alert)), SLOT(statsReceived(libtorrent::stats_alert)));
|
2010-12-18 15:34:38 +00:00
|
|
|
}
|
|
|
|
|
2014-05-24 20:30:30 +00:00
|
|
|
TorrentSpeedMonitor::~TorrentSpeedMonitor()
|
|
|
|
{}
|
2010-12-18 15:34:38 +00:00
|
|
|
|
2013-03-10 18:04:05 +00:00
|
|
|
void SpeedSample::addSample(int speedDL, int speedUL)
|
2010-12-18 15:34:38 +00:00
|
|
|
{
|
2013-03-10 18:04:05 +00:00
|
|
|
m_speedSamples << Sample<int>(speedDL, speedUL);
|
2012-02-20 17:30:53 +00:00
|
|
|
if (m_speedSamples.size() > max_samples)
|
2010-12-18 15:34:38 +00:00
|
|
|
m_speedSamples.removeFirst();
|
|
|
|
}
|
|
|
|
|
2013-03-10 18:04:05 +00:00
|
|
|
Sample<qreal> SpeedSample::average() const
|
2010-12-18 15:34:38 +00:00
|
|
|
{
|
2013-03-01 09:31:45 +00:00
|
|
|
if (m_speedSamples.empty())
|
2013-03-10 18:04:05 +00:00
|
|
|
return Sample<qreal>();
|
2013-03-01 09:31:45 +00:00
|
|
|
|
2013-03-10 18:04:05 +00:00
|
|
|
qlonglong sumDL = 0;
|
|
|
|
qlonglong sumUL = 0;
|
2013-03-01 09:31:45 +00:00
|
|
|
|
2013-03-10 18:04:05 +00:00
|
|
|
foreach (const Sample<int>& s, m_speedSamples) {
|
|
|
|
sumDL += s.download;
|
|
|
|
sumUL += s.upload;
|
2010-12-18 15:34:38 +00:00
|
|
|
}
|
2013-03-01 09:31:45 +00:00
|
|
|
|
2013-03-10 18:04:05 +00:00
|
|
|
const qreal numSamples = m_speedSamples.size();
|
|
|
|
return Sample<qreal>(sumDL/numSamples, sumUL/numSamples);
|
2010-12-18 15:34:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpeedSample::clear()
|
|
|
|
{
|
|
|
|
m_speedSamples.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TorrentSpeedMonitor::removeSamples(const QString &hash)
|
|
|
|
{
|
|
|
|
m_samples.remove(hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TorrentSpeedMonitor::removeSamples(const QTorrentHandle& h) {
|
|
|
|
try {
|
|
|
|
m_samples.remove(h.hash());
|
2012-02-20 17:56:07 +00:00
|
|
|
} catch(invalid_handle&) {}
|
2010-12-18 15:34:38 +00:00
|
|
|
}
|
|
|
|
|
2014-05-13 22:09:45 +00:00
|
|
|
qlonglong TorrentSpeedMonitor::getETA(const QString &hash, const libtorrent::torrent_status &status) const
|
2010-12-18 15:34:38 +00:00
|
|
|
{
|
2014-05-13 22:09:45 +00:00
|
|
|
if (QTorrentHandle::is_paused(status) || !m_samples.contains(hash))
|
2013-03-01 10:41:46 +00:00
|
|
|
return MAX_ETA;
|
2013-03-01 09:31:45 +00:00
|
|
|
|
2013-03-10 18:04:05 +00:00
|
|
|
const Sample<qreal> speed_average = m_samples[hash].average();
|
2013-03-01 09:31:45 +00:00
|
|
|
|
2014-05-13 22:09:45 +00:00
|
|
|
if (QTorrentHandle::is_seed(status)) {
|
2013-03-10 18:04:05 +00:00
|
|
|
if (!speed_average.upload)
|
2013-03-01 10:41:46 +00:00
|
|
|
return MAX_ETA;
|
2013-03-01 09:31:45 +00:00
|
|
|
|
|
|
|
bool _unused;
|
|
|
|
qreal max_ratio = m_session->getMaxRatioPerTorrent(hash, &_unused);
|
|
|
|
if (max_ratio < 0)
|
2013-03-01 10:41:46 +00:00
|
|
|
return MAX_ETA;
|
2013-03-01 09:31:45 +00:00
|
|
|
|
2014-05-13 22:09:45 +00:00
|
|
|
libtorrent::size_type realDL = status.all_time_download;
|
2013-03-01 09:31:45 +00:00
|
|
|
if (realDL <= 0)
|
2014-05-13 22:09:45 +00:00
|
|
|
realDL = status.total_wanted;
|
2013-03-01 09:31:45 +00:00
|
|
|
|
2014-05-13 22:09:45 +00:00
|
|
|
return (realDL * max_ratio - status.all_time_upload) / speed_average.upload;
|
2013-03-01 09:31:45 +00:00
|
|
|
}
|
2013-03-10 18:04:05 +00:00
|
|
|
|
|
|
|
if (!speed_average.download)
|
|
|
|
return MAX_ETA;
|
|
|
|
|
2014-05-13 22:09:45 +00:00
|
|
|
return (status.total_wanted - status.total_wanted_done) / speed_average.download;
|
2010-12-18 15:34:38 +00:00
|
|
|
}
|
|
|
|
|
2014-05-24 20:30:30 +00:00
|
|
|
void TorrentSpeedMonitor::statsReceived(const stats_alert &stats)
|
2010-12-18 15:34:38 +00:00
|
|
|
{
|
2014-05-24 20:30:30 +00:00
|
|
|
m_samples[misc::toQString(stats.handle.info_hash())].addSample(stats.transferred[stats_alert::download_payload] * 1000 / stats.interval,
|
|
|
|
stats.transferred[stats_alert::upload_payload] * 1000 / stats.interval);
|
2013-11-14 19:56:13 +00:00
|
|
|
}
|