package _VisualDVM.Passes.All;
import _VisualDVM.Passes.Server.ComponentsRepositoryPass;
import _VisualDVM.Repository.Server.ServerCode;
import _VisualDVM.Repository.Server.ServerExchangeUnit_2021;

import java.net.ConnectException;
public class PingComponentsServer extends ComponentsRepositoryPass {
    boolean flag;
    boolean silent;
    @Override
    protected boolean canStart(Object... args) throws Exception {
        flag = false;
        silent = args.length > 0 ? (boolean) args[0] : false;
        return super.canStart(args);
    }
    @Override
    protected void ServerAction() throws Exception {
        Command(new ServerExchangeUnit_2021(ServerCode.Ping));
        flag = true;
    }
    @Override
    protected boolean validate() {
        return flag;
    }
    @Override
    protected void CheckException(Exception ex) {
        if (silent) {
            Throwable cause = getCauseRec(ex);
            if ((cause instanceof ConnectException) && cause.getMessage().contains("Connection refused: connect")) {
                //так как это пинг, игнорируем.
                return;
            }
        }
        super.CheckException(ex);
    }
}