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)
|
eventBus.register(ResultsEvent.class, searchManager)
|
||||||
|
|
||||||
log.info("initializing download manager")
|
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)
|
eventBus.register(UIDownloadEvent.class, downloadManager)
|
||||||
|
|
||||||
log.info("initializing upload manager")
|
log.info("initializing upload manager")
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
package com.muwire.core.download
|
package com.muwire.core.download
|
||||||
|
|
||||||
import com.muwire.core.connection.I2PConnector
|
import com.muwire.core.connection.I2PConnector
|
||||||
|
|
||||||
|
import net.i2p.data.Base64
|
||||||
|
|
||||||
import com.muwire.core.EventBus
|
import com.muwire.core.EventBus
|
||||||
|
import com.muwire.core.Persona
|
||||||
|
|
||||||
import java.util.concurrent.Executor
|
import java.util.concurrent.Executor
|
||||||
import java.util.concurrent.Executors
|
import java.util.concurrent.Executors
|
||||||
|
@ -12,12 +16,19 @@ public class DownloadManager {
|
||||||
private final I2PConnector connector
|
private final I2PConnector connector
|
||||||
private final Executor executor
|
private final Executor executor
|
||||||
private final File incompletes
|
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.eventBus = eventBus
|
||||||
this.connector = connector
|
this.connector = connector
|
||||||
this.incompletes = incompletes
|
this.incompletes = incompletes
|
||||||
|
|
||||||
|
def baos = new ByteArrayOutputStream()
|
||||||
|
me.write(baos)
|
||||||
|
this.meB64 = Base64.encode(baos.toByteArray())
|
||||||
|
|
||||||
incompletes.mkdir()
|
incompletes.mkdir()
|
||||||
|
|
||||||
this.executor = Executors.newCachedThreadPool({ r ->
|
this.executor = Executors.newCachedThreadPool({ r ->
|
||||||
Thread rv = new Thread(r)
|
Thread rv = new Thread(r)
|
||||||
rv.setName("download-worker")
|
rv.setName("download-worker")
|
||||||
|
@ -28,7 +39,7 @@ public class DownloadManager {
|
||||||
|
|
||||||
|
|
||||||
public void onUIDownloadEvent(UIDownloadEvent e) {
|
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,
|
e.result.infohash, e.result.pieceSize, connector, e.result.sender.destination,
|
||||||
incompletes)
|
incompletes)
|
||||||
executor.execute({downloader.download()} as Runnable)
|
executor.execute({downloader.download()} as Runnable)
|
||||||
|
|
|
@ -20,6 +20,7 @@ import java.security.NoSuchAlgorithmException
|
||||||
@Log
|
@Log
|
||||||
class DownloadSession {
|
class DownloadSession {
|
||||||
|
|
||||||
|
private final String meB64
|
||||||
private final Pieces pieces
|
private final Pieces pieces
|
||||||
private final InfoHash infoHash
|
private final InfoHash infoHash
|
||||||
private final Endpoint endpoint
|
private final Endpoint endpoint
|
||||||
|
@ -30,8 +31,9 @@ class DownloadSession {
|
||||||
|
|
||||||
private ByteBuffer mapped
|
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) {
|
int pieceSize, long fileLength) {
|
||||||
|
this.meB64 = meB64
|
||||||
this.pieces = pieces
|
this.pieces = pieces
|
||||||
this.endpoint = endpoint
|
this.endpoint = endpoint
|
||||||
this.infoHash = infoHash
|
this.infoHash = infoHash
|
||||||
|
@ -60,7 +62,8 @@ class DownloadSession {
|
||||||
FileChannel channel
|
FileChannel channel
|
||||||
try {
|
try {
|
||||||
os.write("GET $root\r\n".getBytes(StandardCharsets.US_ASCII))
|
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()
|
os.flush()
|
||||||
String code = readTillRN(is)
|
String code = readTillRN(is)
|
||||||
if (code.startsWith("404 ")) {
|
if (code.startsWith("404 ")) {
|
||||||
|
|
|
@ -15,7 +15,8 @@ import net.i2p.data.Destination
|
||||||
public class Downloader {
|
public class Downloader {
|
||||||
public enum DownloadState { CONNECTING, DOWNLOADING, FAILED, CANCELLED, FINISHED }
|
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 File file
|
||||||
private final Pieces pieces
|
private final Pieces pieces
|
||||||
private final long length
|
private final long length
|
||||||
|
@ -32,9 +33,10 @@ public class Downloader {
|
||||||
private volatile boolean cancelled
|
private volatile boolean cancelled
|
||||||
private volatile Thread downloadThread
|
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,
|
int pieceSizePow2, I2PConnector connector, Destination destination,
|
||||||
File incompletes) {
|
File incompletes) {
|
||||||
|
this.meB64 = meB64
|
||||||
this.downloadManager = downloadManager
|
this.downloadManager = downloadManager
|
||||||
this.file = file
|
this.file = file
|
||||||
this.infoHash = infoHash
|
this.infoHash = infoHash
|
||||||
|
@ -63,7 +65,7 @@ public class Downloader {
|
||||||
endpoint = connector.connect(destination)
|
endpoint = connector.connect(destination)
|
||||||
currentState = DownloadState.DOWNLOADING
|
currentState = DownloadState.DOWNLOADING
|
||||||
while(!pieces.isComplete()) {
|
while(!pieces.isComplete()) {
|
||||||
currentSession = new DownloadSession(pieces, infoHash, endpoint, file, pieceSize, length)
|
currentSession = new DownloadSession(meB64, pieces, infoHash, endpoint, file, pieceSize, length)
|
||||||
currentSession.request()
|
currentSession.request()
|
||||||
writePieces()
|
writePieces()
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import java.nio.charset.StandardCharsets
|
||||||
|
|
||||||
import com.muwire.core.Constants
|
import com.muwire.core.Constants
|
||||||
import com.muwire.core.InfoHash
|
import com.muwire.core.InfoHash
|
||||||
|
import com.muwire.core.Persona
|
||||||
|
|
||||||
import groovy.util.logging.Log
|
import groovy.util.logging.Log
|
||||||
import net.i2p.data.Base64
|
import net.i2p.data.Base64
|
||||||
|
@ -16,6 +17,7 @@ class Request {
|
||||||
|
|
||||||
InfoHash infoHash
|
InfoHash infoHash
|
||||||
Range range
|
Range range
|
||||||
|
Persona downloader
|
||||||
Map<String, String> headers
|
Map<String, String> headers
|
||||||
|
|
||||||
static Request parse(InfoHash infoHash, InputStream is) throws IOException {
|
static Request parse(InfoHash infoHash, InputStream is) throws IOException {
|
||||||
|
@ -85,7 +87,13 @@ class Request {
|
||||||
if (start < 0 || end < start)
|
if (start < 0 || end < start)
|
||||||
throw new IOException("Invalid range $start - $end")
|
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)
|
int percent = (int)((position * 100.0) / total)
|
||||||
"$percent%"
|
"$percent%"
|
||||||
})
|
})
|
||||||
|
closureColumn(header : "Downloader", type : String, read : { row ->
|
||||||
|
row.request.downloader?.getHumanReadableName()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue