use the new bandwidth tracker for downloads as well

pull/53/head
Zlatin Balevsky 2020-10-07 08:17:12 +01:00
parent 7cbf466b9b
commit ef34291124
No known key found for this signature in database
GPG Key ID: A72832072D525E41
1 changed files with 7 additions and 23 deletions

View File

@ -23,6 +23,7 @@ import com.muwire.core.EventBus
import com.muwire.core.connection.I2PConnector import com.muwire.core.connection.I2PConnector
import com.muwire.core.files.FileDownloadedEvent import com.muwire.core.files.FileDownloadedEvent
import com.muwire.core.util.DataUtil import com.muwire.core.util.DataUtil
import com.muwire.core.util.BandwidthCounter
import groovy.util.logging.Log import groovy.util.logging.Log
import net.i2p.data.Destination import net.i2p.data.Destination
@ -71,10 +72,8 @@ public class Downloader {
private final AtomicBoolean hopelessEventFired = new AtomicBoolean() private final AtomicBoolean hopelessEventFired = new AtomicBoolean()
private boolean piecesFileClosed private boolean piecesFileClosed
private final AtomicLong dataSinceLastRead = new AtomicLong(0) private final AtomicLong dataSinceLastRead = new AtomicLong()
private volatile long lastSpeedRead = System.currentTimeMillis() private volatile BandwidthCounter bwCounter = new BandwidthCounter(0)
private ArrayList speedArr = new ArrayList<Integer>()
private int speedPos = 0
public Downloader(EventBus eventBus, DownloadManager downloadManager, ChatServer chatServer, public Downloader(EventBus eventBus, DownloadManager downloadManager, ChatServer chatServer,
Persona me, File file, long length, InfoHash infoHash, Persona me, File file, long length, InfoHash infoHash,
@ -176,28 +175,13 @@ public class Downloader {
int currSpeed = 0 int currSpeed = 0
if (getCurrentState() != DownloadState.DOWNLOADING) if (getCurrentState() != DownloadState.DOWNLOADING)
return currSpeed return currSpeed
long now = System.currentTimeMillis()
if (now < lastSpeedRead + 50)
return speedArr.average()
long dataRead = dataSinceLastRead.getAndSet(0)
currSpeed = (int) (dataRead * 1000.0d / (now - lastSpeedRead))
lastSpeedRead = now
// this is not very accurate since each slot may hold more than a second // this is not very accurate since each slot may hold more than a second
if (speedArr.size() != downloadManager.muSettings.speedSmoothSeconds) { if (bwCounter.getMemory() != downloadManager.muSettings.speedSmoothSeconds)
speedArr.clear() bwCounter = new BandwidthCounter(downloadManager.muSettings.speedSmoothSeconds)
downloadManager.muSettings.speedSmoothSeconds.times { speedArr.add(0) }
speedPos = 0
}
speedArr[speedPos++] = currSpeed
// rolling index over the speedArr
if (speedPos >= speedArr.size())
speedPos=0
speedArr.average()
bwCounter.read((int)dataSinceLastRead.getAndSet(0))
bwCounter.average()
} }
public DownloadState getCurrentState() { public DownloadState getCurrentState() {