/*
 *  Wake up element after random dozen of time services 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.TFail;
import jTMass.core.jALLL.jTObjects.TList;
import jTMass.core.jALLL.jTObjects.TObject;
import jTMass.core.jTMassPlatform.TPlatform;
import java.util.Random;

public class TSRandomWakeUp extends TASBlockingService{

    Random PRandom=new Random();

    @Override
    public boolean reviveService(){
         PPlatform.runandSet(PClient,null);
         return(finished());
    }

    @Override
    public TObject processService(TList parameters, String clientName, String serviceName){
        super.processService(parameters, clientName, serviceName);
	if(parameters!=null)
            if(parameters instanceof TList){
                if(((TList)parameters).size()==1){
                    String paramS=parameters.get(0).getContent();
                    Long param=Long.parseLong(paramS);
                    Long rand=Math.abs(PRandom.nextLong()%param)+1;
                    Long delay=rand;

                    PNextTime=generateReviveEvent(delay);
                    PPlatform.storeActiveService(this);
                    return(null);
		}
                else
                    return(new TFail());
            }
            return(null);
    }

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