mirror of https://github.com/zlatinb/muwire
show who is downloading
parent
656b62fc2e
commit
f41cc39659
|
@ -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")
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 ")) {
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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<String, String> 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)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue