/* */ #include "TorrentDownloadEngine.h" #include "Util.h" TorrentDownloadEngine::TorrentDownloadEngine():filenameFixed(false), torrentMan(NULL) {} TorrentDownloadEngine::~TorrentDownloadEngine() { if(torrentMan != NULL) { delete torrentMan; } } void TorrentDownloadEngine::onEndOfRun() { torrentMan->diskAdaptor->closeFile(); if(torrentMan->downloadComplete()) { torrentMan->remove(); } else { torrentMan->save(); } } void TorrentDownloadEngine::initStatistics() { downloadSpeed = 0; uploadSpeed = 0; cp.reset(); lastCalcStat.reset(); startup.reset(); eta = 0; avgSpeed = 0; downloadLength = 0; totalLength = 0; if(torrentMan->isSelectiveDownloadingMode()) { selectedDownloadLengthDiff = torrentMan->getDownloadLength()-torrentMan->getCompletedLength(); selectedTotalLength = torrentMan->getSelectedTotalLength(); } } int TorrentDownloadEngine::calculateSpeed(long long int length, int elapsed) { int nowSpeed = (int)(length/elapsed); return nowSpeed; } void TorrentDownloadEngine::calculateStat() { TransferStat stat = torrentMan->calculateStat(); downloadSpeed = stat.downloadSpeed; uploadSpeed = stat.uploadSpeed; avgSpeed = calculateSpeed(stat.sessionDownloadLength, startup.difference()); if(avgSpeed < 0) { avgSpeed = 0; } else if(avgSpeed != 0) { eta = (totalLength-downloadLength)/avgSpeed; } } void TorrentDownloadEngine::calculateStatistics() { if(torrentMan->isSelectiveDownloadingMode()) { downloadLength = torrentMan->getDownloadLength()-selectedDownloadLengthDiff; totalLength = selectedTotalLength; } else { downloadLength = torrentMan->getDownloadLength(); totalLength = torrentMan->getTotalLength(); } Time now; if(now.getTimeInMillis()-lastCalcStat.getTimeInMillis() >= 1000) { calculateStat(); lastCalcStat = now; } if(cp.difference() >= 1) { sendStatistics(); cp.reset(); } }