<commit_before>package edu.ufl.cise.cnt5106c;

import edu.ufl.cise.cnt5106c.log.LogHelper;
import edu.ufl.cise.cnt5106c.io.ProtocolazibleObjectInputStream;
import edu.ufl.cise.cnt5106c.io.ProtocolazibleObjectOutputStream;
import edu.ufl.cise.cnt5106c.log.EventLogger;
import edu.ufl.cise.cnt5106c.messages.Choke;
import edu.ufl.cise.cnt5106c.messages.Handshake;
import edu.ufl.cise.cnt5106c.messages.Message;
import edu.ufl.cise.cnt5106c.messages.Unchoke;
import java.io.IOException;
import java.net.Socket;

/**
 *
 * @author <NAME>    (<EMAIL>)
 */
public class ConnectionHandler implements Runnable {

    private final int _localPeerId;
    private final Socket _socket;
    private final ProtocolazibleObjectOutputStream _out;
    private final FileManager _fileMgr;
    private final PeerManager _peerMgr;
    private final boolean _isConnectingPeer;
    private final int _expectedRemotePeerId;
    private int _remotePeerId;

    public ConnectionHandler (int localPeerId, Socket socket, FileManager fileMgr, PeerManager peerMgr)
            throws IOException {
        this (localPeerId, false, -1, socket, fileMgr, peerMgr);
    }

    public ConnectionHandler (int localPeerId, boolean isConnectingPeer, int expectedRemotePeerId,
            Socket socket, FileManager fileMgr, PeerManager peerMgr) throws IOException {
        _socket = socket;
        _localPeerId = localPeerId;
        _isConnectingPeer = isConnectingPeer;
        _expectedRemotePeerId = expectedRemotePeerId;
        _fileMgr = fileMgr;
        _peerMgr = peerMgr;
        _out = new ProtocolazibleObjectOutputStream (_socket.getOutputStream());
        _remotePeerId = -1;
    }

    public int getRemotePeerId(){
        return _remotePeerId;
    }

    @Override
    public void run() {
        try {
            final ProtocolazibleObjectInputStream in = new ProtocolazibleObjectInputStream (_socket.getInputStream());

            // Send handshake
            _out.writeObject (new Handshake (_localPeerId));

            // Receive and check handshake
            Handshake rcvdHandshake = (Handshake) in.readObject();

            _remotePeerId = rcvdHandshake.getPeerId();
            Thread.currentThread().setName (getClass().getName() + "-" + _remotePeerId);
            final EventLogger eventLogger = new EventLogger (_localPeerId);
            final MessageHandler msgHandler = new MessageHandler (_remotePeerId, _fileMgr, _peerMgr, eventLogger);
            if (_isConnectingPeer && (_remotePeerId != _expectedRemotePeerId)) {
                throw new Exception ("Remote peer id " + _remotePeerId + " does not match with the expected id: " + _expectedRemotePeerId);
            }

            // Handshake successful
            eventLogger.peerConnection(_remotePeerId, _isConnectingPeer);<commit_msg>Some refactoring to use remotePeerId to store the id of the remote peer, and to leave expectedRemotePeerId unchanged.<commit_after>package edu.ufl.cise.cnt5106c;

import edu.ufl.cise.cnt5106c.log.LogHelper;
import edu.ufl.cise.cnt5106c.io.ProtocolazibleObjectInputStream;
import edu.ufl.cise.cnt5106c.io.ProtocolazibleObjectOutputStream;
import edu.ufl.cise.cnt5106c.log.EventLogger;
import edu.ufl.cise.cnt5106c.messages.Choke;
import edu.ufl.cise.cnt5106c.messages.Handshake;
import edu.ufl.cise.cnt5106c.messages.Message;
import edu.ufl.cise.cnt5106c.messages.Unchoke;
import java.io.IOException;
import java.net.Socket;

/**
 *
 * @author <NAME>    (<EMAIL>)
 */
public class ConnectionHandler implements Runnable {

    private final int _localPeerId;
    private final Socket _socket;
    private final ProtocolazibleObjectOutputStream _out;
    private final FileManager _fileMgr;
    private final PeerManager _peerMgr;
    private final boolean _isConnectingPeer;
    private final int _expectedRemotePeerId;
    private int _remotePeerId;

    public ConnectionHandler (int localPeerId, Socket socket, FileManager fileMgr, PeerManager peerMgr)
            throws IOException {
        this (localPeerId, false, -1, socket, fileMgr, peerMgr);
    }

    public ConnectionHandler (int localPeerId, boolean isConnectingPeer, int expectedRemotePeerId,
            Socket socket, FileManager fileMgr, PeerManager peerMgr) throws IOException {
        _socket = socket;
        _localPeerId = localPeerId;
        _isConnectingPeer = isConnectingPeer;
        _expectedRemotePeerId = expectedRemotePeerId;
        _fileMgr = fileMgr;
        _peerMgr = peerMgr;
        _out = new ProtocolazibleObjectOutputStream (_socket.getOutputStream());
        _remotePeerId = -1;
    }

    public int getRemotePeerId(){
        return _remotePeerId;
    }

    @Override
    public void run() {
        try {
            final ProtocolazibleObjectInputStream in = new ProtocolazibleObjectInputStream (_socket.getInputStream());

            // Send handshake
            _out.writeObject (new Handshake (_localPeerId));

            // Receive and check handshake
            Handshake rcvdHandshake = (Handshake) in.readObject();
            _remotePeerId = rcvdHandshake.getPeerId();
            Thread.currentThread().setName (getClass().getName() + "-" + _remotePeerId);
            final EventLogger eventLogger = new EventLogger (_localPeerId);
            final MessageHandler msgHandler = new MessageHandler (_remotePeerId, _fileMgr, _peerMgr, eventLogger);
            if (_isConnectingPeer && (_remotePeerId != _expectedRemotePeerId)) {
                throw new Exception ("Remote peer id " + _remotePeerId + " does not match with the expected id: " + _expectedRemotePeerId);
            }

            // Handshake successful
            eventLogger.peerConnection(_remotePeerId, _isConnectingPeer);
{'fraction_non_alphanumeric': 0.07949571003326913, 'fraction_numerical': 0.013307651899842409, 'mean_word_length': 3.2754491017964074, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '1460244', 'n_tokens_mistral': 1654, 'n_tokens_neox': 1619, 'n_words': 390}