diff --git a/TODO.md b/TODO.md index 7688f62b..f8b472c2 100644 --- a/TODO.md +++ b/TODO.md @@ -21,7 +21,6 @@ |---|---|---|---| |"Edit File Details" frame|N/A|GUI|High| |Right-click resync from watched folders table|N/A|GUI|Medium| -|Persist known hosts|N/A|HostCache, infrastructure|High| |Fix reproducible build on Windows| N/A| Build scripts | Low| |Option to disable saving of search tabs| N/A | GUI | Medium | |Progress bar while collection preview is generated | 70 | GUI | Low | diff --git a/host-cache/src/main/groovy/com/muwire/hostcache/HostCache.groovy b/host-cache/src/main/groovy/com/muwire/hostcache/HostCache.groovy index 7517e18b..41c4a92b 100644 --- a/host-cache/src/main/groovy/com/muwire/hostcache/HostCache.groovy +++ b/host-cache/src/main/groovy/com/muwire/hostcache/HostCache.groovy @@ -73,6 +73,7 @@ public class HostCache { File unverified = new File("unverified") verified.mkdir() unverified.mkdir() + hostPool.load(verified) timer.schedule({hostPool.serialize(verified, unverified)} as TimerTask, 10000, 60 * 60 * 1000) session.addMuxedSessionListener(new Listener(hostPool: hostPool, toReturn: 3, crawler: crawler), diff --git a/host-cache/src/main/groovy/com/muwire/hostcache/HostPool.groovy b/host-cache/src/main/groovy/com/muwire/hostcache/HostPool.groovy index a0478faa..242e8cc8 100644 --- a/host-cache/src/main/groovy/com/muwire/hostcache/HostPool.groovy +++ b/host-cache/src/main/groovy/com/muwire/hostcache/HostPool.groovy @@ -1,10 +1,15 @@ package com.muwire.hostcache +import groovy.json.JsonSlurper +import groovy.util.logging.Log +import net.i2p.data.Destination + import java.text.SimpleDateFormat import java.util.stream.Collectors import groovy.json.JsonOutput +@Log class HostPool { private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyyMMdd-HH") @@ -102,4 +107,40 @@ class HostPool { } } } + + synchronized void load(File path) { + File [] serialized = path.listFiles(new FileFilter() { + @Override + boolean accept(File pathname) { + return pathname.length() > 0 + } + }) + if (serialized == null || serialized.length == 0) { + log.info("couldn't find any files to load from.") + return + } + Arrays.sort(serialized, new Comparator() { + @Override + int compare(File o1, File o2) { + return Long.compare(o2.lastModified(), o1.lastModified()) + } + }) + + File toLoad = serialized[0] + log.info("loading from $toLoad") + int loaded = 0 + def slurper = new JsonSlurper() + toLoad.eachLine { + def parsed = slurper.parseText(it) + def host = new Host() + host.destination = new Destination(parsed.destination) + host.verifyTime = parsed.verifyTime + host.leafSlots = parsed.leafSlots + host.peerSlots = parsed.peerSlots + host.verificationFailures = parsed.verificationFailures + addUnverified(host) + loaded++ + } + log.info("loaded ${unverified.size()}/$loaded hosts") + } }