blob: 5dc4be2c8d3037cbc8982baecf4b5819a1c54383 (
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
package derms.net.rmulticast;
import derms.net.ConcurrentMulticastSocket;
import derms.net.Packet;
import derms.util.Wait;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.Set;
import java.util.logging.Logger;
/**
* Periodically send acknowledgements to the group. Allows acks to propagate even when some
* processes wouldn't normally send messages. Also allows processes to populate their set of
* group members.
*/
class Heartbeat implements Runnable {
private static final Duration period = Duration.ofSeconds(30);
private final InetSocketAddress group;
private final InetAddress laddr;
private final Set<MessageID> acks, nacks;
private final ConcurrentMulticastSocket outSock;
private final Logger log;
Heartbeat(InetSocketAddress group, InetAddress laddr, Set<MessageID> acks, Set<MessageID> nacks, ConcurrentMulticastSocket outSock) throws IOException {
this.group = group;
this.laddr = laddr;
this.acks = acks;
this.nacks = nacks;
this.outSock = outSock;
this.log = Logger.getLogger(this.getClass().getName());
}
@Override
public void run() {
try {
for (;;) {
try {
Wait.forDuration(period);
send();
} catch (IOException e) {
log.warning(e.getMessage());
}
}
} catch (InterruptedException e) {
log.info("Interrupted. Shutting down.");
}
}
private void send() throws IOException {
HeartbeatMessage msg = new HeartbeatMessage(
laddr,
acks.toArray(new MessageID[0]),
nacks.toArray(new MessageID[0]));
DatagramPacket pkt = Packet.encode(msg, group);
outSock.send(pkt);
acks.clear();
}
}
|