1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.example.discard;
17
18 import org.jboss.netty.bootstrap.ClientBootstrap;
19 import org.jboss.netty.channel.ChannelFuture;
20 import org.jboss.netty.channel.ChannelPipeline;
21 import org.jboss.netty.channel.ChannelPipelineFactory;
22 import org.jboss.netty.channel.Channels;
23 import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
24 import org.jboss.netty.handler.ssl.SslContext;
25 import org.jboss.netty.handler.ssl.util.InsecureTrustManagerFactory;
26
27 import java.net.InetSocketAddress;
28 import java.util.concurrent.Executors;
29
30
31
32
33 public final class DiscardClient {
34
35 static final boolean SSL = System.getProperty("ssl") != null;
36 static final String HOST = System.getProperty("host", "127.0.0.1");
37 static final int PORT = Integer.parseInt(System.getProperty("port", "8009"));
38 static final int SIZE = Integer.parseInt(System.getProperty("size", "256"));
39
40 public static void main(String[] args) throws Exception {
41
42 final SslContext sslCtx;
43 if (SSL) {
44 sslCtx = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
45 } else {
46 sslCtx = null;
47 }
48
49
50 ClientBootstrap bootstrap = new ClientBootstrap(
51 new NioClientSocketChannelFactory(
52 Executors.newCachedThreadPool(),
53 Executors.newCachedThreadPool()));
54
55 try {
56 bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
57 public ChannelPipeline getPipeline() {
58 ChannelPipeline p = Channels.pipeline();
59 if (sslCtx != null) {
60 p.addLast("ssl", sslCtx.newHandler(HOST, PORT));
61 }
62 p.addLast("discard", new DiscardClientHandler());
63 return p;
64 }
65 });
66
67
68 ChannelFuture future = bootstrap.connect(new InetSocketAddress(HOST, PORT));
69
70
71 future.getChannel().getCloseFuture().sync();
72 } finally {
73
74 bootstrap.releaseExternalResources();
75 }
76 }
77 }