thufir

server

Jul 20th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. package net.bounceme.dur.netty;
  2.  
  3. import io.netty.bootstrap.ServerBootstrap;
  4. import io.netty.channel.ChannelFuture;
  5. import io.netty.channel.ChannelInitializer;
  6. import io.netty.channel.ChannelOption;
  7. import io.netty.channel.ChannelPipeline;
  8. import io.netty.channel.EventLoopGroup;
  9. import io.netty.channel.nio.NioEventLoopGroup;
  10. import io.netty.channel.socket.SocketChannel;
  11. import io.netty.channel.socket.nio.NioServerSocketChannel;
  12. import io.netty.handler.logging.LogLevel;
  13. import io.netty.handler.logging.LoggingHandler;
  14. import io.netty.handler.ssl.SslContext;
  15. import io.netty.handler.ssl.util.SelfSignedCertificate;
  16. import java.util.logging.Logger;
  17.  
  18. public final class EchoServer {
  19.  
  20.     private static final Logger log = Logger.getLogger(EchoServer.class.getName());
  21.     static boolean SSL;//System.getProperty("ssl") != null;
  22.     static int PORT;// Integer.parseInt(System.getProperty("port", "8007"));
  23.     private final MyProps p = new MyProps();
  24.  
  25.     public static void main(String... args) throws Exception {
  26.         new EchoServer().runServer();
  27.     }
  28.  
  29.     private void runServer() throws Exception {
  30.         SSL = p.getSSL();
  31.         PORT = p.getServerPort();
  32.         // Configure SSL.
  33.         final SslContext sslCtx;
  34.         if (SSL) {
  35.             SelfSignedCertificate ssc = new SelfSignedCertificate();
  36.             sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
  37.         } else {
  38.             sslCtx = null;
  39.         }
  40.  
  41.         // Configure the server.
  42.         EventLoopGroup bossGroup = new NioEventLoopGroup(1);
  43.         EventLoopGroup workerGroup = new NioEventLoopGroup();
  44.         try {
  45.             ServerBootstrap b = new ServerBootstrap();
  46.             b
  47.                     .group(bossGroup, workerGroup)
  48.                     .channel(NioServerSocketChannel.class
  49.                     )
  50.                     .option(ChannelOption.SO_BACKLOG, 100)
  51.                     .handler(new LoggingHandler(LogLevel.INFO))
  52.                     .childHandler(new ChannelInitializer<SocketChannel>() {
  53.                         @Override
  54.                         public void initChannel(SocketChannel ch) throws Exception {
  55.                             ChannelPipeline p = ch.pipeline();
  56.                             if (sslCtx != null) {
  57.                                 p.addLast(sslCtx.newHandler(ch.alloc()));
  58.                             }
  59.                             p.addLast(new LoggingHandler(LogLevel.INFO));
  60.                             p.addLast(new PingPongServerHandler());
  61.                         }
  62.                     }
  63.                     );
  64.  
  65.             // Start the server.
  66.             ChannelFuture f = b.bind(PORT).sync();
  67.  
  68.             // Wait until the server socket is closed.
  69.             f.channel().closeFuture().sync();
  70.         } finally {
  71.             // Shut down all event loops to terminate all threads.
  72.             bossGroup.shutdownGracefully();
  73.             workerGroup.shutdownGracefully();
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment