* Reverted back recent changes

* Renamed IdlenessHandler to IdleStateHandler
* Renamed IdlenessEvent to IdleStateEvent
* Added IdleState enum
This commit is contained in:
Trustin Lee 2009-02-12 15:21:25 +00:00
parent 2c3e7565fa
commit cbd2dec0fd
8 changed files with 115 additions and 213 deletions

View File

@ -24,6 +24,12 @@ package org.jboss.netty.handler.timeout;
import static org.jboss.netty.channel.Channels.*; 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.Channel;
import org.jboss.netty.channel.ChannelFuture; import org.jboss.netty.channel.ChannelFuture;
@ -32,16 +38,23 @@ import org.jboss.netty.channel.ChannelFuture;
* @author Trustin Lee (tlee@redhat.com) * @author Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$ * @version $Rev$, $Date$
*/ */
public class DefaultIdlenessEvent implements IdlenessEvent { public class DefaultIdleStateEvent implements IdleStateEvent {
private final Channel channel; private final Channel channel;
private final Set<IdleState> state;
private final long lastActivityTimeMillis;
protected DefaultIdlenessEvent(Channel channel) { public DefaultIdleStateEvent(
Channel channel, Set<IdleState> state, long lastActivityTimeMillis) {
if (channel == null) { if (channel == null) {
throw new NullPointerException("channel"); throw new NullPointerException("channel");
} }
if (state.isEmpty()) {
throw new IllegalArgumentException("state is empty.");
}
this.channel = channel; this.channel = channel;
this.state = Collections.unmodifiableSet(state);
this.lastActivityTimeMillis = lastActivityTimeMillis;
} }
public Channel getChannel() { public Channel getChannel() {
@ -52,8 +65,20 @@ public class DefaultIdlenessEvent implements IdlenessEvent {
return succeededFuture(getChannel()); return succeededFuture(getChannel());
} }
public Set<IdleState> getState() {
return state;
}
public long getLastActivityTimeMillis() {
return lastActivityTimeMillis;
}
@Override @Override
public String toString() { public String toString() {
return getChannel().toString() + " - (IDLE)"; return getChannel().toString() +
" - (IDLE_STATE: " + getState() + ", LAST_ACTIVITY: " +
DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT, Locale.US).format(
new Date(getLastActivityTimeMillis())) + ')';
} }
} }

View File

@ -1,44 +0,0 @@
/*
* 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: 861 $, $Date: 2009-02-12 19:50:43 +0900 (Thu, 12 Feb 2009) $
*/
public class DefaultReaderAndWriterIdlenessEvent extends DefaultIdlenessEvent
implements ReaderIdlenessEvent,
WriterIdlenessEvent {
public DefaultReaderAndWriterIdlenessEvent(Channel channel) {
super(channel);
}
@Override
public String toString() {
return getChannel().toString() + " - (IDLE: READER & WRITER)";
}
}

View File

@ -1,43 +0,0 @@
/*
* 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

@ -1,43 +0,0 @@
/*
* 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

@ -22,13 +22,12 @@
*/ */
package org.jboss.netty.handler.timeout; package org.jboss.netty.handler.timeout;
import org.jboss.netty.channel.ChannelEvent;
/** /**
* @author The Netty Project (netty-dev@lists.jboss.org) * @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com) * @author Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$ * @version $Rev$, $Date$
*/ */
public interface IdlenessEvent extends ChannelEvent { public enum IdleState {
// This is a tag interface. READER_IDLE,
WRITER_IDLE;
} }

View File

@ -22,11 +22,16 @@
*/ */
package org.jboss.netty.handler.timeout; package org.jboss.netty.handler.timeout;
import java.util.Set;
import org.jboss.netty.channel.ChannelEvent;
/** /**
* @author The Netty Project (netty-dev@lists.jboss.org) * @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com) * @author Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$ * @version $Rev$, $Date$
*/ */
public interface ReaderIdlenessEvent extends IdlenessEvent { public interface IdleStateEvent extends ChannelEvent {
// This is a tag interface to determine which (reader or write) is idle. Set<IdleState> getState();
long getLastActivityTimeMillis();
} }

View File

