mirror of https://github.com/zlatinb/muwire
rudimentary speed gauge
parent
abbb741d73
commit
6e0f1778b7
|
@ -20,6 +20,8 @@ import java.security.NoSuchAlgorithmException
|
||||||
@Log
|
@Log
|
||||||
class DownloadSession {
|
class DownloadSession {
|
||||||
|
|
||||||
|
private static int SAMPLES = 10
|
||||||
|
|
||||||
private final String meB64
|
private final String meB64
|
||||||
private final Pieces pieces
|
private final Pieces pieces
|
||||||
private final InfoHash infoHash
|
private final InfoHash infoHash
|
||||||
|
@ -29,6 +31,9 @@ class DownloadSession {
|
||||||
private final long fileLength
|
private final long fileLength
|
||||||
private final MessageDigest digest
|
private final MessageDigest digest
|
||||||
|
|
||||||
|
private final ArrayDeque<Long> timestamps = new ArrayDeque<>(SAMPLES)
|
||||||
|
private final ArrayDeque<Integer> reads = new ArrayDeque<>(SAMPLES)
|
||||||
|
|
||||||
private ByteBuffer mapped
|
private ByteBuffer mapped
|
||||||
|
|
||||||
DownloadSession(String meB64, Pieces pieces, InfoHash infoHash, Endpoint endpoint, File file,
|
DownloadSession(String meB64, Pieces pieces, InfoHash infoHash, Endpoint endpoint, File file,
|
||||||
|
@ -122,6 +127,13 @@ class DownloadSession {
|
||||||
throw new IOException()
|
throw new IOException()
|
||||||
synchronized(this) {
|
synchronized(this) {
|
||||||
mapped.put(tmp, 0, read)
|
mapped.put(tmp, 0, read)
|
||||||
|
|
||||||
|
if (timestamps.size() == SAMPLES) {
|
||||||
|
timestamps.removeFirst()
|
||||||
|
reads.removeFirst()
|
||||||
|
}
|
||||||
|
timestamps.addLast(System.currentTimeMillis())
|
||||||
|
reads.addLast(read)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,4 +156,13 @@ class DownloadSession {
|
||||||
return 0
|
return 0
|
||||||
mapped.position()
|
mapped.position()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
synchronized int speed() {
|
||||||
|
if (timestamps.size() < SAMPLES)
|
||||||
|
return 0
|
||||||
|
long interval = timestamps.last - timestamps.first
|
||||||
|
int totalRead = 0
|
||||||
|
reads.each { totalRead += it }
|
||||||
|
(int)(totalRead * 1000.0 / interval)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,6 +109,12 @@ public class Downloader {
|
||||||
currentSession.positionInPiece()
|
currentSession.positionInPiece()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int speed() {
|
||||||
|
if (currentSession == null)
|
||||||
|
return 0
|
||||||
|
currentSession.speed()
|
||||||
|
}
|
||||||
|
|
||||||
public DownloadState getCurrentState() {
|
public DownloadState getCurrentState() {
|
||||||
currentState
|
currentState
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,6 +109,7 @@ class MainFrameView {
|
||||||
int pieceSize = row.downloader.pieceSize // TODO: fix for last piece
|
int pieceSize = row.downloader.pieceSize // TODO: fix for last piece
|
||||||
"$position/$pieceSize bytes"
|
"$position/$pieceSize bytes"
|
||||||
})
|
})
|
||||||
|
closureColumn(header: "Speed (bytes/second)", type:Integer, read :{row -> row.downloader.speed()})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue