<commit_before>package regionalcontroller;


import java.util.Timer;
import java.util.TimerTask;

import communication.RPCCall;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import plugincore.PluginEngine;
import shared.MsgEvent;
import shared.MsgEventType;


public class AgentDiscovery {

    private Timer cleanUpTimer;
    private static RPCCall rpcc;
    private static final Logger logger = LoggerFactory.getLogger(AgentDiscovery.class);

    public AgentDiscovery() throws Exception {

        rpcc = new RPCCall();

        //cleanUpTimer = new Timer();
        //cleanUpTimer.scheduleAtFixedRate(new DiscoveryCleanUpTask(), 500, PluginEngine.config.getWatchDogTimer() * 3);
    }

    public void perfupdate(MsgEvent pe) {
        //send to controller if global WatchPerf
        //System.out.println("WatchPerf: " + pe.getParamsString());
        /*
       if(!PluginEngine.controllerChannel.updatePerf(pe))
	   {
			System.out.println("Controller : AgentDiscovery : Failed to updatePerf for WatchPerf on Controller");
	   }
	   */
    }

    public void discover(MsgEvent le) {
        try {
            String discoverString = le.getParam("src_region") + "-" + le.getParam("src_agent") + "-" + le.getParam("src_plugin");

            if (PluginEngine.discoveryMap.containsKey(discoverString)) {
                System.out.println("Discovery underway for : discoverString=" + discoverString);
            } else {

                PluginEngine.discoveryMap.put(discoverString, System.currentTimeMillis());

                if ((le.getMsgType() == MsgEventType.CONFIG) && (le.getMsgBody().equals("disabled"))) {
                    //if we see a agent enable command respond to it
                    System.out.println("Remove Node: " + le.getParamsString());
                    if (le.getParam("src_plugin") == null) //if plugin discover plugin info as well
                    {
                        PluginEngine.gdb.removeNode(le.getParam("src_region"), le.getParam("src_agent"), null);
                        System.out.println("AGENT REMOVED: Region:" + le.getParam("src_region") + " Agent:" + le.getParam("src_agent"));
                    } else {
                        PluginEngine.gdb.removeNode(le.getParam("src_region"), le.getParam("src_agent"), le.getParam("src_plugin"));
                        System.out.println("PLUGIN REMOVED: Region:" + le.getParam("src_region") + " Agent:" + le.getParam("src_agent") + " Plugin:" + le.getParam("src_plugin"));

                    }
                    //le.setReturn(); //reverse
                    //PluginEngine.msgInQueue.offer(le);
             /*
			 le.setMsgPlugin(null);
			 le.setMsgRegion(le.getParam("src_region"));
			 le.setMsgAgent(le.getParam("src_agent"));
			 le.removeParam("src_plugin");
			 le.setMsgBody("controllerdisabled");
			 le.setParam("dst_region", le.getParam("src_region"));
			 le.setParam("dst_agent", le.getParam("src_agent"));
			 le.setSrc(PluginEngine.region, PluginEngine.agent, PluginEngine.plugin);
		     //le.setDst(me.getParam("src_region"),me.getParam("src_agent"),me.getParam("src_plugin"));
			 
			 PluginEngine.msgInQueue.offer(le);
			  */
                } else if ((le.getMsgType() == MsgEventType.CONFIG) && (le.getMsgBody().equals("enabled"))) {
                    //if we see a agent enable command respond to it
                    System.out.println("AGENTDISCOVER: Region:" + le.getParam("src_region") + " Agent:" + le.getParam("src_agent"));
                    le.setMsgPlugin(null);
                    le.setMsgRegion(le.getParam("src_region"));
                    le.setMsgAgent(le.getParam("src_agent"));
                    le.removeParam("src_plugin");
                    le.setMsgBody("controllerenabled");
                    le.setParam("dst_region", le.getParam("src_region"));
                    le.setParam("dst_agent", le.getParam("src_agent"));
                    le.setSrc(PluginEngine.region, PluginEngine.agent, PluginEngine.plugin);
                    //le.setDst(me.getParam("src_region"),me.getParam("src_agent"),me.getParam("src_plugin"));
                    PluginEngine.msgInQueue.offer(le);

                } else if (le.getMsgType() == MsgEventType.WATCHDOG) {
                    long watchRunTime = 0; //determine message runtime
                    long watchTimeStamp = 0; //determine message timestamp
                    try {
                        watchRunTime = Long.parseLong(le.getParam("runtime"));
                        watchTimeStamp = Long.parseLong(le.getParam("timestamp"));
                        le.setParam("timestamp", String.valueOf(System.currentTimeMillis()));
                    } catch (Exception ex) {
                        //PluginEngine.clog.getError("Problem with WATCHDOG parameters: " + ex.toString());
                        logger.info("Controller : AgentDiscovery : Problem with WATCHDOG parameters: " + ex.toString());
                    }

                    if (!PluginEngine.gdb.isNode(le.getParam("src_region"), le.getParam("src_agent"), null)) //add if it does not exist
                    {
                        MsgEvent de;
                        if (le.getParam("src_plugin") != null) {

                            String src_plugin = le.getParam("src_plugin");
                            le.removeParam("src_plugin");
                            le.setMsgPlugin(null);
                            de = refreshCmd(le); //build the discovery message discover host

                            le.setParam("src_plugin", src_plugin);
                            le.setMsgPlugin(src_plugin);
                        } else {
                            de = refreshCmd(le); //build the discovery message discover host

                        }
                        //Adding Node: region-grzv agent-ynyt null

                        if(PluginEngine.gdb.addNode(le.getParam("src_region"), le.getParam("src_agent"), null)) {
                            logger.info("WATCHDOG DISCOVERED: Region:" + le.getParam("src_region") + " Agent:" + le.getParam("src_agent"));
                        }
                        //PluginEngine.gdb.setNodeParams(le.getParam("src_region"), le.getParam("src_agent"), null, de.getParams());


                    } else //agent already exist
                    {
                        if (le.getParam("src_plugin") == null) //update agent
                        {
                            long oldRunTime = 0l;

                            if (oldRunTime > watchRunTime) //if plugin config has been reset refresh
                            {
                                System.out.println("AGENT CONFIGRUATION RESET");
                                MsgEvent newDep = refreshCmd(le);
                                PluginEngine.gdb.setNodeParams(le.getParam("src_region"), le.getParam("src_agent"), null, newDep.getParams());
                            } else {
                                PluginEngine.gdb.setNodeParam(le.getParam("src_region"), le.getParam("src_agent"), null, "runtime", le.getParam("runtime"));
                                PluginEngine.gdb.setNodeParam(le.getParam("src_region"), le.getParam("src_agent"), null, "timestamp", le.getParam("timestamp"));
                            }
                        }
                    }

                    if (le.getParam("src_plugin") != null) //if plugin discover plugin info as well
                    {
                        if (!PluginEngine.gdb.isNode(le.getParam("src_region"), le.getParam("src_agent"), le.getParam("src_plugin"))) //agent does not exist
                        {

                            MsgEvent dep = refreshCmd(le);

                            if(PluginEngine.gdb.addNode(le.getParam("src_region"), le.getParam("src_agent"), le.getParam("src_plugin"))) {
                                System.out.println("WATCHDOG DISCOVERED: Region:" + le.getParam("src_region") + " Agent:" + le.getParam("src_agent") + " plugin:" + le.getParam("src_plugin"));
                            }
                            //PluginEngine.gdb.setNodeParams(le.getParam("src_region"), le.getParam("src_agent"), le.getParam("src_plugin"), dep.getParams());

<commit_msg>Import of existing regional controller functions<commit_after>package regionalcontroller;


import java.util.Timer;
import java.util.TimerTask;

import communication.RPCCall;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import plugincore.PluginEngine;
import shared.MsgEvent;
import shared.MsgEventType;


public class AgentDiscovery {

    private Timer cleanUpTimer;
    private static RPCCall rpcc;
    private static final Logger logger = LoggerFactory.getLogger(AgentDiscovery.class);

    public AgentDiscovery() throws Exception {

        rpcc = new RPCCall();

        //cleanUpTimer = new Timer();
        //cleanUpTimer.scheduleAtFixedRate(new DiscoveryCleanUpTask(), 500, PluginEngine.config.getWatchDogTimer() * 3);
    }

    public void perfupdate(MsgEvent pe) {
        //send to controller if global WatchPerf
        //System.out.println("WatchPerf: " + pe.getParamsString());
        /*
       if(!PluginEngine.controllerChannel.updatePerf(pe))
	   {
			System.out.println("Controller : AgentDiscovery : Failed to updatePerf for WatchPerf on Controller");
	   }
	   */
    }

    public void discover(MsgEvent le) {
        try {
            String discoverString = le.getParam("src_region") + "-" + le.getParam("src_agent") + "-" + le.getParam("src_plugin");

            if (PluginEngine.discoveryMap.containsKey(discoverString)) {
                System.out.println("Discovery underway for : discoverString=" + discoverString);
            } else {

                PluginEngine.discoveryMap.put(discoverString, System.currentTimeMillis());

                if ((le.getMsgType() == MsgEventType.CONFIG) && (le.getMsgBody().equals("disabled"))) {
                    //if we see a agent enable command respond to it
                    System.out.println("Remove Node: " + le.getParamsString());
                    if (le.getParam("src_plugin") == null) //if plugin discover plugin info as well
                    {
                        PluginEngine.gdb.removeNode(le.getParam("src_region"), le.getParam("src_agent"), null);
                        System.out.println("AGENT REMOVED: Region:" + le.getParam("src_region") + " Agent:" + le.getParam("src_agent"));
                    } else {
                        PluginEngine.gdb.removeNode(le.getParam("src_region"), le.getParam("src_agent"), le.getParam("src_plugin"));
                        System.out.println("PLUGIN REMOVED: Region:" + le.getParam("src_region") + " Agent:" + le.getParam("src_agent") + " Plugin:" + le.getParam("src_plugin"));

                    }
                    //le.setReturn(); //reverse
                    //PluginEngine.msgInQueue.offer(le);
             /*
			 le.setMsgPlugin(null);
			 le.setMsgRegion(le.getParam("src_region"));
			 le.setMsgAgent(le.getParam("src_agent"));
			 le.removeParam("src_plugin");
			 le.setMsgBody("controllerdisabled");
			 le.setParam("dst_region", le.getParam("src_region"));
			 le.setParam("dst_agent", le.getParam("src_agent"));
			 le.setSrc(PluginEngine.region, PluginEngine.agent, PluginEngine.plugin);
		     //le.setDst(me.getParam("src_region"),me.getParam("src_agent"),me.getParam("src_plugin"));
			 
			 PluginEngine.msgInQueue.offer(le);
			  */
                } else if ((le.getMsgType() == MsgEventType.CONFIG) && (le.getMsgBody().equals("enabled"))) {
                    //if we see a agent enable command respond to it
                    System.out.println("AGENTDISCOVER: Region:" + le.getParam("src_region") + " Agent:" + le.getParam("src_agent"));
                    le.setMsgPlugin(null);
                    le.setMsgRegion(le.getParam("src_region"));
                    le.setMsgAgent(le.getParam("src_agent"));
                    le.removeParam("src_plugin");
                    le.setMsgBody("controllerenabled");
                    le.setParam("dst_region", le.getParam("src_region"));
                    le.setParam("dst_agent", le.getParam("src_agent"));
                    le.setSrc(PluginEngine.region, PluginEngine.agent, PluginEngine.plugin);
                    //le.setDst(me.getParam("src_region"),me.getParam("src_agent"),me.getParam("src_plugin"));
                    PluginEngine.msgInQueue.offer(le);

                } else if (le.getMsgType() == MsgEventType.WATCHDOG) {
                    long watchRunTime = 0; //determine message runtime
                    long watchTimeStamp = 0; //determine message timestamp
                    try {
                        watchRunTime = Long.parseLong(le.getParam("runtime"));
                        watchTimeStamp = Long.parseLong(le.getParam("timestamp"));
                        le.setParam("timestamp", String.valueOf(System.currentTimeMillis()));
                    } catch (Exception ex) {
                        //PluginEngine.clog.getError("Problem with WATCHDOG parameters: " + ex.toString());
                        logger.info("Controller : AgentDiscovery : Problem with WATCHDOG parameters: " + ex.toString());
                    }

                    if (!PluginEngine.gdb.isNode(le.getParam("src_region"), le.getParam("src_agent"), null)) //add if it does not exist
                    {
                        MsgEvent de;
                        if (le.getParam("src_plugin") != null) {

                            String src_plugin = le.getParam("src_plugin");
                            le.removeParam("src_plugin");
                            le.setMsgPlugin(null);
                            de = refreshCmd(le); //build the discovery message discover host

                            le.setParam("src_plugin", src_plugin);
                            le.setMsgPlugin(src_plugin);
                        } else {
                            de = refreshCmd(le); //build the discovery message discover host

                        }
                        //Adding Node: region-grzv agent-ynyt null

                        if(PluginEngine.gdb.addNode(le.getParam("src_region"), le.getParam("src_agent"), null)) {
                            logger.info("WATCHDOG DISCOVERED: Region:" + le.getParam("src_region") + " Agent:" + le.getParam("src_agent"));
                        }
                        //PluginEngine.gdb.setNodeParams(le.getParam("src_region"), le.getParam("src_agent"), null, de.getParams());


                    } else //agent already exist
                    {
                        if (le.getParam("src_plugin") == null) //update agent
                        {
                            long oldRunTime = 0l;

                            if (oldRunTime > watchRunTime) //if plugin config has been reset refresh
                            {
                                System.out.println("AGENT CONFIGRUATION RESET");
                                MsgEvent newDep = refreshCmd(le);
                                PluginEngine.gdb.setNodeParams(le.getParam("src_region"), le.getParam("src_agent"), null, newDep.getParams());
                            } else {
                                PluginEngine.gdb.setNodeParam(le.getParam("src_region"), le.getParam("src_agent"), null, "runtime", le.getParam("runtime"));
                                PluginEngine.gdb.setNodeParam(le.getParam("src_region"), le.getParam("src_agent"), null, "timestamp", le.getParam("timestamp"));
                            }
                        }
                    }

                    if (le.getParam("src_plugin") != null) //if plugin discover plugin info as well
                    {
                        if (!PluginEngine.gdb.isNode(le.getParam("src_region"), le.getParam("src_agent"), le.getParam("src_plugin"))) //agent does not exist
                        {

                            MsgEvent dep = refreshCmd(le);

                            if(PluginEngine.gdb.addNode(le.getParam("src_region"), le.getParam("src_agent"), le.getParam("src_plugin"))) {
                                logger.info("WATCHDOG DISCOVERED: Region:" + le.getParam("src_region") + " Agent:" + le.getParam("src_agent") + " plugin:" + le.getParam("src_plugin"));
                            }
                            //PluginEngine.gdb.setNodeParams(le.getParam("src_region"), le.getParam("src_agent"), le.getParam("src_plugin"), dep.getParams());


{'fraction_non_alphanumeric': 0.1271042751604699, 'fraction_numerical': 0.001089984255782972, 'mean_word_length': 1.8547968885047537, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2008692', 'n_tokens_mistral': 4566, 'n_tokens_neox': 4238, 'n_words': 735}