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