* Removed the getter methods in IdlenedssEvent and replaced them with subtypes

** Added ReaderIdlenessEvent and WriterIdlenessEvent
* Added protected methods which enables a user to override what to do on idleness / timeout
This commit is contained in:
Trustin Lee 2009-02-12 10:50:43 +00:00
parent 59b67420bf
commit 0e5f798889
7 changed files with 110 additions and 34 deletions

View File

@ -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)";
}
}

View File

@ -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)";
}
}

View File

@ -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)";
}
}

View File

@ -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.
}

View File

@ -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()));
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}