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 org.jboss.netty.example.proxy;
17  
18  import java.net.InetSocketAddress;
19  import java.util.concurrent.Executor;
20  import java.util.concurrent.Executors;
21  
22  import org.jboss.netty.bootstrap.ServerBootstrap;
23  import org.jboss.netty.channel.socket.ClientSocketChannelFactory;
24  import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
25  import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
26  
27  public class HexDumpProxy {
28  
29      private final int localPort;
30      private final String remoteHost;
31      private final int remotePort;
32  
33      public HexDumpProxy(int localPort, String remoteHost, int remotePort) {
34          this.localPort = localPort;
35          this.remoteHost = remoteHost;
36          this.remotePort = remotePort;
37      }
38  
39      public void run() {
40          System.err.println(
41                  "Proxying *:" + localPort + " to " +
42                  remoteHost + ':' + remotePort + " ...");
43  
44          // Configure the bootstrap.
45          Executor executor = Executors.newCachedThreadPool();
46          ServerBootstrap sb = new ServerBootstrap(
47                  new NioServerSocketChannelFactory(executor, executor));
48  
49          // Set up the event pipeline factory.
50          ClientSocketChannelFactory cf =
51                  new NioClientSocketChannelFactory(executor, executor);
52  
53          sb.setPipelineFactory(
54                  new HexDumpProxyPipelineFactory(cf, remoteHost, remotePort));
55  
56          // Start up the server.
57          sb.bind(new InetSocketAddress(localPort));
58      }
59  
60      public static void main(String[] args) throws Exception {
61          // Validate command line options.
62          if (args.length != 3) {
63              System.err.println(
64                      "Usage: " + HexDumpProxy.class.getSimpleName() +
65                      " <local port> <remote host> <remote port>");
66              return;
67          }
68  
69          // Parse command line options.
70          int localPort = Integer.parseInt(args[0]);
71          String remoteHost = args[1];
72          int remotePort = Integer.parseInt(args[2]);
73  
74          new HexDumpProxy(localPort, remoteHost, remotePort).run();
75      }
76  }