package cn.com.ty.lift.ud.neety.server.handler;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.ty.lift.ud.neety.support.WebSoketSupport;

/**
 * @author chenyi
 * @date 2018/3/29 14:42
 */

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * 心跳检测
 * 
 * @author chenyi
 */

@Slf4j
public class ImIdleHandler extends IdleStateHandler implements ChannelHandler {

    private static final WebSoketSupport support = new WebSoketSupport();

    public ImIdleHandler(int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds) {
        super(readerIdleTimeSeconds, writerIdleTimeSeconds, allIdleTimeSeconds);
    }

    public ImIdleHandler(long readerIdleTime, long writerIdleTime, long allIdleTime, TimeUnit unit) {
        super(readerIdleTime, writerIdleTime, allIdleTime, unit);
        log.info("心跳检测。。。。。。。1");
    }

    /**
     * 心跳检测.超过检测时间关闭链路
     */
    @Override
    public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception {

//        log.info("心跳检测。。。。。。。2,:{}", evt.state());
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent)evt;
            if (event.state() == IdleState.READER_IDLE) {
//                support.logout(ctx.channel());
            }
        } else {
            super.userEventTriggered(ctx, evt);
        }

    }
}
