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