mirror of https://github.com/zlatinb/muwire
Add a new state for downloads rejected due to slot limit
parent
622c18730c
commit
b95a9b62b1
|
@ -0,0 +1,7 @@
|
||||||
|
package com.muwire.core.download
|
||||||
|
|
||||||
|
class DownloadRejectedException extends Exception {
|
||||||
|
DownloadRejectedException() {
|
||||||
|
super()
|
||||||
|
}
|
||||||
|
}
|
|
@ -121,7 +121,7 @@ class DownloadSession {
|
||||||
* @return true if the response was consumed, false if it cannot be satisfied.
|
* @return true if the response was consumed, false if it cannot be satisfied.
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
public boolean consumeResponse() throws IOException {
|
public boolean consumeResponse() throws IOException, DownloadRejectedException {
|
||||||
OutputStream os = endpoint.getOutputStream()
|
OutputStream os = endpoint.getOutputStream()
|
||||||
InputStream is = endpoint.getInputStream()
|
InputStream is = endpoint.getInputStream()
|
||||||
|
|
||||||
|
@ -138,8 +138,14 @@ class DownloadSession {
|
||||||
endpoint.close()
|
endpoint.close()
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (code == 429) {
|
||||||
|
log.warning("rejected")
|
||||||
|
endpoint.close()
|
||||||
|
throw new DownloadRejectedException()
|
||||||
|
}
|
||||||
|
|
||||||
if (!(code == 200 || code == 416)) {
|
if (!(code == 200 || code == 416 )) {
|
||||||
log.warning("unknown code $code")
|
log.warning("unknown code $code")
|
||||||
endpoint.close()
|
endpoint.close()
|
||||||
return false
|
return false
|
||||||
|
|
|
@ -15,7 +15,7 @@ import net.i2p.data.Destination
|
||||||
@Log
|
@Log
|
||||||
abstract class Downloader {
|
abstract class Downloader {
|
||||||
|
|
||||||
enum DownloadState { COPYING, CONNECTING, HASHLIST, DOWNLOADING, FAILED, HOPELESS, CANCELLED, PAUSED, FINISHED }
|
enum DownloadState { COPYING, CONNECTING, HASHLIST, DOWNLOADING, REJECTED, FAILED, HOPELESS, CANCELLED, PAUSED, FINISHED }
|
||||||
|
|
||||||
protected static final ExecutorService executorService = Executors.newCachedThreadPool({r ->
|
protected static final ExecutorService executorService = Executors.newCachedThreadPool({r ->
|
||||||
Thread rv = new Thread(r)
|
Thread rv = new Thread(r)
|
||||||
|
|
|
@ -27,7 +27,7 @@ class HashListSession {
|
||||||
this.endpoint = endpoint
|
this.endpoint = endpoint
|
||||||
}
|
}
|
||||||
|
|
||||||
InfoHash request() throws IOException {
|
InfoHash request() throws IOException, DownloadRejectedException {
|
||||||
InputStream is = endpoint.getInputStream()
|
InputStream is = endpoint.getInputStream()
|
||||||
OutputStream os = endpoint.getOutputStream()
|
OutputStream os = endpoint.getOutputStream()
|
||||||
|
|
||||||
|
@ -37,6 +37,8 @@ class HashListSession {
|
||||||
os.flush()
|
os.flush()
|
||||||
|
|
||||||
String code = readTillRN(is)
|
String code = readTillRN(is)
|
||||||
|
if (code.startsWith("429"))
|
||||||
|
throw new DownloadRejectedException()
|
||||||
if (!code.startsWith("200"))
|
if (!code.startsWith("200"))
|
||||||
throw new IOException("unknown code $code")
|
throw new IOException("unknown code $code")
|
||||||
|
|
||||||
|
|
|
@ -143,12 +143,16 @@ class NetworkDownloader extends Downloader {
|
||||||
|
|
||||||
protected DownloadState getSpecificState() {
|
protected DownloadState getSpecificState() {
|
||||||
boolean allFinished = true
|
boolean allFinished = true
|
||||||
|
boolean allRejected = true
|
||||||
activeWorkers.values().each {
|
activeWorkers.values().each {
|
||||||
allFinished &= it.currentState == WorkerState.FINISHED
|
allFinished &= it.currentState == WorkerState.FINISHED
|
||||||
|
allRejected &= it.rejected
|
||||||
}
|
}
|
||||||
if (allFinished) {
|
if (allFinished) {
|
||||||
if (pieces.isComplete())
|
if (pieces.isComplete())
|
||||||
return DownloadState.FINISHED
|
return DownloadState.FINISHED
|
||||||
|
if (allRejected)
|
||||||
|
return DownloadState.REJECTED
|
||||||
if (!hasLiveSources())
|
if (!hasLiveSources())
|
||||||
return DownloadState.HOPELESS
|
return DownloadState.HOPELESS
|
||||||
return DownloadState.FAILED
|
return DownloadState.FAILED
|
||||||
|
@ -335,7 +339,7 @@ class NetworkDownloader extends Downloader {
|
||||||
private final Destination destination
|
private final Destination destination
|
||||||
private volatile WorkerState currentState = WorkerState.NEW
|
private volatile WorkerState currentState = WorkerState.NEW
|
||||||
private volatile Thread downloadThread
|
private volatile Thread downloadThread
|
||||||
private volatile boolean cancelled
|
private volatile boolean cancelled, rejected
|
||||||
private final LinkedList<DownloadSession> sessionQueue = new LinkedList<>()
|
private final LinkedList<DownloadSession> sessionQueue = new LinkedList<>()
|
||||||
private final Set<Integer> available = new HashSet<>()
|
private final Set<Integer> available = new HashSet<>()
|
||||||
|
|
||||||
|
@ -414,6 +418,8 @@ class NetworkDownloader extends Downloader {
|
||||||
endpoint, browse, feed, chat, message)
|
endpoint, browse, feed, chat, message)
|
||||||
headSession.performRequest()
|
headSession.performRequest()
|
||||||
}
|
}
|
||||||
|
} catch (DownloadRejectedException rejected) {
|
||||||
|
this.rejected = true
|
||||||
} catch (Exception bad) {
|
} catch (Exception bad) {
|
||||||
if (!cancelled) {
|
if (!cancelled) {
|
||||||
log.log(Level.WARNING, "Exception while downloading", DataUtil.findRoot(bad))
|
log.log(Level.WARNING, "Exception while downloading", DataUtil.findRoot(bad))
|
||||||
|
|
|
@ -583,6 +583,7 @@ COPY_TO_CLIPBOARD=Copy To Clipboard
|
||||||
COPYING=Copying
|
COPYING=Copying
|
||||||
CONNECTING=Connecting
|
CONNECTING=Connecting
|
||||||
HASHLIST=Hash List
|
HASHLIST=Hash List
|
||||||
|
REJECTED=Rejected
|
||||||
FAILED=Failed
|
FAILED=Failed
|
||||||
HOPELESS=Hopeless
|
HOPELESS=Hopeless
|
||||||
CANCELLED=Cancelled
|
CANCELLED=Cancelled
|
||||||
|
|
|
@ -348,7 +348,8 @@ class MainFrameModel {
|
||||||
downloads.each {
|
downloads.each {
|
||||||
def state = it.downloader.currentState
|
def state = it.downloader.currentState
|
||||||
if (state == Downloader.DownloadState.FAILED ||
|
if (state == Downloader.DownloadState.FAILED ||
|
||||||
state == Downloader.DownloadState.DOWNLOADING)
|
state == Downloader.DownloadState.DOWNLOADING ||
|
||||||
|
state == Downloader.DownloadState.REJECTED)
|
||||||
it.downloader.resume()
|
it.downloader.resume()
|
||||||
}
|
}
|
||||||
updateTablePreservingSelection("downloads-table")
|
updateTablePreservingSelection("downloads-table")
|
||||||
|
|
|
@ -874,6 +874,7 @@ class MainFrameView {
|
||||||
model.retryButtonEnabled = false
|
model.retryButtonEnabled = false
|
||||||
break
|
break
|
||||||
case Downloader.DownloadState.FAILED:
|
case Downloader.DownloadState.FAILED:
|
||||||
|
case Downloader.DownloadState.REJECTED:
|
||||||
model.cancelButtonEnabled = true
|
model.cancelButtonEnabled = true
|
||||||
model.retryButtonEnabled = true
|
model.retryButtonEnabled = true
|
||||||
model.resumeButtonText = "RETRY"
|
model.resumeButtonText = "RETRY"
|
||||||
|
@ -911,6 +912,8 @@ class MainFrameView {
|
||||||
downloaders.each { allPaused &= it.getCurrentState() == Downloader.DownloadState.PAUSED}
|
downloaders.each { allPaused &= it.getCurrentState() == Downloader.DownloadState.PAUSED}
|
||||||
boolean allFailed = true
|
boolean allFailed = true
|
||||||
downloaders.each { allFailed &= it.getCurrentState() == Downloader.DownloadState.FAILED}
|
downloaders.each { allFailed &= it.getCurrentState() == Downloader.DownloadState.FAILED}
|
||||||
|
boolean allRejected = true
|
||||||
|
downloaders.each { allRejected &= it.getCurrentState() == Downloader.DownloadState.REJECTED}
|
||||||
boolean allFinished = true
|
boolean allFinished = true
|
||||||
downloaders.each { allFinished &= it.getCurrentState() == Downloader.DownloadState.FINISHED}
|
downloaders.each { allFinished &= it.getCurrentState() == Downloader.DownloadState.FINISHED}
|
||||||
|
|
||||||
|
@ -919,7 +922,7 @@ class MainFrameView {
|
||||||
model.retryButtonEnabled = true
|
model.retryButtonEnabled = true
|
||||||
model.resumeButtonText = "RESUME"
|
model.resumeButtonText = "RESUME"
|
||||||
}
|
}
|
||||||
if (allFailed) {
|
if (allFailed || allRejected) {
|
||||||
model.retryButtonEnabled = true
|
model.retryButtonEnabled = true
|
||||||
model.pauseButtonEnabled = false
|
model.pauseButtonEnabled = false
|
||||||
model.resumeButtonText = "RETRY"
|
model.resumeButtonText = "RETRY"
|
||||||
|
|
Loading…
Reference in New Issue