@ -22,6 +22,8 @@
*/ */
package org.jboss.netty.handler.timeout; package org.jboss.netty.handler.timeout;
import java.util.EnumSet;
import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelHandlerContext;
@ -39,7 +41,9 @@ import org.jboss.netty.util.ExternalResourceReleasable;
* @version $Rev$, $Date$ * @version $Rev$, $Date$
*/ */
@ChannelPipelineCoverage("one") @ChannelPipelineCoverage("one")
public class IdlenessHandler extends SimpleChannelUpstreamHandler implements LifeCycleAwareChannelHandler, ExternalResourceReleasable { public class IdleStateHandler extends SimpleChannelUpstreamHandler
implements LifeCycleAwareChannelHandler,
ExternalResourceReleasable {
final Timer timer; final Timer timer;
@ -53,13 +57,27 @@ public class IdlenessHandler extends SimpleChannelUpstreamHandler implements Lif
private volatile WriterIdleTimeoutTask writerIdleTimeoutTask; private volatile WriterIdleTimeoutTask writerIdleTimeoutTask;
volatile long lastWriteTime; volatile long lastWriteTime;
public IdlenessHandler( final long bothIdleTimeMillis;
Timer timer, long readerIdleTimeMillis, long writerIdleTimeMillis) { volatile Timeout bothIdleTimeout;
this(timer, readerIdleTimeMillis, writerIdleTimeMillis, TimeUnit.MILLISECONDS); private volatile BothIdleTimeoutTask bothIdleTimeoutTask;
volatile long lastIoTime;
public IdleStateHandler(
Timer timer,
long readerIdleTimeMillis,
long writerIdleTimeMillis,
long bothIdleTimeMillis) {
this(timer,
readerIdleTimeMillis, writerIdleTimeMillis, bothIdleTimeMillis,
TimeUnit.MILLISECONDS);
} }
public IdlenessHandler( public IdleStateHandler(
Timer timer, long readerIdleTime, long writerIdleTime, TimeUnit unit) { Timer timer,
long readerIdleTime, long writerIdleTime, long bothIdleTime,
TimeUnit unit) {
if (timer == null) { if (timer == null) {
throw new NullPointerException("timer"); throw new NullPointerException("timer");
} }
@ -70,6 +88,7 @@ public class IdlenessHandler extends SimpleChannelUpstreamHandler implements Lif
this.timer = timer; this.timer = timer;
readerIdleTimeMillis = unit.toMillis(readerIdleTime); readerIdleTimeMillis = unit.toMillis(readerIdleTime);
writerIdleTimeMillis = unit.toMillis(writerIdleTime); writerIdleTimeMillis = unit.toMillis(writerIdleTime);
bothIdleTimeMillis = unit.toMillis(bothIdleTime);
} }
public void releaseExternalResources() { public void releaseExternalResources() {
@ -109,7 +128,7 @@ public class IdlenessHandler extends SimpleChannelUpstreamHandler implements Lif
@Override @Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
throws Exception { throws Exception {
lastReadTime = System.currentTimeMillis(); lastReadTime = lastIoTime = System.currentTimeMillis();
ctx.sendUpstream(e); ctx.sendUpstream(e);
} }
@ -117,13 +136,13 @@ public class IdlenessHandler extends SimpleChannelUpstreamHandler implements Lif
public void writeComplete(ChannelHandlerContext ctx, WriteCompletionEvent e) public void writeComplete(ChannelHandlerContext ctx, WriteCompletionEvent e)
throws Exception { throws Exception {
if (e.getWrittenAmount() > 0) { if (e.getWrittenAmount() > 0) {
lastWriteTime = System.currentTimeMillis(); lastWriteTime = lastIoTime = System.currentTimeMillis();
} }
ctx.sendUpstream(e); ctx.sendUpstream(e);
} }
private void initialize(ChannelHandlerContext ctx) { private void initialize(ChannelHandlerContext ctx) {
lastReadTime = lastWriteTime = System.currentTimeMillis(); lastReadTime = lastWriteTime = lastIoTime = System.currentTimeMillis();
readerIdleTimeoutTask = new ReaderIdleTimeoutTask(ctx); readerIdleTimeoutTask = new ReaderIdleTimeoutTask(ctx);
writerIdleTimeoutTask = new WriterIdleTimeoutTask(ctx); writerIdleTimeoutTask = new WriterIdleTimeoutTask(ctx);
if (readerIdleTimeMillis > 0) { if (readerIdleTimeMillis > 0) {
@ -134,6 +153,10 @@ public class IdlenessHandler extends SimpleChannelUpstreamHandler implements Lif
writerIdleTimeout = timer.newTimeout( writerIdleTimeout = timer.newTimeout(
writerIdleTimeoutTask, writerIdleTimeMillis, TimeUnit.MILLISECONDS); writerIdleTimeoutTask, writerIdleTimeMillis, TimeUnit.MILLISECONDS);
} }
if (bothIdleTimeMillis > 0) {
bothIdleTimeout = timer.newTimeout(
bothIdleTimeoutTask, bothIdleTimeMillis, TimeUnit.MILLISECONDS);
}
} }
private void destroy() { private void destroy() {
@ -143,22 +166,19 @@ public class IdlenessHandler extends SimpleChannelUpstreamHandler implements Lif
if (writerIdleTimeout != null) { if (writerIdleTimeout != null) {
writerIdleTimeout.cancel(); writerIdleTimeout.cancel();
} }
if (bothIdleTimeout != null) {
bothIdleTimeout.cancel();
}
readerIdleTimeout = null; readerIdleTimeout = null;
readerIdleTimeoutTask = null; readerIdleTimeoutTask = null;
writerIdleTimeout = null; writerIdleTimeout = null;
writerIdleTimeoutTask = null; writerIdleTimeoutTask = null;
bothIdleTimeout = null;
bothIdleTimeoutTask = null;
} }
protected void onReaderIdleness(ChannelHandlerContext ctx) { protected void channelIdle(ChannelHandlerContext ctx, Set<IdleState> state, long lastActivityTimeMillis) {
ctx.sendUpstream(new DefaultReaderIdlenessEvent(ctx.getChannel())); ctx.sendUpstream(new DefaultIdleStateEvent(ctx.getChannel(), state, lastActivityTimeMillis));
}
protected void onWriterIdleness(ChannelHandlerContext ctx) {
ctx.sendUpstream(new DefaultWriterIdlenessEvent(ctx.getChannel()));
}
protected void onReaderAndWriterIdleness(ChannelHandlerContext ctx) {
ctx.sendUpstream(new DefaultReaderAndWriterIdlenessEvent(ctx.getChannel()));
} }
private final class ReaderIdleTimeoutTask implements TimerTask { private final class ReaderIdleTimeoutTask implements TimerTask {
@ -170,26 +190,18 @@ public class IdlenessHandler extends SimpleChannelUpstreamHandler implements Lif
} }
public void run(Timeout timeout) throws Exception { public void run(Timeout timeout) throws Exception {
if (timeout.isCancelled()) { if (timeout.isCancelled() || !ctx.getChannel().isOpen()) {
return;
}
if (!ctx.getChannel().isOpen()) {
return; return;
} }
long currentTime = System.currentTimeMillis(); long currentTime = System.currentTimeMillis();
long lastReadTime = IdlenessHandler.this.lastReadTime; long lastReadTime = IdleStateHandler.this.lastReadTime;
long nextDelay = readerIdleTimeMillis - (currentTime - lastReadTime); long nextDelay = readerIdleTimeMillis - (currentTime - lastReadTime);
if (nextDelay <= 0) { if (nextDelay <= 0) {
// 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);
onReaderIdleness(ctx); channelIdle(ctx, EnumSet.of(IdleState.READER_IDLE), lastReadTime);
if (currentTime - lastWriteTime >= writerIdleTimeMillis) {
// FIXME: Suppress double fire
onReaderAndWriterIdleness(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.
readerIdleTimeout = readerIdleTimeout =
@ -208,26 +220,18 @@ public class IdlenessHandler extends SimpleChannelUpstreamHandler implements Lif
} }
public void run(Timeout timeout) throws Exception { public void run(Timeout timeout) throws Exception {
if (timeout.isCancelled()) { if (timeout.isCancelled() || !ctx.getChannel().isOpen()) {
return;
}
if (!ctx.getChannel().isOpen()) {
return; return;
} }
long currentTime = System.currentTimeMillis(); long currentTime = System.currentTimeMillis();
long lastWriteTime = IdlenessHandler.this.lastWriteTime; long lastWriteTime = IdleStateHandler.this.lastWriteTime;
long nextDelay = writerIdleTimeMillis - (currentTime - lastWriteTime); long nextDelay = writerIdleTimeMillis - (currentTime - lastWriteTime);
if (nextDelay <= 0) { if (nextDelay <= 0) {
// 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);
onWriterIdleness(ctx); channelIdle(ctx, EnumSet.of(IdleState.WRITER_IDLE), lastWriteTime);
if (currentTime - lastReadTime >= readerIdleTimeMillis) {
// FIXME: Suppress double fire
onReaderAndWriterIdleness(ctx);
}
} 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 =
@ -235,4 +239,35 @@ public class IdlenessHandler extends SimpleChannelUpstreamHandler implements Lif
} }
} }
} }
private final class BothIdleTimeoutTask implements TimerTask {
private final ChannelHandlerContext ctx;
BothIdleTimeoutTask(ChannelHandlerContext ctx) {
this.ctx = ctx;
}
public void run(Timeout timeout) throws Exception {
if (timeout.isCancelled() || !ctx.getChannel().isOpen()) {
return;
}
long currentTime = System.currentTimeMillis();
long lastIoTime = IdleStateHandler.this.lastIoTime;
long nextDelay = bothIdleTimeMillis - (currentTime - lastIoTime);
if (nextDelay <= 0) {
// Both reader and writer are idle - set a new timeout and
// notify the callback.
bothIdleTimeout =
timer.newTimeout(this, bothIdleTimeMillis, TimeUnit.MILLISECONDS);
channelIdle(ctx, EnumSet.allOf(IdleState.class), lastIoTime);
} else {
// Either read or write occurred before the timeout - set a new
// timeout with shorter delay.
bothIdleTimeout =
timer.newTimeout(this, nextDelay, TimeUnit.MILLISECONDS);
}
}
}
} }

View File

@ -1,32 +0,0 @@
/*
* 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;
/**
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$
*/
public interface WriterIdlenessEvent extends IdlenessEvent {
// This is a tag interface to determine which (reader or write) is idle.
}