}
private NetServer setServer(int port, final String hostName, final AsyncResultHandler<Void> listenHandler) {
final NetServer server = vertx.createNetServer().connectHandler(new Handler<NetSocket>() {
public void handle(final NetSocket socket) {
final RecordParser parser = RecordParser.newFixed(4, null);
Handler<Buffer> handler = new Handler<Buffer>() {
int size = -1;
public void handle(Buffer buff) {
if (size == -1) {
size = buff.getInt(0);
parser.fixedSizeMode(size);
} else {
BaseMessage received = MessageFactory.read(buff);
if (received.type() == MessageFactory.TYPE_PING) {
// Send back a pong - a byte will do
socket.write(PONG);
} else {
receiveMessage(received);
}
parser.fixedSizeMode(4);
size = -1;
}
}
};
parser.setOutput(handler);
socket.dataHandler(parser);
}
});
server.listen(port, hostName, new AsyncResultHandler<NetServer>() {