From 82706141cfd02d7cf6b1f3ff0739accea82a9ca1 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sat, 1 Nov 2008 22:07:14 +0000 Subject: [PATCH] Ratio calculation improvement: make usage of new all_time_upload and all_time_download variables in torrent_status. --- src/GUI.cpp | 2 +- src/bittorrent.cpp | 97 ++++++------------------------------------ src/bittorrent.h | 7 +-- src/qtorrenthandle.cpp | 10 +++++ src/qtorrenthandle.h | 2 + 5 files changed, 28 insertions(+), 90 deletions(-) diff --git a/src/GUI.cpp b/src/GUI.cpp index 6c4c835a6..9da24adc6 100644 --- a/src/GUI.cpp +++ b/src/GUI.cpp @@ -644,7 +644,7 @@ void GUI::closeEvent(QCloseEvent *e) { writeSettings(); // Do some BT related saving BTSession->saveDHTEntry(); - BTSession->saveFastResumeAndRatioData(); + BTSession->saveFastResumeData(); // Accept exit e->accept(); qApp->exit(); diff --git a/src/bittorrent.cpp b/src/bittorrent.cpp index efb6530f1..83a65de66 100644 --- a/src/bittorrent.cpp +++ b/src/bittorrent.cpp @@ -620,8 +620,6 @@ void bittorrent::deleteTorrent(QString hash, bool permanent) { } // Remove tracker errors trackersErrors.remove(hash); - // Remove it from ratio table - ratioData.remove(hash); int index = finishedTorrents.indexOf(hash); if(index != -1) { finishedTorrents.removeAt(index); @@ -742,7 +740,7 @@ void bittorrent::setFinishedTorrent(QString hash) { } } // Save fast resume data - saveFastResumeAndRatioData(hash); + saveFastResumeData(hash); //emit torrentSwitchedtoFinished(hash); } @@ -754,7 +752,7 @@ bool bittorrent::pauseTorrent(QString hash) { h.pause(); change = true; // Save fast resume data - saveFastResumeAndRatioData(hash); + saveFastResumeData(hash); if(queueingEnabled) { updateDownloadQueue(); updateUploadQueue(); @@ -988,8 +986,6 @@ void bittorrent::addTorrent(QString path, bool fromScanDir, QString from_url, bo loadWebSeeds(hash); // Load speed limit from hard drive loadTorrentSpeedLimits(hash); - // Load ratio data - loadDownloadUploadForTorrent(hash); // Load trackers bool loaded_trackers = loadTrackerFile(hash); // Doing this to order trackers well @@ -1265,35 +1261,6 @@ void bittorrent::loadFilesPriorities(QTorrentHandle &h) { h.prioritize_files(v); } -void bittorrent::loadDownloadUploadForTorrent(QString hash) { - QDir torrentBackup(misc::qBittorrentPath() + "BT_backup"); - // Checking if torrentBackup Dir exists - // create it if it is not - if(! torrentBackup.exists()) { - torrentBackup.mkpath(torrentBackup.path()); - } -// qDebug("Loading ratio data for %s", hash.toUtf8().data()); - QFile ratio_file(torrentBackup.path()+QDir::separator()+ hash + ".ratio"); - if(!ratio_file.exists() || !ratio_file.open(QIODevice::ReadOnly | QIODevice::Text)) { - return; - } - QByteArray data = ratio_file.readAll(); - QList data_list = data.split(' '); - if(data_list.size() != 2) { - std::cerr << "Corrupted ratio file for torrent: " << hash.toStdString() << '\n'; - return; - } - QPair downUp; - downUp.first = (size_type)data_list.at(0).toLongLong(); - downUp.second = (size_type)data_list.at(1).toLongLong(); - if(downUp.first < 0 || downUp.second < 0) { - qDebug("** Overflow in ratio!!! fixing..."); - downUp.first = 0; - downUp.second = 0; - } - ratioData[hash] = downUp; -} - float bittorrent::getUncheckedTorrentProgress(QString hash) const { QFile paused_file(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".paused"); paused_file.open(QIODevice::ReadOnly | QIODevice::Text); @@ -1314,59 +1281,24 @@ float bittorrent::getUncheckedTorrentProgress(QString hash) const { } float bittorrent::getRealRatio(QString hash) const{ - QPair downUpInfo = ratioData.value(hash, QPair(0,0)); - size_type download = downUpInfo.first; - size_type upload = downUpInfo.second; QTorrentHandle h = getTorrentHandle(hash); - download += h.total_payload_download(); - Q_ASSERT(download >= 0); - upload += h.total_payload_upload(); - Q_ASSERT(upload >= 0); - if(download == 0){ - if(upload == 0) - return 1.; - return 10.; - } - float ratio = (double)upload / (double)download; + Q_ASSERT(h.all_time_download() >= 0); + Q_ASSERT(h.all_time_upload() >= 0); + if(h.all_time_download() == 0) { + if(h.all_time_upload() == 0) + return 1.; + return 10.; + } + float ratio = (float)h.all_time_upload()/(float)h.all_time_download(); Q_ASSERT(ratio >= 0.); if(ratio > 10.) ratio = 10.; return ratio; } -// To remember share ratio or a torrent, we must save current -// total_upload and total_upload and reload them on startup -void bittorrent::saveDownloadUploadForTorrent(QString hash) { - qDebug("Saving ratio data for torrent %s", hash.toUtf8().data()); - QDir torrentBackup(misc::qBittorrentPath() + QString::fromUtf8("BT_backup")); - // Checking if torrentBackup Dir exists - // create it if it is not - if(! torrentBackup.exists()) { - torrentBackup.mkpath(torrentBackup.path()); - } - QTorrentHandle h = getTorrentHandle(hash); - if(!h.is_valid()) { - qDebug("/!\\ Error: Invalid handle"); - return; - } - QPair ratioInfo = ratioData.value(hash, QPair(0,0)); - size_type download = h.total_payload_download(); - download += ratioInfo.first; - size_type upload = h.total_payload_upload(); - upload += ratioInfo.second; - Q_ASSERT(download >= 0 && upload >= 0); - QFile ratio_file(torrentBackup.path()+QDir::separator()+ hash + QString::fromUtf8(".ratio")); - if(!ratio_file.open(QIODevice::WriteOnly | QIODevice::Text)) { - std::cerr << "Couldn't save ratio data for torrent: " << hash.toStdString() << '\n'; - return; - } - ratio_file.write(misc::toQByteArray(download) + QByteArray(" ") + misc::toQByteArray(upload)); - ratio_file.close(); -} - // Only save fast resume data for unfinished and unpaused torrents (Optimization) // Called periodically and on exit -void bittorrent::saveFastResumeAndRatioData() { +void bittorrent::saveFastResumeData() { QString hash; QStringList hashes = getUnfinishedTorrents(); foreach(hash, hashes) { @@ -1379,7 +1311,7 @@ void bittorrent::saveFastResumeAndRatioData() { // Do not need to save fast resume data for paused torrents continue; } - saveFastResumeAndRatioData(hash); + saveFastResumeData(hash); } hashes = getFinishedTorrents(); foreach(hash, hashes) { @@ -1392,7 +1324,6 @@ void bittorrent::saveFastResumeAndRatioData() { // Do not need to save ratio data for paused torrents continue; } - saveDownloadUploadForTorrent(hash); } } @@ -1421,7 +1352,7 @@ void bittorrent::addPeerBanMessage(QString ip, bool from_ipfilter) { peerBanMessages.append(QString::fromUtf8("")+ QTime::currentTime().toString(QString::fromUtf8("hh:mm:ss")) + QString::fromUtf8(" - ")+tr("%1 was banned due to corrupt pieces", "x.y.z.w was banned").arg(ip)); } -void bittorrent::saveFastResumeAndRatioData(QString hash) { +void bittorrent::saveFastResumeData(QString hash) { QString file; QDir torrentBackup(misc::qBittorrentPath() + "BT_backup"); // Checking if torrentBackup Dir exists @@ -1442,8 +1373,6 @@ void bittorrent::saveFastResumeAndRatioData(QString hash) { // Write fast resume data h.save_resume_data(); } - // Save ratio data - saveDownloadUploadForTorrent(hash); } } diff --git a/src/bittorrent.h b/src/bittorrent.h index d34fdf1f1..558dd8c21 100644 --- a/src/bittorrent.h +++ b/src/bittorrent.h @@ -56,7 +56,6 @@ class bittorrent : public QObject { QString defaultSavePath; QHash TorrentsStartTime; QHash TorrentsStartData; - QHash > ratioData; QHash > trackersErrors; QStringList consoleMessages; QStringList peerBanMessages; @@ -133,8 +132,8 @@ class bittorrent : public QObject { void resumeAllTorrents(); void saveDHTEntry(); void preAllocateAllFiles(bool b); - void saveFastResumeAndRatioData(); - void saveFastResumeAndRatioData(QString hash); + void saveFastResumeData(); + void saveFastResumeData(QString hash); void enableDirectoryScanning(QString scan_dir); void disableDirectoryScanning(); void enablePeerExchange(); @@ -144,8 +143,6 @@ class bittorrent : public QObject { void resumeUnfinishedTorrents(); void saveTorrentSpeedLimits(QString hash); void loadTorrentSpeedLimits(QString hash); - void saveDownloadUploadForTorrent(QString hash); - void loadDownloadUploadForTorrent(QString hash); void handleDownloadFailure(QString url, QString reason); void loadWebSeeds(QString fileHash); void updateDownloadQueue(); diff --git a/src/qtorrenthandle.cpp b/src/qtorrenthandle.cpp index 5ca06e335..2be8cb768 100644 --- a/src/qtorrenthandle.cpp +++ b/src/qtorrenthandle.cpp @@ -232,6 +232,16 @@ void QTorrentHandle::file_progress(std::vector& fp) { return h.file_progress(fp); } +size_type QTorrentHandle::all_time_download() { + Q_ASSERT(h.is_valid()); + return h.status().all_time_download; +} + +size_type QTorrentHandle::all_time_upload() { + Q_ASSERT(h.is_valid()); + return h.status().all_time_upload; +} + size_type QTorrentHandle::total_payload_download() { Q_ASSERT(h.is_valid()); return h.status().total_payload_download; diff --git a/src/qtorrenthandle.h b/src/qtorrenthandle.h index f46ea4cc1..fdfae791e 100644 --- a/src/qtorrenthandle.h +++ b/src/qtorrenthandle.h @@ -87,6 +87,8 @@ class QTorrentHandle { void file_progress(std::vector& fp); size_type total_payload_download(); size_type total_payload_upload(); + size_type all_time_upload(); + size_type all_time_download(); QStringList files_path() const; int num_uploads() const; bool is_seed() const;