private void sendPeerList(FindClosePeersPacket packet, Destination destination) { Collection<Destination> closestPeers = bucketManager.getClosestPeers(packet.getKey(), KademliaConstants.K); PeerList peerList = new PeerList(closestPeers); sendQueue.sendResponse(peerList, destination, packet.getPacketId()); }
@Override public byte[] toByteArray() { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { writeHeader(outputStream); outputStream.write(key.toByteArray()); } catch (IOException e) { log.error("Can't write to ByteArrayOutputStream.", e); } return outputStream.toByteArray(); }
@Test public void toByteArrayAndBack() throws Exception { byte[] arrayA = findCloseNodesPacket.toByteArray(); byte[] arrayB = new FindClosePeersPacket(arrayA).toByteArray(); assertArrayEquals("The two arrays differ!", arrayA, arrayB); } }
@Before public void setUp() throws Exception { Hash key = new Hash(new byte[] {-48, 78, 66, 58, -79, 87, 38, -103, -60, -27, 108, 55, 117, 37, -99, 93, -23, -102, -83, 20, 44, -80, 65, 89, -68, -73, 69, 51, 115, 79, 24, 127}); findCloseNodesPacket = new FindClosePeersPacket(key); }
/** * Returns a <code>FindClosePeersPacket</code> that matches a given {@link UniqueId} * from a {@link Collection} of packets, or <code>null</code> if no match. * @param packets * @param packetId * @return */ private FindClosePeersPacket getPacketById(Collection<FindClosePeersPacket> packets, UniqueId packetId) { for (FindClosePeersPacket packet: packets) if (packetId.equals(packet.getPacketId())) return packet; return null; } };
addLocalResults(key); else { FindClosePeersPacket packet = new FindClosePeersPacket(key); pendingRequests.put(peer, packet); sendQueue.send(packet, peer);