diff --git a/src/main/java/org/jboss/netty/handler/timeout/DefaultIdlenessEvent.java b/src/main/java/org/jboss/netty/handler/timeout/DefaultIdlenessEvent.java index 90616a3fce..81479d5f32 100644 --- a/src/main/java/org/jboss/netty/handler/timeout/DefaultIdlenessEvent.java +++ b/src/main/java/org/jboss/netty/handler/timeout/DefaultIdlenessEvent.java @@ -35,47 +35,25 @@ import org.jboss.netty.channel.ChannelFuture; public class DefaultIdlenessEvent implements IdlenessEvent { private final Channel channel; - private final long lastReadTime; - private final long lastWriteTime; - private final long readerIdleTime; - private final long writerIdleTime; - - public DefaultIdlenessEvent( - Channel channel, - long lastReadTime, long lastWriteTime, - long readerIdleTime, long writerIdleTime) { + protected DefaultIdlenessEvent(Channel channel) { if (channel == null) { throw new NullPointerException("channel"); } this.channel = channel; - this.lastReadTime = lastReadTime; - this.lastWriteTime = lastWriteTime; - this.readerIdleTime = readerIdleTime; - this.writerIdleTime = writerIdleTime; } public Channel getChannel() { return channel; } - public boolean isReaderIdle() { - return System.currentTimeMillis() + readerIdleTime > lastReadTime; - } - - public boolean isWriterIdle() { - return System.currentTimeMillis() + writerIdleTime > lastWriteTime; - } - public ChannelFuture getFuture() { return succeededFuture(getChannel()); } @Override public String toString() { - return getChannel().toString() + " - (IDLE: " + - (isReaderIdle()? 'R' : '_') + - (isWriterIdle()? 'W' : '_') + ')'; + return getChannel().toString() + " - (IDLE)"; } } diff --git a/src/main/java/org/jboss/netty/handler/timeout/DefaultReaderIdlenessEvent.java b/src/main/java/org/jboss/netty/handler/timeout/DefaultReaderIdlenessEvent.java new file mode 100644 index 0000000000..a85fb4799f --- /dev/null +++ b/src/main/java/org/jboss/netty/handler/timeout/DefaultReaderIdlenessEvent.java @@ -0,0 +1,43 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2009, Red Hat Middleware LLC, and individual contributors + * by the @author tags. See the COPYRIGHT.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.netty.handler.timeout; + +import org.jboss.netty.channel.Channel; + +/** + * @author The Netty Project (netty-dev@lists.jboss.org) + * @author Trustin Lee (tlee@redhat.com) + * @version $Rev$, $Date$ + */ +public class DefaultReaderIdlenessEvent extends DefaultIdlenessEvent + implements ReaderIdlenessEvent { + + public DefaultReaderIdlenessEvent(Channel channel) { + super(channel); + } + + @Override + public String toString() { + return getChannel().toString() + " - (IDLE: READER)"; + } +} diff --git a/src/main/java/org/jboss/netty/handler/timeout/DefaultWriterIdlenessEvent.java b/src/main/java/org/jboss/netty/handler/timeout/DefaultWriterIdlenessEvent.java new file mode 100644 index 0000000000..4dda353ba4 --- /dev/null +++ b/src/main/java/org/jboss/netty/handler/timeout/DefaultWriterIdlenessEvent.java @@ -0,0 +1,43 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2009, Red Hat Middleware LLC, and individual contributors + * by the @author tags. See the COPYRIGHT.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.netty.handler.timeout; + +import org.jboss.netty.channel.Channel; + +/** + * @author The Netty Project (netty-dev@lists.jboss.org) + * @author Trustin Lee (tlee@redhat.com) + * @version $Rev$, $Date$ + */ +public class DefaultWriterIdlenessEvent extends DefaultIdlenessEvent + implements ReaderIdlenessEvent { + + public DefaultWriterIdlenessEvent(Channel channel) { + super(channel); + } + + @Override + public String toString() { + return getChannel().toString() + " - (IDLE: WRITER)"; + } +} diff --git a/src/main/java/org/jboss/netty/handler/timeout/IdlenessEvent.java b/src/main/java/org/jboss/netty/handler/timeout/IdlenessEvent.java index 81c537511f..90bd0afbd7 100644 --- a/src/main/java/org/jboss/netty/handler/timeout/IdlenessEvent.java +++ b/src/main/java/org/jboss/netty/handler/timeout/IdlenessEvent.java @@ -30,6 +30,5 @@ import org.jboss.netty.channel.ChannelEvent; * @version $Rev$, $Date$ */ public interface IdlenessEvent extends ChannelEvent { - boolean isReaderIdle(); - boolean isWriterIdle(); + // This is a tag interface. } diff --git a/src/main/java/org/jboss/netty/handler/timeout/IdlenessHandler.java b/src/main/java/org/jboss/netty/handler/timeout/IdlenessHandler.java index 409d49348d..0c7ee8f1d4 100644 --- a/src/main/java/org/jboss/netty/handler/timeout/IdlenessHandler.java +++ b/src/main/java/org/jboss/netty/handler/timeout/IdlenessHandler.java @@ -173,15 +173,18 @@ public class IdlenessHandler extends SimpleChannelUpstreamHandler implements Lif // Reader is idle - set a new timeout and notify the callback. readerIdleTimeout = timer.newTimeout(this, readerIdleTimeMillis, TimeUnit.MILLISECONDS); - ctx.sendUpstream(new DefaultIdlenessEvent( - ctx.getChannel(), lastReadTime, lastWriteTime, - readerIdleTimeMillis, writerIdleTimeMillis)); + onReaderIdleness(ctx); } else { // Read occurred before the timeout - set a new timeout with shorter delay. readerIdleTimeout = timer.newTimeout(this, nextDelay, TimeUnit.MILLISECONDS); } } + + } + + protected void onReaderIdleness(ChannelHandlerContext ctx) { + ctx.sendUpstream(new DefaultReaderIdlenessEvent(ctx.getChannel())); } private final class WriterIdleTimeoutTask implements TimerTask { @@ -208,9 +211,7 @@ public class IdlenessHandler extends SimpleChannelUpstreamHandler implements Lif // Writer is idle - set a new timeout and notify the callback. writerIdleTimeout = timer.newTimeout(this, writerIdleTimeMillis, TimeUnit.MILLISECONDS); - ctx.sendUpstream(new DefaultIdlenessEvent( - ctx.getChannel(), lastReadTime, lastWriteTime, - readerIdleTimeMillis, writerIdleTimeMillis)); + onWriterIdleness(ctx); } else { // Write occurred before the timeout - set a new timeout with shorter delay. writerIdleTimeout = @@ -218,4 +219,8 @@ public class IdlenessHandler extends SimpleChannelUpstreamHandler implements Lif } } } + + protected void onWriterIdleness(ChannelHandlerContext ctx) { + ctx.sendUpstream(new DefaultWriterIdlenessEvent(ctx.getChannel())); + } } diff --git a/src/main/java/org/jboss/netty/handler/timeout/ReadTimeoutHandler.java b/src/main/java/org/jboss/netty/handler/timeout/ReadTimeoutHandler.java index 5788eeda45..d91dc2a923 100644 --- a/src/main/java/org/jboss/netty/handler/timeout/ReadTimeoutHandler.java +++ b/src/main/java/org/jboss/netty/handler/timeout/ReadTimeoutHandler.java @@ -145,7 +145,7 @@ public class ReadTimeoutHandler extends SimpleChannelUpstreamHandler implements // Read timed out - set a new timeout and notify the callback. ReadTimeoutHandler.this.timeout = timer.newTimeout(this, timeoutMillis, TimeUnit.MILLISECONDS); - Channels.fireExceptionCaught(ctx, EXCEPTION); + onReadTimeout(ctx); } else { // Read occurred before the timeout - set a new timeout with shorter delay. ReadTimeoutHandler.this.timeout = @@ -153,4 +153,8 @@ public class ReadTimeoutHandler extends SimpleChannelUpstreamHandler implements } } } + + protected void onReadTimeout(ChannelHandlerContext ctx) { + Channels.fireExceptionCaught(ctx, EXCEPTION); + } } diff --git a/src/main/java/org/jboss/netty/handler/timeout/WriteTimeoutHandler.java b/src/main/java/org/jboss/netty/handler/timeout/WriteTimeoutHandler.java index 9fba76798d..faa084fa22 100644 --- a/src/main/java/org/jboss/netty/handler/timeout/WriteTimeoutHandler.java +++ b/src/main/java/org/jboss/netty/handler/timeout/WriteTimeoutHandler.java @@ -114,8 +114,12 @@ public class WriteTimeoutHandler extends SimpleChannelDownstreamHandler implemen // Mark the future as failure if (future.setFailure(EXCEPTION)) { // If succeeded to mark as failure, notify the pipeline, too. - Channels.fireExceptionCaught(ctx, EXCEPTION); + onWriteTimeout(ctx); } } } + + protected void onWriteTimeout(ChannelHandlerContext ctx) { + Channels.fireExceptionCaught(ctx, EXCEPTION); + } }