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.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.ClassResolvers;
28  import org.jboss.netty.handler.codec.serialization.ObjectDecoder;
29  import org.jboss.netty.handler.codec.serialization.ObjectEncoder;
30  
31  /**
32   * Modification of {@link EchoClient} which utilizes Java object serialization.
33   */
34  public class ObjectEchoClient {
35  
36      private final String host;
37      private final int port;
38      private final int firstMessageSize;
39  
40      public ObjectEchoClient(String host, int port, int firstMessageSize) {
41          this.host = host;
42          this.port = port;
43          this.firstMessageSize = firstMessageSize;
44      }
45  
46      public void run() {
47          // Configure the client.
48          ClientBootstrap bootstrap = new ClientBootstrap(
49                  new NioClientSocketChannelFactory(
50                          Executors.newCachedThreadPool(),
51                          Executors.newCachedThreadPool()));
52  
53          // Set up the pipeline factory.
54          bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
55              public ChannelPipeline getPipeline() throws Exception {
56                  return Channels.pipeline(
57                          new ObjectEncoder(),
58                          new ObjectDecoder(
59                                  ClassResolvers.cacheDisabled(getClass().getClassLoader())),
60                          new ObjectEchoClientHandler(firstMessageSize));
61              }
62          });
63  
64          // Start the connection attempt.
65          bootstrap.connect(new InetSocketAddress(host, port));
66      }
67  
68      public static void main(String[] args) throws Exception {
69          // Print usage if no argument is specified.
70          if (args.length < 2 || args.length > 3) {
71              System.err.println(
72                      "Usage: " + ObjectEchoClient.class.getSimpleName() +
73                      " <host> <port> [<first message size>]");
74              return;
75          }
76  
77          // Parse options.
78          final String host = args[0];
79          final int port = Integer.parseInt(args[1]);
80          final int firstMessageSize;
81  
82          if (args.length == 3) {
83              firstMessageSize = Integer.parseInt(args[2]);
84          } else {
85              firstMessageSize = 256;
86          }
87  
88          new ObjectEchoClient(host, port, firstMessageSize).run();
89      }
90  }