Search in sources :

Example 1 with PacketListener

use of i2p.bote.network.PacketListener in project i2p.i2p-bote by i2p.

the class ClosestNodesLookupTask method call.

public List<Destination> call() throws InterruptedException {
    log.debug("Looking up nodes closest to " + key);
    PacketListener packetListener = new IncomingPacketHandler();
    i2pReceiver.addPacketListener(packetListener);
    try {
        // get a list of all unlocked peers (we don't how many we really need because some may not respond)
        notQueriedYet.addAll(bucketManager.getAllUnlockedPeers());
        logStatus();
        startTime = getTime();
        do {
            // send new requests if less than alpha are pending
            while (pendingRequests.size() < KademliaConstants.ALPHA && !notQueriedYet.isEmpty()) {
                // query the closest unqueried peer
                Destination peer = notQueriedYet.first();
                notQueriedYet.remove(peer);
                // if the peer is us, do a local lookup; otherwise, send a request to the peer
                if (localDestination.equals(peer))
                    addLocalResults(key);
                else {
                    FindClosePeersPacket packet = new FindClosePeersPacket(key);
                    pendingRequests.put(peer, packet);
                    sendQueue.send(packet, peer);
                }
                logStatus();
            }
            // handle timeouts
            for (Map.Entry<Destination, FindClosePeersPacket> request : pendingRequests.entrySet()) if (hasTimedOut(request.getValue(), REQUEST_TIMEOUT)) {
                Destination peer = request.getKey();
                log.debug("FindCloseNodes request to peer " + Util.toShortenedBase32(peer) + " timed out.");
                bucketManager.noResponse(peer);
                pendingRequests.remove(peer);
            }
            TimeUnit.SECONDS.sleep(1);
        } while (!isDone());
        log.debug("Node lookup for " + key + " found " + responses.size() + " nodes (may include local node).");
        synchronized (responses) {
            Iterator<Destination> i = responses.iterator();
            while (i.hasNext()) log.debug("  Node: " + Util.toBase32(i.next()));
        }
    } finally {
        i2pReceiver.removePacketListener(packetListener);
    }
    return getResults();
}
Also used : Destination(net.i2p.data.Destination) PacketListener(i2p.bote.network.PacketListener) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FindClosePeersPacket(i2p.bote.packet.dht.FindClosePeersPacket)

Aggregations

PacketListener (i2p.bote.network.PacketListener)1 FindClosePeersPacket (i2p.bote.packet.dht.FindClosePeersPacket)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Destination (net.i2p.data.Destination)1