<commit_before>/* 
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package net.grinder;

import net.grinder.AgentDaemon.AgentShutDownListener;
import net.grinder.common.GrinderException;
import net.grinder.common.GrinderProperties;
import net.grinder.communication.*;
import net.grinder.engine.agent.Agent;
import net.grinder.engine.common.AgentControllerConnectorFactory;
import net.grinder.engine.communication.AgentControllerServerListener;
import net.grinder.engine.communication.AgentDownloadGrinderMessage;
import net.grinder.engine.communication.AgentUpdateGrinderMessage;
import net.grinder.engine.communication.LogReportGrinderMessage;
import net.grinder.engine.controller.AgentControllerIdentityImplementation;
import net.grinder.message.console.AgentControllerProcessReportMessage;
import net.grinder.message.console.AgentControllerState;
import net.grinder.messages.agent.StartGrinderMessage;
import net.grinder.messages.console.AgentAddress;
import net.grinder.util.LogCompressUtils;
import net.grinder.util.NetworkUtils;
import net.grinder.util.thread.Condition;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ArrayUtils;
import org.ngrinder.common.constants.AgentConstants;
import org.ngrinder.common.constants.InternalConstants;
import org.ngrinder.common.util.CRC32ChecksumUtils;
import org.ngrinder.infra.AgentConfig;
import org.ngrinder.monitor.collector.SystemDataCollector;
import org.ngrinder.monitor.controller.model.SystemDataModel;
import org.ngrinder.monitor.share.domain.SystemInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FilenameFilter;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;

import static org.ngrinder.common.constants.InternalConstants.PROP_INTERNAL_NGRINDER_VERSION;
import static org.ngrinder.common.util.NoOp.noOp;
import static org.ngrinder.common.util.Preconditions.checkNotNull;

/**
 * Agent Controller which handles agent start and stop.
 *
 * @author <NAME>
 * @since 3.0
 */
public class AgentController implements Agent, AgentConstants {

	private static final Logger LOGGER = LoggerFactory.getLogger("agent controller");
	private final AgentConfig agentConfig;

	private Timer m_timer;
	private final Condition m_eventSynchronisation = new Condition();
	private final AgentControllerIdentityImplementation m_agentIdentity;
	private final AgentControllerServerListener m_agentControllerServerListener;
	private FanOutStreamSender m_fanOutStreamSender;
	private final AgentControllerConnectorFactory m_connectorFactory = new AgentControllerConnectorFactory(
			ConnectionType.AGENT);
	private final Condition m_eventSyncCondition;
	private volatile AgentControllerState m_state = AgentControllerState.STARTED;

	private SystemDataCollector agentSystemDataCollector = new SystemDataCollector();

	private int m_connectionPort = 0;

	private static SystemDataModel emptySystemDataModel = new SystemDataModel();

	private AgentUpdateHandler agentUpdateHandler;

	private int retryCount = 0;

	private String version;

	/**
	 * Constructor.
	 *
	 * @param eventSyncCondition event sync condition to wait until agent start to run.
	 */
	public AgentController(Condition eventSyncCondition, AgentConfig agentConfig) throws GrinderException {
		this.m_eventSyncCondition = eventSyncCondition;

		this.agentConfig = agentConfig;
		this.version = agentConfig.getInternalProperties().getProperty(PROP_INTERNAL_NGRINDER_VERSION);
		this.m_agentControllerServerListener = new AgentControllerServerListener(m_eventSynchronisation, LOGGER);
		// Set it with the default name
		final InetAddress addressWithSocket = NetworkUtils.getAddressWithSocket(agentConfig.getControllerIP(), agentConfig.getControllerPort());
		this.m_agentIdentity = new AgentControllerIdentityImplementation(agentConfig.getAgentHostID(),
				addressWithSocket == null ? NetworkUtils.DEFAULT_LOCAL_HOST_ADDRESS : addressWithSocket.getHostAddress());
		this.m_agentIdentity.setRegion(agentConfig.getRegion());
		this.agentSystemDataCollector = new SystemDataCollector();
		this.agentSystemDataCollector.setAgentHome(agentConfig.getHome().getDirectory());
		this.agentSystemDataCollector.refresh();
	}


