Browse Source

use stats_alert in TorrentSpeedMonitor

Conflicts:
	src/qtlibtorrent/qbtsession.cpp
adaptive-webui-19844
Ivan Sorokin 11 years ago
parent
commit
c2a23f2265
  1. 6
      src/qtlibtorrent/qbtsession.cpp
  2. 2
      src/qtlibtorrent/qbtsession.h
  3. 43
      src/qtlibtorrent/torrentspeedmonitor.cpp
  4. 17
      src/qtlibtorrent/torrentspeedmonitor.h

6
src/qtlibtorrent/qbtsession.cpp

@ -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())); 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 // 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 // Load previous state
loadSessionState(); loadSessionState();
// Enabling plugins // Enabling plugins
@ -158,7 +158,6 @@ QBtSession::QBtSession()
configureSession(); configureSession();
// Torrent speed monitor // Torrent speed monitor
m_speedMonitor = new TorrentSpeedMonitor(this); m_speedMonitor = new TorrentSpeedMonitor(this);
m_speedMonitor->start();
m_torrentStatistics = new TorrentStatistics(this, this); m_torrentStatistics = new TorrentStatistics(this, this);
// To download from urls // To download from urls
downloader = new DownloadThread(this); 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)) { else if (state_update_alert *p = dynamic_cast<state_update_alert *>(a)) {
emit stateUpdate(p->status); emit stateUpdate(p->status);
} }
else if (stats_alert *p = dynamic_cast<stats_alert *>(a)) {
emit statsReceived(*p);
}
} catch (const std::exception& e) { } catch (const std::exception& e) {
qWarning() << "Caught exception in readAlerts(): " << e.what(); qWarning() << "Caught exception in readAlerts(): " << e.what();
} }

2
src/qtlibtorrent/qbtsession.h

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

43
src/qtlibtorrent/torrentspeedmonitor.cpp

@ -28,7 +28,6 @@
* Contact : chris@qbittorrent.org * Contact : chris@qbittorrent.org
*/ */
#include <QMutexLocker>
#include <QList> #include <QList>
#include <QDateTime> #include <QDateTime>
#include <vector> #include <vector>
@ -61,28 +60,16 @@ private:
QList<Sample<int> > m_speedSamples; QList<Sample<int> > m_speedSamples;
}; };
TorrentSpeedMonitor::TorrentSpeedMonitor(QBtSession* session) : TorrentSpeedMonitor::TorrentSpeedMonitor(QBtSession* session)
QThread(session), m_abort(false), m_session(session) : m_session(session)
{ {
connect(m_session, SIGNAL(deletedTorrent(QString)), SLOT(removeSamples(QString))); connect(m_session, SIGNAL(deletedTorrent(QString)), SLOT(removeSamples(QString)));
connect(m_session, SIGNAL(pausedTorrent(QTorrentHandle)), SLOT(removeSamples(QTorrentHandle))); connect(m_session, SIGNAL(pausedTorrent(QTorrentHandle)), SLOT(removeSamples(QTorrentHandle)));
connect(m_session, SIGNAL(statsReceived(libtorrent::stats_alert)), SLOT(statsReceived(libtorrent::stats_alert)));
} }
TorrentSpeedMonitor::~TorrentSpeedMonitor() { 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);
}
void SpeedSample::addSample(int speedDL, int speedUL) 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 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)) if (QTorrentHandle::is_paused(status) || !m_samples.contains(hash))
return MAX_ETA; 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; 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(); 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);
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&) {}
}
} }

17
src/qtlibtorrent/torrentspeedmonitor.h

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

Loading…
Cancel
Save