View Javadoc
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 io.netty.example.uptime;
17  
18  import io.netty.bootstrap.Bootstrap;
19  import io.netty.channel.ChannelHandler.Sharable;
20  import io.netty.channel.ChannelHandlerContext;
21  import io.netty.channel.EventLoop;
22  import io.netty.channel.SimpleChannelInboundHandler;
23  import io.netty.handler.timeout.IdleState;
24  import io.netty.handler.timeout.IdleStateEvent;
25  
26  import java.util.concurrent.TimeUnit;
27  
28  /**
29   * Keep reconnecting to the server while printing out the current uptime and
30   * connection attempt getStatus.
31   */
32  @Sharable
33  public class UptimeClientHandler extends SimpleChannelInboundHandler<Object> {
34  
35      long startTime = -1;
36  
37      @Override
38      public void channelActive(ChannelHandlerContext ctx) {
39          if (startTime < 0) {
40              startTime = System.currentTimeMillis();
41          }
42          println("Connected to: " + ctx.channel().remoteAddress());
43      }
44  
45      @Override
46      public void messageReceived(ChannelHandlerContext ctx, Object msg) {
47          // Discard received data
48      }
49  
50      @Override
51      public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
52          if (!(evt instanceof IdleStateEvent)) {
53              return;
54          }
55  
56          IdleStateEvent e = (IdleStateEvent) evt;
57          if (e.state() == IdleState.READER_IDLE) {
58              // The connection was OK but there was no traffic for last period.
59              println("Disconnecting due to no inbound traffic");
60              ctx.close();
61          }
62      }
63  
64      @Override
65      public void channelInactive(final ChannelHandlerContext ctx) {
66          println("Disconnected from: " + ctx.channel().remoteAddress());
67      }
68  
69      @Override
70      public void channelUnregistered(final ChannelHandlerContext ctx) throws Exception {
71          println("Sleeping for: " + UptimeClient.RECONNECT_DELAY + 's');
72  
73          final EventLoop loop = ctx.channel().eventLoop();
74          loop.schedule(new Runnable() {
75              @Override
76              public void run() {
77                  println("Reconnecting to: " + UptimeClient.HOST + ':' + UptimeClient.PORT);
78                  UptimeClient.connect(UptimeClient.configureBootstrap(new Bootstrap(), loop));
79              }
80          }, UptimeClient.RECONNECT_DELAY, TimeUnit.SECONDS);
81      }
82  
83      @Override
84      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
85          cause.printStackTrace();
86          ctx.close();
87      }
88  
89      void println(String msg) {
90          if (startTime < 0) {
91              System.err.format("[SERVER IS DOWN] %s%n", msg);
92          } else {
93              System.err.format("[UPTIME: %5ds] %s%n", (System.currentTimeMillis() - startTime) / 1000, msg);
94          }
95      }
96  }