From c3d97acd030a64c71e8c653020483d817c1795b8 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Wed, 4 May 2011 16:53:31 +0900 Subject: [PATCH] Fixed a bug in the uptime example where uptime is reset even when it shouldn't be. --- .../netty/example/uptime/UptimeClientHandler.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jboss/netty/example/uptime/UptimeClientHandler.java b/src/main/java/org/jboss/netty/example/uptime/UptimeClientHandler.java index 78fad897a9..c0e404204d 100644 --- a/src/main/java/org/jboss/netty/example/uptime/UptimeClientHandler.java +++ b/src/main/java/org/jboss/netty/example/uptime/UptimeClientHandler.java @@ -24,6 +24,7 @@ import org.jboss.netty.channel.ChannelHandlerContext; import org.jboss.netty.channel.ChannelStateEvent; import org.jboss.netty.channel.ExceptionEvent; import org.jboss.netty.channel.SimpleChannelUpstreamHandler; +import org.jboss.netty.handler.timeout.ReadTimeoutException; import org.jboss.netty.util.Timeout; import org.jboss.netty.util.Timer; import org.jboss.netty.util.TimerTask; @@ -39,9 +40,11 @@ import org.jboss.netty.util.TimerTask; */ public class UptimeClientHandler extends SimpleChannelUpstreamHandler { + // We assume that we are tracking only one server in this example. + private static long startTime = -1; + final ClientBootstrap bootstrap; private final Timer timer; - private long startTime = -1; public UptimeClientHandler(ClientBootstrap bootstrap, Timer timer) { this.bootstrap = bootstrap; @@ -84,6 +87,13 @@ public class UptimeClientHandler extends SimpleChannelUpstreamHandler { startTime = -1; println("Failed to connect: " + cause.getMessage()); } + if (cause instanceof ReadTimeoutException) { + // The connection was OK but there was no traffic for last period. + println("Disconnecting due to no inbound traffic"); + } + else { + cause.printStackTrace(); + } ctx.getChannel().close(); }