* 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 { public class DefaultIdlenessEvent implements IdlenessEvent {
private final Channel channel; 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) { if (channel == null) {
throw new NullPointerException("channel"); throw new NullPointerException("channel");
} }
this.channel = channel; this.channel = channel;
this.lastReadTime = lastReadTime;
this.lastWriteTime = lastWriteTime;
this.readerIdleTime = readerIdleTime;
this.writerIdleTime = writerIdleTime;
} }
public Channel getChannel() { public Channel getChannel() {
return channel; return channel;
} }
public boolean isReaderIdle() {
return System.currentTimeMillis() + readerIdleTime > lastReadTime;
}
public boolean isWriterIdle() {
return System.currentTimeMillis() + writerIdleTime > lastWriteTime;
}
public ChannelFuture getFuture() { public ChannelFuture getFuture() {
return succeededFuture(getChannel()); return succeededFuture(getChannel());
} }
@Override @Override
public String toString() { public String toString() {
return getChannel().toString() + " - (IDLE: " + return getChannel().toString() + " - (IDLE)";
(isReaderIdle()? 'R' : '_') +
(isWriterIdle()? 'W' : '_') + ')';
} }
} }

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$ * @version $Rev$, $Date$
*/ */
public interface IdlenessEvent extends ChannelEvent { public interface IdlenessEvent extends ChannelEvent {
boolean isReaderIdle(); // This is a tag interface.
boolean isWriterIdle();
} }

View File

@ -173,15 +173,18 @@ public class IdlenessHandler extends SimpleChannelUpstreamHandler implements Lif
// Reader is idle - set a new timeout and notify the callback. // Reader is idle - set a new timeout and notify the callback.
readerIdleTimeout = readerIdleTimeout =
timer.newTimeout(this, readerIdleTimeMillis, TimeUnit.MILLISECONDS); timer.newTimeout(this, readerIdleTimeMillis, TimeUnit.MILLISECONDS);
ctx.sendUpstream(new DefaultIdlenessEvent( onReaderIdleness(ctx);
ctx.getChannel(), lastReadTime, lastWriteTime,
readerIdleTimeMillis, writerIdleTimeMillis));
} else { } else {
// Read occurred before the timeout - set a new timeout with shorter delay. // Read occurred before the timeout - set a new timeout with shorter delay.
readerIdleTimeout = readerIdleTimeout =
timer.newTimeout(this, nextDelay, TimeUnit.MILLISECONDS); timer.newTimeout(this, nextDelay, TimeUnit.MILLISECONDS);
} }
} }
}
protected void onReaderIdleness(ChannelHandlerContext ctx) {
ctx.sendUpstream(new DefaultReaderIdlenessEvent(ctx.getChannel()));
} }
private final class WriterIdleTimeoutTask implements TimerTask { 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. // Writer is idle - set a new timeout and notify the callback.
writerIdleTimeout = writerIdleTimeout =
timer.newTimeout(this, writerIdleTimeMillis, TimeUnit.MILLISECONDS); timer.newTimeout(this, writerIdleTimeMillis, TimeUnit.MILLISECONDS);
ctx.sendUpstream(new DefaultIdlenessEvent( onWriterIdleness(ctx);
ctx.getChannel(), lastReadTime, lastWriteTime,
readerIdleTimeMillis, writerIdleTimeMillis));
} else { } else {
// Write occurred before the timeout - set a new timeout with shorter delay. // Write occurred before the timeout - set a new timeout with shorter delay.
writerIdleTimeout = 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. // Read timed out - set a new timeout and notify the callback.
ReadTimeoutHandler.this.timeout = ReadTimeoutHandler.this.timeout =
timer.newTimeout(this, timeoutMillis, TimeUnit.MILLISECONDS); timer.newTimeout(this, timeoutMillis, TimeUnit.MILLISECONDS);
Channels.fireExceptionCaught(ctx, EXCEPTION); onReadTimeout(ctx);
} else { } else {
// Read occurred before the timeout - set a new timeout with shorter delay. // Read occurred before the timeout - set a new timeout with shorter delay.
ReadTimeoutHandler.this.timeout = 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 // Mark the future as failure
if (future.setFailure(EXCEPTION)) { if (future.setFailure(EXCEPTION)) {
// If succeeded to mark as failure, notify the pipeline, too. // 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);
}
} }