Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.bounceme.dur.netty;
- import io.netty.bootstrap.ServerBootstrap;
- import io.netty.channel.ChannelFuture;
- import io.netty.channel.ChannelInitializer;
- import io.netty.channel.ChannelOption;
- import io.netty.channel.ChannelPipeline;
- import io.netty.channel.EventLoopGroup;
- import io.netty.channel.nio.NioEventLoopGroup;
- import io.netty.channel.socket.SocketChannel;
- import io.netty.channel.socket.nio.NioServerSocketChannel;
- import io.netty.handler.logging.LogLevel;
- import io.netty.handler.logging.LoggingHandler;
- import io.netty.handler.ssl.SslContext;
- import io.netty.handler.ssl.util.SelfSignedCertificate;
- import java.util.logging.Logger;
- public final class EchoServer {
- private static final Logger log = Logger.getLogger(EchoServer.class.getName());
- static boolean SSL;//System.getProperty("ssl") != null;
- static int PORT;// Integer.parseInt(System.getProperty("port", "8007"));
- private final MyProps p = new MyProps();
- public static void main(String... args) throws Exception {
- new EchoServer().runServer();
- }
- private void runServer() throws Exception {
- SSL = p.getSSL();
- PORT = p.getServerPort();
- // Configure SSL.
- final SslContext sslCtx;
- if (SSL) {
- SelfSignedCertificate ssc = new SelfSignedCertificate();
- sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
- } else {
- sslCtx = null;
- }
- // Configure the server.
- EventLoopGroup bossGroup = new NioEventLoopGroup(1);
- EventLoopGroup workerGroup = new NioEventLoopGroup();
- try {
- ServerBootstrap b = new ServerBootstrap();
- b
- .group(bossGroup, workerGroup)
- .channel(NioServerSocketChannel.class
- )
- .option(ChannelOption.SO_BACKLOG, 100)
- .handler(new LoggingHandler(LogLevel.INFO))
- .childHandler(new ChannelInitializer<SocketChannel>() {
- @Override
- public void initChannel(SocketChannel ch) throws Exception {
- ChannelPipeline p = ch.pipeline();
- if (sslCtx != null) {
- p.addLast(sslCtx.newHandler(ch.alloc()));
- }
- p.addLast(new LoggingHandler(LogLevel.INFO));
- p.addLast(new PingPongServerHandler());
- }
- }
- );
- // Start the server.
- ChannelFuture f = b.bind(PORT).sync();
- // Wait until the server socket is closed.
- f.channel().closeFuture().sync();
- } finally {
- // Shut down all event loops to terminate all threads.
- bossGroup.shutdownGracefully();
- workerGroup.shutdownGracefully();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment