/*
 *  Network managing and observing 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.jTMassModel.TModelConnection;
import jTMass.core.jTMassModel.jModelData.TModelData;
import jTMass.core.jTMassModel.jModelData.TSpeechAct;
import jTMass.core.jTMassPlatform.TPlatform;
import jTMass.core.jTMassSimulator.jTCalendar.TCalendarEvent;
import java.util.LinkedList;

/**
 * - bcs - broadcast
 * - pa - platform agents (synchronous)
 * - na - neighbour agents (deamon) - pa + facilitator
 */
public class TSNetworkServices extends TASDeamon{

    @Override
    public boolean reviveService(){
        if(TMass.getSimulator().getModelTime()<500){
            LinkedList<TSpeechAct> messages=PPlatform.getPCU().getMessages(this);
                if(!messages.isEmpty()){
                    for(TModelData data:messages)
                        if(data instanceof TSpeechAct)
                            if(((TSpeechAct)data).getAct()==TSpeechAct._SA_Inform){
                                TSpeechAct newMessage=new TSpeechAct(TSpeechAct._SA_Inform,
                                        PPlatform.getElementName(), PClient, TSpeechAct._CL_ALLL,
                                        "(connected,("+data.getSender()+","+data.getContentS()+"))");
                                PPlatform.getOutputBuffer().add(newMessage);
                                PPlatform.getInputBuffer().remove(data);
                            }
                    messages.clear();
            }
            return(true);
        }
        else
            return(super.finished());
    }

    void broadcastAskAboutAgents(){
        TSpeechAct act=new TSpeechAct(TSpeechAct._SA_Inform_Ref, PPlatform.getElementName(),
                "~broadcast~" ,TSpeechAct._CL_ALLL, "("+PPlatform.getElementName()+",connectedAgents)", this.getServiceName(), "");
        for(int i=2;i<2+PPlatform.getnoEnvironments();i++){
            TModelConnection connection=PPlatform.getOutputConnection(i);
            connection.setData(act);

            TCalendarEvent ev=new TCalendarEvent(connection.getInputElement(),TCalendarEvent._EventExecute,
                      TMass.getSimulator().getModelTime(),TMass.getSimulator().getModelTime()+1,
                      PPlatform);
            TMass.getSimulator().getCalendar().insertEvent(ev);

            connection.getInputElement();
        }
    }

    @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 service=parameters.get(0).getContent();
                    if(service.contentEquals("af")){
                        PPlatform.storeActiveService(this);
                        PPlatform.runandSet(PClient,null);
                        PNextTime=new Long(0);
                        broadcastAskAboutAgents();
                        return(null);
                    }
                }
        return(new TFail());
    }

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