<commit_before>package torrent.download.tracker;

import java.util.LinkedList;
import java.util.List;

import org.johnnei.utils.ThreadUtils;
import org.johnnei.utils.config.Config;

import torrent.download.Torrent;
import torrent.download.peer.Peer;

public class PeerConnectorPool {
	
	private List<PeerConnector> connectors;
	
	public PeerConnectorPool() {
		connectors = new LinkedList<>();
		final int connectorCount = Config.getConfig().getInt("peer-max_concurrent_connecting");
		final int peerLimitPerConnector = Config.getConfig().getInt("peer-max_connecting") / connectorCount;
		
		for (int i = 0; i < connectorCount; i++) {
			PeerConnector connector = new PeerConnector(peerLimitPerConnector);
			Thread thread = new Thread(connector, String.format("Peer Connector #%d", i));
			thread.setDaemon(true);
			thread.start();
		}
	}
	
	/**
	 * Queues a peer to be connected
	 * @param p the peer to be connected
	 */
	public void addPeer(Peer peer) {
		PeerConnector connector = connectors.stream().min((a, b) -> a.getFreeCapacity() - b.getFreeCapacity()).get();
		
		if (connector.getFreeCapacity() == 0) {
			System.err.println("[PeerConnectorPool] Overflowing in peers. Can't distribute peers!");
			// TODO Implement a backlog of peers
			return;
		}
		
		connector.addPeer(peer);
		ThreadUtils.notify(connector.PEER_JOB_NOTIFY);
	}

	public int getConnectingCountFor(Torrent torrent) {
		return connectors.stream().mapToInt(c -> c.getConnectingCountFor(torrent)).sum();
	}<commit_msg>Corrected the adding of the PeerConnectors to the pool<commit_after>package torrent.download.tracker;

import java.util.LinkedList;
import java.util.List;

import org.johnnei.utils.ThreadUtils;
import org.johnnei.utils.config.Config;

import torrent.download.Torrent;
import torrent.download.peer.Peer;

public class PeerConnectorPool {
	
	private List<PeerConnector> connectors;
	
	public PeerConnectorPool() {
		connectors = new LinkedList<>();
		final int connectorCount = Config.getConfig().getInt("peer-max_concurrent_connecting");
		final int peerLimitPerConnector = Config.getConfig().getInt("peer-max_connecting") / connectorCount;
		
		for (int i = 0; i < connectorCount; i++) {
			PeerConnector connector = new PeerConnector(peerLimitPerConnector);
			connectors.add(connector);
			Thread thread = new Thread(connector, String.format("Peer Connector #%d", i));
			thread.setDaemon(true);
			thread.start();
		}
	}
	
	/**
	 * Queues a peer to be connected
	 * @param p the peer to be connected
	 */
	public void addPeer(Peer peer) {
		PeerConnector connector = connectors.stream().min((a, b) -> a.getFreeCapacity() - b.getFreeCapacity()).get();
		
		if (connector.getFreeCapacity() == 0) {
			System.err.println("[PeerConnectorPool] Overflowing in peers. Can't distribute peers!");
			// TODO Implement a backlog of peers
			return;
		}
		
		connector.addPeer(peer);
		ThreadUtils.notify(connector.PEER_JOB_NOTIFY);
	}

	public int getConnectingCountFor(Torrent torrent) {
		return connectors.stream().mapToInt(c -> c.getConnectingCountFor(torrent)).sum();
	}
{'fraction_non_alphanumeric': 0.1233278955954323, 'fraction_numerical': 0.0013050570962479609, 'mean_word_length': 4.741573033707865, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7550636', 'n_tokens_mistral': 1031, 'n_tokens_neox': 923, 'n_words': 217}