mirror of https://github.com/zlatinb/muwire
add a failed download state
parent
9d75550b6f
commit
16c51e7cd6
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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 {
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue