respond to discovery of new hosts and connection attempts

pull/4/head
Zlatin Balevsky 2018-07-22 09:37:32 +01:00
parent 2e95831a24
commit db98d63caf
1 changed files with 29 additions and 0 deletions

View File

@ -3,6 +3,8 @@ package com.muwire.core.hostcache
import java.util.concurrent.ConcurrentHashMap
import com.muwire.core.MuWireSettings
import com.muwire.core.connection.ConnectionAttemptStatus
import com.muwire.core.connection.ConnectionEvent
import com.muwire.core.trust.TrustLevel
import com.muwire.core.trust.TrustService
@ -35,6 +37,33 @@ class HostCache {
timer.cancel()
}
void onHostDiscoveredEvent(HostDiscoveredEvent e) {
if (hosts.containsKey(e.destination))
return
Host host = new Host(e.destination)
if (allowHost(host)) {
hosts.put(e.destination, host)
}
}
void onConnectionEvent(ConnectionEvent e) {
Host host = hosts.get(e.destination)
if (host == null) {
host = new Host(e.destination)
hosts.put(e.destination, host)
}
switch(e.status) {
case ConnectionAttemptStatus.SUCCESSFUL:
case ConnectionAttemptStatus.REJECTED:
host.onConnect()
break
case ConnectionAttemptStatus.FAILED:
host.onFailure()
break
}
}
List<Destination> getHosts(int n) {
List<Destination> rv = new ArrayList<>(hosts.keySet())
rv.retainAll {allowHost(it)}