<commit_before>package service;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.KeyPair;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;

import service.handler.ConnectionHandler;
import service.handler.NonPOVHandler;
import service.handler.fourstep.ChainHashAndLSNHandler;
import service.handler.fourstep.DoubleChainHashHandler;
import service.handler.twostep.CSNHandler;
import service.handler.twostep.ChainHashHandler;
import utility.Utils;

/**
 *
 * @author Scott
 */
public class SocketServer extends Thread {
    private int port;
    private int numThreads;
    private ServerSocket serverSocket;
    private ExecutorService pool;
    private Constructor handlerCtor;
    private KeyPair keyPair;
    
    public SocketServer(Class handler) {
        this(handler, 3000);
    }
    
    public SocketServer(Class<? extends ConnectionHandler> handler, int port) {
        this.port = port;
        this.numThreads = Runtime.getRuntime().availableProcessors();
        
        try {
            this.handlerCtor = handler.getDeclaredConstructor(Socket.class, KeyPair.class);
        } catch (NoSuchMethodException | SecurityException ex) {
            Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        keyPair = Utils.readKeyPair("service_provider.key");
    }
    
    @Override
    public void run() {
        try {
            serverSocket = new ServerSocket(port);
            pool = Executors.newFixedThreadPool(numThreads);
            
            do {
                Socket socket = serverSocket.accept();
                
                pool.execute((Runnable) handlerCtor.newInstance(socket, keyPair));
            } while (true);
        } catch (IOException ex) {
            Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                serverSocket.close();
                pool.shutdown();
            } catch (IOException ex) {
                Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public static void main(String[] args) {
        Utils.cleanAllAttestations();
        
        new SocketServer(NonPOVHandler.class, Config.NONPOV_SERVICE_PORT).start();
        new SocketServer(CSNHandler.class, Config.CSN_SERVICE_PORT).start();
        new SocketServer(ChainHashHandler.class, Config.CHAINHASH_SERVICE_PORT).start();
        new SocketServer(ChainHashAndLSNHandler.class, Config.CHAINHASH_LSN_SERVICE_PORT).start();
        new SocketServer(DoubleChainHashHandler.class, Config.DOUBLECHAINHASH_SERVICE_PORT).start();
    }
}
<commit_msg>prevent race condition caused failed audits<commit_after>package service;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.ServerSocket;
import java.net.Socket;
import java.security.KeyPair;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;

import service.handler.ConnectionHandler;
import service.handler.NonPOVHandler;
import service.handler.fourstep.ChainHashAndLSNHandler;
import service.handler.fourstep.DoubleChainHashHandler;
import service.handler.twostep.CSNHandler;
import service.handler.twostep.ChainHashHandler;
import utility.Utils;

/**
 *
 * @author Scott
 */
public class SocketServer extends Thread {
    private int port;
    private int numThreads;
    private ServerSocket serverSocket;
    private ExecutorService pool;
    private Constructor handlerCtor;
    private KeyPair keyPair;
    
    public SocketServer(Class handler) {
        this(handler, 3000);
    }
    
    public SocketServer(Class<? extends ConnectionHandler> handler, int port) {
        this.port = port;
        this.numThreads = 1; // Runtime.getRuntime().availableProcessors();
        
        try {
            this.handlerCtor = handler.getDeclaredConstructor(Socket.class, KeyPair.class);
        } catch (NoSuchMethodException | SecurityException ex) {
            Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        keyPair = Utils.readKeyPair("service_provider.key");
    }
    
    @Override
    public void run() {
        try {
            serverSocket = new ServerSocket(port);
            pool = Executors.newFixedThreadPool(numThreads);
            
            do {
                Socket socket = serverSocket.accept();
                
                pool.execute((Runnable) handlerCtor.newInstance(socket, keyPair));
            } while (true);
        } catch (IOException ex) {
            Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalArgumentException ex) {
            Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvocationTargetException ex) {
            Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                serverSocket.close();
                pool.shutdown();
            } catch (IOException ex) {
                Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    public static void main(String[] args) {
        Utils.cleanAllAttestations();
        
        new SocketServer(NonPOVHandler.class, Config.NONPOV_SERVICE_PORT).start();
        new SocketServer(CSNHandler.class, Config.CSN_SERVICE_PORT).start();
        new SocketServer(ChainHashHandler.class, Config.CHAINHASH_SERVICE_PORT).start();
        new SocketServer(ChainHashAndLSNHandler.class, Config.CHAINHASH_LSN_SERVICE_PORT).start();
        new SocketServer(DoubleChainHashHandler.class, Config.DOUBLECHAINHASH_SERVICE_PORT).start();
    }
}

{'fraction_non_alphanumeric': 0.10337370242214533, 'fraction_numerical': 0.0012975778546712802, 'mean_word_length': 2.888452914798206, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '7461320', 'n_tokens_mistral': 1921, 'n_tokens_neox': 1905, 'n_words': 346}