// Path: zing-common/src/main/java/org/zxc/zing/common/handler/NettyDecoder.java
// public class NettyDecoder extends ByteToMessageDecoder{
// 
//     private static final Logger log = LoggerFactory.getLogger(NettyDecoder.class);
// 
//     @Override
//     protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
// 
//         log.info("try decode:"+in.toString());
// 
//         if (in.readableBytes() < 4) {
//             log.info("no enough readable bytes");
//             return;
//         }
// 
//         int dataLength = in.readInt();
//         if (dataLength < 0) {
//             ctx.close();
//         }
// 
//         log.info("try decode data length:"+dataLength);
// 
//         if (in.readableBytes() < dataLength) {
//             in.resetReaderIndex();
//         }
// 
//         log.info("try decode doDecode");
// 
//         byte[] data = new byte[dataLength];
//         in.readBytes(data);
// 
//         Object deserialized = Serializer.deserialize(data);
//         out.add(deserialized);
// 
//     }
// }
// 
// Path: zing-common/src/main/java/org/zxc/zing/common/handler/NettyEncoder.java
// public class NettyEncoder extends MessageToByteEncoder {
// 
//     private static final Logger log = LoggerFactory.getLogger(NettyEncoder.class);
// 
//     @Override
//     protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
// 
//         log.info("encoding start");
// 
//         byte[] bytes = Serializer.serialize(msg);
//         out.writeInt(bytes.length);
//         out.writeBytes(bytes);
// 
//         log.info("msg encoded length:"+bytes.length);
// 
//     }
// }


import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zxc.zing.common.handler.NettyDecoder;
import org.zxc.zing.common.handler.NettyEncoder;
package org.zxc.zing.server.remote;


/**
 * Created by xuanchen.zhao on 16-1-17.
 */
public class NettyServer {

    private static final Logger log = LoggerFactory.getLogger(NettyServer.class);

    private static volatile boolean started = false;

    public static void start(int port) {
        if (!started) {
            synchronized (NettyServer.class) {
                if (!started) {
                    EventLoopGroup bossGroup = new NioEventLoopGroup();
                    EventLoopGroup workerGroup = new NioEventLoopGroup();

                    ServerBootstrap bootstrap = new ServerBootstrap();
                    bootstrap.group(bossGroup, workerGroup)
                            .channel(NioServerSocketChannel.class)
                            .childHandler(new ChannelInitializer<SocketChannel>() {
                                @Override
                                protected void initChannel(SocketChannel ch) throws Exception {
                                    ch.pipeline().addLast(new NettyDecoder(), new NettyEncoder(), new NettyServerHandler());
                                }
                            }).option(ChannelOption.SO_BACKLOG, 128)
                            .childOption(ChannelOption.SO_KEEPALIVE, true);

// Path: zing-common/src/main/java/org/zxc/zing/common/handler/NettyDecoder.java
// public class NettyDecoder extends ByteToMessageDecoder{
// 
//     private static final Logger log = LoggerFactory.getLogger(NettyDecoder.class);
// 
//     @Override
//     protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
// 
//         log.info("try decode:"+in.toString());
// 
//         if (in.readableBytes() < 4) {
//             log.info("no enough readable bytes");
//             return;
//         }
// 
//         int dataLength = in.readInt();
//         if (dataLength < 0) {
//             ctx.close();
//         }
// 
//         log.info("try decode data length:"+dataLength);
// 
//         if (in.readableBytes() < dataLength) {
//             in.resetReaderIndex();
//         }
// 
//         log.info("try decode doDecode");
// 
//         byte[] data = new byte[dataLength];
//         in.readBytes(data);
// 
//         Object deserialized = Serializer.deserialize(data);
//         out.add(deserialized);
// 
//     }
// }
// 
// Path: zing-common/src/main/java/org/zxc/zing/common/handler/NettyEncoder.java
// public class NettyEncoder extends MessageToByteEncoder {
// 
//     private static final Logger log = LoggerFactory.getLogger(NettyEncoder.class);
// 
//     @Override
//     protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
// 
//         log.info("encoding start");
// 
//         byte[] bytes = Serializer.serialize(msg);
//         out.writeInt(bytes.length);
//         out.writeBytes(bytes);
// 
//         log.info("msg encoded length:"+bytes.length);
// 
//     }
// }


// Path: zing-server/src/main/java/org/zxc/zing/server/remote/NettyServer.java
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zxc.zing.common.handler.NettyDecoder;
import org.zxc.zing.common.handler.NettyEncoder;

package org.zxc.zing.server.remote;


/**
 * Created by xuanchen.zhao on 16-1-17.
 */
public class NettyServer {

    private static final Logger log = LoggerFactory.getLogger(NettyServer.class);

    private static volatile boolean started = false;

    public static void start(int port) {
        if (!started) {
            synchronized (NettyServer.class) {
                if (!started) {
                    EventLoopGroup bossGroup = new NioEventLoopGroup();
                    EventLoopGroup workerGroup = new NioEventLoopGroup();

                    ServerBootstrap bootstrap = new ServerBootstrap();
                    bootstrap.group(bossGroup, workerGroup)
                            .channel(NioServerSocketChannel.class)
                            .childHandler(new ChannelInitializer<SocketChannel>() {
                                @Override
                                protected void initChannel(SocketChannel ch) throws Exception {
                                    ch.pipeline().addLast(new NettyDecoder(), new NettyEncoder(), new NettyServerHandler());
                                }
                            }).option(ChannelOption.SO_BACKLOG, 128)
                            .childOption(ChannelOption.SO_KEEPALIVE, true);

                    try {