1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-10 14:57:52 +00:00

Code clean up

This commit is contained in:
Christophe Dumez 2011-01-11 19:05:24 +00:00
parent f9f3642116
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 "torrentspeedmonitor.h"
#include <QList>
#include <vector>
#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<int> 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();

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
#define TORRENTSPEEDMONITOR_H
#include <QString>
#include <QThread>
#include <QWaitCondition>
#include <QList>
#include <QHash>
#include <QMutex>
#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<int> 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<QString, SpeedSample> m_samples;
mutable QMutex mutex;
mutable QMutex m_mutex;
QBtSession *m_session;
};