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.proxy;
17  
18  import io.netty.bootstrap.Bootstrap;
19  import io.netty.buffer.Unpooled;
20  import io.netty.channel.Channel;
21  import io.netty.channel.ChannelFuture;
22  import io.netty.channel.ChannelFutureListener;
23  import io.netty.channel.ChannelHandlerAdapter;
24  import io.netty.channel.ChannelHandlerContext;
25  import io.netty.channel.ChannelOption;
26  
27  public class HexDumpProxyFrontendHandler extends ChannelHandlerAdapter {
28  
29      private final String remoteHost;
30      private final int remotePort;
31  
32      private volatile Channel outboundChannel;
33  
34      public HexDumpProxyFrontendHandler(String remoteHost, int remotePort) {
35          this.remoteHost = remoteHost;
36          this.remotePort = remotePort;
37      }
38  
39      @Override
40      public void channelActive(ChannelHandlerContext ctx) {
41          final Channel inboundChannel = ctx.channel();
42  
43          // Start the connection attempt.
44          Bootstrap b = new Bootstrap();
45          b.group(inboundChannel.eventLoop())
46           .channel(ctx.channel().getClass())
47           .handler(new HexDumpProxyBackendHandler(inboundChannel))
48           .option(ChannelOption.AUTO_READ, false);
49          ChannelFuture f = b.connect(remoteHost, remotePort);
50          outboundChannel = f.channel();
51          f.addListener(new ChannelFutureListener() {
52              @Override
53              public void operationComplete(ChannelFuture future) {
54                  if (future.isSuccess()) {
55                      // connection complete start to read first data
56                      inboundChannel.read();
57                  } else {
58                      // Close the connection if the connection attempt has failed.
59                      inboundChannel.close();
60                  }
61              }
62          });
63      }
64  
65      @Override
66      public void channelRead(final ChannelHandlerContext ctx, Object msg) {
67          if (outboundChannel.isActive()) {
68              outboundChannel.writeAndFlush(msg).addListener(new ChannelFutureListener() {
69                  @Override
70                  public void operationComplete(ChannelFuture future) {
71                      if (future.isSuccess()) {
72                          // was able to flush out data, start to read the next chunk
73                          ctx.channel().read();
74                      } else {
75                          future.channel().close();
76                      }
77                  }
78              });
79          }
80      }
81  
82      @Override
83      public void channelInactive(ChannelHandlerContext ctx) {
84          if (outboundChannel != null) {
85              closeOnFlush(outboundChannel);
86          }
87      }
88  
89      @Override
90      public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
91          cause.printStackTrace();
92          closeOnFlush(ctx.channel());
93      }
94  
95      /**
96       * Closes the specified channel after all queued write requests are flushed.
97       */
98      static void closeOnFlush(Channel ch) {
99          if (ch.isActive()) {
100             ch.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
101         }
102     }
103 }