use of i2p.bote.packet.dht.FindClosePeersPacket in project i2p.i2p-bote by i2p.
the class FindClosePeersPacketTest method toByteArrayAndBack.
@Test
public void toByteArrayAndBack() throws Exception {
byte[] arrayA = findCloseNodesPacket.toByteArray();
byte[] arrayB = new FindClosePeersPacket(arrayA).toByteArray();
assertArrayEquals("The two arrays differ!", arrayA, arrayB);
}
use of i2p.bote.packet.dht.FindClosePeersPacket 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();
}
use of i2p.bote.packet.dht.FindClosePeersPacket in project i2p.i2p-bote by i2p.
the class KademliaDHT method packetReceived.
// PacketListener implementation
@Override
public void packetReceived(CommunicationPacket packet, Destination sender, long receiveTime) {
if (packet instanceof FindClosePeersPacket)
sendPeerList((FindClosePeersPacket) packet, sender);
else if (packet instanceof StoreRequest) {
DhtStorablePacket packetToStore = ((StoreRequest) packet).getPacketToStore();
storeLocally(packetToStore, sender);
} else if (packet instanceof RetrieveRequest) {
RetrieveRequest retrieveRequest = (RetrieveRequest) packet;
DhtStorageHandler storageHandler = storageHandlers.get(retrieveRequest.getDataType());
if (storageHandler != null) {
DhtStorablePacket storedPacket = storageHandler.retrieve(retrieveRequest.getKey());
// if requested packet found, send it to the requester
Collection<ResponsePacket> response = ResponsePacket.create(storedPacket, StatusCode.OK, retrieveRequest.getPacketId());
if (storedPacket != null)
log.debug("Packet found for retrieve request: [" + retrieveRequest + "], replying to sender: [" + Util.toBase32(sender) + "]");
else
log.debug("No matching packet found for retrieve request: [" + retrieveRequest + "]");
sendQueue.send(response, sender);
} else
log.warn("No storage handler found for type " + packet.getClass().getSimpleName() + ".");
} else if (packet instanceof DeleteRequest) {
DeleteRequest delRequest = (DeleteRequest) packet;
DhtStorageHandler storageHandler = storageHandlers.get(delRequest.getDataType());
if (storageHandler instanceof DeletionAwareDhtFolder<?>)
((DeletionAwareDhtFolder<?>) storageHandler).process(delRequest);
} else if (packet instanceof DeletionQuery) {
DhtStorageHandler storageHandler = storageHandlers.get(EncryptedEmailPacket.class);
if (storageHandler instanceof DeletionAwareDhtFolder) {
Hash dhtKey = ((DeletionQuery) packet).getDhtKey();
UniqueId delAuthorization = ((DeletionAwareDhtFolder<?>) storageHandler).getDeleteAuthorization(dhtKey);
// If we know the Delete Authorization for the DHT key, send it to the peer
if (delAuthorization != null) {
DeletionInfoPacket delInfo = new DeletionInfoPacket();
delInfo.put(dhtKey, delAuthorization);
Collection<ResponsePacket> response = ResponsePacket.create(delInfo, StatusCode.OK, packet.getPacketId());
sendQueue.send(response, sender);
}
}
}
// bucketManager is not registered as a PacketListener, so notify it here
bucketManager.packetReceived(packet, sender, receiveTime);
}
use of i2p.bote.packet.dht.FindClosePeersPacket in project i2p.i2p-bote by i2p.
the class FindClosePeersPacketTest method setUp.
@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);
}
Aggregations