<commit_before>/***************************************************************************
 * Copyright 2013 Kieker Project (http://kieker-monitoring.net)
 *
 * 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 kieker.tools.bridge.connector.tcp;

import java.io.DataInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.Socket;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentMap;

import kieker.common.logging.Log;
import kieker.common.logging.LogFactory;
import kieker.common.record.IMonitoringRecord;
import kieker.tools.bridge.LookupEntity;
import kieker.tools.bridge.connector.ConnectorDataTransmissionException;
import kieker.tools.bridge.connector.ConnectorEndOfDataException;

/**
 * Handles one TCP connection for the multi server.
 * 
 * @author <NAME>
 * @since 1.8
 * 
 */
public class TCPMultiServerConnectionRunnable implements Runnable {
	// TODO Consider to make the buffer length variable by configuration. Otherwise: Why is this an appropriate size? (Nils)
	private static final int BUF_LEN = 65536;

	private static final Log LOG = LogFactory.getLog(TCPMultiServerConnectionRunnable.class);

	private final Socket socket;
	private final byte[] buffer = new byte[BUF_LEN];

	private final ConcurrentMap<Integer, LookupEntity> lookupEntityMap;

	private final BlockingQueue<IMonitoringRecord> recordQueue;
	// TODO The active flag should be volatile, as the setActive method could probably be called from another thread (Nils)
	private boolean active;

	/**
	 * Create a service thread.
	 * 
	 * @param socket
	 *            service socket
	 * @param lookupEntityMap
	 *            map for constructor and parameter fields of records combined with the record id
	 * @param recordQueue
	 *            Queue of the server to retrieve all deserialized records
	 */
	public TCPMultiServerConnectionRunnable(final Socket socket,
			final ConcurrentMap<Integer, LookupEntity> lookupEntityMap,
			final BlockingQueue<IMonitoringRecord> recordQueue) {
		this.socket = socket;
		this.lookupEntityMap = lookupEntityMap;
		this.recordQueue = recordQueue;
	}

	/**
	 * Main loop of the connection runnable.
	 */
	public void run() {
		this.active = true;
		try {
			final DataInputStream in = new DataInputStream(this.socket.getInputStream());
			while (this.active) {
				try {
					this.recordQueue.put(this.deserialize(in));
				} catch (final InterruptedException e) {
					this.active = false;
					LOG.warn("Listener " + Thread.currentThread().getId() + " died. Cause " + e.getMessage());
				} catch (final ConnectorDataTransmissionException e) {<commit_msg>Another hint for the bridge code.<commit_after>/***************************************************************************
 * Copyright 2013 Kieker Project (http://kieker-monitoring.net)
 *
 * 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 kieker.tools.bridge.connector.tcp;

import java.io.DataInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.Socket;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentMap;

import kieker.common.logging.Log;
import kieker.common.logging.LogFactory;
import kieker.common.record.IMonitoringRecord;
import kieker.tools.bridge.LookupEntity;
import kieker.tools.bridge.connector.ConnectorDataTransmissionException;
import kieker.tools.bridge.connector.ConnectorEndOfDataException;

/**
 * Handles one TCP connection for the multi server.
 * 
 * @author <NAME>
 * @since 1.8
 * 
 */
public class TCPMultiServerConnectionRunnable implements Runnable {
	// TODO Consider to make the buffer length variable by configuration. Otherwise: Why is this an appropriate size? (Nils)
	private static final int BUF_LEN = 65536;

	private static final Log LOG = LogFactory.getLog(TCPMultiServerConnectionRunnable.class);

	private final Socket socket;
	private final byte[] buffer = new byte[BUF_LEN];

	private final ConcurrentMap<Integer, LookupEntity> lookupEntityMap;

	private final BlockingQueue<IMonitoringRecord> recordQueue;
	// TODO The active flag should be volatile, as the setActive method could probably be called from another thread (Nils)
	private boolean active;

	/**
	 * Create a service thread.
	 * 
	 * @param socket
	 *            service socket
	 * @param lookupEntityMap
	 *            map for constructor and parameter fields of records combined with the record id
	 * @param recordQueue
	 *            Queue of the server to retrieve all deserialized records
	 */
	public TCPMultiServerConnectionRunnable(final Socket socket,
			final ConcurrentMap<Integer, LookupEntity> lookupEntityMap,
			final BlockingQueue<IMonitoringRecord> recordQueue) {
		this.socket = socket;
		this.lookupEntityMap = lookupEntityMap;
		this.recordQueue = recordQueue;
	}

	/**
	 * Main loop of the connection runnable.
	 */
	public void run() {
		this.active = true;
		try {
			final DataInputStream in = new DataInputStream(this.socket.getInputStream());
			while (this.active) {
				try {
					this.recordQueue.put(this.deserialize(in));
					// TODO Consider to send also the exception itself to the logger in the following (Nils)
				} catch (final InterruptedException e) {
					this.active = false;
					LOG.warn("Listener " + Thread.currentThread().getId() + " died. Cause " + e.getMessage());
				} catch (final ConnectorDataTransmissionException e) {
{'fraction_non_alphanumeric': 0.1239406779661017, 'fraction_numerical': 0.004539951573849879, 'mean_word_length': 4.46198347107438, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '4827252', 'n_tokens_mistral': 1844, 'n_tokens_neox': 1686, 'n_words': 590}