diff --git a/src/qtlibtorrent/torrentspeedmonitor.cpp b/src/qtlibtorrent/torrentspeedmonitor.cpp index 4634c42ec..7546281c5 100644 --- a/src/qtlibtorrent/torrentspeedmonitor.cpp +++ b/src/qtlibtorrent/torrentspeedmonitor.cpp @@ -1,10 +1,58 @@ +/* + * 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 -#include "torrentspeedmonitor.h" +#include +#include + #include "qbtsession.h" #include "misc.h" +#include "torrentspeedmonitor.h" using namespace libtorrent; +class SpeedSample { + +public: + SpeedSample(){} + void addSample(int s); + float average() const; + void clear(); + +private: + static const int max_samples = 30; + +private: + QList m_speedSamples; +}; + TorrentSpeedMonitor::TorrentSpeedMonitor(QBtSession* session) : QThread(session), m_abort(false), m_session(session) { @@ -21,10 +69,10 @@ TorrentSpeedMonitor::~TorrentSpeedMonitor() { void TorrentSpeedMonitor::run() { do { - mutex.lock(); + m_mutex.lock(); getSamples(); - m_abortCond.wait(&mutex, 1000); - mutex.unlock(); + m_abortCond.wait(&m_mutex, 1000); + m_mutex.unlock(); } while(!m_abort); } @@ -63,7 +111,7 @@ void TorrentSpeedMonitor::removeSamples(const QTorrentHandle& h) { qlonglong TorrentSpeedMonitor::getETA(const QString &hash) const { - QMutexLocker locker(&mutex); + QMutexLocker locker(&m_mutex); QTorrentHandle h = m_session->getTorrentHandle(hash); if(h.is_paused() || !m_samples.contains(hash)) return -1; const float speed_average = m_samples.value(hash).average(); diff --git a/src/qtlibtorrent/torrentspeedmonitor.h b/src/qtlibtorrent/torrentspeedmonitor.h index 131de01b0..4a29ac4e4 100644 --- a/src/qtlibtorrent/torrentspeedmonitor.h +++ b/src/qtlibtorrent/torrentspeedmonitor.h @@ -1,43 +1,58 @@ +/* + * 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 + */ + #ifndef TORRENTSPEEDMONITOR_H #define TORRENTSPEEDMONITOR_H +#include #include #include -#include #include #include #include "qtorrenthandle.h" class QBtSession; - -class SpeedSample { -public: - SpeedSample(){} - void addSample(int s); - float average() const; - void clear(); - -private: - static const int max_samples = 30; - -private: - QList m_speedSamples; -}; +class SpeedSample; class TorrentSpeedMonitor : public QThread { - Q_OBJECT + Q_OBJECT public: - explicit TorrentSpeedMonitor(QBtSession* session); - ~TorrentSpeedMonitor(); - qlonglong getETA(const QString &hash) const; + explicit TorrentSpeedMonitor(QBtSession* session); + ~TorrentSpeedMonitor(); + qlonglong getETA(const QString &hash) const; protected: void run(); -signals: - private: void getSamples(); @@ -52,7 +67,7 @@ private: bool m_abort; QWaitCondition m_abortCond; QHash m_samples; - mutable QMutex mutex; + mutable QMutex m_mutex; QBtSession *m_session; };