Added IdleState.ALL_IDLE - was able to simplify even more

This commit is contained in:
Trustin Lee 2009-02-12 15:29:35 +00:00
parent cbd2dec0fd
commit 2c8f669030
4 changed files with 30 additions and 20 deletions

View File

@ -25,10 +25,8 @@ package org.jboss.netty.handler.timeout;
import static org.jboss.netty.channel.Channels.*;
import java.text.DateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.Locale;
import java.util.Set;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
@ -41,19 +39,19 @@ import org.jboss.netty.channel.ChannelFuture;
public class DefaultIdleStateEvent implements IdleStateEvent {
private final Channel channel;
private final Set<IdleState> state;
private final IdleState state;
private final long lastActivityTimeMillis;
public DefaultIdleStateEvent(
Channel channel, Set<IdleState> state, long lastActivityTimeMillis) {
Channel channel, IdleState state, long lastActivityTimeMillis) {
if (channel == null) {
throw new NullPointerException("channel");
}
if (state.isEmpty()) {
throw new IllegalArgumentException("state is empty.");
if (state == null) {
throw new NullPointerException("state");
}
this.channel = channel;
this.state = Collections.unmodifiableSet(state);
this.state = state;
this.lastActivityTimeMillis = lastActivityTimeMillis;
}
@ -65,7 +63,7 @@ public class DefaultIdleStateEvent implements IdleStateEvent {
return succeededFuture(getChannel());
}
public Set<IdleState> getState() {
public IdleState getState() {
return state;
}
@ -75,8 +73,7 @@ public class DefaultIdleStateEvent implements IdleStateEvent {
@Override
public String toString() {
return getChannel().toString() +
" - (IDLE_STATE: " + getState() + ", LAST_ACTIVITY: " +
return getChannel().toString() + " - (" + getState() + " since " +
DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT, Locale.US).format(
new Date(getLastActivityTimeMillis())) + ')';

View File

@ -22,6 +22,7 @@
*/
package org.jboss.netty.handler.timeout;
/**
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
@ -29,5 +30,6 @@ package org.jboss.netty.handler.timeout;
*/
public enum IdleState {
READER_IDLE,
WRITER_IDLE;
WRITER_IDLE,
ALL_IDLE;
}

View File

@ -22,8 +22,6 @@
*/
package org.jboss.netty.handler.timeout;
import java.util.Set;
import org.jboss.netty.channel.ChannelEvent;
/**
@ -32,6 +30,6 @@ import org.jboss.netty.channel.ChannelEvent;
* @version $Rev$, $Date$
*/
public interface IdleStateEvent extends ChannelEvent {
Set<IdleState> getState();
IdleState getState();
long getLastActivityTimeMillis();
}

View File

@ -22,8 +22,8 @@
*/
package org.jboss.netty.handler.timeout;
import java.util.EnumSet;
import java.util.Set;
import static org.jboss.netty.channel.Channels.*;
import java.util.concurrent.TimeUnit;
import org.jboss.netty.channel.ChannelHandlerContext;
@ -177,7 +177,8 @@ public class IdleStateHandler extends SimpleChannelUpstreamHandler
bothIdleTimeoutTask = null;
}
protected void channelIdle(ChannelHandlerContext ctx, Set<IdleState> state, long lastActivityTimeMillis) {
protected void channelIdle(
ChannelHandlerContext ctx, IdleState state, long lastActivityTimeMillis) throws Exception {
ctx.sendUpstream(new DefaultIdleStateEvent(ctx.getChannel(), state, lastActivityTimeMillis));
}
@ -201,7 +202,11 @@ public class IdleStateHandler extends SimpleChannelUpstreamHandler
// Reader is idle - set a new timeout and notify the callback.
readerIdleTimeout =
timer.newTimeout(this, readerIdleTimeMillis, TimeUnit.MILLISECONDS);
channelIdle(ctx, EnumSet.of(IdleState.READER_IDLE), lastReadTime);
try {
channelIdle(ctx, IdleState.READER_IDLE, lastReadTime);
} catch (Throwable t) {
fireExceptionCaught(ctx, t);
}
} else {
// Read occurred before the timeout - set a new timeout with shorter delay.
readerIdleTimeout =
@ -231,7 +236,11 @@ public class IdleStateHandler extends SimpleChannelUpstreamHandler
// Writer is idle - set a new timeout and notify the callback.
writerIdleTimeout =
timer.newTimeout(this, writerIdleTimeMillis, TimeUnit.MILLISECONDS);
channelIdle(ctx, EnumSet.of(IdleState.WRITER_IDLE), lastWriteTime);
try {
channelIdle(ctx, IdleState.WRITER_IDLE, lastReadTime);
} catch (Throwable t) {
fireExceptionCaught(ctx, t);
}
} else {
// Write occurred before the timeout - set a new timeout with shorter delay.
writerIdleTimeout =
@ -261,7 +270,11 @@ public class IdleStateHandler extends SimpleChannelUpstreamHandler
// notify the callback.
bothIdleTimeout =
timer.newTimeout(this, bothIdleTimeMillis, TimeUnit.MILLISECONDS);
channelIdle(ctx, EnumSet.allOf(IdleState.class), lastIoTime);
try {
channelIdle(ctx, IdleState.ALL_IDLE, lastReadTime);
} catch (Throwable t) {
fireExceptionCaught(ctx, t);
}
} else {
// Either read or write occurred before the timeout - set a new
// timeout with shorter delay.