package com.mortals.xhx.daemon.applicationservice;

import com.mortals.framework.springcloud.service.IApplicationStartedService;
import com.mortals.xhx.daemon.netty.config.NettyServerConfig;
import com.mortals.xhx.daemon.netty.server.controlserver.ControlServer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.mortals.xhx.common.key.Constant.SERVER_PORT;

/**
 * netty udp 服务启动
 *
 * @author: zxfei
 * @date: 2022/4/8 11:10
 */
@Component
public class NettyStartedService implements IApplicationStartedService {

    private static Log logger = LogFactory.getLog(NettyStartedService.class);

    @Autowired
    private ControlServer controlServer;

    private Thread thread = null;


    @Override
    public void start() {
        logger.info("开始netty服务....");
        thread = new Thread(() -> {
            controlServer.run(SERVER_PORT);
        });
        thread.setDaemon(true);
        thread.start();

    }

    @Override
    public void stop() {
        logger.info("停止服务..");

        if (thread != null) {
            thread.interrupt();
        }
    }

    @Override
    public int getOrder() {
        return 10;
    }

}
