add waitForLoad() method to loadable services
This commit is contained in:
13
core/src/main/groovy/com/muwire/core/Service.groovy
Normal file
13
core/src/main/groovy/com/muwire/core/Service.groovy
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
@@ -5,6 +5,7 @@ import java.util.stream.Collectors
|
|||||||
import com.muwire.core.DownloadedFile
|
import com.muwire.core.DownloadedFile
|
||||||
import com.muwire.core.EventBus
|
import com.muwire.core.EventBus
|
||||||
import com.muwire.core.InfoHash
|
import com.muwire.core.InfoHash
|
||||||
|
import com.muwire.core.Service
|
||||||
import com.muwire.core.SharedFile
|
import com.muwire.core.SharedFile
|
||||||
|
|
||||||
import groovy.json.JsonOutput
|
import groovy.json.JsonOutput
|
||||||
@@ -12,7 +13,7 @@ import groovy.json.JsonSlurper
|
|||||||
import net.i2p.data.Base32
|
import net.i2p.data.Base32
|
||||||
import net.i2p.data.Destination
|
import net.i2p.data.Destination
|
||||||
|
|
||||||
class PersisterService {
|
class PersisterService extends Service {
|
||||||
|
|
||||||
final File location
|
final File location
|
||||||
final EventBus listener
|
final EventBus listener
|
||||||
@@ -36,7 +37,7 @@ class PersisterService {
|
|||||||
timer.cancel()
|
timer.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
private void load() {
|
void load() {
|
||||||
if (location.exists() && location.isFile()) {
|
if (location.exists() && location.isFile()) {
|
||||||
def slurper = new JsonSlurper()
|
def slurper = new JsonSlurper()
|
||||||
try {
|
try {
|
||||||
@@ -53,6 +54,7 @@ class PersisterService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
timer.schedule({persistFiles()} as TimerTask, 0, interval)
|
timer.schedule({persistFiles()} as TimerTask, 0, interval)
|
||||||
|
loaded = true
|
||||||
}
|
}
|
||||||
|
|
||||||
private static FileLoadedEvent fromJson(def json) {
|
private static FileLoadedEvent fromJson(def json) {
|
||||||
|
@@ -3,6 +3,7 @@ package com.muwire.core.hostcache
|
|||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
|
|
||||||
import com.muwire.core.MuWireSettings
|
import com.muwire.core.MuWireSettings
|
||||||
|
import com.muwire.core.Service
|
||||||
import com.muwire.core.connection.ConnectionAttemptStatus
|
import com.muwire.core.connection.ConnectionAttemptStatus
|
||||||
import com.muwire.core.connection.ConnectionEvent
|
import com.muwire.core.connection.ConnectionEvent
|
||||||
import com.muwire.core.trust.TrustLevel
|
import com.muwire.core.trust.TrustLevel
|
||||||
@@ -12,7 +13,7 @@ import groovy.json.JsonOutput
|
|||||||
import groovy.json.JsonSlurper
|
import groovy.json.JsonSlurper
|
||||||
import net.i2p.data.Destination
|
import net.i2p.data.Destination
|
||||||
|
|
||||||
class HostCache {
|
class HostCache extends Service {
|
||||||
|
|
||||||
final TrustService trustService
|
final TrustService trustService
|
||||||
final File storage
|
final File storage
|
||||||
@@ -73,7 +74,7 @@ class HostCache {
|
|||||||
rv[0..n-1]
|
rv[0..n-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
private void load() {
|
void load() {
|
||||||
if (storage.exists()) {
|
if (storage.exists()) {
|
||||||
JsonSlurper slurper = new JsonSlurper()
|
JsonSlurper slurper = new JsonSlurper()
|
||||||
storage.eachLine {
|
storage.eachLine {
|
||||||
@@ -86,6 +87,7 @@ class HostCache {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
timer.schedule({save()} as TimerTask, interval, interval)
|
timer.schedule({save()} as TimerTask, interval, interval)
|
||||||
|
loaded = true
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean allowHost(Host host) {
|
private boolean allowHost(Host host) {
|
||||||
|
@@ -1,9 +1,11 @@
|
|||||||
package com.muwire.core.trust
|
package com.muwire.core.trust
|
||||||
|
|
||||||
|
import com.muwire.core.Service
|
||||||
|
|
||||||
import net.i2p.data.Destination
|
import net.i2p.data.Destination
|
||||||
import net.i2p.util.ConcurrentHashSet
|
import net.i2p.util.ConcurrentHashSet
|
||||||
|
|
||||||
class TrustService {
|
class TrustService extends Service {
|
||||||
|
|
||||||
final File persistGood, persistBad
|
final File persistGood, persistBad
|
||||||
final long persistInterval
|
final long persistInterval
|
||||||
@@ -30,7 +32,7 @@ class TrustService {
|
|||||||
timer.cancel()
|
timer.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
private void load() {
|
void load() {
|
||||||
if (persistGood.exists()) {
|
if (persistGood.exists()) {
|
||||||
persistGood.eachLine {
|
persistGood.eachLine {
|
||||||
good.add(new Destination(it))
|
good.add(new Destination(it))
|
||||||
@@ -42,6 +44,7 @@ class TrustService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
timer.schedule({persist()} as TimerTask, persistInterval, persistInterval)
|
timer.schedule({persist()} as TimerTask, persistInterval, persistInterval)
|
||||||
|
loaded = true
|
||||||
}
|
}
|
||||||
|
|
||||||
private void persist() {
|
private void persist() {
|
||||||
|
Reference in New Issue
Block a user