1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.example.uptime;
17
18 import org.jboss.netty.bootstrap.ClientBootstrap;
19 import org.jboss.netty.channel.ChannelHandlerContext;
20 import org.jboss.netty.channel.ChannelStateEvent;
21 import org.jboss.netty.channel.ExceptionEvent;
22 import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
23 import org.jboss.netty.handler.timeout.ReadTimeoutException;
24 import org.jboss.netty.util.Timeout;
25 import org.jboss.netty.util.Timer;
26 import org.jboss.netty.util.TimerTask;
27
28 import java.net.ConnectException;
29 import java.net.InetSocketAddress;
30 import java.util.concurrent.TimeUnit;
31
32
33
34
35
36 public class UptimeClientHandler extends SimpleChannelUpstreamHandler {
37
38 final ClientBootstrap bootstrap;
39 private final Timer timer;
40 private long startTime = -1;
41
42 public UptimeClientHandler(ClientBootstrap bootstrap, Timer timer) {
43 this.bootstrap = bootstrap;
44 this.timer = timer;
45 }
46
47 InetSocketAddress getRemoteAddress() {
48 return (InetSocketAddress) bootstrap.getOption("remoteAddress");
49 }
50
51 @Override
52 public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
53 println("Disconnected from: " + getRemoteAddress());
54 }
55
56 @Override
57 public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) {
58 println("Sleeping for: " + UptimeClient.RECONNECT_DELAY + 's');
59 timer.newTimeout(new TimerTask() {
60 public void run(Timeout timeout) {
61 println("Reconnecting to: " + getRemoteAddress());
62 bootstrap.connect();
63 }
64 }, UptimeClient.RECONNECT_DELAY, TimeUnit.SECONDS);
65 }
66
67 @Override
68 public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
69 if (startTime < 0) {
70 startTime = System.currentTimeMillis();
71 }
72
73 println("Connected to: " + getRemoteAddress());
74 }
75
76 @Override
77 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
78 Throwable cause = e.getCause();
79 if (cause instanceof ConnectException) {
80 startTime = -1;
81 println("Failed to connect: " + cause.getMessage());
82 }
83 if (cause instanceof ReadTimeoutException) {
84
85 println("Disconnecting due to no inbound traffic");
86 } else {
87 cause.printStackTrace();
88 }
89 ctx.getChannel().close();
90 }
91
92 private void println(String msg) {
93 if (startTime < 0) {
94 System.err.format("[SERVER IS DOWN] %s%n", msg);
95 } else {
96 System.err.format("[UPTIME: %5ds] %s%n", (System.currentTimeMillis() - startTime) / 1000, msg);
97 }
98 }
99 }