mirror of https://github.com/zlatinb/muwire
automatic resume of failed downloads
parent
fba0b001c0
commit
dd230c4dfc
|
@ -2,6 +2,8 @@ package com.muwire.webui;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
@ -19,8 +21,16 @@ public class DownloadManager {
|
||||||
|
|
||||||
private final Map<InfoHash,Downloader> downloaders = new ConcurrentHashMap<>();
|
private final Map<InfoHash,Downloader> downloaders = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
private final Timer retryTimer = new Timer("download-resumer", true);
|
||||||
|
private long lastRetryTime;
|
||||||
|
|
||||||
public DownloadManager(Core core) {
|
public DownloadManager(Core core) {
|
||||||
this.core = core;
|
this.core = core;
|
||||||
|
retryTimer.schedule(new ResumeTask(), 1000, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
void shutdown() {
|
||||||
|
retryTimer.cancel();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onDownloadStartedEvent(DownloadStartedEvent e) {
|
public void onDownloadStartedEvent(DownloadStartedEvent e) {
|
||||||
|
@ -72,4 +82,28 @@ public class DownloadManager {
|
||||||
core.getEventBus().publish(event);
|
core.getEventBus().publish(event);
|
||||||
delay();
|
delay();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private class ResumeTask extends TimerTask {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if (core.getShutdown().get())
|
||||||
|
return;
|
||||||
|
int retryInterval = core.getMuOptions().getDownloadRetryInterval();
|
||||||
|
if (retryInterval > 0) {
|
||||||
|
retryInterval *= 1000;
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
|
if (now - lastRetryTime > retryInterval) {
|
||||||
|
lastRetryTime = now;
|
||||||
|
for (Downloader d : downloaders.values()) {
|
||||||
|
Downloader.DownloadState state = d.getCurrentState();
|
||||||
|
if (state == Downloader.DownloadState.FAILED ||
|
||||||
|
state == Downloader.DownloadState.DOWNLOADING)
|
||||||
|
d.resume();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,6 +40,14 @@ public class DownloadServlet extends HttpServlet {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destroy() {
|
||||||
|
if (downloadManager != null)
|
||||||
|
downloadManager.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
if (downloadManager == null) {
|
if (downloadManager == null) {
|
||||||
|
|
Loading…
Reference in New Issue