package com.tsi.comm.server.tcp;

import com.tsi.comm.server.repository.TsiAlarmManager;
import com.tsi.comm.server.xnet.NettyTcpServer;
import com.tsi.comm.server.xnet.NettyUtils;
import com.tsi.comm.server.config.TsiCvimServerConfig;
import com.tsi.comm.server.tcp.codec.CvimServerByteBufMessageDecoder;
import com.tsi.comm.server.tcp.codec.CvimServerEncoder;
import com.tsi.comm.server.tcp.handler.CvimServerInboundMessageHandler;
import com.tsi.comm.server.tcp.initializer.CvimServerInitializer;
import com.tsi.common.spring.SpringUtils;
import com.tsi.common.utils.OS;
import io.netty.channel.epoll.Epoll;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Slf4j
@Service
public class TsiCvimServer extends NettyTcpServer {

    private final TsiCvimServerConfig config;

    public TsiCvimServer(TsiCvimServerConfig config) {
        super(config);
        this.config = config;
    }

    @PostConstruct
    public void initialize() {
        super.setChannelInitializer(new CvimServerInitializer(this.config));
        if (!OS.isWindows()) {
            if (!Epoll.isAvailable()) {
                log.warn("TsiCvimServer Epoll not support: {}", Epoll.unavailabilityCause().toString());
            }
        }
        if (NettyUtils.isEpollAvailable()) {
            log.info("Server is running in Linux EPOLL mode.");
        }
        else {
            log.info("Server is running in Windows NIO mode.");
        }
    }
}
