// Path: src/main/java/com/lefu/remote/netty/EventLoopGroupFactory.java
// public class EventLoopGroupFactory {
// 	private static final Logger log = LoggerFactory.getLogger(EventLoopGroupFactory.class);
// 	
// 	/**
// 	 * 创建 {@link NioEventLoopGroup}
// 	 * @param size
// 	 * @return
// 	 */
// 	public static NioEventLoopGroup newNioLoopGroup(int size) {
// 		NioEventLoopGroup group = null;
// 		if (size <= 0) {
// 			group = new NioEventLoopGroup();
// 		} else {
// 			group = new NioEventLoopGroup(size);
// 		}
// 		log.info("Create new NioEventLoopGroup with {} threads", group.executorCount());
// 		return group;
// 	}
// 	
// 	/**
// 	 * 创建 {@link OioEventLoopGroup}
// 	 * @param maxChannel 线程池能处理的最大 {@link Channel} 数量
// 	 * @return
// 	 */
// 	public static OioEventLoopGroup newOioLoopGroup(int maxChannel) {
// 		OioEventLoopGroup group = null;
// 		if (maxChannel <= 0) {
// 			maxChannel = 1;
// 			group = new OioEventLoopGroup(maxChannel);
// 		} else {
// 			group = new OioEventLoopGroup(maxChannel);
// 		}
// 		log.info("Create new OioEventLoopGroup with {} max channels", maxChannel);
// 		return group;
// 	}
// 	
// }


import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.lefu.remote.netty.EventLoopGroupFactory;
package com.lefu.remote.netty.server;





/**
 * {@link ServerBootstrap} 的基础配置类
 * <pre>
 * 任务类继承此类即可方便的实现Netty的服务端。
 * </pre>
 * @author jiang.li
 *
 */
public abstract class AbstractServerConfigure implements NettyServer {
	private final Logger log = LoggerFactory.getLogger(getClass());
	protected ServerBootstrap bootstrap;
	protected NioEventLoopGroup bossGroup;
	protected NioEventLoopGroup workGroup;
	protected ChannelInitializer<? extends Channel> channelInitializer;
	protected Map<Integer, Channel> serverChannels = new Hashtable<Integer, Channel>();
	protected LogLevel nettyLogLevel = LogLevel.INFO;
	
	private AtomicBoolean isStarted = new AtomicBoolean(false);
	private AtomicBoolean isShutdown = new AtomicBoolean(false);
	private int bossGroupSize = Runtime.getRuntime().availableProcessors();
	private int workGroupSize = Runtime.getRuntime().availableProcessors() * 4;
	private boolean bossGroupSetted = false;
	private boolean workGroupSetted = false;
	
	/**
	 * 配置 Parent options 和 Child options
	 * @param bootstrap
	 */
	protected abstract void options(ServerBootstrap bootstrap);
	
	@Override
	public void init() throws Exception {
		if (isStarted.get()) {
			throw new IllegalStateException("Netty server bootstrap already started!");
		}
		configure();
		if (channelInitializer == null) {
			throw new IllegalStateException("ChannelInitializer is null, unset in configure() ?");
		}
		if (!bossGroupSetted) {
			bossGroup = EventLoopGroupFactory.newNioLoopGroup(bossGroupSize);
		}
		if (!workGroupSetted) {
// Path: src/main/java/com/lefu/remote/netty/EventLoopGroupFactory.java
// public class EventLoopGroupFactory {
// 	private static final Logger log = LoggerFactory.getLogger(EventLoopGroupFactory.class);
// 	
// 	/**
// 	 * 创建 {@link NioEventLoopGroup}
// 	 * @param size
// 	 * @return
// 	 */
// 	public static NioEventLoopGroup newNioLoopGroup(int size) {
// 		NioEventLoopGroup group = null;
// 		if (size <= 0) {
// 			group = new NioEventLoopGroup();
// 		} else {
// 			group = new NioEventLoopGroup(size);
// 		}
// 		log.info("Create new NioEventLoopGroup with {} threads", group.executorCount());
// 		return group;
// 	}
// 	
// 	/**
// 	 * 创建 {@link OioEventLoopGroup}
// 	 * @param maxChannel 线程池能处理的最大 {@link Channel} 数量
// 	 * @return
// 	 */
// 	public static OioEventLoopGroup newOioLoopGroup(int maxChannel) {
// 		OioEventLoopGroup group = null;
// 		if (maxChannel <= 0) {
// 			maxChannel = 1;
// 			group = new OioEventLoopGroup(maxChannel);
// 		} else {
// 			group = new OioEventLoopGroup(maxChannel);
// 		}
// 		log.info("Create new OioEventLoopGroup with {} max channels", maxChannel);
// 		return group;
// 	}
// 	
// }


// Path: src/main/java/com/lefu/remote/netty/server/AbstractServerConfigure.java
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.lefu.remote.netty.EventLoopGroupFactory;

package com.lefu.remote.netty.server;





/**
 * {@link ServerBootstrap} 的基础配置类
 * <pre>
 * 任务类继承此类即可方便的实现Netty的服务端。
 * </pre>
 * @author jiang.li
 *
 */
public abstract class AbstractServerConfigure implements NettyServer {
	private final Logger log = LoggerFactory.getLogger(getClass());
	protected ServerBootstrap bootstrap;
	protected NioEventLoopGroup bossGroup;
	protected NioEventLoopGroup workGroup;
	protected ChannelInitializer<? extends Channel> channelInitializer;
	protected Map<Integer, Channel> serverChannels = new Hashtable<Integer, Channel>();
	protected LogLevel nettyLogLevel = LogLevel.INFO;
	
	private AtomicBoolean isStarted = new AtomicBoolean(false);
	private AtomicBoolean isShutdown = new AtomicBoolean(false);
	private int bossGroupSize = Runtime.getRuntime().availableProcessors();
	private int workGroupSize = Runtime.getRuntime().availableProcessors() * 4;
	private boolean bossGroupSetted = false;
	private boolean workGroupSetted = false;
	
	/**
	 * 配置 Parent options 和 Child options
	 * @param bootstrap
	 */
	protected abstract void options(ServerBootstrap bootstrap);
	
	@Override
	public void init() throws Exception {
		if (isStarted.get()) {
			throw new IllegalStateException("Netty server bootstrap already started!");
		}
		configure();
		if (channelInitializer == null) {
			throw new IllegalStateException("ChannelInitializer is null, unset in configure() ?");
		}
		if (!bossGroupSetted) {
			bossGroup = EventLoopGroupFactory.newNioLoopGroup(bossGroupSize);
		}
		if (!workGroupSetted) {
			workGroup = EventLoopGroupFactory.newNioLoopGroup(workGroupSize);