@Override public void packetReceived(CommunicationPacket packet, Destination sender, long receiveTime) { // synchronize access to lastSentPacket (which can be null, so synchronize on "this") synchronized(this) { if (lastSentPacket!=null && packet instanceof ResponsePacket) { ResponsePacket responsePacket = (ResponsePacket)packet; boolean packetIdMatches = lastSentPacket.getPacketId().equals(responsePacket.getPacketId()); boolean statusOk = StatusCode.OK == responsePacket.getStatusCode(); if (packetIdMatches && statusOk) confirmationReceived.countDown(); } } } }
@Override public void packetReceived(CommunicationPacket packet, Destination sender, long receiveTime) { if (packet instanceof ResponsePacket) { ResponsePacket responsePacket = (ResponsePacket)packet; synchronized(pendingRequests) { FindClosePeersPacket request = getPacketById(pendingRequests.values(), responsePacket.getPacketId()); // find the request the node list is in response to // if the packet is in response to a pending request, update responses + notQueriedYet + pendingRequests if (request != null) { log.debug("Response to FindCloseNodesPacket received from " + Util.toShortenedBase32(sender)); responses.add(sender); DataPacket payload = responsePacket.getPayload(); if (payload instanceof PeerList) updatePeers((PeerList)payload, sender, receiveTime); pendingRequests.remove(sender); } } } else if (packet instanceof MalformedCommunicationPacket) pendingRequests.remove(sender); // since it is not generally possible to tell if an invalid comm packet is in response to a certain request, always remove invalid packets from the pending list }