From 0f07562de3150099106c2404eec36de3cf5f1a3b Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Fri, 21 Jun 2019 12:39:16 +0100 Subject: [PATCH] pass new sources to active downloaders --- .../main/groovy/com/muwire/core/Core.groovy | 4 ++- .../core/download/DownloadManager.groovy | 25 ++++++++++++++++++- .../muwire/core/download/Downloader.groovy | 8 ++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/Core.groovy b/core/src/main/groovy/com/muwire/core/Core.groovy index 0f3787b7..17b7eda0 100644 --- a/core/src/main/groovy/com/muwire/core/Core.groovy +++ b/core/src/main/groovy/com/muwire/core/Core.groovy @@ -13,6 +13,7 @@ import com.muwire.core.connection.I2PConnector import com.muwire.core.connection.LeafConnectionManager import com.muwire.core.connection.UltrapeerConnectionManager import com.muwire.core.download.DownloadManager +import com.muwire.core.download.SourceDiscoveredEvent import com.muwire.core.download.UIDownloadCancelledEvent import com.muwire.core.download.UIDownloadEvent import com.muwire.core.files.FileDownloadedEvent @@ -203,11 +204,12 @@ public class Core { eventBus.register(ResultsEvent.class, searchManager) log.info("initializing download manager") - downloadManager = new DownloadManager(eventBus, i2pConnector, home, me) + downloadManager = new DownloadManager(eventBus, trustService, props, i2pConnector, home, me) eventBus.register(UIDownloadEvent.class, downloadManager) eventBus.register(UILoadedEvent.class, downloadManager) eventBus.register(FileDownloadedEvent.class, downloadManager) eventBus.register(UIDownloadCancelledEvent.class, downloadManager) + eventBus.register(SourceDiscoveredEvent.class, downloadManager) log.info("initializing upload manager") UploadManager uploadManager = new UploadManager(eventBus, fileManager) 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 8687eed1..d782aa27 100644 --- a/core/src/main/groovy/com/muwire/core/download/DownloadManager.groovy +++ b/core/src/main/groovy/com/muwire/core/download/DownloadManager.groovy @@ -3,6 +3,8 @@ package com.muwire.core.download import com.muwire.core.connection.I2PConnector import com.muwire.core.files.FileDownloadedEvent import com.muwire.core.files.FileHasher +import com.muwire.core.trust.TrustLevel +import com.muwire.core.trust.TrustService import com.muwire.core.util.DataUtil import groovy.json.JsonBuilder @@ -14,6 +16,7 @@ import net.i2p.util.ConcurrentHashSet import com.muwire.core.EventBus import com.muwire.core.InfoHash +import com.muwire.core.MuWireSettings import com.muwire.core.Persona import com.muwire.core.UILoadedEvent @@ -24,6 +27,8 @@ import java.util.concurrent.Executors public class DownloadManager { private final EventBus eventBus + private final TrustService trustService + private final MuWireSettings muSettings private final I2PConnector connector private final Executor executor private final File incompletes, home @@ -31,8 +36,11 @@ public class DownloadManager { private final Map downloaders = new ConcurrentHashMap<>() - public DownloadManager(EventBus eventBus, I2PConnector connector, File home, Persona me) { + public DownloadManager(EventBus eventBus, TrustService trustService, MuWireSettings muSettings, + I2PConnector connector, File home, Persona me) { this.eventBus = eventBus + this.trustService = trustService + this.muSettings = muSettings this.connector = connector this.incompletes = new File(home,"incompletes") this.home = home @@ -108,6 +116,21 @@ public class DownloadManager { } } + void onSourceDiscoveredEvent(SourceDiscoveredEvent e) { + Downloader downloader = downloaders.get(e.infoHash) + if (downloader == null) + return + boolean ok = false + switch(trustService.getLevel(e.source.destination)) { + case TrustLevel.TRUSTED: ok = true; break + case TrustLevel.NEUTRAL: ok = muSettings.allowUntrusted; break + case TrustLevel.DISTRUSTED: ok = false; break + } + + if (ok) + downloader.addSource(e.source.destination) + } + void onFileDownloadedEvent(FileDownloadedEvent e) { downloaders.remove(e.downloader.infoHash) persistDownloaders() 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 e2c2d2d2..b802c710 100644 --- a/core/src/main/groovy/com/muwire/core/download/Downloader.groovy +++ b/core/src/main/groovy/com/muwire/core/download/Downloader.groovy @@ -221,6 +221,14 @@ public class Downloader { } } + void addSource(Destination d) { + if (activeWorkers.containsKey(d)) + return + DownloadWorker newWorker = new DownloadWorker(d) + activeWorkers.put(d, newWorker) + executorService.submit(newWorker) + } + class DownloadWorker implements Runnable { private final Destination destination private volatile WorkerState currentState