diff --git a/core/src/main/groovy/com/muwire/core/trust/TrustEvent.groovy b/core/src/main/groovy/com/muwire/core/trust/TrustEvent.groovy new file mode 100644 index 00000000..bb9e1554 --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/trust/TrustEvent.groovy @@ -0,0 +1,11 @@ +package com.muwire.core.trust + +import com.muwire.core.Event + +import net.i2p.data.Destination + +class TrustEvent extends Event { + + Destination destination + TrustLevel level +} diff --git a/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy b/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy new file mode 100644 index 00000000..778df57b --- /dev/null +++ b/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy @@ -0,0 +1,64 @@ +package com.muwire.core.trust + +import net.i2p.data.Destination +import net.i2p.util.ConcurrentHashSet + +class TrustService { + + final File persistGood, persistBad + final long persistInterval + + final Set good = new ConcurrentHashSet<>() + final Set bad = new ConcurrentHashSet<>() + + final Timer timer + + TrustService(File persistGood, File persistBad, long persistInterval) { + this.persistBad = persistBad + this.persistGood = persistGood + this.persistInterval = persistInterval + this.timer = new Timer("trust-persister",true) + } + + void start() { + timer.schedule({load()} as TimerTask, 1) + } + + void stop() { + timer.cancel() + } + + private void load() { + // TODO: load good and bad + timer.schedule({persist()} as TimerTask, persistInterval, persistInterval) + } + + private void persist() { + // TODO: persist good and bad + } + + TrustLevel getLevel(Destination dest) { + if (good.contains(dest)) + return TrustLevel.TRUSTED + else if (bad.contains(dest)) + return TrustLevel.DISTRUSTED + TrustLevel.NEUTRAL + } + + void onTrustEvent(TrustEvent e) { + switch(e.level) { + case TrustLevel.TRUSTED: + bad.remove(e.destination) + good.add(e.destination) + break + case TrustLevel.DISTRUSTED: + good.remove(e.destination) + bad.add(e.destination) + break + case TrustLevel.NEUTRAL: + good.remove(e.destination) + bad.remove(e.destination) + break + } + } +} diff --git a/core/src/main/java/com/muwire/core/trust/TrustLevel.java b/core/src/main/java/com/muwire/core/trust/TrustLevel.java new file mode 100644 index 00000000..84455686 --- /dev/null +++ b/core/src/main/java/com/muwire/core/trust/TrustLevel.java @@ -0,0 +1,5 @@ +package com.muwire.core.trust; + +public enum TrustLevel { + TRUSTED, NEUTRAL, DISTRUSTED +}