final boolean[] success = {false};
final Throwable[] exception = {null};
int port = AvailablePortFinder.getNextAvailable(1025);
IoAcceptor acceptor = createAcceptor();
acceptor.setHandler(new IoHandlerAdapter() {
private int index = 0;
@Override
public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
exception[0] = cause;
session.close();
}
@Override
public void messageReceived(IoSession session, Object message) throws Exception {
IoBuffer buffer = (IoBuffer) message;
while (buffer.hasRemaining()) {
int x = buffer.getInt();
if (x != index) {
throw new Exception(String.format("Integer at %d was %d but should have been %d", index, x, index));
}
index++;
}
if (index > FILE_SIZE / 4) {
throw new Exception("Read too much data");
}
if (index == FILE_SIZE / 4) {
success[0] = true;
session.close();
}
}
});
acceptor.bind(new InetSocketAddress(port));
IoConnector connector = createConnector();
connector.setHandler(new IoHandlerAdapter() {
@Override
public void exceptionCaught(IoSession session, Throwable cause)
throws Exception {
exception[0] = cause;
session.close();