1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-11 07:18:08 +00:00
qBittorrent/src/qtlibtorrent/torrentspeedmonitor.cpp

172 lines
4.8 KiB
C++
Raw Normal View History

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
*/
2010-12-18 15:34:38 +00:00
#include <QMutexLocker>
2011-01-11 19:05:24 +00:00
#include <QList>
#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;
2011-01-11 19:05:24 +00:00
class SpeedSample {
public:
2012-02-20 17:56:07 +00:00
SpeedSample() {}
void addSample(qint32 speedDL, qint32 speedUL);
QPair<qreal, qreal> average() const;
2011-01-11 19:05:24 +00:00
void clear();
private:
static const int max_samples = 30;
private:
QList<QPair<qint32, qint32> > m_speedSamples;
2011-01-11 19:05:24 +00:00
};
2010-12-18 15:34:38 +00:00
TorrentSpeedMonitor::TorrentSpeedMonitor(QBtSession* session) :
QThread(session), m_abort(false), m_session(session)
{
connect(m_session, SIGNAL(deletedTorrent(QString)), SLOT(removeSamples(QString)));
connect(m_session, SIGNAL(pausedTorrent(QTorrentHandle)), SLOT(removeSamples(QTorrentHandle)));
}
TorrentSpeedMonitor::~TorrentSpeedMonitor() {
m_abort = true;
m_abortCond.wakeOne();
wait();
}
void TorrentSpeedMonitor::run()
{
do {
2011-01-11 19:05:24 +00:00
m_mutex.lock();
2010-12-18 15:34:38 +00:00
getSamples();
2011-01-11 19:05:24 +00:00
m_abortCond.wait(&m_mutex, 1000);
m_mutex.unlock();
2010-12-18 15:34:38 +00:00
} while(!m_abort);
}
void SpeedSample::addSample(qint32 speedDL, qint32 speedUL)
2010-12-18 15:34:38 +00:00
{
m_speedSamples << qMakePair(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();
}
QPair<qreal, qreal> SpeedSample::average() const
2010-12-18 15:34:38 +00:00
{
if (m_speedSamples.empty())
return qMakePair(0., 0.);
qlonglong sumDL = 0, sumUL =0;
QPair<qint32, qint32> p;
foreach (p, m_speedSamples) {
sumDL += p.first;
sumUL += p.second;
2010-12-18 15:34:38 +00:00
}
qreal numSamples = m_speedSamples.size();
return qMakePair(
(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
}
qlonglong TorrentSpeedMonitor::getETA(const QString &hash) const
{
2011-01-11 19:05:24 +00:00
QMutexLocker locker(&m_mutex);
2010-12-18 15:34:38 +00:00
QTorrentHandle h = m_session->getTorrentHandle(hash);
if (h.is_paused() || !m_samples.contains(hash))
return MAX_ETA;
const QPair<qreal, qreal> speed_average = m_samples.value(hash).average();
if(h.is_seed()) {
if (speed_average.second == 0)
return MAX_ETA;
bool _unused;
qreal max_ratio = m_session->getMaxRatioPerTorrent(hash, &_unused);
if (max_ratio < 0)
return MAX_ETA;
libtorrent::size_type realDL = h.all_time_download();
if (realDL <= 0)
realDL = h.total_wanted();
return (realDL * max_ratio - h.all_time_upload()) / speed_average.second;
} else {
if (speed_average.first == 0)
return MAX_ETA;
return (h.total_wanted() - h.total_done()) / speed_average.first;
}
2010-12-18 15:34:38 +00:00
}
void TorrentSpeedMonitor::getSamples()
{
const std::vector<torrent_handle> torrents = m_session->getSession()->get_torrents();
2012-07-13 22:28:23 +00:00
std::vector<torrent_handle>::const_iterator it = torrents.begin();
std::vector<torrent_handle>::const_iterator itend = torrents.end();
for ( ; it != itend; ++it) {
2010-12-18 15:34:38 +00:00
try {
2011-04-17 10:29:44 +00:00
#if LIBTORRENT_VERSION_MINOR > 15
torrent_status st = it->status(0x0);
2012-02-20 17:30:53 +00:00
if (!st.paused)
m_samples[misc::toQString(it->info_hash())].addSample(st.download_payload_rate, st.upload_payload_rate);
2011-04-17 10:29:44 +00:00
#else
2012-02-20 17:30:53 +00:00
if (!it->is_paused())
m_samples[misc::toQString(it->info_hash())].addSample(it->status().download_payload_rate, it->status().upload_payload_rate);
2011-04-17 10:29:44 +00:00
#endif
2012-02-20 17:56:07 +00:00
} catch(invalid_handle&) {}
2010-12-18 15:34:38 +00:00
}
}