From 4740e8b4f5b03261980464530bef2c2619056ded Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Wed, 3 Jul 2019 19:46:24 +0100 Subject: [PATCH] log hostcache stats --- .../com/muwire/hostcache/HostCache.groovy | 3 +++ .../com/muwire/hostcache/HostPool.groovy | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+) 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 3a1b6eaa..61f18b2f 100644 --- a/host-cache/src/main/groovy/com/muwire/hostcache/HostCache.groovy +++ b/host-cache/src/main/groovy/com/muwire/hostcache/HostCache.groovy @@ -64,6 +64,9 @@ public class HostCache { Timer timer = new Timer("timer", true) timer.schedule({hostPool.age()} as TimerTask, 1000,1000) timer.schedule({crawler.startCrawl()} as TimerTask, 10000, 10000) + File verified = new File("verified.json") + File unverified = new File("unverified.json") + timer.schedule({hostPool.serialize(verified, unverified)} as TimerTask, 10000, 60 * 60 * 1000) session.addMuxedSessionListener(new Listener(hostPool: hostPool, toReturn: 2, crawler: crawler), I2PSession.PROTO_DATAGRAM, I2PSession.PORT_ANY) 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 64136851..c59e5c47 100644 --- a/host-cache/src/main/groovy/com/muwire/hostcache/HostPool.groovy +++ b/host-cache/src/main/groovy/com/muwire/hostcache/HostPool.groovy @@ -2,6 +2,8 @@ package com.muwire.hostcache import java.util.stream.Collectors +import groovy.json.JsonOutput + class HostPool { final def maxFailures @@ -74,4 +76,25 @@ class HostPool { } } } + + synchronized void serialize(File verifiedFile, File unverifiedFile) { + write(verifiedFile, verified.values()) + write(unverifiedFile, unverified.values()) + } + + private void write(File target, Collection hosts) { + JsonOutput jsonOutput = new JsonOutput() + target.withPrintWriter { writer -> + hosts.each { + def json = [:] + json.destination = it.destination.toBase64() + json.verifyTime = it.verifyTime + json.leafSlots = it.leafSlots + json.peerSlots = it.peerSlots + json.verificationFailures = it.verificationFailures + def str = jsonOutput.toJson(json) + writer.println(str) + } + } + } }