All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openmuc.jeebus.ship.state.smepin.CheckBusyWait Maven / Gradle / Ivy

/*
 * Copyright 2024 Fraunhofer ISE
 *
 * This file is part of jEEBus.SHIP
 * For more information visit http://www.openmuc.org
 *
 * jEEBus.SHIP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jEEBus.SHIP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with jEEBus.SHIP. If not, see http://www.gnu.org/licenses/.
 */

package org.openmuc.jeebus.ship.state.smepin;

import com.google.common.base.Stopwatch;
import org.openmuc.jeebus.ship.message.smepin.PinInputPermissionType;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class CheckBusyWait extends Check {

    private Stopwatch penaltyTimer;

    private ScheduledExecutorService ses
        = Executors.newSingleThreadScheduledExecutor();

    private int penalty = pinCallBacks.getInvalidPins() > 5 ?
        pinCallBacks.getPenaltySixthAttempt() :
        pinCallBacks.getPenaltyThirdAttempt();

    public CheckBusyWait(SmePinSubState subState) {
        super(subState.getStateCB(), subState.pinCallBacks);
        penaltyTimer = Stopwatch.createStarted();

        // schedule next sub-state after penalty reached
        ses.schedule(this::next, penalty, TimeUnit.SECONDS);
    }

    @Override
    protected void procedure() {
        if (penalty > penaltyTimer.elapsed(TimeUnit.SECONDS)) {
            pinCallBacks.commonSendPinRequirement();
        }
        else {
            ses.shutdown();
            next();
        }
    }

    @Override
    public void setNextSubState() {
        if (!ses.isShutdown()) {
            ses.shutdownNow();
        }
        pinCallBacks.setOwnInputPermission(PinInputPermissionType.OK);
        pinCallBacks.commonSendPinRequirement();
        changeStateCB.nextSubState(new CheckListen(this));
    }

    @Override
    public String toString() {
        return "CHECK_BUSY_WAIT";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy