From c2a23f2265a85fbc36c73bdcca76ed89941a3513 Mon Sep 17 00:00:00 2001 From: Ivan Sorokin Date: Sun, 25 May 2014 00:30:30 +0400 Subject: [PATCH] use stats_alert in TorrentSpeedMonitor Conflicts: src/qtlibtorrent/qbtsession.cpp --- src/qtlibtorrent/qbtsession.cpp | 6 ++-- src/qtlibtorrent/qbtsession.h | 2 ++ src/qtlibtorrent/torrentspeedmonitor.cpp | 43 +++++------------------- src/qtlibtorrent/torrentspeedmonitor.h | 17 +++------- 4 files changed, 18 insertions(+), 50 deletions(-) diff --git a/src/qtlibtorrent/qbtsession.cpp b/src/qtlibtorrent/qbtsession.cpp index 3d5ebef22..29d736c82 100755 --- a/src/qtlibtorrent/qbtsession.cpp +++ b/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())); // 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(a)) { emit stateUpdate(p->status); } + else if (stats_alert *p = dynamic_cast(a)) { + emit statsReceived(*p); + } } catch (const std::exception& e) { qWarning() << "Caught exception in readAlerts(): " << e.what(); } diff --git a/src/qtlibtorrent/qbtsession.h b/src/qtlibtorrent/qbtsession.h index fff6849b2..46012b9b8 100755 --- a/src/qtlibtorrent/qbtsession.h +++ b/src/qtlibtorrent/qbtsession.h @@ -47,6 +47,7 @@ #include #include #include +#include #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 &statuses); + void statsReceived(const libtorrent::stats_alert&); private: // Bittorrent diff --git a/src/qtlibtorrent/torrentspeedmonitor.cpp b/src/qtlibtorrent/torrentspeedmonitor.cpp index a4400445d..0d70b5502 100644 --- a/src/qtlibtorrent/torrentspeedmonitor.cpp +++ b/src/qtlibtorrent/torrentspeedmonitor.cpp @@ -28,7 +28,6 @@ * Contact : chris@qbittorrent.org */ -#include #include #include #include @@ -61,28 +60,16 @@ private: QList > 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 torrents = m_session->getSession()->get_torrents(); - - std::vector::const_iterator it = torrents.begin(); - std::vector::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); } diff --git a/src/qtlibtorrent/torrentspeedmonitor.h b/src/qtlibtorrent/torrentspeedmonitor.h index ab5ab3ac1..0a6dbbcc1 100644 --- a/src/qtlibtorrent/torrentspeedmonitor.h +++ b/src/qtlibtorrent/torrentspeedmonitor.h @@ -32,39 +32,30 @@ #define TORRENTSPEEDMONITOR_H #include -#include -#include #include -#include #include "qtorrenthandle.h" +#include 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 m_samples; - mutable QMutex m_mutex; QBtSession *m_session; };