Search in sources :

Example 1 with DhtResults

use of i2p.bote.network.DhtResults in project i2p.i2p-bote by i2p.

the class KademliaDHT method getDhtResults.

/**
 * Returns all <code>DhtStorablePacket</code> packets that have been received as a response to a send batch,
 * plus <code>localResult</code> if it is non-<code>null</code>.
 * @param batch
 * @param localResult
 */
private DhtResults getDhtResults(PacketBatch batch, DhtStorablePacket localResult) {
    Map<Destination, DataPacket> responses = batch.getResponses();
    DhtResults results = new DhtResults();
    for (Entry<Destination, DataPacket> result : responses.entrySet()) {
        DataPacket packet = result.getValue();
        if (packet instanceof DhtStorablePacket)
            results.put(result.getKey(), (DhtStorablePacket) packet);
    }
    int totalResponses = responses.size();
    if (localResult != null) {
        results.put(localDestination, localResult);
        totalResponses++;
    }
    results.setTotalResponses(totalResponses);
    return results;
}
Also used : DhtStorablePacket(i2p.bote.packet.dht.DhtStorablePacket) Destination(net.i2p.data.Destination) DataPacket(i2p.bote.packet.DataPacket) DhtResults(i2p.bote.network.DhtResults)

Example 2 with DhtResults

use of i2p.bote.network.DhtResults in project i2p.i2p-bote by i2p.

the class I2PBote method lookupInDirectory.

public Contact lookupInDirectory(String name) throws InterruptedException {
    Hash key = EmailIdentity.calculateHash(name);
    if (null == dht) {
        return null;
    }
    DhtResults results = dht.findOne(key, Contact.class);
    if (!results.isEmpty()) {
        DhtStorablePacket packet = results.getPackets().iterator().next();
        if (packet instanceof Contact) {
            Contact contact = (Contact) packet;
            try {
                if (contact.verify())
                    return contact;
            } catch (GeneralSecurityException e) {
                log.error("Can't verify Contact", e);
            }
        }
    }
    return null;
}
Also used : DhtStorablePacket(i2p.bote.packet.dht.DhtStorablePacket) GeneralSecurityException(java.security.GeneralSecurityException) Hash(net.i2p.data.Hash) DhtResults(i2p.bote.network.DhtResults) Contact(i2p.bote.packet.dht.Contact)

Example 3 with DhtResults

use of i2p.bote.network.DhtResults in project i2p.i2p-bote by i2p.

the class KademliaDHT method find.

private DhtResults find(Hash key, Class<? extends DhtStorablePacket> dataType, boolean exhaustive) throws InterruptedException {
    final Collection<Destination> closeNodes = getClosestNodes(key);
    log.info("Querying localhost + " + closeNodes.size() + " peers for data type " + dataType.getSimpleName() + ", Kademlia key " + key);
    DhtStorablePacket localResult = findLocally(key, dataType);
    // if a local packet exists and one result is requested, return the local packet
    if (!exhaustive && localResult != null) {
        log.debug("Locally stored packet found for hash " + key + " and data type " + dataType.getSimpleName());
        DhtResults results = new DhtResults();
        results.put(localDestination, localResult);
        return results;
    }
    // Send the retrieve requests
    PacketBatch batch = new PacketBatch();
    for (Destination node : closeNodes) if (// local has already been taken care of
    !localDestination.equals(node))
        batch.putPacket(new RetrieveRequest(key, dataType), node);
    sendQueue.send(batch);
    batch.awaitSendCompletion();
    // wait for replies
    if (exhaustive)
        batch.awaitAllResponses(RESPONSE_TIMEOUT, TimeUnit.SECONDS);
    else
        batch.awaitFirstReply(RESPONSE_TIMEOUT, TimeUnit.SECONDS);
    log.info(batch.getResponses().size() + " response packets received for hash " + key + " and data type " + dataType.getSimpleName());
    sendQueue.remove(batch);
    return getDhtResults(batch, localResult);
}
Also used : DhtStorablePacket(i2p.bote.packet.dht.DhtStorablePacket) RetrieveRequest(i2p.bote.packet.dht.RetrieveRequest) Destination(net.i2p.data.Destination) PacketBatch(i2p.bote.network.PacketBatch) DhtResults(i2p.bote.network.DhtResults)

Aggregations

DhtResults (i2p.bote.network.DhtResults)3 DhtStorablePacket (i2p.bote.packet.dht.DhtStorablePacket)3 Destination (net.i2p.data.Destination)2 PacketBatch (i2p.bote.network.PacketBatch)1 DataPacket (i2p.bote.packet.DataPacket)1 Contact (i2p.bote.packet.dht.Contact)1 RetrieveRequest (i2p.bote.packet.dht.RetrieveRequest)1 GeneralSecurityException (java.security.GeneralSecurityException)1 Hash (net.i2p.data.Hash)1