1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.example.echo;
17
18 import org.jboss.netty.bootstrap.ServerBootstrap;
19 import org.jboss.netty.channel.ChannelPipeline;
20 import org.jboss.netty.channel.ChannelPipelineFactory;
21 import org.jboss.netty.channel.Channels;
22 import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
23 import org.jboss.netty.handler.ssl.SslContext;
24 import org.jboss.netty.handler.ssl.util.SelfSignedCertificate;
25
26 import java.net.InetSocketAddress;
27 import java.util.concurrent.Executors;
28
29
30
31
32 public final class EchoServer {
33
34 static final boolean SSL = System.getProperty("ssl") != null;
35 static final int PORT = Integer.parseInt(System.getProperty("port", "8007"));
36
37 public static void main(String[] args) throws Exception {
38
39 final SslContext sslCtx;
40 if (SSL) {
41 SelfSignedCertificate ssc = new SelfSignedCertificate();
42 sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
43 } else {
44 sslCtx = null;
45 }
46
47
48 ServerBootstrap bootstrap = new ServerBootstrap(
49 new NioServerSocketChannelFactory(
50 Executors.newCachedThreadPool(),
51 Executors.newCachedThreadPool()));
52
53 bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
54 public ChannelPipeline getPipeline() {
55 ChannelPipeline p = Channels.pipeline();
56 if (sslCtx != null) {
57 p.addLast("ssl", sslCtx.newHandler());
58 }
59 p.addLast("echo", new EchoServerHandler());
60 return p;
61 }
62 });
63
64 bootstrap.setOption("child.tcpNoDelay", true);
65 bootstrap.setOption("child.receiveBufferSize", 1048576);
66 bootstrap.setOption("child.sendBufferSize", 1048576);
67
68
69 bootstrap.bind(new InetSocketAddress(PORT));
70 }
71 }