blob: 5fd8ce52714768e61d922505d5d06ea1d3feeb01 (
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
package derms.net.rmulticast;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
class ReceivedSet<T extends MessagePayload> {
private final Queue<Message<T>> received;
ReceivedSet() {
this.received = new ConcurrentLinkedQueue<Message<T>>();
}
/**
* Add a message to the set if it is not already present.
*
* @param msg The message to add to the set.
* @return True if the set did not already contain the specified message.
*/
// TODO: faster insertion.
boolean add(Message<T> msg) {
if (contains(msg))
return false;
received.add(msg);
return true;
}
// TODO: faster search.
Message<T> getByID(MessageID mid) throws NoSuchElementException {
for (Message<T> msg : received)
if (msg.id().equals(mid))
return msg;
throw new NoSuchElementException("message " + mid + " not in received list.");
}
boolean contains(MessageID mid) {
try {
Message<T> msg = getByID(mid);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
boolean contains(Message<T> msg) {
return contains(msg.id());
}
/** Remove the specified message from the set, if it is present. */
void remove(Message<T> msg) {
received.remove(msg);
}
/** Retrieves, but does not remove, the oldest message, or returns null if the set is empty. */
Message<T> peekOldest() {
return received.peek();
}
Message<T> mostRecentSentBy(InetAddress member) throws NoSuchElementException {
Message<T> recent = null;
for (Message<T> msg : received) {
if (msg.sender.equals(member))
recent = msg;
}
if (recent == null)
throw new NoSuchElementException("no message from " + member + " in received list.");
return recent;
}
List<Message<T>> allSentBy(InetAddress sender) {
List<Message<T>> sent = new ArrayList<Message<T>>();
for (Message<T> msg : received) {
if (msg.sender.equals(sender))
sent.add(msg);
}
return sent;
}
}
|