Code clean up

This commit is contained in:
Christophe Dumez 2011-01-11 19:05:24 +00:00
commit f306d02ac9
2 changed files with 90 additions and 27 deletions

View file

@ -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 <QMutexLocker> #include <QMutexLocker>
#include "torrentspeedmonitor.h" #include <QList>
#include <vector>
#include "qbtsession.h" #include "qbtsession.h"
#include "misc.h" #include "misc.h"
#include "torrentspeedmonitor.h"
using namespace libtorrent; 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<int> m_speedSamples;
};
TorrentSpeedMonitor::TorrentSpeedMonitor(QBtSession* session) : TorrentSpeedMonitor::TorrentSpeedMonitor(QBtSession* session) :
QThread(session), m_abort(false), m_session(session) QThread(session), m_abort(false), m_session(session)
{ {
@ -21,10 +69,10 @@ TorrentSpeedMonitor::~TorrentSpeedMonitor() {
void TorrentSpeedMonitor::run() void TorrentSpeedMonitor::run()
{ {
do { do {
mutex.lock(); m_mutex.lock();
getSamples(); getSamples();
m_abortCond.wait(&mutex, 1000); m_abortCond.wait(&m_mutex, 1000);
mutex.unlock(); m_mutex.unlock();
} while(!m_abort); } while(!m_abort);
} }
@ -63,7 +111,7 @@ void TorrentSpeedMonitor::removeSamples(const QTorrentHandle& h) {
qlonglong TorrentSpeedMonitor::getETA(const QString &hash) const qlonglong TorrentSpeedMonitor::getETA(const QString &hash) const
{ {
QMutexLocker locker(&mutex); QMutexLocker locker(&m_mutex);
QTorrentHandle h = m_session->getTorrentHandle(hash); QTorrentHandle h = m_session->getTorrentHandle(hash);
if(h.is_paused() || !m_samples.contains(hash)) return -1; if(h.is_paused() || !m_samples.contains(hash)) return -1;
const float speed_average = m_samples.value(hash).average(); const float speed_average = m_samples.value(hash).average();

View file

@ -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 #ifndef TORRENTSPEEDMONITOR_H
#define TORRENTSPEEDMONITOR_H #define TORRENTSPEEDMONITOR_H
#include <QString>
#include <QThread> #include <QThread>
#include <QWaitCondition> #include <QWaitCondition>
#include <QList>
#include <QHash> #include <QHash>
#include <QMutex> #include <QMutex>
#include "qtorrenthandle.h" #include "qtorrenthandle.h"
class QBtSession; class QBtSession;
class SpeedSample;
class SpeedSample {
public:
SpeedSample(){}
void addSample(int s);
float average() const;
void clear();
private:
static const int max_samples = 30;
private:
QList<int> m_speedSamples;
};
class TorrentSpeedMonitor : public QThread class TorrentSpeedMonitor : public QThread
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit TorrentSpeedMonitor(QBtSession* session); explicit TorrentSpeedMonitor(QBtSession* session);
~TorrentSpeedMonitor(); ~TorrentSpeedMonitor();
qlonglong getETA(const QString &hash) const; qlonglong getETA(const QString &hash) const;
protected: protected:
void run(); void run();
signals:
private: private:
void getSamples(); void getSamples();
@ -52,7 +67,7 @@ private:
bool m_abort; bool m_abort;
QWaitCondition m_abortCond; QWaitCondition m_abortCond;
QHash<QString, SpeedSample> m_samples; QHash<QString, SpeedSample> m_samples;
mutable QMutex mutex; mutable QMutex m_mutex;
QBtSession *m_session; QBtSession *m_session;
}; };