Add a new state for downloads rejected due to slot limit

dbus-notify
Zlatin Balevsky 2022-03-08 16:25:41 +00:00
parent 622c18730c
commit b95a9b62b1
No known key found for this signature in database
GPG Key ID: A72832072D525E41
8 changed files with 33 additions and 7 deletions

View File

@ -0,0 +1,7 @@
package com.muwire.core.download
class DownloadRejectedException extends Exception {
DownloadRejectedException() {
super()
}
}

View File

@ -121,7 +121,7 @@ class DownloadSession {
* @return true if the response was consumed, false if it cannot be satisfied.
* @throws IOException
*/
public boolean consumeResponse() throws IOException {
public boolean consumeResponse() throws IOException, DownloadRejectedException {
OutputStream os = endpoint.getOutputStream()
InputStream is = endpoint.getInputStream()
@ -138,8 +138,14 @@ class DownloadSession {
endpoint.close()
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")
endpoint.close()
return false

View File

@ -15,7 +15,7 @@ import net.i2p.data.Destination
@Log
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 ->
Thread rv = new Thread(r)

View File

@ -27,7 +27,7 @@ class HashListSession {
this.endpoint = endpoint
}
InfoHash request() throws IOException {
InfoHash request() throws IOException, DownloadRejectedException {
InputStream is = endpoint.getInputStream()
OutputStream os = endpoint.getOutputStream()
@ -37,6 +37,8 @@ class HashListSession {
os.flush()
String code = readTillRN(is)
if (code.startsWith("429"))
throw new DownloadRejectedException()
if (!code.startsWith("200"))
throw new IOException("unknown code $code")

View File

@ -143,12 +143,16 @@ class NetworkDownloader extends Downloader {
protected DownloadState getSpecificState() {
boolean allFinished = true
boolean allRejected = true
activeWorkers.values().each {
allFinished &= it.currentState == WorkerState.FINISHED
allRejected &= it.rejected
}
if (allFinished) {
if (pieces.isComplete())
return DownloadState.FINISHED
if (allRejected)
return DownloadState.REJECTED
if (!hasLiveSources())
return DownloadState.HOPELESS
return DownloadState.FAILED
@ -335,7 +339,7 @@ class NetworkDownloader extends Downloader {
private final Destination destination
private volatile WorkerState currentState = WorkerState.NEW
private volatile Thread downloadThread
private volatile boolean cancelled
private volatile boolean cancelled, rejected
private final LinkedList<DownloadSession> sessionQueue = new LinkedList<>()
private final Set<Integer> available = new HashSet<>()
@ -414,6 +418,8 @@ class NetworkDownloader extends Downloader {
endpoint, browse, feed, chat, message)
headSession.performRequest()
}
} catch (DownloadRejectedException rejected) {
this.rejected = true
} catch (Exception bad) {
if (!cancelled) {
log.log(Level.WARNING, "Exception while downloading", DataUtil.findRoot(bad))

View File

@ -583,6 +583,7 @@ COPY_TO_CLIPBOARD=Copy To Clipboard
COPYING=Copying
CONNECTING=Connecting
HASHLIST=Hash List
REJECTED=Rejected
FAILED=Failed
HOPELESS=Hopeless
CANCELLED=Cancelled

View File

@ -348,7 +348,8 @@ class MainFrameModel {
downloads.each {
def state = it.downloader.currentState
if (state == Downloader.DownloadState.FAILED ||
state == Downloader.DownloadState.DOWNLOADING)
state == Downloader.DownloadState.DOWNLOADING ||
state == Downloader.DownloadState.REJECTED)
it.downloader.resume()
}
updateTablePreservingSelection("downloads-table")

View File

@ -874,6 +874,7 @@ class MainFrameView {
model.retryButtonEnabled = false
break
case Downloader.DownloadState.FAILED:
case Downloader.DownloadState.REJECTED:
model.cancelButtonEnabled = true
model.retryButtonEnabled = true
model.resumeButtonText = "RETRY"
@ -911,6 +912,8 @@ class MainFrameView {
downloaders.each { allPaused &= it.getCurrentState() == Downloader.DownloadState.PAUSED}
boolean allFailed = true
downloaders.each { allFailed &= it.getCurrentState() == Downloader.DownloadState.FAILED}
boolean allRejected = true
downloaders.each { allRejected &= it.getCurrentState() == Downloader.DownloadState.REJECTED}
boolean allFinished = true
downloaders.each { allFinished &= it.getCurrentState() == Downloader.DownloadState.FINISHED}
@ -919,7 +922,7 @@ class MainFrameView {
model.retryButtonEnabled = true
model.resumeButtonText = "RESUME"
}
if (allFailed) {
if (allFailed || allRejected) {
model.retryButtonEnabled = true
model.pauseButtonEnabled = false
model.resumeButtonText = "RETRY"