From 07b8724e8f4988f3b0cc71a1b2a1864bdf85dbc5 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Mon, 23 Jul 2018 16:09:43 +0100 Subject: [PATCH] add waitForLoad() method to loadable services --- core/src/main/groovy/com/muwire/core/Service.groovy | 13 +++++++++++++ .../com/muwire/core/files/PersisterService.groovy | 6 ++++-- .../com/muwire/core/hostcache/HostCache.groovy | 6 ++++-- .../com/muwire/core/trust/TrustService.groovy | 7 +++++-- 4 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 core/src/main/groovy/com/muwire/core/Service.groovy diff --git a/core/src/main/groovy/com/muwire/core/Service.groovy b/core/src/main/groovy/com/muwire/core/Service.groovy new file mode 100644 index 00000000..23436dee --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/Service.groovy @@ -0,0 +1,13 @@ +package com.muwire.core + +abstract class Service { + + volatile boolean loaded + + abstract void load() + + void waitForLoad() { + while (!loaded) + Thread.sleep(10) + } +} 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 57136afc..884465c0 100644 --- a/core/src/main/groovy/com/muwire/core/files/PersisterService.groovy +++ b/core/src/main/groovy/com/muwire/core/files/PersisterService.groovy @@ -5,6 +5,7 @@ import java.util.stream.Collectors import com.muwire.core.DownloadedFile import com.muwire.core.EventBus import com.muwire.core.InfoHash +import com.muwire.core.Service import com.muwire.core.SharedFile import groovy.json.JsonOutput @@ -12,7 +13,7 @@ import groovy.json.JsonSlurper import net.i2p.data.Base32 import net.i2p.data.Destination -class PersisterService { +class PersisterService extends Service { final File location final EventBus listener @@ -36,7 +37,7 @@ class PersisterService { timer.cancel() } - private void load() { + void load() { if (location.exists() && location.isFile()) { def slurper = new JsonSlurper() try { @@ -53,6 +54,7 @@ class PersisterService { } } timer.schedule({persistFiles()} as TimerTask, 0, interval) + loaded = true } private static FileLoadedEvent fromJson(def json) { diff --git a/core/src/main/groovy/com/muwire/core/hostcache/HostCache.groovy b/core/src/main/groovy/com/muwire/core/hostcache/HostCache.groovy index 45d72cd1..a906bd5d 100644 --- a/core/src/main/groovy/com/muwire/core/hostcache/HostCache.groovy +++ b/core/src/main/groovy/com/muwire/core/hostcache/HostCache.groovy @@ -3,6 +3,7 @@ package com.muwire.core.hostcache import java.util.concurrent.ConcurrentHashMap import com.muwire.core.MuWireSettings +import com.muwire.core.Service import com.muwire.core.connection.ConnectionAttemptStatus import com.muwire.core.connection.ConnectionEvent import com.muwire.core.trust.TrustLevel @@ -12,7 +13,7 @@ import groovy.json.JsonOutput import groovy.json.JsonSlurper import net.i2p.data.Destination -class HostCache { +class HostCache extends Service { final TrustService trustService final File storage @@ -73,7 +74,7 @@ class HostCache { rv[0..n-1] } - private void load() { + void load() { if (storage.exists()) { JsonSlurper slurper = new JsonSlurper() storage.eachLine { @@ -86,6 +87,7 @@ class HostCache { } } timer.schedule({save()} as TimerTask, interval, interval) + loaded = true } private boolean allowHost(Host host) { diff --git a/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy b/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy index 9053148d..8bd5d8e9 100644 --- a/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy +++ b/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy @@ -1,9 +1,11 @@ package com.muwire.core.trust +import com.muwire.core.Service + import net.i2p.data.Destination import net.i2p.util.ConcurrentHashSet -class TrustService { +class TrustService extends Service { final File persistGood, persistBad final long persistInterval @@ -30,7 +32,7 @@ class TrustService { timer.cancel() } - private void load() { + void load() { if (persistGood.exists()) { persistGood.eachLine { good.add(new Destination(it)) @@ -42,6 +44,7 @@ class TrustService { } } timer.schedule({persist()} as TimerTask, persistInterval, persistInterval) + loaded = true } private void persist() {