dev@grizzly.java.net

Grizzly 2.0: Standalone filterchain processing example

From: Oleksiy Stashok <Oleksiy.Stashok_at_Sun.COM>
Date: Mon, 22 Sep 2008 19:12:30 +0200

Hi,

currently I'm working on subj implementation and have something
completed.
Please take a look at the example [1].

It's simple example, where you can read/write Strings directly, using
standalone filterchain processing.
What do you think?

Thanks.

WBR,
Alexey.

[1]

public class FilterChainTest extends TestCase {
     public static final int PORT = 7778;

     public void testStandaloneFilter() throws Exception {
         Connection connection = null;
         TCPNIOTransport transport =
TransportManager.instance().createTCPTransport();
         transport.getFilterChain().add(new TransportFilter());
         transport.getFilterChain().add(new UTFStringFilter());
         transport.getFilterChain().add(new EchoFilter());


         try {
             transport.bind(PORT);
             transport.start();

             ConnectFuture future =
transport.connectAsync("localhost", PORT);
             connection = (TCPNIOConnection) future.get(10,
TimeUnit.SECONDS);
             assertTrue(connection != null);


             String message = "Hello world!";

             FilterChain filterChain = transport.getFilterChain();

             WriteResult result = filterChain.write(connection,
message);
             assertEquals(result.getMessage(), message);

             ReadResult readResult = filterChain.read(connection);

             String responseMessage = (String) readResult.getMessage();

             assertEquals(message, responseMessage);
         } finally {
             if (connection != null) {
                 connection.close();
             }

             transport.stop();
             TransportManager.instance().close();
         }
     }
}