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 [] hash = digest.digest()
|
||||||
byte [] expected = new byte[32]
|
byte [] expected = new byte[32]
|
||||||
System.arraycopy(infoHash.getHashList(), piece * 32, expected, 0, 32)
|
System.arraycopy(infoHash.getHashList(), piece * 32, expected, 0, 32)
|
||||||
if (hash != expected) {
|
if (hash != expected)
|
||||||
log.warning("hash mismatch")
|
throw new BadHashException()
|
||||||
endpoint.close()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
pieces.markDownloaded(piece)
|
pieces.markDownloaded(piece)
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
@ -2,13 +2,18 @@ package com.muwire.core.download
|
||||||
|
|
||||||
import com.muwire.core.InfoHash
|
import com.muwire.core.InfoHash
|
||||||
import com.muwire.core.connection.Endpoint
|
import com.muwire.core.connection.Endpoint
|
||||||
|
|
||||||
|
import java.util.logging.Level
|
||||||
|
|
||||||
import com.muwire.core.Constants
|
import com.muwire.core.Constants
|
||||||
import com.muwire.core.connection.I2PConnector
|
import com.muwire.core.connection.I2PConnector
|
||||||
|
|
||||||
|
import groovy.util.logging.Log
|
||||||
import net.i2p.data.Destination
|
import net.i2p.data.Destination
|
||||||
|
|
||||||
|
@Log
|
||||||
public class Downloader {
|
public class Downloader {
|
||||||
public enum DownloadState { CONNECTING, DOWNLOADING, FINISHED }
|
public enum DownloadState { CONNECTING, DOWNLOADING, FAILED, FINISHED }
|
||||||
|
|
||||||
private final File file
|
private final File file
|
||||||
private final Pieces pieces
|
private final Pieces pieces
|
||||||
|
@ -43,14 +48,22 @@ public class Downloader {
|
||||||
}
|
}
|
||||||
|
|
||||||
void download() {
|
void download() {
|
||||||
Endpoint endpoint = connector.connect(destination)
|
Endpoint endpoint = null
|
||||||
currentState = DownloadState.DOWNLOADING
|
try {
|
||||||
while(!pieces.isComplete()) {
|
endpoint = connector.connect(destination)
|
||||||
currentSession = new DownloadSession(pieces, infoHash, endpoint, file, pieceSize, length)
|
currentState = DownloadState.DOWNLOADING
|
||||||
currentSession.request()
|
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() {
|
public long donePieces() {
|
||||||
|
|
Loading…
Reference in New Issue