use of i2p.bote.network.DhtException in project i2p.i2p-bote by i2p.
the class KademliaDHT method store.
@Override
public void store(DhtStorablePacket packet) throws DhtException, InterruptedException {
Hash key = packet.getDhtKey();
log.info("Looking up nodes to store a " + packet.getClass().getSimpleName() + " with key " + key);
List<Destination> closeNodes = getClosestNodes(key);
if (closeNodes.isEmpty())
throw new DhtException("Cannot store packet because no storage nodes found.");
// store on local node if appropriate
if (!closeNodes.contains(localDestination))
if (closeNodes.size() < KademliaConstants.K || isCloser(localDestination, closeNodes.get(0), key))
closeNodes.add(localDestination);
log.info("Storing a " + packet.getClass().getSimpleName() + " with key " + key + " on " + closeNodes.size() + " nodes");
PacketBatch batch = new PacketBatch();
for (Destination node : closeNodes) if (localDestination.equals(node))
storeLocally(packet, null);
else {
// use a separate packet id for each request
StoreRequest storeRequest = new StoreRequest(packet);
batch.putPacket(storeRequest, node);
}
sendQueue.send(batch);
batch.awaitSendCompletion();
// TODO awaitAllResponses, repeat if necessary
sendQueue.remove(batch);
}
use of i2p.bote.network.DhtException in project i2p.i2p-bote by i2p.
the class OutboxProcessor method sendToOne.
/**
* Sends an {@link Email} to one recipient.
* @param senderIdentity The sender's Email Identity, or <code>null</code> for anonymous emails
* @param recipient
* @param email
* @throws MessagingException
* @throws DhtException
* @throws GeneralSecurityException
* @throws PasswordException
* @throws InterruptedException
*/
private void sendToOne(EmailIdentity senderIdentity, String recipient, Email email) throws MessagingException, DhtException, GeneralSecurityException, PasswordException, InterruptedException {
// only used for logging
String logSuffix = null;
try {
logSuffix = "Recipient = '" + recipient + "' Message ID = '" + email.getMessageID() + "'";
log.info("Sending email: " + logSuffix);
EmailDestination recipientDest = new EmailDestination(recipient);
EmailIdentity.IdentityConfig identityConfig;
if (email.isAnonymous())
identityConfig = configuration;
else
identityConfig = senderIdentity.getWrappedConfig(configuration);
int hops = identityConfig.getNumStoreHops();
long minDelay = identityConfig.getRelayMinDelay() * 60 * 1000;
long maxDelay = identityConfig.getRelayMaxDelay() * 60 * 1000;
int relayRedundancy = identityConfig.getRelayRedundancy();
int maxPacketSize = getMaxEmailPacketSize(hops);
Collection<UnencryptedEmailPacket> emailPackets = email.createEmailPackets(senderIdentity, identities, recipient, maxPacketSize);
IndexPacket indexPacket = new IndexPacket(recipientDest);
EmailMetadata metadata = email.getMetadata();
for (UnencryptedEmailPacket unencryptedPacket : emailPackets) {
EncryptedEmailPacket emailPacket = new EncryptedEmailPacket(unencryptedPacket, recipientDest);
send(emailPacket, hops, minDelay, maxDelay, relayRedundancy);
indexPacket.put(emailPacket);
metadata.addPacketInfo(recipientDest, emailPacket.getDhtKey(), emailPacket.getDeleteVerificationHash());
}
send(indexPacket, hops, minDelay, maxDelay, relayRedundancy);
outbox.saveMetadata(email);
} catch (GeneralSecurityException e) {
log.error("Invalid recipient address. " + logSuffix, e);
outbox.setStatus(email, new EmailStatus(Status.INVALID_RECIPIENT, recipient));
throw e;
} catch (MessagingException e) {
log.error("Can't create email packets. " + logSuffix, e);
outbox.setStatus(email, new EmailStatus(Status.ERROR_CREATING_PACKETS, e.getLocalizedMessage()));
throw e;
} catch (DhtException e) {
log.error("Can't store email packet on the DHT. " + logSuffix, e);
outbox.setStatus(email, new EmailStatus(Status.ERROR_SENDING, e.getLocalizedMessage()));
throw e;
} catch (IOException e) {
log.error("Can't save metadata. " + logSuffix, e);
outbox.setStatus(email, new EmailStatus(Status.ERROR_SAVING_METADATA, e.getLocalizedMessage()));
}
}
Aggregations