From 2e95831a248603d0cf1645b459255d2c331cc53f Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Sun, 22 Jul 2018 05:32:26 +0100 Subject: [PATCH] HostCache functionality --- .../muwire/core/hostcache/HostCache.groovy | 79 ++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) 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 d66a0962..a3f5a2da 100644 --- a/core/src/main/groovy/com/muwire/core/hostcache/HostCache.groovy +++ b/core/src/main/groovy/com/muwire/core/hostcache/HostCache.groovy @@ -1,12 +1,89 @@ package com.muwire.core.hostcache +import java.util.concurrent.ConcurrentHashMap + +import com.muwire.core.MuWireSettings +import com.muwire.core.trust.TrustLevel import com.muwire.core.trust.TrustService +import groovy.json.JsonOutput +import groovy.json.JsonSlurper +import net.i2p.data.Destination + class HostCache { final TrustService trustService - public HostCache(TrustService trustService) { + final File storage + final int interval + final Timer timer + final MuWireSettings settings + final Map hosts = new ConcurrentHashMap<>() + + public HostCache(TrustService trustService, File storage, int interval, MuWireSettings settings) { this.trustService = trustService + this.storage = storage + this.interval = interval + this.settings = settings + this.timer = new Timer("host-persister",true) } + void start() { + timer.schedule({load()} as TimerTask, 1) + } + + void stop() { + timer.cancel() + } + + List getHosts(int n) { + List rv = new ArrayList<>(hosts.keySet()) + rv.retainAll {allowHost(it)} + if (rv.size() <= n) + return rv + Collections.shuffle(rv) + rv[0..n-1] + } + + private void load() { + if (storage.exists()) { + JsonSlurper slurper = new JsonSlurper() + storage.eachLine { + def entry = slurper.parseText(it) + Destination dest = new Destination(entry.destination) + Host host = new Host(dest) + host.failures = Integer.valueOf(String.valueOf(entry.failures)) + if (allowHost(host)) + hosts.put(dest, host) + } + } + timer.schedule({save()} as TimerTask, interval, interval) + } + + private boolean allowHost(Host host) { + if (host.isFailed()) + return false + TrustLevel trust = trustService.getLevel(host.destination) + switch(trust) { + case TrustLevel.DISTRUSTED : + return false + case TrustLevel.TRUSTED : + return true + case TrustLevel.NEUTRAL : + return settings.allowUntrusted() + } + false + } + + private void save() { + storage.delete() + hosts.each { dest, host -> + if (allowHost(host)) { + def map = [:] + map.destination = dest.toBase64() + map.failures = host.failures + def json = JsonOutput.toJson(map) + storage.append("${json}\n") + } + } + } }