final boolean randomSearch = routingBuilder.locationKey() == null;
int active = 0;
for (int i = 0; i < routingMechanism.parallel(); i++) {
if (routingMechanism.futureResponse(i) == null && !routingMechanism.isStopCreatingNewFutures()) {
final PeerAddress next;
if (randomSearch) {
next = routingMechanism.pollRandomInQueueToAsk(rnd);
} else {
next = routingMechanism.pollFirstInQueueToAsk();
}
if (next != null) {
routingMechanism.addToAlreadyAsked(next);
active++;
// if we search for a random peer, then the peer should
// return the address farest away.
final Number160 locationKey2 = randomSearch ? next.peerId().xor(Number160.MAX_VALUE)
: routingBuilder.locationKey();
routingBuilder.locationKey(locationKey2);
if(LOG.isWarnEnabled() ) {
//routing is per default UDP, don't show warning if the other TCP/UDP is used
if(channelCreator.availableUDPPermits()==0 && !routingBuilder.isForceTCP()) {
LOG.warn("sanity check faild UDP: {}, {}",i,Thread.currentThread().getName());
} else if(channelCreator.availableTCPPermits()==0 && routingBuilder.isForceTCP()) {
LOG.warn("sanity check faild TCP: {}, {}",i,Thread.currentThread().getName());
}
}
routingMechanism.futureResponse(i, neighbors.closeNeighbors(next,
routingBuilder.searchValues(), type, channelCreator, routingBuilder));
LOG.debug("get close neighbors: {} on {}", next, i);
}
} else if (routingMechanism.futureResponse(i) != null) {
LOG.debug("activity on {}", i);
active++;
}
}
if (active == 0) {
LOG.debug("no activity, closing");
routingMechanism.neighbors(routingBuilder);
routingMechanism.cancel();
return;
}
final boolean last = active == 1;
final FutureForkJoin<FutureResponse> fp = new FutureForkJoin<FutureResponse>(1, false,
routingMechanism.futureResponses());
fp.addListener(new BaseFutureAdapter<FutureForkJoin<FutureResponse>>() {
@Override
public void operationComplete(final FutureForkJoin<FutureResponse> future) throws Exception {
final boolean finished;
if (future.isSuccess()) {
Message lastResponse = future.last().responseMessage();
PeerAddress remotePeer = lastResponse.sender();
routingMechanism.addPotentialHits(remotePeer);
Collection<PeerAddress> newNeighbors = lastResponse.neighborsSet(0).neighbors();
Integer resultSize = lastResponse.intAt(0);
Number160 keyDigest = lastResponse.key(0);