wip on crawler

pull/4/head
Zlatin Balevsky 2018-07-10 21:28:38 +01:00
parent 54ab7407fa
commit 286e71b50a
1 changed files with 64 additions and 0 deletions

View File

@ -0,0 +1,64 @@
package com.muwire.hostcache
import net.i2p.data.Destination
class Crawler {
final def pinger
final def hostPool
final Map<Destination, Host> 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())
}
}