Search in sources :

Example 6 with RelayRequest

use of i2p.bote.packet.relay.RelayRequest in project i2p.i2p-bote by i2p.

the class RelayPacketSender method run.

@Override
public void run() {
    while (!Thread.interrupted()) {
        try {
            Iterator<RelayRequest> iterator = packetFolder.iterator();
            while (iterator.hasNext()) {
                RelayRequest packet = iterator.next();
                if (System.currentTimeMillis() >= packet.getSendTime()) {
                    log.debug("Sending relay packet to destination " + Util.toBase32(packet.getNextDestination()));
                    CountDownLatch sentSignal;
                    Destination nextDestination = packet.getNextDestination();
                    // synchronize access to lastSentPacket (which can be null, so synchronize on "this")
                    synchronized (this) {
                        lastSentPacket = packet;
                        confirmationReceived = new CountDownLatch(1);
                        sentSignal = sendQueue.send(lastSentPacket, nextDestination);
                    }
                    sentSignal.await();
                    TimeUnit.MINUTES.sleep(2);
                    // if confirmation has been received, delete the packet
                    if (confirmationReceived.await(0, TimeUnit.SECONDS)) {
                        log.debug("Confirmation received from relay peer " + Util.toShortenedBase32(nextDestination) + ", deleting packet: " + packet);
                        iterator.remove();
                    }
                }
            }
            TimeUnit.MINUTES.sleep(pause);
        } catch (InterruptedException e) {
            break;
        } catch (RuntimeException e) {
            // catch unexpected exceptions to keep the thread running
            log.error("Exception caught in RelayPacketSender loop", e);
        }
    }
    log.debug("RelayPacketSender thread interrupted, exiting.");
}
Also used : Destination(net.i2p.data.Destination) CountDownLatch(java.util.concurrent.CountDownLatch) RelayRequest(i2p.bote.packet.relay.RelayRequest)

Aggregations

RelayRequest (i2p.bote.packet.relay.RelayRequest)6 StoreRequest (i2p.bote.packet.dht.StoreRequest)3 Destination (net.i2p.data.Destination)2 UniqueId (i2p.bote.UniqueId)1 EmailDestination (i2p.bote.email.EmailDestination)1 CommunicationPacket (i2p.bote.packet.CommunicationPacket)1 MalformedPacketException (i2p.bote.packet.MalformedPacketException)1 DhtStorablePacket (i2p.bote.packet.dht.DhtStorablePacket)1 EncryptedEmailPacket (i2p.bote.packet.dht.EncryptedEmailPacket)1 UnencryptedEmailPacket (i2p.bote.packet.dht.UnencryptedEmailPacket)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 DataFormatException (net.i2p.data.DataFormatException)1 Before (org.junit.Before)1 Test (org.junit.Test)1