From 9bde00b7de4cacd1afb9188aa0ccf67f9b86ea57 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Fri, 29 Jun 2007 16:49:45 +0000 Subject: [PATCH] Improved ETA calculation --- src/bittorrent.cpp | 30 +++++++++++++----------------- src/bittorrent.h | 7 +++---- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/bittorrent.cpp b/src/bittorrent.cpp index 4c0ea64f6..753ffeee1 100644 --- a/src/bittorrent.cpp +++ b/src/bittorrent.cpp @@ -21,14 +21,13 @@ #include #include -#include #include #include "bittorrent.h" #include "misc.h" #include "downloadThread.h" -#define ETAS_MAX_VALUES 5 +#define ETAS_MAX_VALUES 8 // Main constructor bittorrent::bittorrent(){ @@ -45,9 +44,10 @@ bittorrent::bittorrent(){ DHTEnabled = false; // Enabling metadata plugin s->add_extension(&create_metadata_plugin); - timerAlerts = new QTimer(this); - connect(timerAlerts, SIGNAL(timeout()), this, SLOT(readAlerts())); - timerAlerts->start(3000); + connect(&timerAlerts, SIGNAL(timeout()), this, SLOT(readAlerts())); + timerAlerts.start(3000); + connect(&ETARefresher, SIGNAL(timeout()), this, SLOT(updateETAs())); + ETARefresher.start(6000); // To download from urls downloader = new downloadThread(this); connect(downloader, SIGNAL(downloadFinished(const QString&, const QString&, int, const QString&)), this, SLOT(processDownloadedFile(const QString&, const QString&, int, const QString&))); @@ -56,7 +56,6 @@ bittorrent::bittorrent(){ // Main destructor bittorrent::~bittorrent(){ disableDirectoryScanning(); - delete timerAlerts; delete downloader; delete s; } @@ -74,20 +73,19 @@ void bittorrent::updateETAs(){ QString hash = QString(misc::toString(h.info_hash()).c_str()); QList listEtas = ETAstats.value(hash, QList()); if(listEtas.size() == ETAS_MAX_VALUES){ + listEtas.removeFirst(); + } + torrent_status torrentStatus = h.status(); + torrent_info ti = h.get_torrent_info(); + if(torrentStatus.download_payload_rate != 0){ + listEtas << (long)((ti.total_size()-torrentStatus.total_done)/(double)torrentStatus.download_payload_rate); + ETAstats[hash] = listEtas; long moy = 0; long val; foreach(val, listEtas){ moy += val; } - ETAs[hash] = (long) ((double)moy/(double)ETAS_MAX_VALUES); - ETAstats[hash] = QList(); - }else{ - torrent_status torrentStatus = h.status(); - torrent_info ti = h.get_torrent_info(); - if(torrentStatus.download_payload_rate != 0){ - listEtas << (long)((ti.total_size()-torrentStatus.total_done)/(double)torrentStatus.download_payload_rate); - ETAstats[hash] = listEtas; - } + ETAs[hash] = (long) ((double)moy/(double)listEtas.size()); } } } @@ -710,8 +708,6 @@ void bittorrent::readAlerts(){ } a = s->pop_alert(); } - // ETAs - updateETAs(); } void bittorrent::reloadTorrent(const torrent_handle &h, bool compact_mode){ diff --git a/src/bittorrent.h b/src/bittorrent.h index b5b4bc595..4b0e1134e 100644 --- a/src/bittorrent.h +++ b/src/bittorrent.h @@ -22,6 +22,7 @@ #define __BITTORRENT_H__ #include +#include #include #include @@ -40,9 +41,6 @@ #include "deleteThread.h" -class QTimer; -class QString; - using namespace libtorrent; class downloadThread; @@ -55,7 +53,7 @@ class bittorrent : public QObject{ bool DHTEnabled; QString scan_dir; QTimer *timerScan; - QTimer *timerAlerts; + QTimer timerAlerts; downloadThread *downloader; QStringList supported_preview_extensions; QString defaultSavePath; @@ -63,6 +61,7 @@ class bittorrent : public QObject{ QStringList torrentsUnchecked; QHash > ETAstats; QHash ETAs; + QTimer ETARefresher; protected: QString getSavePath(const QString& hash);