package com.jiayue.ipfcst.fileupload.IEC102;

import ch.qos.logback.classic.Logger;
import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

/**
 * netty服务端初始化
 *
 * @author xsl
 * @version 3.0
 */
public class ServerFor102InitChannel extends ChannelInitializer<SocketChannel> {
  private int readerIdleTime;

  ServerFor102InitChannel(Integer readerIdleTime) {
    this.readerIdleTime = readerIdleTime;
  }

  @Override
  protected void initChannel(SocketChannel socketChannel) throws Exception {
    socketChannel.config().setAllocator(UnpooledByteBufAllocator.DEFAULT);
    socketChannel.pipeline().addLast(new IdleStateHandler(
      readerIdleTime,
      0,
      0,
      TimeUnit.SECONDS));
    //添加编解码
    socketChannel.pipeline().addLast("decoder", new ServerFor102Decoder());
    socketChannel.pipeline().addLast("encoder", new ServerFor102Encoder());
    socketChannel.pipeline().addLast(new ServerFor102TransitHandler(readerIdleTime));
  }
}
