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.factorial;
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.Channel;
23  import org.jboss.netty.channel.ChannelFuture;
24  import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
25  
26  /**
27   * Sends a sequence of integers to a {@link FactorialServer} to calculate
28   * the factorial of the specified integer.
29   *
30   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
31   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
32   *
33   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
34   */
35  public class FactorialClient {
36  
37      public static void main(String[] args) throws Exception {
38          // Print usage if no argument is specified.
39          if (args.length != 3) {
40              System.err.println(
41                      "Usage: " + FactorialClient.class.getSimpleName() +
42                      " <host> <port> <count>");
43              return;
44          }
45  
46          // Parse options.
47          String host = args[0];
48          int port = Integer.parseInt(args[1]);
49          int count = Integer.parseInt(args[2]);
50          if (count <= 0) {
51              throw new IllegalArgumentException("count must be a positive integer.");
52          }
53  
54          // Configure the client.
55          ClientBootstrap bootstrap = new ClientBootstrap(
56                  new NioClientSocketChannelFactory(
57                          Executors.newCachedThreadPool(),
58                          Executors.newCachedThreadPool()));
59  
60          // Set up the event pipeline factory.
61          bootstrap.setPipelineFactory(new FactorialClientPipelineFactory(count));
62  
63          // Make a new connection.
64          ChannelFuture connectFuture =
65              bootstrap.connect(new InetSocketAddress(host, port));
66  
67          // Wait until the connection is made successfully.
68          Channel channel = connectFuture.awaitUninterruptibly().getChannel();
69  
70          // Get the handler instance to retrieve the answer.
71          FactorialClientHandler handler =
72              (FactorialClientHandler) channel.getPipeline().getLast();
73  
74          // Print out the answer.
75          System.err.format(
76                  "Factorial of %,d is: %,d", count, handler.getFactorial());
77  
78          // Shut down all thread pools to exit.
79          bootstrap.releaseExternalResources();
80      }
81  }