package com.netease.mingle.rpc.server;

import com.netease.mingle.rpc.shared.HttpChannelInitializer;
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.nio.NioEventLoop;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.codec.http.HttpServerCodec;

/**
 * Created by Michael Jiang on 2016/11/27.
 */
public class RpcServer {
    private int servicePort;

    public RpcServer(int servicePort) {
        this.servicePort = servicePort;
    }

    public void start() {
        NioEventLoopGroup group = new NioEventLoopGroup();
        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(group).channel(NioServerSocketChannel.class)
                .localAddress(servicePort)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast("decoder", new HttpRequestDecoder());
                        pipeline.addLast("codec", new HttpServerCodec());
                        pipeline.addLast("aggregator", new HttpObjectAggregator(64 * 1024));
                        pipeline.addLast(new ServiceHandler());
                        pipeline.addLast("encoder", new HttpResponseEncoder());
                    }
                })
                .option(ChannelOption.SO_BACKLOG, 128)
                .childOption(ChannelOption.SO_KEEPALIVE, true);

        try {
            ChannelFuture future = bootstrap.bind().sync();
            System.out.println("Rpc Server is started and listening on port:" + servicePort);
            future.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }
    }

    public static void main(String[] args) {
        RpcServer rpcServer = new RpcServer(1616);
        rpcServer.start();
    }
}

@@ -37,69 +37,8 @@
 r;%0A%0A
-import com.netease.mingle.rpc.shared.HttpChannelInitializer;%0A
 impo
@@ -243,50 +243,8 @@
 ne;%0A
-import io.netty.channel.nio.NioEventLoop;%0A
 impo
@@ -616,16 +616,30 @@
 c;%0A%0A/**%0A
+ * RPC Server%0A
  * Creat

add rpc