1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
28
29
30
31
32
33
34
35 public class FactorialClient {
36
37 public static void main(String[] args) throws Exception {
38
39 if (args.length != 3) {
40 System.err.println(
41 "Usage: " + FactorialClient.class.getSimpleName() +
42 " <host> <port> <count>");
43 return;
44 }
45
46
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
55 ClientBootstrap bootstrap = new ClientBootstrap(
56 new NioClientSocketChannelFactory(
57 Executors.newCachedThreadPool(),
58 Executors.newCachedThreadPool()));
59
60
61 bootstrap.setPipelineFactory(new FactorialClientPipelineFactory(count));
62
63
64 ChannelFuture connectFuture =
65 bootstrap.connect(new InetSocketAddress(host, port));
66
67
68 Channel channel = connectFuture.awaitUninterruptibly().getChannel();
69
70
71 FactorialClientHandler handler =
72 (FactorialClientHandler) channel.getPipeline().getLast();
73
74
75 System.err.format(
76 "Factorial of %,d is: %,d", count, handler.getFactorial());
77
78
79 bootstrap.releaseExternalResources();
80 }
81 }