package com.its.vds.xnettcp.center;

import com.its.vds.xnettcp.center.codec.CenterTcpServerDecoder;
import com.its.vds.xnettcp.center.codec.CenterTcpServerEncoder;
import com.its.vds.xnettcp.center.handler.CenterTcpServerIdleStateHandler;
import com.its.vds.xnettcp.center.handler.CenterTcpServerInboundHandler;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class CenterTcpServerInitializer extends ChannelInitializer<Channel> {

    private final CenterTcpServerInboundHandler centerTcpServerInboundHandler;
    private final CenterTcpServerEncoder centerTcpServerEncoder;

    @Override
    protected void initChannel(Channel channel) throws Exception {
        CenterTcpServerIdleStateHandler idleStateHandler = new CenterTcpServerIdleStateHandler(0, 0, 0);
        ChannelPipeline pipeline = channel.pipeline();
        pipeline.addLast(new LoggingHandler(LogLevel.WARN));
        pipeline.addLast("centerServerIdleStateHandler", idleStateHandler);
        pipeline.addLast("centerServerDecoder", new CenterTcpServerDecoder());
        pipeline.addLast("centerServerInboundHandler", this.centerTcpServerInboundHandler);
        pipeline.addLast("centerServerEncoder", this.centerTcpServerEncoder);
    }
}
