NioUdpClient client = new NioUdpClient();
client.getSessionConfig().setIdleTimeInMillis(IdleStatus.READ_IDLE, 2000);
client.setFilters(new MyCodec(), new Handler());
DatagramSocket serverSocket = new DatagramSocket();
int port = serverSocket.getLocalPort();
// warm up
Thread.sleep(100);
final long t0 = System.currentTimeMillis();
// now connect the clients
List<IoFuture<IoSession>> cf = new ArrayList<IoFuture<IoSession>>();
for (int i = 0; i < CLIENT_COUNT; i++) {
cf.add(client.connect(new InetSocketAddress("localhost", port)));
}
// does the session open message was fired ?
assertTrue(openLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
// gather sessions from futures
IoSession[] sessions = new IoSession[CLIENT_COUNT];
for (int i = 0; i < CLIENT_COUNT; i++) {
sessions[i] = cf.get(i).get();
assertNotNull(sessions[i]);
}
// receive and send back some message
for (int i = 0; i < CLIENT_COUNT; i++) {
byte[] receiveData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String sentence = new String(receivePacket.getData());
LOG.info("RECEIVED :" + sentence);
InetAddress IPAddress = receivePacket.getAddress();
int clientPort = receivePacket.getPort();
DatagramPacket sendPacket = new DatagramPacket("tata".getBytes(), "tata".getBytes().length, IPAddress,
clientPort);
serverSocket.send(sendPacket);
}
// does response was wrote and sent ?
assertTrue(msgSentLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
// test is message was received by the client
assertTrue(msgReadLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
// the session idled
assertEquals(CLIENT_COUNT, idleLatch.getCount());
// close the session
assertEquals(CLIENT_COUNT, closedLatch.getCount());
serverSocket.close();
// does the session close event was fired ?
assertTrue(closedLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
long t1 = System.currentTimeMillis();