/*
 *  Asynchronous platform services root class
 *
 *  2004 - 2011 Frantisek Zboril jr.
 *  Faculty of Information Technology
 *  zborilf@fit.vutbr.cz
 *  http://www.fit.vutbr.cz/~zborilf
 *
 * (C) Brno University of Technology
 * Freeware licence VUT v Brně
 * https://wis.fit.vutbr.cz/FIT/db/dir.php/dr/VZ2007/externiInformace/softwareNaWebu/Priloha2_rozh23.pdf

 */

package jTMass.core.jTMassPlatform.jTServices;

import jTMass.TMass;
import jTMass.core.jALLL.jTObjects.TList;
import jTMass.core.jALLL.jTObjects.TObject;
import jTMass.core.jTMassPlatform.TPlatform;
import jTMass.core.jTMassSimulator.jTCalendar.TCalendarEvent;

public abstract class TASAsynchronousService extends TAService{
    
    Long PNextTime=new Long(0);
    Long PFinishTime;
    String PClient;

    // ===== STORE THIS AS AN ACTIVE SERVICE =====

    void storeService(){
        PPlatform.storeActiveService(this);
    }

   // ===== STORE/READ SERVICE FINISH TIME =====

    boolean finished(){
        PFinishTime=TMass.getSimulator().getModelTime();
        return(false);
    }

    public Long getFinishTime(){
        return(PFinishTime);
    }


    // ===== RECALL ACTIVITY - NEW EVENT IN CALENDAR =====

    Long generateReviveEvent(Long delay){
        Long nextTime=TMass.getSimulator().getModelTime()+delay;
        TMass.getSimulator().getCalendar().insertEvent(
                new TCalendarEvent(PPlatform,TCalendarEvent._EventExecute,
                TMass.getSimulator().getModelTime(),nextTime,PPlatform)
        );
        return(nextTime);
    }

    // ===== RECAL/REVIVE SERVICE ACTIVITY ABSTRACT CLASS =====

    abstract public boolean reviveService(); // true - still active ;
                                             // false - finished


    // ===== CHECK MODEL TIME WITH REVOKE TIME =====


    public boolean isTime(){
        if((TMass.getSimulator().getModelTime().equals(PNextTime))||(PNextTime==0))
            return(true);
        else
            return(false); // PNextTime==0 - revive allways
    }

    @Override
    public TObject processService(TList parameters, String clientName, String serviceName) {
        super.processService(parameters, clientName, serviceName);
        PClient=clientName;
        return(null);
    }

    // ===== CONSTRUCTOR =====

    public TASAsynchronousService(TPlatform platform){
        super(platform);
    }
}