public static void main(final String[] args) throws Exception {
log.info("init");
final SocketUDT accept = new SocketUDT(TypeUDT.DATAGRAM);
accept.setBlocking(true);
accept.bind(localSocketAddress());
accept.listen(1);
socketAwait(accept, StatusUDT.LISTENING);
log.info("accept : {}", accept);
final SocketUDT client = new SocketUDT(TypeUDT.DATAGRAM);
client.setBlocking(true);
client.bind(localSocketAddress());
socketAwait(client, StatusUDT.OPENED);
client.connect(accept.getLocalSocketAddress());
socketAwait(client, StatusUDT.CONNECTED);
log.info("client : {}", client);
final SocketUDT server = accept.accept();
server.setBlocking(true);
socketAwait(server, StatusUDT.CONNECTED);
log.info("server : {}", server);
final AtomicBoolean isOn = new AtomicBoolean(true);
final Runnable clientTask = new Runnable() {
@Override
public void run() {
try {
while (isOn.get()) {
runCore();
}
} catch (final Exception e) {
log.error("", e);
}
}
final ByteBuffer buffer = ByteBuffer.allocateDirect(size);
long sequence;
void runCore() throws Exception {
buffer.rewind();
buffer.putLong(0, sequence++);
final TimerContext timer = sendTime.time();
final int count = client.send(buffer);
timer.stop();
if (count != size) {
throw new Exception("count");
}
sendRate.mark(count);
}
};
final Runnable serverTask = new Runnable() {
@Override
public void run() {
try {
while (isOn.get()) {
runCore();
}
} catch (final Exception e) {
log.error("", e);
}
}
final ByteBuffer buffer = ByteBuffer.allocateDirect(size);
long sequence;
void runCore() throws Exception {
buffer.rewind();
final TimerContext timer = recvTime.time();
final int count = server.receive(buffer);
timer.stop();
if (count != size) {
throw new Exception("count");
}
if (this.sequence++ != buffer.getLong(0)) {
throw new Exception("sequence");
}
recvRate.mark(count);
}
};
final ExecutorService executor = Executors.newFixedThreadPool(2);
executor.submit(clientTask);
executor.submit(serverTask);
ConsoleReporterUDT.enable(3, TimeUnit.SECONDS);
Thread.sleep(time);
isOn.set(false);
Thread.sleep(1 * 1000);
executor.shutdownNow();
Metrics.defaultRegistry().shutdown();
accept.close();
client.close();
server.close();
log.info("done");
}