<commit_before>/*
 * Copyright 1999-2010 <NAME> (l.garulli--at--orientechnologies.com)
 *
 * 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.orientechnologies.orient.server.network;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.net.BindException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;

import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.orient.core.config.OConfiguration;
import com.orientechnologies.orient.core.serialization.serializer.OStringSerializerHelper;
import com.orientechnologies.orient.server.OClientConnection;
import com.orientechnologies.orient.server.OClientConnectionManager;
import com.orientechnologies.orient.server.config.OServerCommandConfiguration;
import com.orientechnologies.orient.server.network.protocol.ONetworkProtocol;
import com.orientechnologies.orient.server.network.protocol.http.command.OServerCommand;

public class OServerNetworkListener extends Thread {
	private ServerSocket											serverSocket;
	private InetSocketAddress									inboundAddr;
	private Class<? extends ONetworkProtocol>	protocolType;
	private volatile int											connectionSerial	= 0;
	private volatile boolean									active						= true;
	private OServerCommand[]									commands;

	@SuppressWarnings("unchecked")
	public OServerNetworkListener(final String iHostName, final String iHostPortRange, final String iProtocolName,
			final Class<? extends ONetworkProtocol> iProtocol, final OServerCommandConfiguration[] iCommands) {
		if (iProtocol == null)
			throw new IllegalArgumentException("Can't start listener: protocol not found");

		listen(iHostName, iHostPortRange, iProtocolName);
		protocolType = iProtocol;

		if (iCommands != null) {
			// CREATE COMMANDS
			commands = new OServerCommand[iCommands.length];
			Constructor<OServerCommand> c;
			for (int i = 0; i < iCommands.length; ++i) {
				try {
					c = (Constructor<OServerCommand>) Class.forName(iCommands[i].implementation).getConstructor(
							OServerCommandConfiguration.class);
					commands[0] = (OServerCommand) c.newInstance(new Object[] { iCommands[i] });
				} catch (Exception e) {
					throw new IllegalArgumentException("Can't create custom command '" + iCommands[i] + "'", e);
				}
			}
		}
		start();
	}

	public void shutdown() {
		this.active = false;
	}

	/**
	 * Initialize a server socket for communicating with the client.
	 * 
	 * @param iHostPortRange
	 * @param iHostName
	 */
	private void listen(final String iHostName, final String iHostPortRange, final String iProtocolName) {
		int[] ports;

		if (OStringSerializerHelper.contains(iHostPortRange, ',')) {
			// MULTIPLE ENUMERATED PORTS
			String[] portValues = iHostPortRange.split(",");
			ports = new int[portValues.length];
			for (int i = 0; i < portValues.length; ++i)
				ports[i] = Integer.parseInt(portValues[i]);

		} else if (OStringSerializerHelper.contains(iHostPortRange, '-')) {
			// MULTIPLE RANGE PORTS
			String[] limits = iHostPortRange.split("-");
			int lowerLimit = Integer.parseInt(limits[0]);
			int upperLimit = Integer.parseInt(limits[1]);
			ports = new int[upperLimit - lowerLimit + 1];
			for (int i = 0; i < upperLimit - lowerLimit + 1; ++i)
				ports[i] = lowerLimit + i;

		} else
			// SINGLE PORT SPECIFIED
			ports = new int[] { Integer.parseInt(iHostPortRange) };

		for (int port : ports) {
			inboundAddr = new InetSocketAddress(iHostName, port);
			try {
				serverSocket = new java.net.ServerSocket(port);

				if (serverSocket.isBound()) {
					OLogManager.instance().info(this,
							"Listening " + iProtocolName + " connections on " + inboundAddr.getHostName() + ":" + inboundAddr.getPort());
					return;
				}
			} catch (BindException be) {
				OLogManager.instance().info(this, "Port %s:%d busy, trying the next available...", iHostName, port);
			} catch (SocketException se) {
				OLogManager.instance().error(this, "Unable to create socket", se);
				System.exit(1);
			} catch (IOException ioe) {
				OLogManager.instance().error(this, "Unable to read data from an open socket", ioe);
				System.err.println("Unable to read data from an open socket.");<commit_msg>Fixed issue reported by <NAME> <<EMAIL>> on listen of tcp/ip server socket. Now the ip-address is bound to the right network interface.<commit_after>/*
 * Copyright 1999-2010 <NAME> (l.garulli--at--orientechnologies.com)
 *
 * 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.orientechnologies.orient.server.network;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.net.BindException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;

import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.orient.core.config.OConfiguration;
import com.orientechnologies.orient.core.serialization.serializer.OStringSerializerHelper;
import com.orientechnologies.orient.server.OClientConnection;
import com.orientechnologies.orient.server.OClientConnectionManager;
import com.orientechnologies.orient.server.config.OServerCommandConfiguration;
import com.orientechnologies.orient.server.network.protocol.ONetworkProtocol;
import com.orientechnologies.orient.server.network.protocol.http.command.OServerCommand;

public class OServerNetworkListener extends Thread {
	private ServerSocket											serverSocket;
	private InetSocketAddress									inboundAddr;
	private Class<? extends ONetworkProtocol>	protocolType;
	private volatile int											connectionSerial	= 0;
	private volatile boolean									active						= true;
	private OServerCommand[]									commands;

	@SuppressWarnings("unchecked")
	public OServerNetworkListener(final String iHostName, final String iHostPortRange, final String iProtocolName,
			final Class<? extends ONetworkProtocol> iProtocol, final OServerCommandConfiguration[] iCommands) {
		if (iProtocol == null)
			throw new IllegalArgumentException("Can't start listener: protocol not found");

		listen(iHostName, iHostPortRange, iProtocolName);
		protocolType = iProtocol;

		if (iCommands != null) {
			// CREATE COMMANDS
			commands = new OServerCommand[iCommands.length];
			Constructor<OServerCommand> c;
			for (int i = 0; i < iCommands.length; ++i) {
				try {
					c = (Constructor<OServerCommand>) Class.forName(iCommands[i].implementation).getConstructor(
							OServerCommandConfiguration.class);
					commands[0] = (OServerCommand) c.newInstance(new Object[] { iCommands[i] });
				} catch (Exception e) {
					throw new IllegalArgumentException("Can't create custom command '" + iCommands[i] + "'", e);
				}
			}
		}
		start();
	}

	public void shutdown() {
		this.active = false;
	}

	/**
	 * Initialize a server socket for communicating with the client.
	 * 
	 * @param iHostPortRange
	 * @param iHostName
	 */
	private void listen(final String iHostName, final String iHostPortRange, final String iProtocolName) {
		int[] ports;

		if (OStringSerializerHelper.contains(iHostPortRange, ',')) {
			// MULTIPLE ENUMERATED PORTS
			String[] portValues = iHostPortRange.split(",");
			ports = new int[portValues.length];
			for (int i = 0; i < portValues.length; ++i)
				ports[i] = Integer.parseInt(portValues[i]);

		} else if (OStringSerializerHelper.contains(iHostPortRange, '-')) {
			// MULTIPLE RANGE PORTS
			String[] limits = iHostPortRange.split("-");
			int lowerLimit = Integer.parseInt(limits[0]);
			int upperLimit = Integer.parseInt(limits[1]);
			ports = new int[upperLimit - lowerLimit + 1];
			for (int i = 0; i < upperLimit - lowerLimit + 1; ++i)
				ports[i] = lowerLimit + i;

		} else
			// SINGLE PORT SPECIFIED
			ports = new int[] { Integer.parseInt(iHostPortRange) };

		for (int port : ports) {
			inboundAddr = new InetSocketAddress(iHostName, port);
			try {
				serverSocket = new java.net.ServerSocket(port, 0, InetAddress.getByName(iHostName));

				if (serverSocket.isBound()) {
					OLogManager.instance().info(this,
							"Listening " + iProtocolName + " connections on " + inboundAddr.getHostName() + ":" + inboundAddr.getPort());
					return;
				}
			} catch (BindException be) {
				OLogManager.instance().info(this, "Port %s:%d busy, trying the next available...", iHostName, port);
			} catch (SocketException se) {
				OLogManager.instance().error(this, "Unable to create socket", se);
				System.exit(1);
			} catch (IOException ioe) {
				OLogManager.instance().error(this, "Unable to read data from an open socket", ioe);
				System.err.println("Unable to read data from an open socket.");
{'fraction_non_alphanumeric': 0.10939112487100103, 'fraction_numerical': 0.0046439628482972135, 'mean_word_length': 4.269711799891245, 'pattern_counts': {'":': 2, '<': 22, '<?xml version=': 0, '>': 16, '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': '3638597', 'n_tokens_mistral': 3198, 'n_tokens_neox': 2813, 'n_words': 774}