static final Logger LOG = LoggerFactory.getLogger(NioUdpEchoServer.class);
public static void main(final String[] args) {
LOG.info("starting echo server");
final NioUdpServer server = new NioUdpServer();
// create the filter chain for this service
server.setFilters(new LoggingFilter("LoggingFilter1"), new IoFilter() {
@Override
public void sessionOpened(final IoSession session) {
LOG.info("session {} open", session);
}
@Override
public void sessionIdle(final IoSession session, final IdleStatus status) {
LOG.info("session {} idle", session);
}
@Override
public void sessionClosed(final IoSession session) {
LOG.info("session {} open", session);
}
@Override
public void messageWriting(final IoSession session, WriteRequest message,
final WriteFilterChainController controller) {
// we just push the message in the chain
controller.callWriteNextFilter(message);
}
@Override
public void messageReceived(final IoSession session, final Object message,
final ReadFilterChainController controller) {
if (message instanceof ByteBuffer) {
LOG.info("echoing");
session.write(message);
}
}
@Override
public void messageSent(final IoSession session, final Object message) {
LOG.info("message {} sent", message);
}
});
server.setIoHandler(new AbstractIoHandler() {
@Override
public void sessionOpened(final IoSession session) {
LOG.info("session opened {}", session);
final String welcomeStr = "welcome\n";
final ByteBuffer bf = ByteBuffer.allocate(welcomeStr.length());
bf.put(welcomeStr.getBytes());
bf.flip();
session.write(bf);
}
});
try {
final SocketAddress address = new InetSocketAddress(9999);
server.bind(address);
LOG.debug("Running the server for 25 sec");
Thread.sleep(25000);
LOG.debug("Unbinding the UDP port");
server.unbind();
} catch (final InterruptedException e) {
LOG.error("Interrupted exception", e);
}
}