// Path: netty-http/src/main/java/http/Config.java
// public class Config {
//     public Integer getBacklog() {
//         return 1280;
//     }
// 
//     public Integer getPort() {
//         return 9999;
//     }
// 
//     public Integer getClientMaxBodySize() {
//         return 1024*1024*10;
//     }
// 
//     public Integer getTaskThreadPoolSize() {
//         return 0;
//     }
// 
//     public Integer getEventLoopThreadCount() {
//         return 10;
//     }
// }
// 
// Path: netty-http/src/main/java/http/DefaultServerInitializer.java
// public class DefaultServerInitializer extends ChannelInitializer<SocketChannel> {
//     private final Config conf;
// 
//     //private final EventExecutorGroup executor;
//     private final ExecutorService executor;
//     private final ConfigurableApplicationContext applicationContext;
// 
//     public DefaultServerInitializer(Config conf, ConfigurableApplicationContext applicationContext) {
//         this.conf = conf;
//         this.applicationContext = applicationContext;
// 
//         this.executor = new SearchWorker(conf.getTaskThreadPoolSize(), "SearchWorkder", new ArrayBlockingQueue(5));
// 
// //                new DefaultEventExecutorGroup(
// //                conf.getTaskThreadPoolSize());
//     }
// 
//     @Override
//     protected void initChannel(SocketChannel ch) throws Exception {
//         final ChannelPipeline pipeline = ch.pipeline();
//         pipeline.addLast("httpDecoder", new HttpRequestDecoder());
// 
//         pipeline.addLast("httpAggregator", new HttpObjectAggregator(conf.getClientMaxBodySize()));
//         pipeline.addLast("httpResponseEncoder", new HttpResponseEncoder());
//         pipeline.addLast("httpMyResponseHandler", new HttpSearchResponseHandler());
// 
//         pipeline.addLast("httpSearchDecoder", new SearchQueryDecoder());
// 
//         RestClient restClient = applicationContext.getBean("elasticRestClient", RestClient.class);
// 
//         pipeline.addLast("httpSearchHandler", new HttpSearchHandler(this.executor, restClient));
//     }
// }


import http.Config;
import http.DefaultServerInitializer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollServerSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.context.ConfigurableApplicationContext;



/**
 * Created by i311352 on 2/16/2017.
 */
public class HttpServerBoot {
    ConfigurableApplicationContext context;
    private Config conf = new Config();


    public HttpServerBoot(ConfigurableApplicationContext configurableApplicationContext) {
        this.context = configurableApplicationContext;
    }

    protected static boolean isEpollAvailable = false;

    static {
        isEpollAvailable = Epoll.isAvailable();
    }

    public void run() throws Exception {
        ServerBootstrap b = new ServerBootstrap();
        try {
            if (isEpollAvailable) {
                b.group(new EpollEventLoopGroup(this.conf.getEventLoopThreadCount()))
                 .channel(EpollServerSocketChannel.class);
            } else {
                b.group(new NioEventLoopGroup(this.conf.getEventLoopThreadCount()))
                 .channel(NioServerSocketChannel.class);
            }

// Path: netty-http/src/main/java/http/Config.java
// public class Config {
//     public Integer getBacklog() {
//         return 1280;
//     }
// 
//     public Integer getPort() {
//         return 9999;
//     }
// 
//     public Integer getClientMaxBodySize() {
//         return 1024*1024*10;
//     }
// 
//     public Integer getTaskThreadPoolSize() {
//         return 0;
//     }
// 
//     public Integer getEventLoopThreadCount() {
//         return 10;
//     }
// }
// 
// Path: netty-http/src/main/java/http/DefaultServerInitializer.java
// public class DefaultServerInitializer extends ChannelInitializer<SocketChannel> {
//     private final Config conf;
// 
//     //private final EventExecutorGroup executor;
//     private final ExecutorService executor;
//     private final ConfigurableApplicationContext applicationContext;
// 
//     public DefaultServerInitializer(Config conf, ConfigurableApplicationContext applicationContext) {
//         this.conf = conf;
//         this.applicationContext = applicationContext;
// 
//         this.executor = new SearchWorker(conf.getTaskThreadPoolSize(), "SearchWorkder", new ArrayBlockingQueue(5));
// 
// //                new DefaultEventExecutorGroup(
// //                conf.getTaskThreadPoolSize());
//     }
// 
//     @Override
//     protected void initChannel(SocketChannel ch) throws Exception {
//         final ChannelPipeline pipeline = ch.pipeline();
//         pipeline.addLast("httpDecoder", new HttpRequestDecoder());
// 
//         pipeline.addLast("httpAggregator", new HttpObjectAggregator(conf.getClientMaxBodySize()));
//         pipeline.addLast("httpResponseEncoder", new HttpResponseEncoder());
//         pipeline.addLast("httpMyResponseHandler", new HttpSearchResponseHandler());
// 
//         pipeline.addLast("httpSearchDecoder", new SearchQueryDecoder());
// 
//         RestClient restClient = applicationContext.getBean("elasticRestClient", RestClient.class);
// 
//         pipeline.addLast("httpSearchHandler", new HttpSearchHandler(this.executor, restClient));
//     }
// }


// Path: netty-http/src/main/java/HttpServerBoot.java
import http.Config;
import http.DefaultServerInitializer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.Epoll;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollServerSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.context.ConfigurableApplicationContext;



/**
 * Created by i311352 on 2/16/2017.
 */
public class HttpServerBoot {
    ConfigurableApplicationContext context;
    private Config conf = new Config();


    public HttpServerBoot(ConfigurableApplicationContext configurableApplicationContext) {
        this.context = configurableApplicationContext;
    }

    protected static boolean isEpollAvailable = false;

    static {
        isEpollAvailable = Epoll.isAvailable();
    }

    public void run() throws Exception {
        ServerBootstrap b = new ServerBootstrap();
        try {
            if (isEpollAvailable) {
                b.group(new EpollEventLoopGroup(this.conf.getEventLoopThreadCount()))
                 .channel(EpollServerSocketChannel.class);
            } else {
                b.group(new NioEventLoopGroup(this.conf.getEventLoopThreadCount()))
                 .channel(NioServerSocketChannel.class);
            }

            b.childHandler(new DefaultServerInitializer(conf, context))
