* 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 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;
@ -32,16 +38,23 @@ import org.jboss.netty.channel.ChannelFuture;
* @author Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$
*/
public class DefaultIdlenessEvent implements IdlenessEvent {
public class DefaultIdleStateEvent implements IdleStateEvent {
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) {
throw new NullPointerException("channel");
}
if (state.isEmpty()) {
throw new IllegalArgumentException("state is empty.");
}
this.channel = channel;
this.state = Collections.unmodifiableSet(state);
this.lastActivityTimeMillis = lastActivityTimeMillis;
}
public Channel getChannel() {
@ -52,8 +65,20 @@ public class DefaultIdlenessEvent implements IdlenessEvent {
return succeededFuture(getChannel());
}
public Set<IdleState> getState() {
return state;
}
public long getLastActivityTimeMillis() {
return lastActivityTimeMillis;
}
@Override
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;
import org.jboss.netty.channel.ChannelEvent;
/**
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$
*/
public interface IdlenessEvent extends ChannelEvent {
// This is a tag interface.
public enum IdleState {
READER_IDLE,
WRITER_IDLE;
}

View File

@ -22,11 +22,16 @@
*/
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 Trustin Lee (tlee@redhat.com)
* @version $Rev$, $Date$
*/
public interface ReaderIdlenessEvent extends IdlenessEvent {
// This is a tag interface to determine which (reader or write) is idle.
public interface IdleStateEvent extends ChannelEvent {
Set<IdleState> getState();
long getLastActivityTimeMillis();
}

View File

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