package com.nullspace.http; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.nullspace.server.NSServer; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; public class NSHttpServer extends NSServer { private static Logger mLogger = LoggerFactory.getLogger(NSHttpServer.class); private static String mLocalDir = "./res"; public static void SetResourceDir(String dir) { mLocalDir = dir; } public static String ResourceDir() { return mLocalDir; } public NSHttpServer(String ip, int port) { super(ip, port); } @Override protected void Run() { EventLoopGroup group1 = new NioEventLoopGroup(1); EventLoopGroup group2 = new NioEventLoopGroup(8); try { ServerBootstrap b = new ServerBootstrap(); b.group(group1, group2); b.channel(NioServerSocketChannel.class); // b.handler(new LoggingHandler(LogLevel.INFO)); b.childHandler(new NSHttpFilter()); ChannelFuture f = b.bind(mIP, mPort).sync(); f.channel().closeFuture().sync(); } catch (Exception e) { mLogger.debug("server: " + e.getMessage()); } finally { group1.shutdownGracefully(); group2.shutdownGracefully(); } } @Override public String toString() { StringBuilder build = new StringBuilder(); build.append("HTTP Server Started ...\n").append(" IP: ").append(mIP).append(" at PORT: ").append(mPort); return build.toString(); } }