org.jboss.netty.channel
Class SimpleChannelHandler

java.lang.Object
  extended by org.jboss.netty.channel.SimpleChannelHandler
All Implemented Interfaces:
ChannelDownstreamHandler, ChannelHandler, ChannelUpstreamHandler
Direct Known Subclasses:
HttpContentEncoder, IdleStateAwareChannelHandler

public class SimpleChannelHandler
extends Object
implements ChannelUpstreamHandler, ChannelDownstreamHandler

A ChannelHandler which provides an individual handler method for each event type. This handler down-casts the received upstream or or downstream event into more meaningful sub-type event and calls an appropriate handler method with the down-cast event. For an upstream event, the names of the methods are identical to the upstream event names, as introduced in the ChannelEvent documentation. For a downstream event, the names of the methods starts with the name of the operation and ends with "Requested" (e.g. writeRequested.)

Please use SimpleChannelUpstreamHandler or SimpleChannelDownstreamHandler if you want to intercept only upstream or downstream events.

Overriding the handleUpstream and handleDownstream method

You can override the handleUpstream and handleDownstream method just like overriding an ordinary Java method. Please make sure to call super.handleUpstream() or super.handleDownstream() so that other handler methods are invoked properly:

public class MyChannelHandler extends SimpleChannelHandler {

     @Override
     public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {

         // Log all channel state changes.
         if (e instanceof ChannelStateEvent) {
             logger.info("Channel state changed: " + e);
         }

         super.handleUpstream(ctx, e);
     }

     @Override
     public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {

         // Log all channel state changes.
         if (e instanceof MessageEvent) {
             logger.info("Writing:: " + e);
         }

         super.handleDownstream(ctx, e);
     }
 }

Version:
$Rev: 2122 $, $Date: 2010-02-02 11:00:04 +0900 (Tue, 02 Feb 2010) $
Author:
The Netty Project, Trustin Lee

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.jboss.netty.channel.ChannelHandler
ChannelHandler.Sharable
 
Constructor Summary
SimpleChannelHandler()
          Creates a new instance.
 
Method Summary
 void bindRequested(ChannelHandlerContext ctx, ChannelStateEvent e)
          Invoked when Channel.bind(SocketAddress) was called.
 void channelBound(ChannelHandlerContext ctx, ChannelStateEvent e)
          Invoked when a Channel is open and bound to a local address, but not connected.
 void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
          Invoked when a Channel was closed and all its related resources were released.
 void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
          Invoked when a Channel is open, bound to a local address, and connected to a remote address.
 void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e)
          Invoked when a Channel was disconnected from its remote peer.
 void channelInterestChanged(ChannelHandlerContext ctx, ChannelStateEvent e)
          Invoked when a Channel's interestOps was changed.
 void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
          Invoked when a Channel is open, but not bound nor connected.
 void channelUnbound(ChannelHandlerContext ctx, ChannelStateEvent e)
          Invoked when a Channel was unbound from the current local address.
 void childChannelClosed(ChannelHandlerContext ctx, ChildChannelStateEvent e)
          Invoked when a child Channel was closed.
 void childChannelOpen(ChannelHandlerContext ctx, ChildChannelStateEvent e)
          Invoked when a child Channel was open.
 void closeRequested(ChannelHandlerContext ctx, ChannelStateEvent e)
          Invoked when Channel.close() was called.
 void connectRequested(ChannelHandlerContext ctx, ChannelStateEvent e)
          Invoked when Channel.connect(SocketAddress) was called.
 void disconnectRequested(ChannelHandlerContext ctx, ChannelStateEvent e)
          Invoked when Channel.disconnect() was called.
 void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
          Invoked when an exception was raised by an I/O thread or a ChannelHandler.
 void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e)
          Handles the specified downstream event.
 void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e)
          Handles the specified upstream event.
 void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
          Invoked when a message object (e.g: ChannelBuffer) was received from a remote peer.
 void setInterestOpsRequested(ChannelHandlerContext ctx, ChannelStateEvent e)
          Invoked when Channel.setInterestOps(int) was called.
 void unbindRequested(ChannelHandlerContext ctx, ChannelStateEvent e)
          Invoked when Channel.unbind() was called.
 void writeComplete(ChannelHandlerContext ctx, WriteCompletionEvent e)
          Invoked when something was written into a Channel.
 void writeRequested(ChannelHandlerContext ctx, MessageEvent e)
          Invoked when Channel.write(Object) is called.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleChannelHandler

public SimpleChannelHandler()
Creates a new instance.

Method Detail

handleUpstream

public void handleUpstream(ChannelHandlerContext ctx,
                           ChannelEvent e)
                    throws Exception
Handles the specified upstream event. Down-casts the received upstream event into more meaningful sub-type event and calls an appropriate handler method with the down-casted event.

