package vut.msa.transport;

import java.io.IOException;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;
import org.apache.log4j.Logger;
import vut.msa.security.SSLContextFactory;

/**
 *
 * @author starosta
 */
public class TransportServer extends Thread {

    private static final Logger log = Logger.getLogger(TransportServer.class);

    public static final int DEFAULT_PORT = 9999;
    private static final int MAX_CONNECTIONS = 10;

    private SSLContext sslContext;
    private int activeConnections;

    public TransportServer(String keystore, int port) throws Exception {

        //Inicializuj SSL
        activeConnections = 0;
        log.info("TransportServer inicialization.");

        try {
            sslContext = new SSLContextFactory(keystore).getContext();
        } catch (IOException ex) {
            log.error(ex.getMessage());
            log.error("Could not listen on port: 9999.");
            //System.exit(-1);
        }

        SSLServerSocketFactory factory = sslContext.getServerSocketFactory();
        SSLServerSocket server = (SSLServerSocket) factory.createServerSocket(port);

        String[] supported = server.getSupportedCipherSuites();
        String[] anonCipherSuitesSupported = new String[supported.length];

        int numAnonCipherSuitesSupported = 0;
        for (int i = 0; i < supported.length; i++) {
            if (supported[i].indexOf("_anon_") > 0) {
                anonCipherSuitesSupported[numAnonCipherSuitesSupported++] =
                        supported[i];
            }
        }

        String[] oldEnabled = server.getEnabledCipherSuites();
        String[] newEnabled = new String[oldEnabled.length
                + numAnonCipherSuitesSupported];
        System.arraycopy(oldEnabled, 0, newEnabled, 0, oldEnabled.length);
        System.arraycopy(anonCipherSuitesSupported, 0, newEnabled,
                oldEnabled.length, numAnonCipherSuitesSupported);
        server.setEnabledCipherSuites(newEnabled);

        log.info("Enabled cipher suites: " + newEnabled.length);
        
        while ((activeConnections++ < MAX_CONNECTIONS) || (MAX_CONNECTIONS == 0)) {

            //TODO: Dorob ziskavanie poctu pripojenych uzivatelov
            new TransportServerThread((SSLSocket) server.accept()).start();
        }

        server.close();
    }

    //PROPERTY: activeConnections
    public int getActiveConnections() { return activeConnections; }
   
    public static void main(String[] args) throws IOException {
        try {
            new TransportServer("backup.keys", DEFAULT_PORT);
        } catch (Exception ex) {
            log.error(ex.getMessage());
        }
    }
     
    @Override
    public void run() {

        log.info("TransportServer thread started");
        /*
        try {
            TransportServer server = new TransportServer();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        */
    }
}
