blob: 2ccccd179c9e00c82d056d8aac7b1c43faaebd96 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
package derms.net.rmulticast;
import java.io.Serializable;
import java.time.Duration;
import java.time.Instant;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.logging.Logger;
/** If a message is not positively acknowledged after some time, Timeout puts it in the retransmissions list. */
class Timeout<T extends Serializable & Hashable> implements Runnable {
private static final Duration timeout = Duration.ofSeconds(1);
private final Message<T> msg;
private final Set<MessageID> positiveAcks;
private final BlockingQueue<Message<T>> retransmissions;
private final Logger log;
Timeout(Message<T> msg, Set<MessageID> positiveAcks, BlockingQueue<Message<T>> retransmissions) {
this.msg = msg;
this.positiveAcks = positiveAcks;
this.retransmissions = retransmissions;
this.log = Logger.getLogger(this.getClass().getName());
}
@Override
public void run() {
try {
for (;;) {
Wait.forDuration(timeout);
if (positiveAcks.contains(msg.id())) {
log.info("Message " + msg.id() + "positively ack'ed.");
return;
} else {
log.info("Message " + msg.id() + " not ack'ed after " + timeout + "; retransmitting.");
retransmissions.put(msg);
}
}
} catch (InterruptedException e) {
log.info("Timeout thread interrupted: " + e.getMessage());
}
}
}
|