net.tomp2p.connection
Class IdleStateHandler

java.lang.Object
  extended by org.jboss.netty.channel.SimpleChannelUpstreamHandler
      extended by net.tomp2p.connection.IdleStateHandler
All Implemented Interfaces:
org.jboss.netty.channel.ChannelHandler, org.jboss.netty.channel.ChannelUpstreamHandler, org.jboss.netty.channel.LifeCycleAwareChannelHandler, org.jboss.netty.util.ExternalResourceReleasable

public class IdleStateHandler
extends org.jboss.netty.channel.SimpleChannelUpstreamHandler
implements org.jboss.netty.channel.LifeCycleAwareChannelHandler, org.jboss.netty.util.ExternalResourceReleasable

Triggers an IdleStateEvent when a Channel has not performed read, write, or both operation for a while.

Supported idle states

Property Meaning
readerIdleTime an IdleStateEvent whose state is IdleState.READER_IDLE will be triggered when no read was performed for the specified period of time. Specify 0 to disable.
writerIdleTime an IdleStateEvent whose state is IdleState.WRITER_IDLE will be triggered when no write was performed for the specified period of time. Specify 0 to disable.
allIdleTime an IdleStateEvent whose state is IdleState.ALL_IDLE will be triggered when neither read nor write was performed for the specified period of time. Specify 0 to disable.
 // An example that sends a ping message when there is no outbound traffic
 // for 30 seconds.  The connection is closed when there is no inbound traffic
 // for 60 seconds.
 public class MyPipelineFactory implements ChannelPipelineFactory {
     private final Timer timer;
     public MyPipelineFactory(Timer timer) {
         this.timer = timer;
     }
     public ChannelPipeline getPipeline() {
         return Channels.pipeline(
             new IdleStateHandler(timer, 60, 30, 0), // timer must be shared.
             new MyHandler());
     }
 }
 // Handler should handle the IdleStateEvent triggered by IdleStateHandler.
 public class MyHandler extends IdleStateAwareChannelHandler {
     @Override
     public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) {
         if (e.getState() == IdleState.READER_IDLE) {
             e.getChannel().close();
         } else if (e.getState() == IdleState.WRITER_IDLE) {
             e.getChannel().write(new PingMessage());
         }
     }
 }
 ServerBootstrap bootstrap = ...;
 Timer timer = new HashedWheelTimer();
 ...
 bootstrap.setPipelineFactory(new MyPipelineFactory(timer));
 ...
 
The Timer which was specified when the ReadTimeoutHandler is created should be stopped manually by calling releaseExternalResources() or Timer.stop() when your application shuts down.

Version:
$Rev: 2224 $, $Date: 2010-03-30 17:02:32 +0900 (Tue, 30 Mar 2010) $
Author:
The Netty Project, Trustin Lee
See Also:
ReadTimeoutHandler, WriteTimeoutHandler

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.jboss.netty.channel.ChannelHandler
org.jboss.netty.channel.ChannelHandler.Sharable
 
Constructor Summary
IdleStateHandler(org.jboss.netty.util.Timer timer, int allIdleTimeSeconds)
          Creates a new instance.
IdleStateHandler(org.jboss.netty.util.Timer timer, long allIdleTime, TimeUnit unit)
          Creates a new instance.
 
Method Summary
 void afterAdd(org.jboss.netty.channel.ChannelHandlerContext ctx)
           
 void afterRemove(org.jboss.netty.channel.ChannelHandlerContext ctx)
           
 void beforeAdd(org.jboss.netty.channel.ChannelHandlerContext ctx)
           
 void beforeRemove(org.jboss.netty.channel.ChannelHandlerContext ctx)
           
 void channelClosed(org.jboss.netty.channel.ChannelHandlerContext ctx, org.jboss.netty.channel.ChannelStateEvent e)
           
protected  void channelIdle(org.jboss.netty.channel.ChannelHandlerContext ctx, org.jboss.netty.handler.timeout.IdleState state, long lastActivityTimeMillis)
           
 void channelOpen(org.jboss.netty.channel.ChannelHandlerContext ctx, org.jboss.netty.channel.ChannelStateEvent e)
           
 void messageReceived(org.jboss.netty.channel.ChannelHandlerContext ctx, org.jboss.netty.channel.MessageEvent e)
           
 void releaseExternalResources()
          Stops the Timer which was specified in the constructor of this handler.
 void reset()
           
 void writeComplete(org.jboss.netty.channel.ChannelHandlerContext ctx, org.jboss.netty.channel.WriteCompletionEvent e)
           
 
