<commit_before>package net.i2p.router.client;
/*
 * free (adj.): unencumbered; not under the control of others
 * Written by jrandom in 2003 and released into the public domain 
 * with no warranty of any kind, either expressed or implied.  
 * It probably won't make your computer catch on fire, or eat 
 * your children, but it might.  Use at your own risk.
 *
 */

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import net.i2p.client.I2PClient;
import net.i2p.util.Log;
import net.i2p.router.RouterContext;

/**
 * Listen for connections on the specified port, and toss them onto the client manager's
 * set of connections once they are established.
 *
 * @author jrandom
 */
public class ClientListenerRunner implements Runnable {
    private Log _log;
    private RouterContext _context;
    private ClientManager _manager;
    private ServerSocket _socket;
    private int _port;
    private boolean _running;
    private long _nextFailDelay = 1000;
    
    public ClientListenerRunner(RouterContext context, ClientManager manager, int port) {
        _context = context;
        _log = _context.logManager().getLog(ClientListenerRunner.class);
        _manager = manager;
        _port = port;
        _running = false;
    }
    
    public void setPort(int port) { _port = port; }
    public int getPort() { return _port; }
    
    /** max time to bind */
    private final static int MAX_FAIL_DELAY = 5*60*1000;
    
    /** 
     * Start up the socket listener, listens for connections, and
     * fires those connections off via {@link #runConnection runConnection}.  
     * This only returns if the socket cannot be opened or there is a catastrophic
     * failure.
     *
     */
    public void runServer() {
        _running = true;
        int curDelay = 0;
        while ( (_running) && (curDelay < MAX_FAIL_DELAY) ) {
            try {
                _log.info("Starting up listening for connections on port " + _port);
                _socket = new ServerSocket(_port);
                curDelay = 0;
                while (_running) {
                    try {
                        Socket socket = _socket.accept();
                        if (validate(socket)) {
                            if (_log.shouldLog(Log.DEBUG))
                                _log.debug("Connection received");
                            runConnection(socket);
                        } else {
                            socket.close();
                            if (_log.shouldLog(Log.WARN))
                                _log.warn("Refused connection from " + socket.getInetAddress());
                        }
                    } catch (IOException ioe) {
                        _log.error("Server error accepting", ioe);
                    } catch (Throwable t) {
                        _log.error("Fatal error running client listener - killing the thread!", t);
                        return;
                    }
                }
            } catch (IOException ioe) {
                _log.error("Error listening on port " + _port, ioe);
            }

            if (_socket != null) {
                try { _socket.close(); } catch (IOException ioe) {}
                _socket = null; 
            }

            _log.error("Error listening, waiting " + _nextFailDelay + "ms before we try again");
            try { Thread.sleep(_nextFailDelay); } catch (InterruptedException ie) {}
            curDelay += _nextFailDelay;
            _nextFailDelay *= 5;
        }

        _log.error("CANCELING I2CP LISTEN.  delay = " + curDelay, new Exception("I2CP Listen cancelled!!!"));
        _running = false;
    }
    
    /** give the i2cp client 5 seconds to show that they're really i2cp clients */
    private final static int CONNECT_TIMEOUT = 5*1000;
    
    private boolean validate(Socket socket) {
        try {
            socket.setSoTimeout(CONNECT_TIMEOUT);
            int read = socket.getInputStream().read();
            if (read != I2PClient.PROTOCOL_BYTE)
                return false;
            socket.setSoTimeout(0);
            return true;
        } catch (IOException ioe) {
            if (_log.shouldLog(Log.WARN))
                _log.warn("Peer did not authenticate themselves as I2CP quickly enough, dropping");
            return false;
        }
    }
    /**
     * Handle the connection by passing it off to a {@link ClientConnectionRunner ClientConnectionRunner}
     *
     */
    protected void runConnection(Socket socket) throws IOException {
        ClientConnectionRunner runner = new ClientConnectionRunner(_context, _manager, socket);
        _manager.registerConnection(runner);
    }
    
    public void stopListening() { 
        _running = false; 
        if (_socket != null) try { 
            _socket.close(); 
            _socket = null;
        } catch (IOException ioe) {}
    }
    public void run() { runServer(); }
}
<commit_msg>kaffe shits a brick if you want the socket's address after .close() (grumble)<commit_after>package net.i2p.router.client;
/*
 * free (adj.): unencumbered; not under the control of others
 * Written by jrandom in 2003 and released into the public domain 
 * with no warranty of any kind, either expressed or implied.  
 * It probably won't make your computer catch on fire, or eat 
 * your children, but it might.  Use at your own risk.
 *
 */

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import net.i2p.client.I2PClient;
import net.i2p.util.Log;
import net.i2p.router.RouterContext;

