package com.jay.monitor.data.server;

import com.alipay.remoting.ConnectionEventProcessor;
import com.alipay.remoting.ConnectionEventType;
import com.alipay.remoting.rpc.RpcServer;
import com.alipay.remoting.rpc.protocol.UserProcessor;
import com.jay.monitor.data.core.enums.MonitorDataConstants;

import java.util.List;

/**
 * 监控数据服务接收器
 *
 * @author ： liukx
 * @time ： 2020/8/26 - 14:41
 */
public class MonitorDataServer {

    /**
     * port
     */
    private int port = MonitorDataConstants.DEFAULT_PORT;

    /**
     * rpc server
     */
    private RpcServer server;

    public MonitorDataServer(int port) {
        this.port = port;
        this.server = new RpcServer(this.port);
    }

    public MonitorDataServer(int port, boolean manageFeatureEnabled) {
        this.port = port;
        this.server = new RpcServer(this.port, manageFeatureEnabled);
    }

    public MonitorDataServer(int port, boolean manageFeatureEnabled, boolean syncStop) {
        this.port = port;
        this.server = new RpcServer(this.port, manageFeatureEnabled, syncStop);
    }

    public boolean start() {
        this.server.startup();
        return true;
    }

    public void stop() {
        this.server.shutdown();
    }

    public RpcServer getRpcServer() {
        return this.server;
    }

    public void registerUserProcessor(UserProcessor<?> processor) {
        this.server.registerUserProcessor(processor);
    }

    public void registerUserProcessor(List<UserProcessor<?>> processorList) {
        for (UserProcessor<?> userProcessor : processorList) {
            registerUserProcessor(userProcessor);
        }
    }

    public void addConnectionEventProcessor(ConnectionEventType type,
                                            ConnectionEventProcessor processor) {
        this.server.addConnectionEventProcessor(type, processor);
    }

    public int getPort() {
        return port;
    }
}
