1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.example.spdy.server;
17
18 import io.netty.bootstrap.ServerBootstrap;
19 import io.netty.channel.Channel;
20 import io.netty.channel.ChannelOption;
21 import io.netty.channel.EventLoopGroup;
22 import io.netty.channel.nio.NioEventLoopGroup;
23 import io.netty.channel.socket.nio.NioServerSocketChannel;
24 import io.netty.handler.logging.LogLevel;
25 import io.netty.handler.logging.LoggingHandler;
26 import io.netty.handler.ssl.ApplicationProtocolConfig;
27 import io.netty.handler.ssl.ApplicationProtocolConfig.Protocol;
28 import io.netty.handler.ssl.ApplicationProtocolConfig.SelectedListenerFailureBehavior;
29 import io.netty.handler.ssl.ApplicationProtocolConfig.SelectorFailureBehavior;
30 import io.netty.handler.ssl.ApplicationProtocolNames;
31 import io.netty.handler.ssl.SslContext;
32 import io.netty.handler.ssl.SslContextBuilder;
33 import io.netty.handler.ssl.util.SelfSignedCertificate;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public final class SpdyServer {
54
55 static final int PORT = Integer.parseInt(System.getProperty("port", "8443"));
56
57 public static void main(String[] args) throws Exception {
58
59 SelfSignedCertificate ssc = new SelfSignedCertificate();
60 SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey())
61 .applicationProtocolConfig(new ApplicationProtocolConfig(
62 Protocol.NPN,
63
64 SelectorFailureBehavior.NO_ADVERTISE,
65
66 SelectedListenerFailureBehavior.ACCEPT,
67 ApplicationProtocolNames.SPDY_3_1,
68 ApplicationProtocolNames.HTTP_1_1))
69 .build();
70
71
72 EventLoopGroup bossGroup = new NioEventLoopGroup(1);
73 EventLoopGroup workerGroup = new NioEventLoopGroup();
74 try {
75 ServerBootstrap b = new ServerBootstrap();
76 b.option(ChannelOption.SO_BACKLOG, 1024);
77 b.group(bossGroup, workerGroup)
78 .channel(NioServerSocketChannel.class)
79 .handler(new LoggingHandler(LogLevel.INFO))
80 .childHandler(new SpdyServerInitializer(sslCtx));
81
82 Channel ch = b.bind(PORT).sync().channel();
83
84 System.err.println("Open your SPDY-enabled web browser and navigate to https://127.0.0.1:" + PORT + '/');
85 System.err.println("If using Chrome browser, check your SPDY sessions at chrome://net-internals/#spdy");
86
87 ch.closeFuture().sync();
88 } finally {
89 bossGroup.shutdownGracefully();
90 workerGroup.shutdownGracefully();
91 }
92 }
93 }