From f41cc39659a5cf234c268fa007614ce3020d6549 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sat, 1 Jun 2019 21:53:14 +0100 Subject: [PATCH] show who is downloading --- core/src/main/groovy/com/muwire/core/Core.groovy | 2 +- .../muwire/core/download/DownloadManager.groovy | 15 +++++++++++++-- .../muwire/core/download/DownloadSession.groovy | 7 +++++-- .../com/muwire/core/download/Downloader.groovy | 8 +++++--- .../groovy/com/muwire/core/upload/Request.groovy | 10 +++++++++- .../views/com/muwire/gui/MainFrameView.groovy | 3 +++ 6 files changed, 36 insertions(+), 9 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/Core.groovy b/core/src/main/groovy/com/muwire/core/Core.groovy index facdb6c5..e795da0e 100644 --- a/core/src/main/groovy/com/muwire/core/Core.groovy +++ b/core/src/main/groovy/com/muwire/core/Core.groovy @@ -166,7 +166,7 @@ public class Core { eventBus.register(ResultsEvent.class, searchManager) log.info("initializing download manager") - DownloadManager downloadManager = new DownloadManager(eventBus, i2pConnector, new File(home, "incompletes")) + DownloadManager downloadManager = new DownloadManager(eventBus, i2pConnector, new File(home, "incompletes"), me) eventBus.register(UIDownloadEvent.class, downloadManager) log.info("initializing upload manager") diff --git a/core/src/main/groovy/com/muwire/core/download/DownloadManager.groovy b/core/src/main/groovy/com/muwire/core/download/DownloadManager.groovy index 29c71af4..3080091d 100644 --- a/core/src/main/groovy/com/muwire/core/download/DownloadManager.groovy +++ b/core/src/main/groovy/com/muwire/core/download/DownloadManager.groovy @@ -1,7 +1,11 @@ package com.muwire.core.download import com.muwire.core.connection.I2PConnector + +import net.i2p.data.Base64 + import com.muwire.core.EventBus +import com.muwire.core.Persona import java.util.concurrent.Executor import java.util.concurrent.Executors @@ -12,12 +16,19 @@ public class DownloadManager { private final I2PConnector connector private final Executor executor private final File incompletes + private final String meB64 - public DownloadManager(EventBus eventBus, I2PConnector connector, File incompletes) { + public DownloadManager(EventBus eventBus, I2PConnector connector, File incompletes, Persona me) { this.eventBus = eventBus this.connector = connector this.incompletes = incompletes + + def baos = new ByteArrayOutputStream() + me.write(baos) + this.meB64 = Base64.encode(baos.toByteArray()) + incompletes.mkdir() + this.executor = Executors.newCachedThreadPool({ r -> Thread rv = new Thread(r) rv.setName("download-worker") @@ -28,7 +39,7 @@ public class DownloadManager { public void onUIDownloadEvent(UIDownloadEvent e) { - def downloader = new Downloader(this, e.target, e.result.size, + def downloader = new Downloader(this, meB64, e.target, e.result.size, e.result.infohash, e.result.pieceSize, connector, e.result.sender.destination, incompletes) executor.execute({downloader.download()} as Runnable) 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 1db3073a..0d4aaef2 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,7 @@ import java.security.NoSuchAlgorithmException @Log class DownloadSession { + private final String meB64 private final Pieces pieces private final InfoHash infoHash private final Endpoint endpoint @@ -30,8 +31,9 @@ class DownloadSession { private ByteBuffer mapped - DownloadSession(Pieces pieces, InfoHash infoHash, Endpoint endpoint, File file, + DownloadSession(String meB64, Pieces pieces, InfoHash infoHash, Endpoint endpoint, File file, int pieceSize, long fileLength) { + this.meB64 = meB64 this.pieces = pieces this.endpoint = endpoint this.infoHash = infoHash @@ -60,7 +62,8 @@ class DownloadSession { FileChannel channel try { os.write("GET $root\r\n".getBytes(StandardCharsets.US_ASCII)) - os.write("Range: $start-$end\r\n\r\n".getBytes(StandardCharsets.US_ASCII)) + os.write("Range: $start-$end\r\n".getBytes(StandardCharsets.US_ASCII)) + os.write("X-Persona: $meB64\r\n\r\n".getBytes(StandardCharsets.US_ASCII)) os.flush() String code = readTillRN(is) if (code.startsWith("404 ")) { 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 6f4ec0f6..809cfb70 100644 --- a/core/src/main/groovy/com/muwire/core/download/Downloader.groovy +++ b/core/src/main/groovy/com/muwire/core/download/Downloader.groovy @@ -15,7 +15,8 @@ import net.i2p.data.Destination public class Downloader { public enum DownloadState { CONNECTING, DOWNLOADING, FAILED, CANCELLED, FINISHED } - private final DownloadManager downloadManager + private final DownloadManager downloadManager + private final String meB64 private final File file private final Pieces pieces private final long length @@ -32,9 +33,10 @@ public class Downloader { private volatile boolean cancelled private volatile Thread downloadThread - public Downloader(DownloadManager downloadManager, File file, long length, InfoHash infoHash, + public Downloader(DownloadManager downloadManager, String meB64, File file, long length, InfoHash infoHash, int pieceSizePow2, I2PConnector connector, Destination destination, File incompletes) { + this.meB64 = meB64 this.downloadManager = downloadManager this.file = file this.infoHash = infoHash @@ -63,7 +65,7 @@ public class Downloader { endpoint = connector.connect(destination) currentState = DownloadState.DOWNLOADING while(!pieces.isComplete()) { - currentSession = new DownloadSession(pieces, infoHash, endpoint, file, pieceSize, length) + currentSession = new DownloadSession(meB64, pieces, infoHash, endpoint, file, pieceSize, length) currentSession.request() writePieces() } diff --git a/core/src/main/groovy/com/muwire/core/upload/Request.groovy b/core/src/main/groovy/com/muwire/core/upload/Request.groovy index d014c9d8..c9a87954 100644 --- a/core/src/main/groovy/com/muwire/core/upload/Request.groovy +++ b/core/src/main/groovy/com/muwire/core/upload/Request.groovy @@ -4,6 +4,7 @@ import java.nio.charset.StandardCharsets import com.muwire.core.Constants import com.muwire.core.InfoHash +import com.muwire.core.Persona import groovy.util.logging.Log import net.i2p.data.Base64 @@ -16,6 +17,7 @@ class Request { InfoHash infoHash Range range + Persona downloader Map headers static Request parse(InfoHash infoHash, InputStream is) throws IOException { @@ -85,7 +87,13 @@ class Request { if (start < 0 || end < start) throw new IOException("Invalid range $start - $end") - new Request( infoHash : infoHash, range : new Range(start, end), headers : headers) + Persona downloader = null + if (headers.containsKey("X-Persona")) { + def encoded = headers["X-Persona"].trim() + def decoded = Base64.decode(encoded) + downloader = new Persona(new ByteArrayInputStream(decoded)) + } + new Request( infoHash : infoHash, range : new Range(start, end), headers : headers, downloader : downloader) } } diff --git a/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy b/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy index 04dc1afa..28c8b2b2 100644 --- a/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/MainFrameView.groovy @@ -136,6 +136,9 @@ class MainFrameView { int percent = (int)((position * 100.0) / total) "$percent%" }) + closureColumn(header : "Downloader", type : String, read : { row -> + row.request.downloader?.getHumanReadableName() + }) } } }