Skip to content

File StopMigration.java

File List > behaviour > evals > StopMigration.java

Go to the documentation of this file

package skydata.behaviour.evals;

import java.time.Instant;
import java.util.Date;

import jade.core.behaviours.TickerBehaviour;
import jade.core.behaviours.WakerBehaviour;
import skydata.internal.agents.SKAgent;
import skydata.internal.behaviours.SKAgentBehaviour;
import skydata.internal.message.SKLMessage;

public class StopMigration extends SKAgentBehaviour {

    public StopMigration(SKAgent agent) {
        super(agent);
    }


    @Override
    public void action() {
        agent.print("[STOP_MIGRATION] started");
        int stopMigrationAfter = (int) agent.args.get("stop_migration_after");
        long remainingTime = stopMigrationAfter*1000;
        Instant now = Instant.now();
        if (agent.hasStorageObject("DATE_STOP_MIGRATION")) {
            remainingTime =  (long) agent.getStorageObject("DATE_STOP_MIGRATION") - now.toEpochMilli() + stopMigrationAfter*1000;
            if(remainingTime < 0) {
                agent.blockMigrate();
                return ;
            }
        } else {
            agent.createStorageObject("DATE_STOP_MIGRATION", now.toEpochMilli());
        }
        agent.print("[STOP_MIGRATION] block migrate in " + remainingTime + " seconds");

        WakerBehaviour wakerBehaviour = new WakerBehaviour(agent, remainingTime) {

            @Override
            protected void onWake() {
                agent.print("[STOP_MIGRATION] stop !");
                agent.blockMigrate();
            }
        };

        agent.addBehaviour(wakerBehaviour);
    }
}