package com.mortals.xhx.daemon.applicationservice;

import com.mortals.framework.springcloud.service.IApplicationStartedService;
import com.mortals.xhx.daemon.netty.server.controlserver.ControlServer;
import lombok.extern.slf4j.Slf4j;
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
@Slf4j
public class NettyStartedService implements IApplicationStartedService {

    @Autowired
    private ControlServer controlServer;

    private Thread thread = null;


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

    }

    @Override
    public void stop() {
        log.info("netty停止服务..");
        if (thread != null) {
            thread.interrupt();
        }
        if (controlServer != null) {
            controlServer.release();
        }


    }

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

}
