From 6e0f1778b7e87fed489077df76bd8a18356d9c23 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Mon, 3 Jun 2019 18:02:10 +0100 Subject: [PATCH] rudimentary speed gauge --- .../core/download/DownloadSession.groovy | 21 +++++++++++++++++++ .../muwire/core/download/Downloader.groovy | 6 ++++++ .../views/com/muwire/gui/MainFrameView.groovy | 1 + 3 files changed, 28 insertions(+) diff --git a/core/src/main/groovy/com/muwire/core/download/DownloadSession.groovy b/core/src/main/groovy/com/muwire/core/download/DownloadSession.groovy index 0d4aaef2..c9798a1f 100644 --- a/core/src/main/groovy/com/muwire/core/download/DownloadSession.groovy +++ b/core/src/main/groovy/com/muwire/core/download/DownloadSession.groovy @@ -20,6 +20,8 @@ import java.security.NoSuchAlgorithmException @Log class DownloadSession { + private static int SAMPLES = 10 + private final String meB64 private final Pieces pieces private final InfoHash infoHash @@ -29,6 +31,9 @@ class DownloadSession { private final long fileLength private final MessageDigest digest + private final ArrayDeque timestamps = new ArrayDeque<>(SAMPLES) + private final ArrayDeque reads = new ArrayDeque<>(SAMPLES) + private ByteBuffer mapped DownloadSession(String meB64, Pieces pieces, InfoHash infoHash, Endpoint endpoint, File file, @@ -122,6 +127,13 @@ class DownloadSession { throw new IOException() synchronized(this) { 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 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) + } } diff --git a/core/src/main/groovy/com/muwire/core/download/Downloader.groovy b/core/src/main/groovy/com/muwire/core/download/Downloader.groovy index 1f3278cc..398b9320 100644 --- a/core/src/main/groovy/com/muwire/core/download/Downloader.groovy +++ b/core/src/main/groovy/com/muwire/core/download/Downloader.groovy @@ -109,6 +109,12 @@ public class Downloader { currentSession.positionInPiece() } + public int speed() { + if (currentSession == null) + return 0 + currentSession.speed() + } + public DownloadState getCurrentState() { currentState } diff --git a/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy b/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy index e0ffc23a..45e12012 100644 --- a/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy @@ -109,6 +109,7 @@ class MainFrameView { int pieceSize = row.downloader.pieceSize // TODO: fix for last piece "$position/$pieceSize bytes" }) + closureColumn(header: "Speed (bytes/second)", type:Integer, read :{row -> row.downloader.speed()}) } } }