	/**
	 * Run the agent controller.
	 *
	 * @throws GrinderException occurs when the test execution is failed.
	 */
	public void run() throws GrinderException {
		synchronized (m_eventSyncCondition) {
			m_eventSyncCondition.notifyAll();
		}

		StartGrinderMessage startMessage = null;
		ConsoleCommunication consoleCommunication = null;
		m_fanOutStreamSender = new FanOutStreamSender(GrinderConstants.AGENT_CONTROLLER_FANOUT_STREAM_THREAD_COUNT);
		m_timer = new Timer(false);
		AgentDaemon agent = new AgentDaemon(checkNotNull(agentConfig,
				"agent.conf should be provided before agent daemon start."));
		try {
			while (true) {
				do {
					if (consoleCommunication == null) {
						final Connector connector = m_connectorFactory.create(agentConfig.getControllerIP(), agentConfig.getControllerPort());
						try {
							consoleCommunication = new ConsoleCommunication(connector);<commit_msg>[NGRINDER-703] Fix bug<commit_after>/* 
 * Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package net.grinder;

import net.grinder.AgentDaemon.AgentShutDownListener;
import net.grinder.common.GrinderException;
import net.grinder.common.GrinderProperties;
import net.grinder.communication.*;
import net.grinder.engine.agent.Agent;
import net.grinder.engine.common.AgentControllerConnectorFactory;
import net.grinder.engine.communication.AgentControllerServerListener;
import net.grinder.engine.communication.AgentDownloadGrinderMessage;
import net.grinder.engine.communication.AgentUpdateGrinderMessage;
import net.grinder.engine.communication.LogReportGrinderMessage;
import net.grinder.engine.controller.AgentControllerIdentityImplementation;
import net.grinder.message.console.AgentControllerProcessReportMessage;
import net.grinder.message.console.AgentControllerState;
import net.grinder.messages.agent.StartGrinderMessage;
import net.grinder.messages.console.AgentAddress;
import net.grinder.util.LogCompressUtils;
import net.grinder.util.NetworkUtils;
import net.grinder.util.thread.Condition;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ArrayUtils;
import org.ngrinder.common.constants.AgentConstants;
import org.ngrinder.common.util.CRC32ChecksumUtils;
import org.ngrinder.infra.AgentConfig;
import org.ngrinder.monitor.collector.SystemDataCollector;
import org.ngrinder.monitor.controller.model.SystemDataModel;
import org.ngrinder.monitor.share.domain.SystemInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;

import static org.ngrinder.common.constants.InternalConstants.PROP_INTERNAL_NGRINDER_VERSION;
import static org.ngrinder.common.util.NoOp.noOp;
import static org.ngrinder.common.util.Preconditions.checkNotNull;

/**
 * Agent Controller which handles agent start and stop.
 *
 * @author <NAME>
 * @since 3.0
 */
public class AgentController implements Agent, AgentConstants {

	private static final Logger LOGGER = LoggerFactory.getLogger("agent controller");
	private final AgentConfig agentConfig;

	private Timer m_timer;
	private final Condition m_eventSynchronisation = new Condition();
	private final AgentControllerIdentityImplementation m_agentIdentity;
	private final AgentControllerServerListener m_agentControllerServerListener;
	private FanOutStreamSender m_fanOutStreamSender;
	private final AgentControllerConnectorFactory m_connectorFactory = new AgentControllerConnectorFactory(
			ConnectionType.AGENT);
	private final Condition m_eventSyncCondition;
	private volatile AgentControllerState m_state = AgentControllerState.STARTED;

	private SystemDataCollector agentSystemDataCollector = new SystemDataCollector();

	private int m_connectionPort = 0;

	private static SystemDataModel emptySystemDataModel = new SystemDataModel();

	private AgentUpdateHandler agentUpdateHandler;

	private int retryCount = 0;

	private String version;

	/**
	 * Constructor.
	 *
	 * @param eventSyncCondition event sync condition to wait until agent start to run.
	 */
	public AgentController(Condition eventSyncCondition, AgentConfig agentConfig) throws GrinderException {
		this.m_eventSyncCondition = eventSyncCondition;

		this.agentConfig = agentConfig;
		this.version = agentConfig.getInternalProperties().getProperty(PROP_INTERNAL_NGRINDER_VERSION);
		this.m_agentControllerServerListener = new AgentControllerServerListener(m_eventSynchronisation, LOGGER);
		// Set it with the default name
		this.m_agentIdentity = new AgentControllerIdentityImplementation(agentConfig.getAgentHostID(), NetworkUtils.DEFAULT_LOCAL_HOST_ADDRESS);
		this.m_agentIdentity.setRegion(agentConfig.getRegion());
		this.agentSystemDataCollector = new SystemDataCollector();
		this.agentSystemDataCollector.setAgentHome(agentConfig.getHome().getDirectory());
		this.agentSystemDataCollector.refresh();
	}


	/**
	 * Run the agent controller.
	 *
	 * @throws GrinderException occurs when the test execution is failed.
	 */
	public void run() throws GrinderException {
		synchronized (m_eventSyncCondition) {
			m_eventSyncCondition.notifyAll();
		}

		StartGrinderMessage startMessage = null;
		ConsoleCommunication consoleCommunication = null;
		m_fanOutStreamSender = new FanOutStreamSender(GrinderConstants.AGENT_CONTROLLER_FANOUT_STREAM_THREAD_COUNT);
		m_timer = new Timer(false);
		AgentDaemon agent = new AgentDaemon(checkNotNull(agentConfig,
				"agent.conf should be provided before agent daemon start."));
		try {
			while (true) {
				do {
					if (consoleCommunication == null) {
						final Connector connector = m_connectorFactory.create(agentConfig.getControllerIP(), agentConfig.getControllerPort());
						try {
							consoleCommunication = new ConsoleCommunication(connector);
{'fraction_non_alphanumeric': 0.07845960050386899, 'fraction_numerical': 0.0024293683642253013, 'mean_word_length': 6.115877080665813, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6344171', 'n_tokens_mistral': 3155, 'n_tokens_neox': 2980, 'n_words': 723}