diff --git a/core/src/main/groovy/com/muwire/core/Core.groovy b/core/src/main/groovy/com/muwire/core/Core.groovy index fcbd51a5..f61f4ef2 100644 --- a/core/src/main/groovy/com/muwire/core/Core.groovy +++ b/core/src/main/groovy/com/muwire/core/Core.groovy @@ -194,6 +194,7 @@ public class Core { DownloadManager downloadManager = new DownloadManager(eventBus, i2pConnector, new File(home, "incompletes"), me) eventBus.register(UIDownloadEvent.class, downloadManager) eventBus.register(UILoadedEvent.class, downloadManager) + eventBus.register(FileDownloadedEvent.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 89ee0cd2..3de8d17e 100644 --- a/core/src/main/groovy/com/muwire/core/download/DownloadManager.groovy +++ b/core/src/main/groovy/com/muwire/core/download/DownloadManager.groovy @@ -1,9 +1,11 @@ package com.muwire.core.download import com.muwire.core.connection.I2PConnector +import com.muwire.core.files.FileDownloadedEvent import net.i2p.data.Base64 import net.i2p.data.Destination +import net.i2p.util.ConcurrentHashSet import com.muwire.core.EventBus import com.muwire.core.Persona @@ -20,6 +22,8 @@ public class DownloadManager { private final File incompletes private final Persona me + private final Set downloaders = new ConcurrentHashSet<>() + public DownloadManager(EventBus eventBus, I2PConnector connector, File incompletes, Persona me) { this.eventBus = eventBus this.connector = connector @@ -51,6 +55,8 @@ public class DownloadManager { def downloader = new Downloader(eventBus, this, me, e.target, size, infohash, pieceSize, connector, destinations, incompletes) + downloaders.add(downloader) + persistDownloaders() executor.execute({downloader.download()} as Runnable) eventBus.publish(new DownloadStartedEvent(downloader : downloader)) } @@ -62,4 +68,12 @@ public class DownloadManager { void onUILoadedEvent(UILoadedEvent e) { // TODO: load downloads here } + + void onFileDownloadedEvent(FileDownloadedEvent e) { + downloaders.remove(e.downloader) + persistDownloaders() + } + private void 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 27dd123b..dcfc9283 100644 --- a/core/src/main/groovy/com/muwire/core/download/Downloader.groovy +++ b/core/src/main/groovy/com/muwire/core/download/Downloader.groovy @@ -205,7 +205,8 @@ public class Downloader { if (downloaded.isComplete() && !eventFired) { piecesFile.delete() eventFired = true - eventBus.publish(new FileDownloadedEvent(downloadedFile : new DownloadedFile(file, infoHash, pieceSize, Collections.emptySet()))) + eventBus.publish(new FileDownloadedEvent(downloadedFile : new DownloadedFile(file, infoHash, pieceSize, Collections.emptySet())), + downloader : Downloader.this) } endpoint?.close() } diff --git a/core/src/main/groovy/com/muwire/core/files/FileDownloadedEvent.groovy b/core/src/main/groovy/com/muwire/core/files/FileDownloadedEvent.groovy index 0af89617..fb0488fd 100644 --- a/core/src/main/groovy/com/muwire/core/files/FileDownloadedEvent.groovy +++ b/core/src/main/groovy/com/muwire/core/files/FileDownloadedEvent.groovy @@ -2,10 +2,11 @@ package com.muwire.core.files import com.muwire.core.DownloadedFile import com.muwire.core.Event +import com.muwire.core.download.Downloader import net.i2p.data.Destination class FileDownloadedEvent extends Event { - + Downloader downloader DownloadedFile downloadedFile }