package com.kza.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by kza on 2015/11/10.
 */
public class KzaServer {

    private static final AtomicInteger i = new AtomicInteger(1);
    private static final int bossGroupSize = Integer.getInteger("server.boss.thread.size", Runtime.getRuntime().availableProcessors());
    private static final int workerGroupSize = Integer.getInteger("server.worker.thread.size", Runtime.getRuntime().availableProcessors() * 2);

    private static final EventLoopGroup bossGroup = new NioEventLoopGroup(bossGroupSize, new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r, "KZA-BossThread-" + i.getAndIncrement());
            t.setDaemon(true);
            return t;
        }
    });
    private static final EventLoopGroup workerGroup = new NioEventLoopGroup(workerGroupSize, new ThreadFactory() {
        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r, "KZA-WorkerThread-" + i.getAndIncrement());
            t.setDaemon(true);
            return t;
        }
    });

    public void start(final int port, final ChannelInitializer<? extends Channel> pipelineFactory) {

        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(bossGroup, workerGroup);
    }
}

@@ -94,16 +94,55 @@
 hannel;%0A
+import io.netty.channel.ChannelFuture;%0A
 import i
@@ -264,16 +264,141 @@
 pGroup;%0A
+import io.netty.channel.socket.nio.NioServerSocketChannel;%0A%0Aimport java.net.InetSocketAddress;%0Aimport java.net.SocketAddress;
 %0Aimport 
@@ -1569,12 +1569,11 @@
 lic 
-void
+int
  sta
@@ -1651,16 +1651,44 @@
 Factory)
+ throws InterruptedException
  %7B%0A%0A    
@@ -1789,17 +1789,481 @@
 erGroup)
-;
+%0A                .channel(NioServerSocketChannel.class)%0A                .childHandler(pipelineFactory);%0A%0A        ChannelFuture future = bootstrap.bind(port).sync();%0A        try %7B%0A            future = bootstrap.bind(port).sync();%0A            SocketAddress socketAddress = future.channel().localAddress();%0A            return ((InetSocketAddress) socketAddress).getPort();%0A        %7D catch (InterruptedException e) %7B%0A            throw new RuntimeException(e);%0A        %7D
 %0A    %7D%0A%7D

update server