Methods inherited from class org.jboss.netty.channel.SimpleChannelUpstreamHandler
channelBound, channelConnected, channelDisconnected, channelInterestChanged, channelUnbound, childChannelClosed, childChannelOpen, exceptionCaught, handleUpstream
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

IdleStateHandler

public IdleStateHandler(org.jboss.netty.util.Timer timer,
                        int allIdleTimeSeconds)
Creates a new instance.

Parameters:
timer - the Timer that is used to trigger the scheduled event. The recommended Timer implementation is HashedWheelTimer.
readerIdleTimeSeconds - an IdleStateEvent whose state is IdleState.READER_IDLE will be triggered when no read was performed for the specified period of time. Specify 0 to disable.
writerIdleTimeSeconds - an IdleStateEvent whose state is IdleState.WRITER_IDLE will be triggered when no write was performed for the specified period of time. Specify 0 to disable.
allIdleTimeSeconds - an IdleStateEvent whose state is IdleState.ALL_IDLE will be triggered when neither read nor write was performed for the specified period of time. Specify 0 to disable.

IdleStateHandler

public IdleStateHandler(org.jboss.netty.util.Timer timer,
                        long allIdleTime,
                        TimeUnit unit)
Creates a new instance.

Parameters:
timer - the Timer that is used to trigger the scheduled event. The recommended Timer implementation is HashedWheelTimer.
readerIdleTime - an IdleStateEvent whose state is IdleState.READER_IDLE will be triggered when no read was performed for the specified period of time. Specify 0 to disable.
writerIdleTime - an IdleStateEvent whose state is IdleState.WRITER_IDLE will be triggered when no write was performed for the specified period of time. Specify 0 to disable.
allIdleTime - an IdleStateEvent whose state is IdleState.ALL_IDLE will be triggered when neither read nor write was performed for the specified period of time. Specify 0 to disable.
unit - the TimeUnit of readerIdleTime, writeIdleTime, and allIdleTime
Method Detail

releaseExternalResources

public void releaseExternalResources()
Stops the Timer which was specified in the constructor of this handler. You should not call this method if the Timer is in use by other objects.

Specified by:
releaseExternalResources in interface org.jboss.netty.util.ExternalResourceReleasable

beforeAdd

public void beforeAdd(org.jboss.netty.channel.ChannelHandlerContext ctx)
               throws Exception
Specified by:
beforeAdd in interface org.jboss.netty.channel.LifeCycleAwareChannelHandler
Throws:
Exception

afterAdd

public void afterAdd(org.jboss.netty.channel.ChannelHandlerContext ctx)
              throws Exception
Specified by:
afterAdd in interface org.jboss.netty.channel.LifeCycleAwareChannelHandler
Throws:
Exception

beforeRemove

public void beforeRemove(org.jboss.netty.channel.ChannelHandlerContext ctx)
                  throws Exception
Specified by:
beforeRemove in interface org.jboss.netty.channel.LifeCycleAwareChannelHandler
Throws:
Exception

afterRemove

public void afterRemove(org.jboss.netty.channel.ChannelHandlerContext ctx)
                 throws Exception
Specified by:
afterRemove in interface org.jboss.netty.channel.LifeCycleAwareChannelHandler
Throws:
Exception

channelOpen

public void channelOpen(org.jboss.netty.channel.ChannelHandlerContext ctx,
                        org.jboss.netty.channel.ChannelStateEvent e)
                 throws Exception
Overrides:
channelOpen in class org.jboss.netty.channel.SimpleChannelUpstreamHandler
Throws:
Exception

channelClosed

public void channelClosed(org.jboss.netty.channel.ChannelHandlerContext ctx,
                          org.jboss.netty.channel.ChannelStateEvent e)
                   throws Exception
Overrides:
channelClosed in class org.jboss.netty.channel.SimpleChannelUpstreamHandler
Throws:
Exception

messageReceived

public void messageReceived(org.jboss.netty.channel.ChannelHandlerContext ctx,
                            org.jboss.netty.channel.MessageEvent e)
                     throws Exception
Overrides:
messageReceived in class org.jboss.netty.channel.SimpleChannelUpstreamHandler
Throws:
Exception

writeComplete

public void writeComplete(org.jboss.netty.channel.ChannelHandlerContext ctx,
                          org.jboss.netty.channel.WriteCompletionEvent e)
                   throws Exception
Overrides:
writeComplete in class org.jboss.netty.channel.SimpleChannelUpstreamHandler
Throws:
Exception

channelIdle

protected void channelIdle(org.jboss.netty.channel.ChannelHandlerContext ctx,
                           org.jboss.netty.handler.timeout.IdleState state,
                           long lastActivityTimeMillis)
                    throws Exception
Throws:
Exception

reset

public void reset()


Copyright © 2011. All Rights Reserved.