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 org.jboss.netty.bootstrap.ClientBootstrap;
19 import org.jboss.netty.channel.Channel;
20 import org.jboss.netty.channel.ChannelFuture;
21 import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
22 import org.jboss.netty.handler.ssl.SslContext;
23 import org.jboss.netty.handler.ssl.util.InsecureTrustManagerFactory;
24
25 import java.net.InetSocketAddress;
26 import java.util.concurrent.Executors;
27
28
29
30
31
32 public final class FactorialClient {
33
34 static final boolean SSL = System.getProperty("ssl") != null;
35 static final String HOST = System.getProperty("host", "127.0.0.1");
36 static final int PORT = Integer.parseInt(System.getProperty("port", "8322"));
37 static final int COUNT = Integer.parseInt(System.getProperty("count", "1000"));
38
39 public static void main(String[] args) throws Exception {
40
41 final SslContext sslCtx;
42 if (SSL) {
43 sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
44 } else {
45 sslCtx = null;
46 }
47
48
49 ClientBootstrap bootstrap = new ClientBootstrap(
50 new NioClientSocketChannelFactory(
51 Executors.newCachedThreadPool(),
52 Executors.newCachedThreadPool()));
53
54 try {
55 bootstrap.setPipelineFactory(new FactorialClientPipelineFactory(sslCtx));
56
57
58 ChannelFuture connectFuture = bootstrap.connect(new InetSocketAddress(HOST, PORT));
59
60
61 Channel channel = connectFuture.sync().getChannel();
62
63
64 FactorialClientHandler handler = (FactorialClientHandler) channel.getPipeline().getLast();
65
66
67 System.err.format("Factorial of %,d is: %,d", COUNT, handler.getFactorial());
68 } finally {
69
70 bootstrap.releaseExternalResources();
71 }
72 }
73 }