<commit_before>/* $Id$ */

package ibis.impl.tcp;

import ibis.io.SerializationInput;
import ibis.ipl.IbisError;
import ibis.ipl.ReadMessage;
import ibis.ipl.ReceivePort;
import ibis.ipl.SendPortIdentifier;

import java.io.IOException;

final class TcpReadMessage implements ReadMessage {
    private SerializationInput in;

    private long sequenceNr = -1;

    private TcpReceivePort port;

    private TcpSendPortIdentifier origin;

    private ConnectionHandler handler;

    boolean isFinished = false;

    long before;

    TcpReadMessage(TcpReceivePort port, SerializationInput in,
            TcpSendPortIdentifier origin, ConnectionHandler handler) {
        this.port = port;
        this.in = in;
        this.origin = origin;
        this.handler = handler;
        before = handler.bufferedInput.bytesRead();
    }

    TcpReadMessage(TcpReadMessage o) {
        this.port = o.port;
        this.in = o.in;
        this.origin = o.origin;
        this.handler = o.handler;
        this.isFinished = false;
        this.sequenceNr = o.sequenceNr;
        before = handler.bufferedInput.bytesRead();
    }

    ConnectionHandler getHandler() {
        return handler;
    }

    public ReceivePort localPort() {
        if (isFinished) {
            throw new IbisError(
                    "Reading data from a message that was already finished");
        }
        return port;
    }

    protected int available() throws IOException {
        if (isFinished) {
            throw new IbisError(
                    "Reading data from a message that was already finished");
        }
        return in.available();
    }

    public long finish() throws IOException {
        long retval = 0;

        if (isFinished) {
            throw new IbisError(
                    "Reading data from a message that was already finished");
        }

        if (Config.STATS) {
            long after = handler.bufferedInput.bytesRead();
            retval = after - before;
            before = after;
            port.count += retval;
        }

        port.finishMessage();
        in.clear();

        return retval;
    }

    public void finish(IOException e) {
        if (isFinished) {
            throw new IbisError(
                    "Reading data from a message that was already finished");
        }
        port.finishMessage(e);
    }

    public SendPortIdentifier origin() {
        if (isFinished) {
            throw new IbisError(
                    "Reading data from a message that was already finished");
        }
        return origin;
    }

    void setSequenceNumber(long s) {
        sequenceNr = s;
    }

    public long sequenceNumber() {
        if (isFinished) {
            throw new IbisError(
                    "Reading data from a message that was already finished");<commit_msg>Prevent race<commit_after>/* $Id$ */

package ibis.impl.tcp;

import ibis.io.SerializationInput;
import ibis.ipl.IbisError;
import ibis.ipl.ReadMessage;
import ibis.ipl.ReceivePort;
import ibis.ipl.SendPortIdentifier;

import java.io.IOException;

final class TcpReadMessage implements ReadMessage {
    private SerializationInput in;

    private long sequenceNr = -1;

    private TcpReceivePort port;

    private TcpSendPortIdentifier origin;

    private ConnectionHandler handler;

    boolean isFinished = false;

    long before;

    TcpReadMessage(TcpReceivePort port, SerializationInput in,
            TcpSendPortIdentifier origin, ConnectionHandler handler) {
        this.port = port;
        this.in = in;
        this.origin = origin;
        this.handler = handler;
        before = handler.bufferedInput.bytesRead();
    }

    TcpReadMessage(TcpReadMessage o) {
        this.port = o.port;
        this.in = o.in;
        this.origin = o.origin;
        this.handler = o.handler;
        this.isFinished = false;
        this.sequenceNr = o.sequenceNr;
        before = handler.bufferedInput.bytesRead();
    }

    ConnectionHandler getHandler() {
        return handler;
    }

    public ReceivePort localPort() {
        if (isFinished) {
            throw new IbisError(
                    "Reading data from a message that was already finished");
        }
        return port;
    }

    protected int available() throws IOException {
        if (isFinished) {
            throw new IbisError(
                    "Reading data from a message that was already finished");
        }
        return in.available();
    }

    public long finish() throws IOException {
        long retval = 0;

        if (isFinished) {
            throw new IbisError(
                    "Reading data from a message that was already finished");
        }

        if (Config.STATS) {
            long after = handler.bufferedInput.bytesRead();
            retval = after - before;
            before = after;
            port.count += retval;
        }

        in.clear();     // Before finishMessage, or else there is a race here.
                        // (Ceriel)

        port.finishMessage();

        return retval;
    }

    public void finish(IOException e) {
        if (isFinished) {
            throw new IbisError(
                    "Reading data from a message that was already finished");
        }
        port.finishMessage(e);
    }

    public SendPortIdentifier origin() {
        if (isFinished) {
            throw new IbisError(
                    "Reading data from a message that was already finished");
        }
        return origin;
    }

    void setSequenceNumber(long s) {
        sequenceNr = s;
    }

    public long sequenceNumber() {
        if (isFinished) {
            throw new IbisError(
                    "Reading data from a message that was already finished");
{'fraction_non_alphanumeric': 0.08030592734225621, 'fraction_numerical': 0.0006952894142186686, 'mean_word_length': 1.8741258741258742, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1771323', 'n_tokens_mistral': 1573, 'n_tokens_neox': 1492, 'n_words': 446}