<commit_before>/* $Id$ */

/** This class handles all incoming connection requests.
 **/
package ibis.impl.tcp;

import ibis.connect.socketFactory.ConnectionPropertiesProvider;
import ibis.io.DummyInputStream;
import ibis.io.DummyOutputStream;
import ibis.ipl.ConnectionRefusedException;
import ibis.ipl.ConnectionTimedOutException;
import ibis.ipl.DynamicProperties;
import ibis.ipl.IbisError;
import ibis.util.IbisSocketFactory;
import ibis.util.ThreadPool;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

final class TcpPortHandler implements Runnable, TcpProtocol, Config {

    private ServerSocket systemServer;

    private ConnectionCache connectionCache = new ConnectionCache();

    private ArrayList receivePorts;

    private final TcpIbisIdentifier me;

    private final int port;

    private boolean quiting = false;

    private final boolean use_brokered_links;

    private final IbisSocketFactory socketFactory;

    TcpPortHandler(TcpIbisIdentifier me, boolean brokered,
            IbisSocketFactory fac) throws IOException {
        this.me = me;

        use_brokered_links = brokered;
        socketFactory = fac;

        systemServer = socketFactory.createServerSocket(0, me.address(), true);
        port = systemServer.getLocalPort();

        if (DEBUG) {
            System.out.println("--> PORTHANDLER: port = " + port);
        }

        receivePorts = new ArrayList();
        ThreadPool.createNew(this, "TcpPortHandler");
    }

    synchronized int register(TcpReceivePort p) {
        if (DEBUG) {
            System.err.println("--> TcpPortHandler registered " + p.name);
        }
        receivePorts.add(p);
        return port;
    }

    synchronized void deRegister(TcpReceivePort p) {
        if (DEBUG) {
            System.err.println("--> TcpPortHandler deregistered " + p.name);
        }
        if (!receivePorts.remove(p)) {
            throw new IbisError(
                    "Tcpporthandler: trying to remove unknown reveiveport");
        }
    }

    void releaseOutput(TcpReceivePortIdentifier ri, OutputStream out) {
        connectionCache.releaseOutput(ri.ibis, out);
    }

    void releaseInput(TcpSendPortIdentifier si, InputStream in) {
        connectionCache.releaseInput(si.ibis, in);
    }

    OutputStream connect(TcpSendPort sp, TcpReceivePortIdentifier receiver,
            long timeout) throws IOException {
        Socket s = null;

        long startTime = System.currentTimeMillis();
<commit_msg>fixed a typo in an exception message<commit_after>/* $Id$ */

/** This class handles all incoming connection requests.
 **/
package ibis.impl.tcp;

import ibis.connect.socketFactory.ConnectionPropertiesProvider;
import ibis.io.DummyInputStream;
import ibis.io.DummyOutputStream;
import ibis.ipl.ConnectionRefusedException;
import ibis.ipl.ConnectionTimedOutException;
import ibis.ipl.DynamicProperties;
import ibis.ipl.IbisError;
import ibis.util.IbisSocketFactory;
import ibis.util.ThreadPool;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

final class TcpPortHandler implements Runnable, TcpProtocol, Config {

    private ServerSocket systemServer;

    private ConnectionCache connectionCache = new ConnectionCache();

    private ArrayList receivePorts;

    private final TcpIbisIdentifier me;

    private final int port;

    private boolean quiting = false;

    private final boolean use_brokered_links;

    private final IbisSocketFactory socketFactory;

    TcpPortHandler(TcpIbisIdentifier me, boolean brokered,
            IbisSocketFactory fac) throws IOException {
        this.me = me;

        use_brokered_links = brokered;
        socketFactory = fac;

        systemServer = socketFactory.createServerSocket(0, me.address(), true);
        port = systemServer.getLocalPort();

        if (DEBUG) {
            System.out.println("--> PORTHANDLER: port = " + port);
        }

        receivePorts = new ArrayList();
        ThreadPool.createNew(this, "TcpPortHandler");
    }

    synchronized int register(TcpReceivePort p) {
        if (DEBUG) {
            System.err.println("--> TcpPortHandler registered " + p.name);
        }
        receivePorts.add(p);
        return port;
    }

    synchronized void deRegister(TcpReceivePort p) {
        if (DEBUG) {
            System.err.println("--> TcpPortHandler deregistered " + p.name);
        }
        if (!receivePorts.remove(p)) {
            throw new IbisError(
                    "Tcpporthandler: trying to remove unknown receiveport");
        }
    }

    void releaseOutput(TcpReceivePortIdentifier ri, OutputStream out) {
        connectionCache.releaseOutput(ri.ibis, out);
    }

    void releaseInput(TcpSendPortIdentifier si, InputStream in) {
        connectionCache.releaseInput(si.ibis, in);
    }

    OutputStream connect(TcpSendPort sp, TcpReceivePortIdentifier receiver,
            long timeout) throws IOException {
        Socket s = null;

        long startTime = System.currentTimeMillis();

{'fraction_non_alphanumeric': 0.08790807951851176, 'fraction_numerical': 0.0003647638154295094, 'mean_word_length': 3.1766945925361765, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '1994406', 'n_tokens_mistral': 1555, 'n_tokens_neox': 1496, 'n_words': 368}