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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
package derms.net.rmulticast;
import derms.net.ConcurrentMulticastSocket;
import derms.net.Packet;
import java.io.IOException;
import java.net.*;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.Logger;
/**
* A reliable multicast protocol that guarantees delivery of messages in the event of a fail-stop.
*
* An implementation of the Trans protocol over IP multicast. Melliar-Smith, et al. "Broadcast Protocols for
* Distributed Systems" in IEEE Transactions on Parallel and Distributed Systems, vol. 1, no. 1, 1990.
*/
public class ReliableMulticast<T extends MessagePayload> {
private final SocketAddress group;
private final InetAddress laddr; // Local address.
private final Set<MessageID> acks; // Positively acknowledged messages.
private final Set<MessageID> nacks; // Negatively acknowledged messages.
private final ReceivedSet<T> received;
private final BlockingQueue<Message<T>> retransmissions; // Messages pending retransmission.
private final Set<InetAddress> groupMembers;
private final ConcurrentMulticastSocket outSock;
private final BlockingQueue<Message<T>> delivered;
private final Logger log;
/**
* Join the specified multicast group.
*
* @param group The IP address and port of the multicast group.
* @param laddr The IP address of the local process.
*/
public ReliableMulticast(InetSocketAddress group, InetAddress laddr) throws IOException {
this.group = group;
this.laddr = laddr;
this.acks = new ConcurrentHashMap<MessageID, Void>().keySet();
this.nacks = new ConcurrentHashMap<MessageID, Void>().keySet();
this.received = new ReceivedSet<T>();
this.retransmissions = new LinkedBlockingQueue<Message<T>>();
this.groupMembers = new ConcurrentHashMap<InetAddress, Void>().keySet();
this.outSock = new ConcurrentMulticastSocket(group.getPort());
this.outSock.joinGroup(group.getAddress());
this.delivered = new LinkedBlockingQueue<Message<T>>();
this.log = Logger.getLogger(this.getClass().getName());
ConcurrentMulticastSocket inSock = new ConcurrentMulticastSocket();
inSock.joinGroup(group.getAddress());
(new Thread(new Receive<T>(inSock, acks, nacks, received, retransmissions, groupMembers, delivered))).start();
(new Thread(new Retransmit<T>(retransmissions, outSock, group))).start();
(new Thread(new Prune<T>(received, groupMembers))).start();
try {
(new Thread(new Heartbeat(group, laddr, acks, nacks, outSock))).start();
} catch (IOException e) {
log.severe("Failed to start heartbeat thread: " + e.getMessage());
throw e;
}
}
/** Send a message to the group. */
public void send(T payload) throws IOException {
Message<T> msg = new Message<T>(
payload,
laddr,
acks.toArray(new MessageID[0]),
nacks.toArray(new MessageID[0]));
DatagramPacket pkt = Packet.encode(msg, group);
outSock.send(pkt);
acks.clear();
(new Thread(new Timeout<T>(msg, acks, retransmissions))).start();
}
/** Receive a message from the group, blocking if necessary until a message arrives. */
public T receive() throws InterruptedException {
Message<T> msg = delivered.take(); // Blocks until a message becomes available.
return msg.payload;
}
}
|