From 05b9b374884d6efeb6439df9966518d43afece42 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Thu, 6 Jun 2019 09:10:09 +0100 Subject: [PATCH] emit an event when all files are loaded --- cli/src/main/groovy/com/muwire/cli/Cli.groovy | 16 +++++++++++++++- .../muwire/core/files/AllFilesLoadedEvent.groovy | 6 ++++++ .../muwire/core/files/PersisterService.groovy | 1 + 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 core/src/main/groovy/com/muwire/core/files/AllFilesLoadedEvent.groovy diff --git a/cli/src/main/groovy/com/muwire/cli/Cli.groovy b/cli/src/main/groovy/com/muwire/cli/Cli.groovy index 07a5b865..1af7e570 100644 --- a/cli/src/main/groovy/com/muwire/cli/Cli.groovy +++ b/cli/src/main/groovy/com/muwire/cli/Cli.groovy @@ -1,7 +1,10 @@ package com.muwire.cli +import java.util.concurrent.CountDownLatch + import com.muwire.core.Core import com.muwire.core.MuWireSettings +import com.muwire.core.files.AllFilesLoadedEvent import com.muwire.core.files.FileHashedEvent import com.muwire.core.files.FileSharedEvent @@ -31,9 +34,20 @@ class Cli { println "Failed to initialize core, exiting" System.exit(1) } - + + def latch = new CountDownLatch(1) + def fileLoader = new Object() { + public void onAllFilesLoadedEvent(AllFilesLoadedEvent e) { + latch.countDown() + } + } + core.eventBus.register(AllFilesLoadedEvent.class, fileLoader) core.startServices() + println "waiting for files to load" + latch.await() + + // now we begin println "MuWire is ready" diff --git a/core/src/main/groovy/com/muwire/core/files/AllFilesLoadedEvent.groovy b/core/src/main/groovy/com/muwire/core/files/AllFilesLoadedEvent.groovy new file mode 100644 index 00000000..df1a140f --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/files/AllFilesLoadedEvent.groovy @@ -0,0 +1,6 @@ +package com.muwire.core.files + +import com.muwire.core.Event + +class AllFilesLoadedEvent extends Event { +} diff --git a/core/src/main/groovy/com/muwire/core/files/PersisterService.groovy b/core/src/main/groovy/com/muwire/core/files/PersisterService.groovy index 7028d32b..86b01234 100644 --- a/core/src/main/groovy/com/muwire/core/files/PersisterService.groovy +++ b/core/src/main/groovy/com/muwire/core/files/PersisterService.groovy @@ -55,6 +55,7 @@ class PersisterService extends Service { } } } + listener.publish(new AllFilesLoadedEvent()) } catch (IllegalArgumentException|NumberFormatException e) { log.log(Level.WARNING, "couldn't load files",e) }