diff --git a/core/src/main/groovy/com/muwire/core/download/DownloadManager.groovy b/core/src/main/groovy/com/muwire/core/download/DownloadManager.groovy index 748b0e71..abc4c712 100644 --- a/core/src/main/groovy/com/muwire/core/download/DownloadManager.groovy +++ b/core/src/main/groovy/com/muwire/core/download/DownloadManager.groovy @@ -31,11 +31,9 @@ import com.muwire.core.collections.FileCollection import com.muwire.core.collections.FileCollectionItem import com.muwire.core.collections.UIDownloadCollectionEvent -import java.util.concurrent.BlockingQueue import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.Executor import java.util.concurrent.Executors -import java.util.concurrent.LinkedBlockingQueue import java.util.logging.Level @Log @@ -54,10 +52,6 @@ public class DownloadManager { private final Map downloaders = new ConcurrentHashMap<>() - private final BlockingQueue pendingStart = new LinkedBlockingQueue<>() - private volatile boolean shutdown - private final Thread pendingProcessor - public DownloadManager(EventBus eventBus, TrustService trustService, MeshManager meshManager, MuWireSettings muSettings, I2PConnector connector, File home, Persona me, ChatServer chatServer, FileManager fileManager) { this.eventBus = eventBus @@ -76,31 +70,6 @@ public class DownloadManager { rv.setDaemon(true) rv }) - - pendingProcessor = new Thread({processDelayed()} as Runnable) - pendingProcessor.setName("download-delayer") - pendingProcessor.setDaemon(true) - pendingProcessor.start() - } - - private void processDelayed() { - while(!shutdown) { - DelayedStart ds = pendingStart.poll() - if (ds != null) { - Downloader downloader = ds.downloader - if (!ds.resume) { - if (!downloader.paused) - executor.execute({ downloader.download() } as Runnable) - eventBus.publish(new DownloadStartedEvent(downloader: downloader)) - } else - executor.execute({downloader.doResume() as Runnable}) - } - Thread.sleep(100) - } - } - - void queueForResume(Downloader downloader) { - pendingStart.offer(new DelayedStart(downloader, true)) } @@ -174,7 +143,8 @@ public class DownloadManager { } downloaders.put(infoHash, downloader) persistDownloaders() - pendingStart.offer(new DelayedStart(downloader, false)) + executor.execute({downloader.download()} as Runnable) + eventBus.publish(new DownloadStartedEvent(downloader: downloader)) return downloader } @@ -196,10 +166,6 @@ public class DownloadManager { persistDownloaders() } - void resume(Downloader downloader) { - executor.execute({downloader.download() as Runnable}) - } - void onUILoadedEvent(UILoadedEvent e) { File downloadsFile = new File(home, "downloads.json") if (!downloadsFile.exists()) @@ -263,8 +229,15 @@ public class DownloadManager { } - downloaders.put(infoHash, downloader) - pendingStart.offer(new DelayedStart(downloader, false)) + try { + if (!downloader.paused) + downloader.download() + downloaders.put(infoHash, downloader) + eventBus.publish(new DownloadStartedEvent(downloader : downloader)) + } catch (IllegalArgumentException bad) { + log.log(Level.WARNING,"cannot start downloader, skipping", bad) + return + } } } @@ -342,8 +315,6 @@ public class DownloadManager { } public void shutdown() { - shutdown = true - pendingProcessor.interrupt() downloaders.values().each { it.stop() } Downloader.executorService.shutdownNow() } @@ -357,13 +328,4 @@ public class DownloadManager { downloaders.values().each { total += it.speed() } total } - - private static class DelayedStart { - final Downloader downloader - final boolean resume - DelayedStart(Downloader downloader, boolean resume) { - this.downloader = downloader - this.resume = resume - } - } } 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 3f667afb..8a0a7cfe 100644 --- a/core/src/main/groovy/com/muwire/core/download/DownloadSession.groovy +++ b/core/src/main/groovy/com/muwire/core/download/DownloadSession.groovy @@ -225,7 +225,7 @@ class DownloadSession { System.arraycopy(infoHash.getHashList(), piece * 32, expected, 0, 32) if (hash != expected) { pieces.markPartial(piece, 0) - throw new BadHashException("bad hash on piece $piece for infoHash " + Base64.encode(infoHash.getRoot())) + throw new BadHashException("bad hash on piece $piece") } eventBus.publish(new SourceVerifiedEvent(infoHash : infoHash, source : endpoint.destination)) 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 9ee1c874..94505647 100644 --- a/core/src/main/groovy/com/muwire/core/download/Downloader.groovy +++ b/core/src/main/groovy/com/muwire/core/download/Downloader.groovy @@ -119,7 +119,7 @@ abstract class Downloader { public void resume() { paused = false - downloadManager.queueForResume(this) + doResume() } protected abstract void doResume();