use stats_alert in TorrentSpeedMonitor

Conflicts:
	src/qtlibtorrent/qbtsession.cpp
This commit is contained in:
Ivan Sorokin 2014-05-25 00:30:30 +04:00
parent 6f38616193
commit c2a23f2265
4 changed files with 18 additions and 50 deletions

View file

@ -134,7 +134,7 @@ QBtSession::QBtSession()
addConsoleMessage("Peer ID: "+misc::toQString(fingerprint(peer_id.toLocal8Bit().constData(), version.at(0), version.at(1), version.at(2), version.at(3)).to_string()));
// Set severity level of libtorrent session
s->set_alert_mask(alert::error_notification | alert::peer_notification | alert::port_mapping_notification | alert::storage_notification | alert::tracker_notification | alert::status_notification | alert::ip_block_notification | alert::progress_notification);
s->set_alert_mask(alert::error_notification | alert::peer_notification | alert::port_mapping_notification | alert::storage_notification | alert::tracker_notification | alert::status_notification | alert::ip_block_notification | alert::progress_notification | alert::stats_notification);
// Load previous state
loadSessionState();
// Enabling plugins
@ -158,7 +158,6 @@ QBtSession::QBtSession()
configureSession();
// Torrent speed monitor
m_speedMonitor = new TorrentSpeedMonitor(this);
m_speedMonitor->start();
m_torrentStatistics = new TorrentStatistics(this, this);
// To download from urls
downloader = new DownloadThread(this);
@ -2574,6 +2573,9 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
else if (state_update_alert *p = dynamic_cast<state_update_alert *>(a)) {
emit stateUpdate(p->status);
}
else if (stats_alert *p = dynamic_cast<stats_alert *>(a)) {
emit statsReceived(*p);
}
} catch (const std::exception& e) {
qWarning() << "Caught exception in readAlerts(): " << e.what();
}

View file

@ -47,6 +47,7 @@
#include <libtorrent/version.hpp>
#include <libtorrent/session.hpp>
#include <libtorrent/ip_filter.hpp>
#include <libtorrent/alert_types.hpp>
#include "qtracker.h"
#include "qtorrenthandle.h"
@ -235,6 +236,7 @@ signals:
void ipFilterParsed(bool error, int ruleCount);
void metadataReceivedHidden(const QTorrentHandle &h);
void stateUpdate(const std::vector<libtorrent::torrent_status> &statuses);
void statsReceived(const libtorrent::stats_alert&);
private:
// Bittorrent

View file

@ -28,7 +28,6 @@
* Contact : chris@qbittorrent.org
*/
#include <QMutexLocker>
#include <QList>
#include <QDateTime>
#include <vector>
@ -61,28 +60,16 @@ private:
QList<Sample<int> > m_speedSamples;
};
TorrentSpeedMonitor::TorrentSpeedMonitor(QBtSession* session) :
QThread(session), m_abort(false), m_session(session)
TorrentSpeedMonitor::TorrentSpeedMonitor(QBtSession* session)
: m_session(session)
{
connect(m_session, SIGNAL(deletedTorrent(QString)), SLOT(removeSamples(QString)));
connect(m_session, SIGNAL(pausedTorrent(QTorrentHandle)), SLOT(removeSamples(QTorrentHandle)));
connect(m_session, SIGNAL(statsReceived(libtorrent::stats_alert)), SLOT(statsReceived(libtorrent::stats_alert)));
}
TorrentSpeedMonitor::~TorrentSpeedMonitor() {
m_abort = true;
m_abortCond.wakeOne();
wait();
}
void TorrentSpeedMonitor::run()
{
do {
m_mutex.lock();
getSamples();
m_abortCond.wait(&m_mutex, 1000);
m_mutex.unlock();
} while(!m_abort);
}
TorrentSpeedMonitor::~TorrentSpeedMonitor()
{}
void SpeedSample::addSample(int speedDL, int speedUL)
{
@ -126,8 +113,6 @@ void TorrentSpeedMonitor::removeSamples(const QTorrentHandle& h) {
qlonglong TorrentSpeedMonitor::getETA(const QString &hash, const libtorrent::torrent_status &status) const
{
QMutexLocker locker(&m_mutex);
if (QTorrentHandle::is_paused(status) || !m_samples.contains(hash))
return MAX_ETA;
@ -155,20 +140,8 @@ qlonglong TorrentSpeedMonitor::getETA(const QString &hash, const libtorrent::tor
return (status.total_wanted - status.total_wanted_done) / speed_average.download;
}
void TorrentSpeedMonitor::getSamples()
void TorrentSpeedMonitor::statsReceived(const stats_alert &stats)
{
const std::vector<torrent_handle> torrents = m_session->getSession()->get_torrents();
std::vector<torrent_handle>::const_iterator it = torrents.begin();
std::vector<torrent_handle>::const_iterator itend = torrents.end();
for ( ; it != itend; ++it) {
try {
torrent_status st = it->status(0x0);
if (!st.paused) {
int up = st.upload_payload_rate;
int down = st.download_payload_rate;
m_samples[misc::toQString(it->info_hash())].addSample(down, up);
}
} catch(invalid_handle&) {}
}
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);
}

View file

@ -32,39 +32,30 @@
#define TORRENTSPEEDMONITOR_H
#include <QString>
#include <QThread>
#include <QWaitCondition>
#include <QHash>
#include <QMutex>
#include "qtorrenthandle.h"
#include <libtorrent/alert_types.hpp>
class QBtSession;
class SpeedSample;
class TorrentSpeedMonitor : public QThread
class TorrentSpeedMonitor : public QObject
{
Q_OBJECT
Q_DISABLE_COPY(TorrentSpeedMonitor)
public:
explicit TorrentSpeedMonitor(QBtSession* session);
~TorrentSpeedMonitor();
qlonglong getETA(const QString &hash, const libtorrent::torrent_status &status) const;
protected:
void run();
private:
void getSamples();
private slots:
void statsReceived(const libtorrent::stats_alert& stats);
void removeSamples(const QString& hash);
void removeSamples(const QTorrentHandle& h);
private:
bool m_abort;
QWaitCondition m_abortCond;
QHash<QString, SpeedSample> m_samples;
mutable QMutex m_mutex;
QBtSession *m_session;
};