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.echo;
17  
18  import org.jboss.netty.bootstrap.ClientBootstrap;
19  import org.jboss.netty.channel.ChannelFuture;
20  import org.jboss.netty.channel.ChannelPipeline;
21  import org.jboss.netty.channel.ChannelPipelineFactory;
22  import org.jboss.netty.channel.Channels;
23  import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
24  import org.jboss.netty.handler.ssl.SslContext;
25  import org.jboss.netty.handler.ssl.util.InsecureTrustManagerFactory;
26  
27  import java.net.InetSocketAddress;
28  import java.util.concurrent.Executors;
29  
30  /**
31   * Sends one message when a connection is open and echoes back any received
32   * data to the server.  Simply put, the echo client initiates the ping-pong
33   * traffic between the echo client and server by sending the first message to
34   * the server.
35   */
36  public final class EchoClient {
37  
38      static final boolean SSL = System.getProperty("ssl") != null;
39      static final String HOST = System.getProperty("host", "127.0.0.1");
40      static final int PORT = Integer.parseInt(System.getProperty("port", "8007"));
41      static final int SIZE = Integer.parseInt(System.getProperty("size", "256"));
42  
43      public static void main(String[] args) throws Exception {
44          // Configure SSL.git
45          final SslContext sslCtx;
46          if (SSL) {
47              sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
48          } else {
49              sslCtx = null;
50          }
51  
52          // Configure the bootstrap.
53          ClientBootstrap bootstrap = new ClientBootstrap(
54                  new NioClientSocketChannelFactory(
55                          Executors.newCachedThreadPool(),
56                          Executors.newCachedThreadPool()));
57  
58          try {
59              bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
60                  public ChannelPipeline getPipeline() {
61                      ChannelPipeline p = Channels.pipeline();
62                      if (sslCtx != null) {
63                          p.addLast("ssl", sslCtx.newHandler(HOST, PORT));
64                      }
65                      p.addLast("echo", new EchoClientHandler());
66                      return p;
67                  }
68              });
69              bootstrap.setOption("tcpNoDelay", true);
70              bootstrap.setOption("receiveBufferSize", 1048576);
71              bootstrap.setOption("sendBufferSize", 1048576);
72  
73              // Start the connection attempt.
74              ChannelFuture future = bootstrap.connect(new InetSocketAddress(HOST, PORT));
75  
76              // Wait until the connection is closed or the connection attempt fails.
77              future.getChannel().getCloseFuture().sync();
78          } finally {
79              // Shut down thread pools to exit.
80              bootstrap.releaseExternalResources();
81          }
82      }
83  }