summaryrefslogtreecommitdiffstats
path: root/src/main/java/derms/net/tomulticast/Receive.java
blob: aad0a0b5e47e76a757ebf414e0c6816f76468648 (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
package derms.net.tomulticast;

import derms.net.MessagePayload;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;

/**
 * Receive messages from the multicast group and place them in the holdback queue.
 * Once previous messages are received, they are transferred from the holdback queue
 * to the delivery queue in total order.
 */
class Receive<T extends MessagePayload> extends TotalOrderMulticast<T> implements Runnable {
    private final PriorityBlockingQueue<Message<T>> holdback;
    private final BlockingQueue<Message<T>> deliver;

    Receive(InetSocketAddress group, InetAddress laddr, BlockingQueue<Message<T>> deliver) throws IOException {
        super(group, laddr);
        this.holdback = new PriorityBlockingQueue<Message<T>>();
        this.deliver = deliver;
    }

    @Override
    public void run() {
        try {
            for (;;) {
                Message<T> msg = sock.receive();
                holdback.put(msg);
                tryDeliver();
            }
        } catch (InterruptedException e) {
            close();
        }
    }

    // Try to move messages from the holdback queue to the delivery queue.
    private void tryDeliver() {
        while (!holdback.isEmpty()) {
            // Messages with lower sequence numbers are removed from the priority queue first.
            Message<T> msg = holdback.peek();
            if (msg == null || msg.seq != seq)
                return;

            // This is the next element in the sequence; deliver it.
            msg = holdback.remove();
            deliver.add(msg);
            incSeq();
        }
    }
}