查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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   * Keep reconnecting to the server while printing out the current uptime and
34   * connection attempt status.
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              // The connection was OK but there was no traffic for last period.
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  }