package co.dc.aptiv.mina;


import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;


/**
 * 客户端逻辑处理类
 *
 * @author mx
 */
public class MinaAbstractHandler extends IoHandlerAdapter {

    private static final Log log = LogFactory.getLog(MinaAbstractHandler.class);

    protected MinaAbstract mina;
    protected QueryRunner queryRunner;

    public MinaAbstractHandler(MinaAbstract mina, QueryRunner queryRunner) {
        this.mina = mina;
        this.queryRunner = queryRunner;
    }

    public MinaAbstractHandler() {
    }

    /**
     * 处理收到的信息
     */
    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        log.info("接收数据:" + message);
    }

    @Override
    public void messageSent(IoSession session, Object message) throws Exception {
    }

    @Override
    public void sessionCreated(IoSession session) throws Exception {
    }

    @Override
    public void sessionOpened(IoSession session) throws Exception {
    }

    /**
     * session关闭时触发
     */
    @Override
    public void sessionClosed(IoSession session) throws Exception {
        log.info("sessionClosed ...");
    }

    @Override
    public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
        if (mina.isConnect()) {
            if (status == IdleStatus.BOTH_IDLE) {
                mina.send("");
            }
        }
    }

    @Override
    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
    }

    @Override
    public void inputClosed(IoSession session) throws Exception {
    }

}
