add waitForLoad() method to loadable services

pull/4/head
Zlatin Balevsky 2018-07-23 16:09:43 +01:00
parent afcc39ada9
commit 07b8724e8f
4 changed files with 26 additions and 6 deletions

View File

@ -0,0 +1,13 @@
package com.muwire.core
abstract class Service {
volatile boolean loaded
abstract void load()
void waitForLoad() {
while (!loaded)
Thread.sleep(10)
}
}

View File

@ -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) {

View File

@ -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) {

View File

@ -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() {