/** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // Create a FilterChain using FilterChainBuilder FilterChainBuilder filterChainBuilder = FilterChainBuilder.stateless(); // Add TransportFilter, which is responsible // for reading and writing data to the connection filterChainBuilder.add(new TransportFilter()); // StringFilter is responsible for Buffer <-> String conversion filterChainBuilder.add(new StringFilter(Charset.forName("UTF-8"))); // EchoFilter is responsible for echoing received messages filterChainBuilder.add(new EchoFilter()); // Create TCP transport final TCPNIOTransport transport = TCPNIOTransportBuilder.newInstance().build(); transport.setProcessor(filterChainBuilder.build()); // binding transport to start listen on certain host and port transport.bind(HOST, PORT); // start the transport transport.start(); }
public void testBlockingRead() throws Exception { final String[] clientMsgs = {"XXXXX", "Hello", "from", "client"}; Connection connection = null; int messageNum = 3; final BlockingQueue<String> intermResultQueue = DataStructures.getLTQInstance(String.class); final PUFilter puFilter = new PUFilter(); FilterChain subProtocolChain = puFilter .getPUFilterChainBuilder() .add(new MergeFilter(clientMsgs.length, intermResultQueue)) .add(new EchoFilter()) .build(); puFilter.register(new SimpleProtocolFinder(clientMsgs[0]), subProtocolChain); FilterChainBuilder filterChainBuilder = FilterChainBuilder.newInstance(); filterChainBuilder.add(new TransportFilter()); filterChainBuilder.add(new StringFilter()); filterChainBuilder.add(puFilter); final TCPNIOTransport transport = TCPNIOTransportBuilder.newInstance().build(); transport.setFilterChain(filterChainBuilder.build()); try { transport.bind(PORT); transport.start(); final BlockingQueue<String> resultQueue = DataStructures.getLTQInstance(String.class); Future<Connection> future = transport.connect("localhost", PORT); connection = future.get(10, TimeUnit.SECONDS); assertTrue(connection != null); FilterChainBuilder clientFilterChainBuilder = FilterChainBuilder.newInstance(); clientFilterChainBuilder.add(new TransportFilter()); clientFilterChainBuilder.add(new StringFilter()); clientFilterChainBuilder.add( new BaseFilter() { @Override public NextAction handleRead(FilterChainContext ctx) throws IOException { resultQueue.add((String) ctx.getMessage()); return ctx.getStopAction(); } }); final FilterChain clientFilterChain = clientFilterChainBuilder.build(); connection.setFilterChain(clientFilterChain); for (int i = 0; i < messageNum; i++) { String clientMessage = ""; for (int j = 0; j < clientMsgs.length; j++) { String msg = clientMsgs[j] + "-" + i; Future<WriteResult> writeFuture = connection.write(msg); assertTrue("Write timeout loop: " + i, writeFuture.get(10, TimeUnit.SECONDS) != null); final String srvInterm = intermResultQueue.poll(10, TimeUnit.SECONDS); assertEquals("Unexpected interm. response (" + i + ", " + j + ")", msg, srvInterm); clientMessage += msg; } final String message = resultQueue.poll(10, TimeUnit.SECONDS); assertEquals("Unexpected response (" + i + ")", clientMessage, message); } } finally { if (connection != null) { connection.closeSilently(); } transport.shutdownNow(); } }
public void testStandalone() throws Exception { TCPNIOTransport transport = TCPNIOTransportBuilder.newInstance().build(); transport.getAsyncQueueIO().getWriter().setMaxPendingBytesPerConnection(-1); int messageSize = 166434; Connection connection = null; StreamReader reader = null; StreamWriter writer = null; try { // Enable standalone mode transport.configureStandalone(true); // Start listen on specific port final TCPNIOServerConnection serverConnection = transport.bind(PORT); // Start transport transport.start(); // Start echo server thread final Thread serverThread = startEchoServerThread(transport, serverConnection, messageSize); // Connect to the server Future<Connection> connectFuture = transport.connect("localhost", PORT); connection = connectFuture.get(10, TimeUnit.SECONDS); assertTrue(connectFuture.isDone()); // fill out buffer byte[] buffer = new byte[messageSize]; for (int i = 0; i < messageSize; i++) { buffer[i] = (byte) (i % 128); } // write buffer writer = StandaloneProcessor.INSTANCE.getStreamWriter(connection); writer.writeByteArray(buffer); writer.flush(); reader = StandaloneProcessor.INSTANCE.getStreamReader(connection); // prepare receiving buffer byte[] receiveBuffer = new byte[messageSize]; Future readFuture = reader.notifyAvailable(messageSize); readFuture.get(20, TimeUnit.SECONDS); // Read the response. reader.readByteArray(receiveBuffer); assertTrue(readFuture.isDone()); // Check the echo result assertTrue(Arrays.equals(buffer, receiveBuffer)); serverThread.join(10 * 1000); } finally { if (connection != null) { connection.closeSilently(); } transport.shutdownNow(); } }
public void testBlockingReadError() throws Exception { final String[] clientMsgs = {"ZZZZZ", "Hello", "from", "client"}; Connection connection = null; final BlockingQueue intermResultQueue = DataStructures.getLTQInstance(); final PUFilter puFilter = new PUFilter(); FilterChain subProtocolChain = puFilter .getPUFilterChainBuilder() .add(new MergeFilter(clientMsgs.length, intermResultQueue)) .add(new EchoFilter()) .build(); puFilter.register(new SimpleProtocolFinder(clientMsgs[0]), subProtocolChain); FilterChainBuilder filterChainBuilder = FilterChainBuilder.newInstance(); filterChainBuilder.add(new TransportFilter()); filterChainBuilder.add(new StringFilter()); filterChainBuilder.add(puFilter); TCPNIOTransport transport = TCPNIOTransportBuilder.newInstance().build(); transport.setFilterChain(filterChainBuilder.build()); try { transport.bind(PORT); transport.start(); Future<Connection> future = transport.connect("localhost", PORT); connection = future.get(10, TimeUnit.SECONDS); assertTrue(connection != null); FilterChainBuilder clientFilterChainBuilder = FilterChainBuilder.newInstance(); clientFilterChainBuilder.add(new TransportFilter()); clientFilterChainBuilder.add(new StringFilter()); final FilterChain clientFilterChain = clientFilterChainBuilder.build(); connection.setFilterChain(clientFilterChain); String msg = clientMsgs[0]; Future<WriteResult> writeFuture = connection.write(msg); assertTrue("Write timeout", writeFuture.get(10, TimeUnit.SECONDS) != null); final String srvInterm = (String) intermResultQueue.poll(10, TimeUnit.SECONDS); assertEquals("Unexpected interm. response", msg, srvInterm); connection.closeSilently(); connection = null; final Exception e = (Exception) intermResultQueue.poll(10, TimeUnit.SECONDS); assertTrue( "Unexpected response. Exception: " + e.getClass() + ": " + e.getMessage(), e instanceof IOException); } finally { if (connection != null) { connection.closeSilently(); } transport.shutdownNow(); } }