diff --git a/webui/src/main/java/com/muwire/webui/DownloadManager.java b/webui/src/main/java/com/muwire/webui/DownloadManager.java index 0800b633..f3478285 100644 --- a/webui/src/main/java/com/muwire/webui/DownloadManager.java +++ b/webui/src/main/java/com/muwire/webui/DownloadManager.java @@ -2,6 +2,8 @@ package com.muwire.webui; import java.util.List; import java.util.Map; +import java.util.Timer; +import java.util.TimerTask; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; import java.util.stream.Stream; @@ -19,8 +21,16 @@ public class DownloadManager { private final Map downloaders = new ConcurrentHashMap<>(); + private final Timer retryTimer = new Timer("download-resumer", true); + private long lastRetryTime; + public DownloadManager(Core core) { this.core = core; + retryTimer.schedule(new ResumeTask(), 1000, 1000); + } + + void shutdown() { + retryTimer.cancel(); } public void onDownloadStartedEvent(DownloadStartedEvent e) { @@ -72,4 +82,28 @@ public class DownloadManager { core.getEventBus().publish(event); 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(); + } + } + } + } + + } } diff --git a/webui/src/main/java/com/muwire/webui/DownloadServlet.java b/webui/src/main/java/com/muwire/webui/DownloadServlet.java index 300908c8..5abdc0f3 100644 --- a/webui/src/main/java/com/muwire/webui/DownloadServlet.java +++ b/webui/src/main/java/com/muwire/webui/DownloadServlet.java @@ -40,6 +40,14 @@ public class DownloadServlet extends HttpServlet { + @Override + public void destroy() { + if (downloadManager != null) + downloadManager.shutdown(); + } + + + @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (downloadManager == null) {