package com.jpsoft.shinestar.listener;

import com.jpsoft.shinestar.config.LapiParamConfig;
import com.jpsoft.shinestar.lapi.LapiTcpServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@Slf4j
@WebListener
@Component
@Profile({"dev","test","production"})
public class LapiWebListener implements ServletContextListener {
    @Autowired
    private LapiTcpServer lapiTcpServer;

    @Autowired
    private LapiParamConfig lapiParamConfig;

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
        System.out.println("lapi始化");

        if (lapiTcpServer!=null){
            lapiTcpServer.start(lapiParamConfig.getPort());
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
        log.warn("lapi销毁");

        if (lapiTcpServer!=null){
            lapiTcpServer.stop();
        }
    }
}