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