/**
 * Listen for connections on the specified port, and toss them onto the client manager's
 * set of connections once they are established.
 *
 * @author jrandom
 */
public class ClientListenerRunner implements Runnable {
    private Log _log;
    private RouterContext _context;
    private ClientManager _manager;
    private ServerSocket _socket;
    private int _port;
    private boolean _running;
    private long _nextFailDelay = 1000;
    
    public ClientListenerRunner(RouterContext context, ClientManager manager, int port) {
        _context = context;
        _log = _context.logManager().getLog(ClientListenerRunner.class);
        _manager = manager;
        _port = port;
        _running = false;
    }
    
    public void setPort(int port) { _port = port; }
    public int getPort() { return _port; }
    
    /** max time to bind */
    private final static int MAX_FAIL_DELAY = 5*60*1000;
    
    /** 
     * Start up the socket listener, listens for connections, and
     * fires those connections off via {@link #runConnection runConnection}.  
     * This only returns if the socket cannot be opened or there is a catastrophic
     * failure.
     *
     */
    public void runServer() {
        _running = true;
        int curDelay = 0;
        while ( (_running) && (curDelay < MAX_FAIL_DELAY) ) {
            try {
                _log.info("Starting up listening for connections on port " + _port);
                _socket = new ServerSocket(_port);
                curDelay = 0;
                while (_running) {
                    try {
                        Socket socket = _socket.accept();
                        if (validate(socket)) {
                            if (_log.shouldLog(Log.DEBUG))
                                _log.debug("Connection received");
                            runConnection(socket);
                        } else {
                            if (_log.shouldLog(Log.WARN))
                                _log.warn("Refused connection from " + socket.getInetAddress());
                            socket.close();
                        }
                    } catch (IOException ioe) {
                        _log.error("Server error accepting", ioe);
                    } catch (Throwable t) {
                        _log.error("Fatal error running client listener - killing the thread!", t);
                        return;
                    }
                }
            } catch (IOException ioe) {
                _log.error("Error listening on port " + _port, ioe);
            }

            if (_socket != null) {
                try { _socket.close(); } catch (IOException ioe) {}
                _socket = null; 
            }

            _log.error("Error listening, waiting " + _nextFailDelay + "ms before we try again");
            try { Thread.sleep(_nextFailDelay); } catch (InterruptedException ie) {}
            curDelay += _nextFailDelay;
            _nextFailDelay *= 5;
        }

        _log.error("CANCELING I2CP LISTEN.  delay = " + curDelay, new Exception("I2CP Listen cancelled!!!"));
        _running = false;
    }
    
    /** give the i2cp client 5 seconds to show that they're really i2cp clients */
    private final static int CONNECT_TIMEOUT = 5*1000;
    
    private boolean validate(Socket socket) {
        try {
            socket.setSoTimeout(CONNECT_TIMEOUT);
            int read = socket.getInputStream().read();
            if (read != I2PClient.PROTOCOL_BYTE)
                return false;
            socket.setSoTimeout(0);
            return true;
        } catch (IOException ioe) {
            if (_log.shouldLog(Log.WARN))
                _log.warn("Peer did not authenticate themselves as I2CP quickly enough, dropping");
            return false;
        }
    }
    /**
     * Handle the connection by passing it off to a {@link ClientConnectionRunner ClientConnectionRunner}
     *
     */
    protected void runConnection(Socket socket) throws IOException {
        ClientConnectionRunner runner = new ClientConnectionRunner(_context, _manager, socket);
        _manager.registerConnection(runner);
    }
    
    public void stopListening() { 
        _running = false; 
        if (_socket != null) try { 
            _socket.close(); 
            _socket = null;
        } catch (IOException ioe) {}
    }
    public void run() { runServer(); }
}

{'fraction_non_alphanumeric': 0.08729680915111379, 'fraction_numerical': 0.007224563515954244, 'mean_word_length': 1.8267158252977878, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1371796', 'n_tokens_mistral': 2631, 'n_tokens_neox': 2499, 'n_words': 808}