package com.its.pis.xnettcp;

import com.its.app.AppUtils;
import com.its.pis.config.ProcessConfig;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Slf4j
@ToString
@Component
public class TcpServerVdsComm extends TcpServerAbstract {

    private ProcessConfig processConfig;
    private String socketType;

    @PostConstruct
    private void init() {
    }
    public void run() {
        log.info("TcpCommServer.init: Start.");
        this.processConfig = (ProcessConfig) AppUtils.getBean(ProcessConfig.class);
//        try {
//            this.serverBootstrap = new ServerBootstrap();
//            //ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
//            if (!Epoll.isAvailable()) {
//                // 윈도우 NIO 서버 실행
//                this.socketType = "NioEventLoopGroup";
//                this.acceptGroups = (EventLoopGroup) new NioEventLoopGroup(applicationConfig.getTcpThreadsAccept());
//                this.workerGroups = (EventLoopGroup) new NioEventLoopGroup(applicationConfig.getTcpThreadsWorker());
//                this.serverBootstrap.group(this.acceptGroups, this.workerGroups);
//                this.serverBootstrap.channel(NioServerSocketChannel.class);
//            } else {
//                // 리눅스 epoll 서버 실행
//                this.socketType = "EpollEventLoopGroup";
//                this.acceptGroups = (EventLoopGroup) new EpollEventLoopGroup(applicationConfig.getTcpThreadsAccept());
//                this.workerGroups = (EventLoopGroup) new EpollEventLoopGroup(applicationConfig.getTcpThreadsWorker());
//                this.serverBootstrap.group(this.acceptGroups, this.workerGroups);
//                this.serverBootstrap.channel(EpollServerSocketChannel.class);
//            }
//
//            this.serverBootstrap.option(ChannelOption.AUTO_READ, true);
//            this.serverBootstrap.option(ChannelOption.SO_BACKLOG, applicationConfig.getTcpBacklog());
//            this.serverBootstrap.option(ChannelOption.SO_RCVBUF, applicationConfig.getTcpRcvBuf());
//            this.serverBootstrap.option(ChannelOption.SO_REUSEADDR, true);
//            this.serverBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, applicationConfig.getConnectTimeoutSec()*1000);
//
//            this.serverBootstrap.childOption(ChannelOption.SO_LINGER, 0);
//            this.serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
//            this.serverBootstrap.childOption(ChannelOption.SO_REUSEADDR, true);
//            this.serverBootstrap.childOption(ChannelOption.TCP_NODELAY, true);
//            this.serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, false);
//            this.serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
//                @Override
//                public void initChannel(SocketChannel ch) throws Exception {
//                    ch.pipeline().addLast("tcpServerIdleStateHandler",  (ChannelHandler)new TcpServerIdleStateHandler(0, 0, applicationConfig.getAllIdleTime(), TimeUnit.SECONDS));
//                    //ch.pipeline().addLast("tcpServerConnectionHandler", (ChannelHandler)new TcpServerConnectionHandler());  // Connection handler
//                    ch.pipeline().addLast("tcpServerEncoder",           (ChannelHandler)new TcpServerEncoder());            // Encoding handler
//                    ch.pipeline().addLast("tcpServerDecoder",           (ChannelHandler)new TcpServerDecoder());            // Decoding handler
//                    ch.pipeline().addLast("tcpServerPacketHandler",     (ChannelHandler)new TcpServerPacketHandler());      // Packet handler
//                }
//            });
//        }
//        catch(Exception e) {
//            this.log.error("TcpCommServer.init: Exception: {}", e.toString());
//        }
//
//        log.info("===============================================================");
//        log.info("=   VDS Communication Server Start.............................");
//        log.info("=   bindAddress: {}", this.applicationConfig.getTcpBindAddress());
//        log.info("=      bindPort: {}", this.applicationConfig.getTcpBindPort());
//        log.info("=       backlog: {}", this.applicationConfig.getTcpBacklog());
//        log.info("= acceptThreads: {}", this.applicationConfig.getTcpThreadsAccept());
//        log.info("= workerThreads: {}", this.applicationConfig.getTcpThreadsWorker());
//        log.info("=   dataThreads: {}", this.applicationConfig.getServerDataThreads());
//        log.info("===============================================================");
//
//        ChannelFuture future = null;
//        try {
//            if (this.applicationConfig.getTcpBindAddress().equals("0.0.0.0")) {
//                future = this.serverBootstrap.bind(this.applicationConfig.getTcpBindPort());
//            }
//            else {
//                future = this.serverBootstrap.bind(this.applicationConfig.getTcpBindAddress(), applicationConfig.getTcpBindPort());
//            }
//            this.channelFuture = future;
//        }
//        catch (Exception e) {
//            this.log.error("{}", e);
//            this.acceptGroups.shutdownGracefully();
//            this.workerGroups.shutdownGracefully();
//        }
        log.info("TcpCommServer.init. ..End. {}", toString());
    }

}
