diff --git a/core/src/main/groovy/com/muwire/core/connection/Connection.groovy b/core/src/main/groovy/com/muwire/core/connection/Connection.groovy index fa1b2ea5..b90c6054 100644 --- a/core/src/main/groovy/com/muwire/core/connection/Connection.groovy +++ b/core/src/main/groovy/com/muwire/core/connection/Connection.groovy @@ -49,6 +49,8 @@ abstract class Connection implements Closeable { protected final String name long lastPingSentTime, lastPongReceivedTime + + private volatile UUID lastPingUUID Connection(EventBus eventBus, Endpoint endpoint, boolean incoming, HostCache hostCache, TrustService trustService, MuWireSettings settings) { @@ -132,7 +134,8 @@ abstract class Connection implements Closeable { def ping = [:] ping.type = "Ping" ping.version = 1 - ping.uuid = UUID.randomUUID().toString() + lastPingUUID = UUID.randomUUID() + ping.uuid = lastPingUUID.toString() messages.put(ping) lastPingSentTime = System.currentTimeMillis() } @@ -177,6 +180,22 @@ abstract class Connection implements Closeable { lastPongReceivedTime = System.currentTimeMillis() if (pong.pongs == null) throw new Exception("Pong doesn't have pongs") + + if (lastPingUUID == null) { + log.fine "$name received an unexpected pong" + return + } + if (pong.uuid == null) { + log.fine "$name pong doesn't have uuid" + return + } + UUID pongUUID = UUID.fromString(pong.uuid) + if (pongUUID != lastPingUUID) { + log.fine "$name ping/pong uuid mismatch" + return + } + lastPingUUID = null + pong.pongs.stream().limit(2).forEach { def dest = new Destination(it) eventBus.publish(new HostDiscoveredEvent(destination: dest))