package com.its.op.xnettcp.client.listener;

import com.its.op.xnettcp.client.NettyTcpClientVds;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j
@RequiredArgsConstructor
public class NettyTcpClientCloseListener implements ChannelFutureListener {

    private final NettyTcpClientVds client;

    /**
     * 채널이 종료(close) 되었을 때 발생하는 이벤트 핸들러
     * @param channelFuture
     * @throws Exception
     */
    @Override
    public void operationComplete(ChannelFuture channelFuture){
        log.warn("NettyTcpClientCloseListener: {}", channelFuture.channel().toString());
        // 채널이 종료되었으므로 재 연결 스케쥴을 등록한다.
        channelFuture.channel().close();
        channelFuture.channel().eventLoop().schedule(this.client, this.client.getReconnectTime(), TimeUnit.SECONDS);
    }
}
