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.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  public class FactorialClient {
31  
32      private final String host;
33      private final int port;
34      private final int count;
35  
36      public FactorialClient(String host, int port, int count) {
37          this.host = host;
38          this.port = port;
39          this.count = count;
40      }
41  
42      public void run() {
43          // Configure the client.
44          ClientBootstrap bootstrap = new ClientBootstrap(
45                  new NioClientSocketChannelFactory(
46                          Executors.newCachedThreadPool(),
47                          Executors.newCachedThreadPool()));
48  
49          // Set up the event pipeline factory.
50          bootstrap.setPipelineFactory(new FactorialClientPipelineFactory(count));
51  
52          // Make a new connection.
53          ChannelFuture connectFuture =
54              bootstrap.connect(new InetSocketAddress(host, port));
55  
56          // Wait until the connection is made successfully.
57          Channel channel = connectFuture.awaitUninterruptibly().getChannel();
58  
59          // Get the handler instance to retrieve the answer.
60          FactorialClientHandler handler =
61              (FactorialClientHandler) channel.getPipeline().getLast();
62  
63          // Print out the answer.
64          System.err.format(
65                  "Factorial of %,d is: %,d", count, handler.getFactorial());
66  
67          // Shut down all thread pools to exit.
68          bootstrap.releaseExternalResources();
69      }
70  
71      public static void main(String[] args) throws Exception {
72          // Print usage if no argument is specified.
73          if (args.length != 3) {
74              System.err.println(
75                      "Usage: " + FactorialClient.class.getSimpleName() +
76                      " <host> <port> <count>");
77              return;
78          }
79  
80          // Parse options.
81          String host = args[0];
82          int port = Integer.parseInt(args[1]);
83          int count = Integer.parseInt(args[2]);
84          if (count <= 0) {
85              throw new IllegalArgumentException("count must be a positive integer.");
86          }
87  
88          new FactorialClient(host, port, count).run();
89      }
90  }