public void addResult(RPCInfo<FindNodeResponse> rpcInfo) { FindNodeResponse response = rpcInfo.getRPC(); BigInteger found = response.getFoundNodeID(); String foundNodeString = found.toString(16); try { if (queriedNodes.contains(found)) { logger.debug("Node " + foundNodeString + " already queried"); } else if (queriedNodes.size() < maxQueries) { logger.debug("Found node " + foundNodeString); closestNode = new KademliaNode(found, response.getIpAddressInet(), response.getPortInteger()); results.add(closestNode); BigInteger delta = found.xor(searchedNode); if (delta.compareTo(distanceFromClosest) < 0) { distanceFromClosest = delta; requestNode(rpcID, closestNode); } } else { logger.debug("Max queries reached, terminating find node"); actualStatus = Status.ENDED; } } catch (UnknownHostException e) { logger.warn(e); } }
public void run() { actualStatus = Status.PROCESSING; // 返回的结果是20个节点, results = new ClosestNodes( Integer.parseInt(System.getProperty("jkademlia.findnode.maxnodes")), searchedNode); logger.info("Looking for closest nodes to " + searchedNodeString); closestNode = contacts.findContact(searchedNode); if (closestNode != null) results.add(closestNode); int amount = Integer.parseInt(System.getProperty("jkademlia.contacts.findamount")); // 现在自己的k桶表里面查找节点,同时进行的是3个 List<KademliaNode> closestNodes = contacts.findClosestContacts(searchedNode, amount); // 找到之后要对这些节点进行消息确认,发送请求 if (closestNodes.size() > 0) { for (KademliaNode node : closestNodes) requestNode(rpcID, node); } actualStatus = Status.WAITING; }
public synchronized List<KademliaNode> getResults() { return results.toList(); }