1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.example.portunification;
17
18 import io.netty.bootstrap.ServerBootstrap;
19 import io.netty.channel.ChannelInitializer;
20 import io.netty.channel.EventLoopGroup;
21 import io.netty.channel.MultiThreadIoEventLoopGroup;
22 import io.netty.channel.nio.NioIoHandler;
23 import io.netty.channel.socket.SocketChannel;
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.SslContext;
28 import io.netty.handler.ssl.SslContextBuilder;
29 import io.netty.handler.ssl.util.SelfSignedCertificate;
30
31
32
33
34
35
36
37
38 public final class PortUnificationServer {
39
40 static final int PORT = Integer.parseInt(System.getProperty("port", "8080"));
41
42 public static void main(String[] args) throws Exception {
43
44 SelfSignedCertificate ssc = new SelfSignedCertificate();
45 final SslContext sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey())
46 .build();
47
48 EventLoopGroup bossGroup = new MultiThreadIoEventLoopGroup(1, NioIoHandler.newFactory());
49 EventLoopGroup workerGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
50 try {
51 ServerBootstrap b = new ServerBootstrap();
52 b.group(bossGroup, workerGroup)
53 .channel(NioServerSocketChannel.class)
54 .handler(new LoggingHandler(LogLevel.INFO))
55 .childHandler(new ChannelInitializer<SocketChannel>() {
56 @Override
57 public void initChannel(SocketChannel ch) throws Exception {
58 ch.pipeline().addLast(new PortUnificationServerHandler(sslCtx));
59 }
60 });
61
62
63 b.bind(PORT).sync().channel().closeFuture().sync();
64 } finally {
65 bossGroup.shutdownGracefully();
66 workerGroup.shutdownGracefully();
67 }
68 }
69 }