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.MultiThreadIoEventLoopGroup;
23 import io.netty.channel.nio.NioIoHandler;
24 import io.netty.channel.socket.nio.NioServerSocketChannel;
25 import io.netty.handler.logging.LogLevel;
26 import io.netty.handler.logging.LoggingHandler;
27 import io.netty.handler.ssl.ApplicationProtocolConfig;
28 import io.netty.handler.ssl.ApplicationProtocolConfig.Protocol;
29 import io.netty.handler.ssl.ApplicationProtocolConfig.SelectedListenerFailureBehavior;
30 import io.netty.handler.ssl.ApplicationProtocolConfig.SelectorFailureBehavior;
31 import io.netty.handler.ssl.ApplicationProtocolNames;
32 import io.netty.handler.ssl.SslContext;
33 import io.netty.handler.ssl.SslContextBuilder;
34 import io.netty.pkitesting.CertificateBuilder;
35 import io.netty.pkitesting.X509Bundle;
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public final class SpdyServer {
50
51 static final int PORT = Integer.parseInt(System.getProperty("port", "8443"));
52
53 public static void main(String[] args) throws Exception {
54
55 X509Bundle ssc = new CertificateBuilder()
56 .subject("cn=localhost")
57 .setIsCertificateAuthority(true)
58 .buildSelfSigned();
59 SslContext sslCtx = SslContextBuilder.forServer(ssc.toKeyManagerFactory())
60 .applicationProtocolConfig(new ApplicationProtocolConfig(
61 Protocol.ALPN,
62
63 SelectorFailureBehavior.NO_ADVERTISE,
64
65 SelectedListenerFailureBehavior.ACCEPT,
66 ApplicationProtocolNames.SPDY_3_1,
67 ApplicationProtocolNames.HTTP_1_1))
68 .build();
69
70
71 EventLoopGroup bossGroup = new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory());
72 EventLoopGroup workerGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
73 try {
74 ServerBootstrap b = new ServerBootstrap();
75 b.option(ChannelOption.SO_BACKLOG, 1024);
76 b.group(bossGroup, workerGroup)
77 .channel(NioServerSocketChannel.class)
78 .handler(new LoggingHandler(LogLevel.INFO))
79 .childHandler(new SpdyServerInitializer(sslCtx));
80
81 Channel ch = b.bind(PORT).sync().channel();
82
83 System.err.println("Open your SPDY-enabled web browser and navigate to https://127.0.0.1:" + PORT + '/');
84 System.err.println("If using Chrome browser, check your SPDY sessions at chrome://net-internals/#spdy");
85
86 ch.closeFuture().sync();
87 } finally {
88 bossGroup.shutdownGracefully();
89 workerGroup.shutdownGracefully();
90 }
91 }
92 }