From 286e71b50a4accc9fbdbd27485e19d5327e6f721 Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Tue, 10 Jul 2018 21:28:38 +0100 Subject: [PATCH] wip on crawler --- .../com/muwire/hostcache/Crawler.groovy | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 host-cache/src/main/groovy/com/muwire/hostcache/Crawler.groovy diff --git a/host-cache/src/main/groovy/com/muwire/hostcache/Crawler.groovy b/host-cache/src/main/groovy/com/muwire/hostcache/Crawler.groovy new file mode 100644 index 00000000..c4c9c004 --- /dev/null +++ b/host-cache/src/main/groovy/com/muwire/hostcache/Crawler.groovy @@ -0,0 +1,64 @@ +package com.muwire.hostcache + +import net.i2p.data.Destination + +class Crawler { + + final def pinger + final def hostPool + + final Map inFlight = new HashMap<>() + + UUID currentUUID + + Crawler(pinger, hostPool) { + this.pinger = pinger + this.hostPool = hostPool + } + + synchronized def handleCrawlerPong(pong, Destination source) { + if (!inFlight.containsKey(source)) { + return + } + Host host = inFlight.remove(source) + + if (pong.uuid == null || pong.leafSlots == null || pong.peerSlots == null || pong.peers == null) { + hostPool.fail(host) + return + } + + UUID uuid; + try { + uuid = UUID.fromString(pong.uuid) + } catch (IllegalArgumentException bad) { + hostPool.fail(host) + return + } + + if (!uuid.equals(currentUUID)) { + hostPool.fail(host) + return + } + + host.leafSlots = parseBoolean(pong.leafSlots) + host.peerSlots = parseBoolean(pong.peerSlots) + + def peers + try { + peers = pong.peers.stream().map({b64 -> new Destination(b64)}).collect(Collectors.toSet()) + } catch (Exception e) { + hostPool.fail(host) + return + } + peers.each { + def newHost = new Host() + newHost.destination = it + hostPool.addUnverified(newHost) + } + hostPool.verify(host) + } + + private static boolean parseBoolean(value) { + return Boolean.parseBoolean(value.toString()) + } +}