View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * 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.objectecho;
17  
18  import java.net.InetSocketAddress;
19  import java.util.concurrent.Executors;
20  
21  import org.jboss.netty.bootstrap.ClientBootstrap;
22  import org.jboss.netty.channel.ChannelPipeline;
23  import org.jboss.netty.channel.ChannelPipelineFactory;
24  import org.jboss.netty.channel.Channels;
25  import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
26  import org.jboss.netty.example.echo.EchoClient;
27  import org.jboss.netty.handler.codec.serialization.ObjectDecoder;
28  import org.jboss.netty.handler.codec.serialization.ObjectEncoder;
29  
30  /**
31   * Modification of {@link EchoClient} which utilizes Java object serialization.
32   *
33   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
34   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
35   *
36   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
37   */
38  public class ObjectEchoClient {
39  
40      public static void main(String[] args) throws Exception {
41          // Print usage if no argument is specified.
42          if (args.length < 2 || args.length > 3) {
43              System.err.println(
44                      "Usage: " + ObjectEchoClient.class.getSimpleName() +
45                      " <host> <port> [<first message size>]");
46              return;
47          }
48  
49          // Parse options.
50          final String host = args[0];
51          final int port = Integer.parseInt(args[1]);
52          final int firstMessageSize;
53  
54          if (args.length == 3) {
55              firstMessageSize = Integer.parseInt(args[2]);
56          } else {
57              firstMessageSize = 256;
58          }
59  
60          // Configure the client.
61          ClientBootstrap bootstrap = new ClientBootstrap(
62                  new NioClientSocketChannelFactory(
63                          Executors.newCachedThreadPool(),
64                          Executors.newCachedThreadPool()));
65  
66          // Set up the pipeline factory.
67          bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
68              public ChannelPipeline getPipeline() throws Exception {
69                  return Channels.pipeline(
70                          new ObjectEncoder(),
71                          new ObjectDecoder(),
72                          new ObjectEchoClientHandler(firstMessageSize));
73              }
74          });
75  
76          // Start the connection attempt.
77          bootstrap.connect(new InetSocketAddress(host, port));
78      }
79  }