Salut,
Oleksiy Stashok wrote:
> Hi,
>
> another Grizzly 2.0 example shows how easy it's possible to track
> connection life-cycle (open, close) with filters.
> Filters in Grizzly 2.0 are able to handle accept, connect and close
> events, occurred on a Connection. These methods are called, once a
> connection has been accepted, connected, closed.
>
> The example contains 2 classes.
Can you explain what the NextAction class is supposed to do? I forgot yo
ask earlier :-)
>
> 1) LifeCycleExample is very similar to the EchoServer, but adds
> additional LifeCycleFilter to the chain. And prints the connection
> statistics.
>
>
> public static void main(String[] args) throws IOException {
> // Create TCP transport
> TCPNIOTransport transport =
> TransportManager.instance().createTCPTransport();
>
> LifeCycleFilter lifeCycleFilter = new LifeCycleFilter();
> // Add TransportFilter, which is responsible
> // for reading and writing data to the connection
> transport.getFilterChain().add(new TransportFilter());
> // Add lifecycle filter to track the connections
> transport.getFilterChain().add(lifeCycleFilter);
> // Add echo filter
> transport.getFilterChain().add(new EchoFilter());
>
> try {
> // binding transport to start listen on certain host and port
> transport.bind(HOST, PORT);
>
> // start the transport
> transport.start();
> System.out.println("Press 'q and ENTER' to exit, or just
> ENTER to see statistics...");
>
> do {
> printStats(lifeCycleFilter);
> } while (System.in.read() != 'q');
> } finally {
> // stop the transport
> transport.stop();
>
> // release TransportManager resources like ThreadPool
> TransportManager.instance().close();
> }
> }
>
> /**
> * Print the lifecycle statistics
> *
> * @param lifeCycleFilter the {_at_link LifeCycleFilter}
> */
> private static void printStats(LifeCycleFilter lifeCycleFilter) {
> System.out.println("The total number of connections ever
> connected: " +
> lifeCycleFilter.getTotalConnections());
> System.out.println("The number of active connections: " +
> lifeCycleFilter.getActiveConnections().size());
> }
>
> 2) The LifeCycleFilter, where we handle 3 life-cycle related actions:
> accept, connect, close.
> For accept and connect - we add new connection to the map and for close
> we remove the connection.
>
> /**
> * Method is called, when new {_at_link Connection} was
> * accepted by a {_at_link Transport}
> *
> * @param ctx the filter chain context
> * @param nextAction next action to be executed
> * by the chain (could be modified)
> * @return the next action to be executed by chain
> * @throws java.io.IOException
> */
> @Override
> public NextAction handleAccept(FilterChainContext ctx, NextAction
> nextAction) throws IOException {
> newConnection(ctx.getConnection());
>
> return nextAction;
> }
So here you means the Listener can be notified when the OP_ACCEPT is
under the hood executed? That's interesting :-)
>
>
> /**
> * Method is called, when new client {_at_link Connection} was
> * connected to some endpoint
> *
> * @param ctx the filter chain context
> * @param nextAction next action to be executed
> * by the chain (could be modified)
> * @return the next action to be executed by chain
> * @throws java.io.IOException
> */
> @Override
> public NextAction handleConnect(FilterChainContext ctx, NextAction
> nextAction) throws IOException {
> newConnection(ctx.getConnection());
>
> return nextAction;
> }
>
> /**
> * Method is called, when the {_at_link Connection} is getting closed
> *
> * @param ctx the filter chain context
> * @param nextAction next action to be executed
> * by the chain (could be modified)
> * @return the next action to be executed by chain
> * @throws java.io.IOException
> */
> @Override
> public NextAction handleClose(FilterChainContext ctx, NextAction
> nextAction) throws IOException {
> activeConnectionsMap.remove(ctx.getConnection());
> return super.handleClose(ctx, nextAction);
> }
>
>
> Complete sources are attached.
>
without looking at the class, would it be better to have:
public NextAction handleEvent(FileChainContext ctx, NextAction
nextAction, TRansportManager.EVENT event){
if (event == EVENT.ACCEPT){
}
Why? That will allow us to add support for ON_EXCEPTION (assuming the
accept or connect fail).
Great work so far.
A+
-- Jeanfrancois
> Thanks.
>
> WBR,
> Alexey.
>
>
>
> ------------------------------------------------------------------------
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: dev-help_at_grizzly.dev.java.net