<commit_before>package codeine.command_peer;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import codeine.api.NodeWithPeerInfo;
import codeine.configuration.Links;
import codeine.jsons.command.CommandInfo;
import codeine.jsons.command.CommandInfoForSpecificNode;
import codeine.model.Constants;
import codeine.model.Constants.UrlParameters;
import codeine.utils.ExceptionUtils;
import codeine.utils.ThreadUtils;
import codeine.utils.network.HttpUtils;

import com.google.common.base.Function;
import com.google.gson.Gson;

public class PeerCommandWorker implements Runnable {
	private static final Logger log = Logger.getLogger(PeerCommandWorker.class);
	private NodeWithPeerInfo node;
	private AllNodesCommandExecuter allNodesCommandExecuter;
	private boolean shouldOutputImmediatly;
	private CommandInfo command_info;
	private Links links;
	private boolean success = false;
	private boolean POST = true;

	public PeerCommandWorker(NodeWithPeerInfo node, AllNodesCommandExecuter allNodesCommandExecuter, CommandInfo command_info, boolean shouldOutputImmediatly, Links links) {
		this.node = node;
		this.allNodesCommandExecuter = allNodesCommandExecuter;
		this.command_info = command_info;
		this.shouldOutputImmediatly = shouldOutputImmediatly;
		this.links = links;
	}

	@Override
	public void run() {
		try {
			execute();
		} finally {
		}
	}

	private long getSleepTime() {
		return 100;
	}
	
	private Pattern pattern = Pattern.compile(".*" + Constants.COMMAND_RESULT + "(-?\\d+).*");
	

	private void execute() {
		String url = links.getPeerLink(node.peer_address()) + Constants.COMMAND_NODE_CONTEXT;
		log.info("commandNode url is " + url);
		try {
			ThreadUtils.sleep(getSleepTime());
			log.info("running worker " + node);
			final StringBuilder result = new StringBuilder();
			Function<String, Void> function = new Function<String, Void>(){
				@Override
				public Void apply(String line) {
					Matcher matcher = pattern.matcher(line.replace("\n", ""));
					if (matcher.matches()) {
						if (0 != Integer.valueOf(matcher.group(1))) {
							allNodesCommandExecuter.fail(node);
						} else {
							allNodesCommandExecuter.nodeSuccess(node);
							success = true;
						}
					}
					else {
						if (shouldOutputImmediatly) {
							allNodesCommandExecuter.writeLine(line);
						}
						result.append(line + "\n");
					}
					return null;
				}
			};
			if (shouldOutputImmediatly){
				writeNodeHeader();
			}
			if (POST) {
				CommandInfoForSpecificNode command_info2 = new CommandInfoForSpecificNode(node.name(), node.alias());
				String postData = UrlParameters.DATA_NAME + "=" + HttpUtils.encode(new Gson().toJson(command_info))
						+"&" + UrlParameters.DATA_ADDITIONAL_COMMAND_INFO_NAME + "=" + HttpUtils.encode(new Gson().toJson(command_info2));<commit_msg>add support for old peers upgrade from new server<commit_after>package codeine.command_peer;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import codeine.api.NodeWithPeerInfo;
import codeine.configuration.Links;
import codeine.jsons.command.CommandInfo;
import codeine.jsons.command.CommandInfoForSpecificNode;
import codeine.model.Constants;
import codeine.model.Constants.UrlParameters;
import codeine.utils.ExceptionUtils;
import codeine.utils.ThreadUtils;
import codeine.utils.network.HttpUtils;

import com.google.common.base.Function;
import com.google.gson.Gson;

public class PeerCommandWorker implements Runnable {
	private static final Logger log = Logger.getLogger(PeerCommandWorker.class);
	private NodeWithPeerInfo node;
	private AllNodesCommandExecuter allNodesCommandExecuter;
	private boolean shouldOutputImmediatly;
	private CommandInfo command_info;
	private Links links;
	private boolean success = false;
	private boolean POST = true;

	public PeerCommandWorker(NodeWithPeerInfo node, AllNodesCommandExecuter allNodesCommandExecuter, CommandInfo command_info, boolean shouldOutputImmediatly, Links links) {
		this.node = node;
		this.allNodesCommandExecuter = allNodesCommandExecuter;
		this.command_info = command_info;
		this.shouldOutputImmediatly = shouldOutputImmediatly;
		this.links = links;
	}

	@Override
	public void run() {
		try {
			execute();
		} finally {
		}
	}

	private long getSleepTime() {
		return 100;
	}
	
	private Pattern pattern = Pattern.compile(".*" + Constants.COMMAND_RESULT + "(-?\\d+).*");
	

	private void execute() {
		String url = links.getPeerLink(node.peer_address()) + Constants.COMMAND_NODE_CONTEXT;
		log.info("commandNode url is " + url);
		try {
			ThreadUtils.sleep(getSleepTime());
			log.info("running worker " + node);
			final StringBuilder result = new StringBuilder();
			Function<String, Void> function = new Function<String, Void>(){
				@Override
				public Void apply(String line) {
					Matcher matcher = pattern.matcher(line.replace("\n", ""));
					if (matcher.matches()) {
						if (0 != Integer.valueOf(matcher.group(1))) {
							allNodesCommandExecuter.fail(node);
						} else {
							allNodesCommandExecuter.nodeSuccess(node);
							success = true;
						}
					}
					else {
						if (shouldOutputImmediatly) {
							allNodesCommandExecuter.writeLine(line);
						}
						result.append(line + "\n");
					}
					return null;
				}
			};
			if (shouldOutputImmediatly){
				writeNodeHeader();
			}
			if (POST && !command_info.name().equals("upgrade_old_peers")) {
				CommandInfoForSpecificNode command_info2 = new CommandInfoForSpecificNode(node.name(), node.alias());
				String postData = UrlParameters.DATA_NAME + "=" + HttpUtils.encode(new Gson().toJson(command_info))
						+"&" + UrlParameters.DATA_ADDITIONAL_COMMAND_INFO_NAME + "=" + HttpUtils.encode(new Gson().toJson(command_info2));
{'fraction_non_alphanumeric': 0.10962937305161066, 'fraction_numerical': 0.0027710426047800486, 'mean_word_length': 4.505243088655862, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4330567', 'n_tokens_mistral': 2034, 'n_tokens_neox': 1730, 'n_words': 356}