add a failed download state

pull/4/head
Zlatin Balevsky 2019-06-01 14:14:20 +01:00
parent 9d75550b6f
commit 16c51e7cd6
3 changed files with 48 additions and 13 deletions

View File

@ -0,0 +1,25 @@
package com.muwire.core.download
class BadHashException extends Exception {
public BadHashException() {
super();
}
public BadHashException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public BadHashException(String message, Throwable cause) {
super(message, cause);
}
public BadHashException(String message) {
super(message);
}
public BadHashException(Throwable cause) {
super(cause);
}
}

View File

@ -127,11 +127,8 @@ class DownloadSession {
byte [] hash = digest.digest()
byte [] expected = new byte[32]
System.arraycopy(infoHash.getHashList(), piece * 32, expected, 0, 32)
if (hash != expected) {
log.warning("hash mismatch")
endpoint.close()
return
}
if (hash != expected)
throw new BadHashException()
pieces.markDownloaded(piece)
} finally {

View File

@ -2,13 +2,18 @@ package com.muwire.core.download
import com.muwire.core.InfoHash
import com.muwire.core.connection.Endpoint
import java.util.logging.Level
import com.muwire.core.Constants
import com.muwire.core.connection.I2PConnector
import groovy.util.logging.Log
import net.i2p.data.Destination
@Log
public class Downloader {
public enum DownloadState { CONNECTING, DOWNLOADING, FINISHED }
public enum DownloadState { CONNECTING, DOWNLOADING, FAILED, FINISHED }
private final File file
private final Pieces pieces
@ -43,14 +48,22 @@ public class Downloader {
}
void download() {
Endpoint endpoint = connector.connect(destination)
currentState = DownloadState.DOWNLOADING
while(!pieces.isComplete()) {
currentSession = new DownloadSession(pieces, infoHash, endpoint, file, pieceSize, length)
currentSession.request()
Endpoint endpoint = null
try {
endpoint = connector.connect(destination)
currentState = DownloadState.DOWNLOADING
while(!pieces.isComplete()) {
currentSession = new DownloadSession(pieces, infoHash, endpoint, file, pieceSize, length)
currentSession.request()
}
currentState = DownloadState.FINISHED
} catch (Exception bad) {
log.log(Level.WARNING,"Exception while downloading",bad)
if (currentState != DownloadState.FINISHED)
currentState = DownloadState.FAILED
} finally {
endpoint?.close()
}
currentState = DownloadState.FINISHED
endpoint.close()
}
public long donePieces() {