Specified by:
handleUpstream in interface ChannelUpstreamHandler
Parameters:
ctx - the context object for this handler
e - the upstream event to process or intercept
Throws:
Exception

messageReceived

public void messageReceived(ChannelHandlerContext ctx,
                            MessageEvent e)
                     throws Exception
Invoked when a message object (e.g: ChannelBuffer) was received from a remote peer.

Throws:
Exception

exceptionCaught

public void exceptionCaught(ChannelHandlerContext ctx,
                            ExceptionEvent e)
                     throws Exception
Invoked when an exception was raised by an I/O thread or a ChannelHandler.

Throws:
Exception

channelOpen

public void channelOpen(ChannelHandlerContext ctx,
                        ChannelStateEvent e)
                 throws Exception
Invoked when a Channel is open, but not bound nor connected.

Throws:
Exception

channelBound

public void channelBound(ChannelHandlerContext ctx,
                         ChannelStateEvent e)
                  throws Exception
Invoked when a Channel is open and bound to a local address, but not connected.

Throws:
Exception

channelConnected

public void channelConnected(ChannelHandlerContext ctx,
                             ChannelStateEvent e)
                      throws Exception
Invoked when a Channel is open, bound to a local address, and connected to a remote address.

Throws:
Exception

channelInterestChanged

public void channelInterestChanged(ChannelHandlerContext ctx,
                                   ChannelStateEvent e)
                            throws Exception
Invoked when a Channel's interestOps was changed.

Throws:
Exception

channelDisconnected

public void channelDisconnected(ChannelHandlerContext ctx,
                                ChannelStateEvent e)
                         throws Exception
Invoked when a Channel was disconnected from its remote peer.

Throws:
Exception

channelUnbound

public void channelUnbound(ChannelHandlerContext ctx,
                           ChannelStateEvent e)
                    throws Exception
Invoked when a Channel was unbound from the current local address.

Throws:
Exception

channelClosed

public void channelClosed(ChannelHandlerContext ctx,
                          ChannelStateEvent e)
                   throws Exception
Invoked when a Channel was closed and all its related resources were released.

Throws:
Exception

writeComplete

public void writeComplete(ChannelHandlerContext ctx,
                          WriteCompletionEvent e)
                   throws Exception
Invoked when something was written into a Channel.

Throws:
Exception

childChannelOpen

public void childChannelOpen(ChannelHandlerContext ctx,
                             ChildChannelStateEvent e)
                      throws Exception
Invoked when a child Channel was open. (e.g. a server channel accepted a connection)

Throws:
Exception

childChannelClosed

public void childChannelClosed(ChannelHandlerContext ctx,
                               ChildChannelStateEvent e)
                        throws Exception
Invoked when a child Channel was closed. (e.g. the accepted connection was closed)

Throws:
Exception

handleDownstream

public void handleDownstream(ChannelHandlerContext ctx,
                             ChannelEvent e)
                      throws Exception
Handles the specified downstream event. Down-casts the received downstream event into more meaningful sub-type event and calls an appropriate handler method with the down-casted event.

Specified by:
handleDownstream in interface ChannelDownstreamHandler
Parameters:
ctx - the context object for this handler
e - the downstream event to process or intercept
Throws:
Exception

writeRequested

public void writeRequested(ChannelHandlerContext ctx,
                           MessageEvent e)
                    throws Exception
Invoked when Channel.write(Object) is called.

Throws:
Exception

bindRequested

public void bindRequested(ChannelHandlerContext ctx,
                          ChannelStateEvent e)
                   throws Exception
Invoked when Channel.bind(SocketAddress) was called.

Throws:
Exception

connectRequested

public void connectRequested(ChannelHandlerContext ctx,
                             ChannelStateEvent e)
                      throws Exception
Invoked when Channel.connect(SocketAddress) was called.

Throws:
Exception

setInterestOpsRequested

public void setInterestOpsRequested(ChannelHandlerContext ctx,
                                    ChannelStateEvent e)
                             throws Exception
Invoked when Channel.setInterestOps(int) was called.

Throws:
Exception

disconnectRequested

public void disconnectRequested(ChannelHandlerContext ctx,
                                ChannelStateEvent e)
                         throws Exception
Invoked when Channel.disconnect() was called.

Throws:
Exception

unbindRequested

public void unbindRequested(ChannelHandlerContext ctx,
                            ChannelStateEvent e)
                     throws Exception
Invoked when Channel.unbind() was called.

Throws:
Exception

closeRequested

public void closeRequested(ChannelHandlerContext ctx,
                           ChannelStateEvent e)
                    throws Exception
Invoked when Channel.close() was called.

Throws:
Exception


Copyright © 2008-2010 JBoss, by Red Hat. All Rights Reserved.