From 16c51e7cd638d7e6e26b643fc9a4c9fc9c8318b7 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sat, 1 Jun 2019 14:14:20 +0100 Subject: [PATCH] add a failed download state --- .../core/download/BadHashException.groovy | 25 ++++++++++++++++ .../core/download/DownloadSession.groovy | 7 ++--- .../muwire/core/download/Downloader.groovy | 29 ++++++++++++++----- 3 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 core/src/main/groovy/com/muwire/core/download/BadHashException.groovy diff --git a/core/src/main/groovy/com/muwire/core/download/BadHashException.groovy b/core/src/main/groovy/com/muwire/core/download/BadHashException.groovy new file mode 100644 index 00000000..c10d21ea --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/download/BadHashException.groovy @@ -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); + } + +} diff --git a/core/src/main/groovy/com/muwire/core/download/DownloadSession.groovy b/core/src/main/groovy/com/muwire/core/download/DownloadSession.groovy index d27799e9..1db3073a 100644 --- a/core/src/main/groovy/com/muwire/core/download/DownloadSession.groovy +++ b/core/src/main/groovy/com/muwire/core/download/DownloadSession.groovy @@ -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 { diff --git a/core/src/main/groovy/com/muwire/core/download/Downloader.groovy b/core/src/main/groovy/com/muwire/core/download/Downloader.groovy index 199dbb15..c8bffc2a 100644 --- a/core/src/main/groovy/com/muwire/core/download/Downloader.groovy +++ b/core/src/main/groovy/com/muwire/core/download/Downloader.groovy @@ -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() {