Search in sources :

Example 1 with DhtException

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);
}
Also used : Destination(net.i2p.data.Destination) PacketBatch(i2p.bote.network.PacketBatch) StoreRequest(i2p.bote.packet.dht.StoreRequest) Hash(net.i2p.data.Hash) DhtException(i2p.bote.network.DhtException)

Example 2 with DhtException

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()));
    }
}
Also used : MessagingException(javax.mail.MessagingException) EmailIdentity(i2p.bote.email.EmailIdentity) EncryptedEmailPacket(i2p.bote.packet.dht.EncryptedEmailPacket) GeneralSecurityException(java.security.GeneralSecurityException) EmailStatus(i2p.bote.folder.Outbox.EmailStatus) EmailMetadata(i2p.bote.email.EmailMetadata) IOException(java.io.IOException) IndexPacket(i2p.bote.packet.dht.IndexPacket) EmailDestination(i2p.bote.email.EmailDestination) UnencryptedEmailPacket(i2p.bote.packet.dht.UnencryptedEmailPacket) DhtException(i2p.bote.network.DhtException)

Aggregations

DhtException (i2p.bote.network.DhtException)2 EmailDestination (i2p.bote.email.EmailDestination)1 EmailIdentity (i2p.bote.email.EmailIdentity)1 EmailMetadata (i2p.bote.email.EmailMetadata)1 EmailStatus (i2p.bote.folder.Outbox.EmailStatus)1 PacketBatch (i2p.bote.network.PacketBatch)1 EncryptedEmailPacket (i2p.bote.packet.dht.EncryptedEmailPacket)1 IndexPacket (i2p.bote.packet.dht.IndexPacket)1 StoreRequest (i2p.bote.packet.dht.StoreRequest)1 UnencryptedEmailPacket (i2p.bote.packet.dht.UnencryptedEmailPacket)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 MessagingException (javax.mail.MessagingException)1 Destination (net.i2p.data.Destination)1 Hash (net.i2p.data.Hash)1