Search in sources :

Example 1 with FindClosePeersPacket

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);
}
Also used : FindClosePeersPacket(i2p.bote.packet.dht.FindClosePeersPacket) Test(org.junit.Test)

Example 2 with FindClosePeersPacket

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();
}
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)

Example 3 with FindClosePeersPacket

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);
}
Also used : DhtStorablePacket(i2p.bote.packet.dht.DhtStorablePacket) RetrieveRequest(i2p.bote.packet.dht.RetrieveRequest) UniqueId(i2p.bote.UniqueId) StoreRequest(i2p.bote.packet.dht.StoreRequest) Hash(net.i2p.data.Hash) FindClosePeersPacket(i2p.bote.packet.dht.FindClosePeersPacket) DeletionAwareDhtFolder(i2p.bote.folder.DeletionAwareDhtFolder) DeletionQuery(i2p.bote.packet.dht.DeletionQuery) DeletionInfoPacket(i2p.bote.packet.dht.DeletionInfoPacket) ResponsePacket(i2p.bote.packet.ResponsePacket) DhtStorageHandler(i2p.bote.network.DhtStorageHandler) DeleteRequest(i2p.bote.packet.dht.DeleteRequest)

Example 4 with FindClosePeersPacket

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);
}
Also used : Hash(net.i2p.data.Hash) FindClosePeersPacket(i2p.bote.packet.dht.FindClosePeersPacket) Before(org.junit.Before)

Aggregations

FindClosePeersPacket (i2p.bote.packet.dht.FindClosePeersPacket)4 Hash (net.i2p.data.Hash)2 UniqueId (i2p.bote.UniqueId)1 DeletionAwareDhtFolder (i2p.bote.folder.DeletionAwareDhtFolder)1 DhtStorageHandler (i2p.bote.network.DhtStorageHandler)1 PacketListener (i2p.bote.network.PacketListener)1 ResponsePacket (i2p.bote.packet.ResponsePacket)1 DeleteRequest (i2p.bote.packet.dht.DeleteRequest)1 DeletionInfoPacket (i2p.bote.packet.dht.DeletionInfoPacket)1 DeletionQuery (i2p.bote.packet.dht.DeletionQuery)1 DhtStorablePacket (i2p.bote.packet.dht.DhtStorablePacket)1 RetrieveRequest (i2p.bote.packet.dht.RetrieveRequest)1 StoreRequest (i2p.bote.packet.dht.StoreRequest)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Destination (net.i2p.data.Destination)1 Before (org.junit.Before)1 Test (org.junit.Test)1