Skeleton of trust framework

This commit is contained in:
Zlatin Balevsky
2018-07-21 15:06:10 +01:00
parent 11167a291b
commit a0c9252a3f
3 changed files with 80 additions and 0 deletions

View File

@@ -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
}

View File

@@ -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<Destination> good = new ConcurrentHashSet<>()
final Set<Destination> 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
}
}
}

View File

@@ -0,0 +1,5 @@
package com.muwire.core.trust;
public enum TrustLevel {
TRUSTED, NEUTRAL, DISTRUSTED
}