<commit_before>/*******************************************************************************
 * Copyright 2015 <NAME> - <EMAIL>
 *
 * 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 com.jfastnet;

import com.jfastnet.idprovider.ClientIdReliableModeIdProvider;
import com.jfastnet.idprovider.IIdProvider;
import com.jfastnet.messages.MessagePart;
import com.jfastnet.peers.javanet.JavaNetPeer;
import com.jfastnet.processors.IMessageReceiverPostProcessor;
import com.jfastnet.processors.IMessageReceiverPreProcessor;
import com.jfastnet.processors.IMessageSenderPostProcessor;
import com.jfastnet.processors.IMessageSenderPreProcessor;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.InetSocketAddress;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/** @author <NAME> - <EMAIL> */
@Slf4j
@Getter
@Accessors(chain = true)
public class State {

	/** UDP peer system to use. (e.g. KryoNetty) */
	private IPeer udpPeer;

	/** Provides the message ids. TODO move to state */
	public IIdProvider idProvider = new ClientIdReliableModeIdProvider();

	/** Are we the host? Server sets this to true on creation. */
	@Setter
	private boolean isHost;

	/** The server holds track of its clients. */
	private Map<Integer, InetSocketAddress> clients = new ConcurrentHashMap<>();

	/** Client will only receive messages, if connected. */
	public volatile boolean connected = false;
	public volatile boolean connectionFailed = false;

	/** Used for receiving bigger messages. Only one byte array buffer may
	 * be processed at any given time. */
	private SortedMap<Long, SortedMap<Integer, MessagePart>> byteArrayBufferMap = new TreeMap<>();

	/** Message log collects messages for resending. */
	private MessageLog messageLog;

	/** List of all added processors. */
	private List<Object> processors = new ArrayList<>();

	/** List of systems that need to be processed every tick. */
	private List<ISimpleProcessable> processables = new ArrayList<>();
	private List<IMessageSenderPreProcessor> messageSenderPreProcessors = new ArrayList<>();
	private List<IMessageSenderPostProcessor> messageSenderPostProcessors = new ArrayList<>();
	private List<IMessageReceiverPreProcessor> messageReceiverPreProcessors = new ArrayList<>();
	private List<IMessageReceiverPostProcessor> messageReceiverPostProcessors = new ArrayList<>();

	@Getter
	private Config config;

	public State(Config config) {
		this.config = config;
		messageLog = new MessageLog(config);
		createIdProvider(config);
		createUdpPeer(config);
		createProcessors(config);
	}

	private void createIdProvider(Config config) {
		try {
			Constructor[] constructors = config.idProviderClass.getConstructors();
			// Search full-blown constructor
			for (Constructor constructor : constructors) {
				Class[] parameterTypes = constructor.getParameterTypes();
				if (parameterTypes.length == 2 && parameterTypes[0] == Config.class && parameterTypes[1] == State.class) {
					idProvider = (IIdProvider) constructor.newInstance(config, this);
				}
			}
			if (idProvider == null) {
				// Try default constructor
				idProvider = config.idProviderClass.newInstance();
			}
		} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
			log.error("Couldn't create udp peer {}", config.udpPeerClass, e);
		}
	}

	private void createUdpPeer(Config config) {
		try {
			Constructor[] constructors = config.udpPeerClass.getConstructors();
			// Search full-blown constructor
			for (Constructor constructor : constructors) {
				Class[] parameterTypes = constructor.getParameterTypes();
				if (parameterTypes.length == 2 && parameterTypes[0] == Config.class && parameterTypes[1] == State.class) {
					udpPeer = (IPeer) constructor.newInstance(config, this);
				}
			}
			if (udpPeer == null) {<commit_msg>idProvider -> from config to state<commit_after>/*******************************************************************************
 * Copyright 2015 <NAME> - <EMAIL>
 *
 * 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 com.jfastnet;

import com.jfastnet.idprovider.ClientIdReliableModeIdProvider;
import com.jfastnet.idprovider.IIdProvider;
import com.jfastnet.messages.MessagePart;
import com.jfastnet.peers.javanet.JavaNetPeer;
import com.jfastnet.processors.IMessageReceiverPostProcessor;
import com.jfastnet.processors.IMessageReceiverPreProcessor;
import com.jfastnet.processors.IMessageSenderPostProcessor;
import com.jfastnet.processors.IMessageSenderPreProcessor;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import javax.naming.ConfigurationException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.InetSocketAddress;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/** @author <NAME> - <EMAIL> */
@Slf4j
@Getter
@Accessors(chain = true)
public class State {

	/** UDP peer system to use. (e.g. KryoNetty) */
	private IPeer udpPeer;

	/** Provides the message ids. TODO move to state */
	public IIdProvider idProvider = new ClientIdReliableModeIdProvider();

	/** Are we the host? Server sets this to true on creation. */
	@Setter
	private boolean isHost;

	/** The server holds track of its clients. */
	private Map<Integer, InetSocketAddress> clients = new ConcurrentHashMap<>();

	/** Client will only receive messages, if connected. */
	public volatile boolean connected = false;
	public volatile boolean connectionFailed = false;

	/** Used for receiving bigger messages. Only one byte array buffer may
	 * be processed at any given time. */
	private SortedMap<Long, SortedMap<Integer, MessagePart>> byteArrayBufferMap = new TreeMap<>();

	/** Message log collects messages for resending. */
	private MessageLog messageLog;

	/** List of all added processors. */
	private List<Object> processors = new ArrayList<>();

	/** List of systems that need to be processed every tick. */
	private List<ISimpleProcessable> processables = new ArrayList<>();
	private List<IMessageSenderPreProcessor> messageSenderPreProcessors = new ArrayList<>();
	private List<IMessageSenderPostProcessor> messageSenderPostProcessors = new ArrayList<>();
	private List<IMessageReceiverPreProcessor> messageReceiverPreProcessors = new ArrayList<>();
	private List<IMessageReceiverPostProcessor> messageReceiverPostProcessors = new ArrayList<>();

	@Getter
	private Config config;

	public State(Config config) {
		this.config = config;
		messageLog = new MessageLog(config);
		createIdProvider(config);
		createUdpPeer(config);
		createProcessors(config);
	}

	private void createIdProvider(Config config) {
		try {
			Constructor[] constructors = config.idProviderClass.getConstructors();
			// Search full-blown constructor
			for (Constructor constructor : constructors) {
				Class[] parameterTypes = constructor.getParameterTypes();
				if (parameterTypes.length == 2 && parameterTypes[0] == Config.class && parameterTypes[1] == State.class) {
					idProvider = (IIdProvider) constructor.newInstance(config, this);
				}
			}
			if (idProvider == null) {
				// Try default constructor
				idProvider = config.idProviderClass.newInstance();
			}
		} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
			log.error("Couldn't create id provider {}", config.udpPeerClass, e);
		}

	}

	private void createUdpPeer(Config config) {
		try {
			Constructor[] constructors = config.udpPeerClass.getConstructors();
			// Search full-blown constructor
			for (Constructor constructor : constructors) {
				Class[] parameterTypes = constructor.getParameterTypes();
				if (parameterTypes.length == 2 && parameterTypes[0] == Config.class && parameterTypes[1] == State.class) {
					udpPeer = (IPeer) constructor.newInstance(config, this);
				}
			}
			if (udpPeer == null) {
{'fraction_non_alphanumeric': 0.13155893536121674, 'fraction_numerical': 0.0036936447582835417, 'mean_word_length': 5.056578947368421, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3614287', 'n_tokens_mistral': 2633, 'n_tokens_neox': 2429, 'n_words': 746}