/*
 * Copyright (c) 2012-2013 by Jens V. Fischer, Zuse Institute Berlin
 *               
 *
 * Licensed under the BSD License, see LICENSE file for details.
 *
 */

package org.xtreemfs.sandbox.benchmarkOSDPerformance;

import java.util.concurrent.ConcurrentLinkedQueue;

import org.xtreemfs.common.libxtreemfs.AdminClient;
import org.xtreemfs.common.libxtreemfs.ClientFactory;
import org.xtreemfs.common.libxtreemfs.Options;
import org.xtreemfs.foundation.SSLOptions;
import org.xtreemfs.foundation.logging.Logging;
import org.xtreemfs.foundation.pbrpc.generatedinterfaces.RPC;

/**
 * @author jensvfischer
 */
public class BenchmarkClientFactory {

    private static ConcurrentLinkedQueue<AdminClient> clients;

    /**
     * Create a AdminClient. The starting and shutdown of the client is managed by the BenchmarkClientFactory
     * (no need to call Client.start() and Client.shutdown())
     * 
     * @param connection
     *            the connection data
     * @return a started AdminClient instance
     * @throws Exception
     */
    static AdminClient getNewClient(ConnectionData connection) {
        return tryCreateClient(connection.dirAddress, connection.userCredentials, connection.sslOptions,
                connection.options);
    }

    static AdminClient getNewClient(String dirAddress, RPC.UserCredentials userCredentials, SSLOptions sslOptions,
            Options options) {
        return tryCreateClient(dirAddress, userCredentials, sslOptions, options);
    }

    static {
        clients = new ConcurrentLinkedQueue<AdminClient>();
        addShutdownHook();
    }

    /* error handling for 'createNewClient()" */
    private static AdminClient tryCreateClient(String dirAddress, RPC.UserCredentials userCredentials,
            SSLOptions sslOptions, Options options) {
        AdminClient client = null;
        try {
            client = createNewClient(dirAddress, userCredentials, sslOptions, options);
        } catch (Exception e) {
            Logging.logMessage(Logging.LEVEL_ERROR, Logging.Category.tool, BenchmarkClientFactory.class,
                    "Could not create new AdminClient. Errormessage: %s", e.getMessage());
            Thread.yield(); // allow logger to catch up
            e.printStackTrace();
            System.exit(42);
        }
        return client;
    }

    private static AdminClient createNewClient(String dirAddress, RPC.UserCredentials userCredentials,
            SSLOptions sslOptions, Options options) throws Exception {
        AdminClient client = ClientFactory.createAdminClient(dirAddress, userCredentials, sslOptions, options);
        clients.add(client);
        client.start();
        return client;
    }

    /* shutdown all clients add exit */
    private static void addShutdownHook() {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() {
                for (AdminClient client : clients) {
                    client.shutdown();
                }
                Logging.logMessage(Logging.LEVEL_INFO, Logging.Category.tool, Runtime.getRuntime(),
                        "Shutting down %s clients", clients.size());
            }
        });
    }
}

@@ -1565,35 +1565,8 @@
 ();%0A
-        addShutdownHook();%0A
     
@@ -2698,31 +2698,14 @@
 nts 
-add exit */%0A    private
+*/%0A   
  sta
@@ -2717,128 +2717,28 @@
 oid 
-addS
+s
 hutdown
-Hook() %7B%0A        Runtime.getRuntime().addShutdownHook(new Thread() %7B%0A            public void run() %7B%0A        
+Clients() %7B%0A
     
@@ -2774,32 +2774,24 @@
  clients) %7B%0A
-        
             
@@ -2817,34 +2817,18 @@
         
-        %7D%0A        
+%7D%0A
         
@@ -2914,32 +2914,8 @@
 e(),
-%0A                       
  %22Sh
@@ -2942,25 +2942,8 @@
 ts%22,
- clients.size());
 %0A   
@@ -2955,19 +2955,26 @@
     
-%7D%0A
     
-    %7D
+clients.size()
 );%0A 
@@ -2978,10 +2978,11 @@
 ;%0A    %7D%0A
+%0A
 %7D%0A

Refactor BenchmarkClientFactory: Remove shutdownHook