blob: ecfece40cb42d0f1dcb088e664d63a29a9d4c65c (
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
|
package derms.net.runicast;
import derms.net.ConcurrentDatagramSocket;
import derms.net.MessagePayload;
import derms.net.Packet;
import derms.util.ThreadPool;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Logger;
public class ReliableUnicastSender<T extends MessagePayload> {
private static final Duration soTimeout = Duration.ofMillis(500); // Socket timeout.
private final AtomicLong next; // Next sequence number.
private final AtomicLong unacked; // Sequence number of first unacknowledged message.
private final Queue<Message<T>> sent;
private final ConcurrentDatagramSocket sock;
private final Logger log;
private final ExecutorService pool;
/**
* @param raddr Remote IP address to connect to.
*/
ReliableUnicastSender(InetSocketAddress raddr) throws IOException {
this.next = new AtomicLong(0);
this.unacked = new AtomicLong(0);
this.sent = new LinkedBlockingQueue<Message<T>>();
this.sock = new ConcurrentDatagramSocket();
this.sock.connect(raddr);
this.sock.setSoTimeout(soTimeout);
this.log = Logger.getLogger(getClass().getName());
this.pool = Executors.newCachedThreadPool();
pool.execute(new ReceiveAcks<T>(unacked, sent, sock));
pool.execute(new Retransmit<T>(unacked, sent, sock));
}
public void send(T payload) throws IOException {
Message<T> msg = new Message<T>(next.get(), payload);
DatagramPacket pkt = Packet.encode(msg, sock.getRemoteSocketAddress());
sock.send(pkt);
sent.add(msg);
next.incrementAndGet();
}
/** Wait for all messages to be acknowledged and close the connection. */
public void close() throws InterruptedException {
// Wait for receiver to acknowledge all sent messages.
while (unacked.get() < next.get()) {
Thread.yield();
if (Thread.interrupted())
throw new InterruptedException();
}
closeNow();
}
/** Close the connection immediately, without waiting for acknowledgements. */
public void closeNow() {
log.info("Shutting down.");
ThreadPool.shutDown(pool, log);
sock.close();
}
}
|