/*
 * Copyright 2012 The Clustermeister Team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.nethad.clustermeister.provisioning.torque;

import com.github.nethad.clustermeister.api.NodeConfiguration;
import com.github.nethad.clustermeister.api.NodeType;
import com.github.nethad.clustermeister.provisioning.jppf.JPPFDriverConfigurationSource;

/**
 *
 * @author thomas
 */
public class TorqueJPPFTestSetup {
    private static final int NUMBER_OF_NODES = 6;

    public static void main(String... args) {
        new TorqueJPPFTestSetup().execute();
    }

    private void execute() {
        System.setProperty("jppf.config.plugin", JPPFDriverConfigurationSource.class.getCanonicalName());

        startDriver();
        startNodes();
    }

    private void startDriver() {
//        JPPFDriver.main("noLauncher");

//        ProcessLauncher processLauncher = new ProcessLauncher("org.jppf.server.JPPFDriver");
//        processLauncher.run();

        TorqueLocalRunner runner = new TorqueLocalRunner();
        runner.start();

//        try {
//            Thread.sleep(5000);
//            System.out.println("runner.stopDriver()");
//            runner.stopDriver();
//        } catch (InterruptedException ex) {
//            Logger.getLogger(TorqueJPPFTestSetup.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }

    private void startNodes() {
		//        System.out.println("Start "+NUMBER_OF_NODES+" nodes.");
		//        new TorqueJPPFNodeDeployer().execute(NUMBER_OF_NODES);
		TorqueNodeManager torqueNodeManager = new TorqueNodeManager(null);
		torqueNodeManager.deployResources();
		NodeConfiguration nodeConfiguration = new TorqueNodeConfiguration(NodeType.NODE);
		for (int i = 0; i<NUMBER_OF_NODES; i++) {
			torqueNodeManager.addNode(nodeConfiguration);
		}
    }

    private class TorqueLocalRunner extends Thread {

        private TorqueJPPFDriverDeployer deployer;

        @Override
        public void run() {
            System.out.println("Start driver");
            deployer = new TorqueJPPFDriverDeployer();
            deployer.execute();
        }

        public void stopDriver() {
            deployer.stopLocalDriver();
        }
    }
}

@@ -665,16 +665,66 @@
 orque;%0A%0A
+import com.github.nethad.clustermeister.api.Node;%0A
 import c
@@ -920,16 +920,75 @@
 nSource;
+%0Aimport com.google.common.util.concurrent.ListenableFuture;
 %0A%0A/**%0A *
@@ -1184,32 +1184,78 @@
 execute();%0A    %7D
+%0A%09private TorqueNodeManager torqueNodeManager;
 %0A%0A    private vo
@@ -1374,16 +1374,67 @@
 Name());
+%0A%09%09torqueNodeManager = new TorqueNodeManager(null);
 %0A%0A      
@@ -1679,24 +1679,26 @@
 her.run();%0A%0A
+//
         Torq
@@ -1741,24 +1741,26 @@
 alRunner();%0A
+//
         runn
@@ -1767,25 +1767,242 @@
 er.start();%0A
-%0A
+%09%09NodeConfiguration nodeConfiguration = new TorqueNodeConfiguration(NodeType.DRIVER);%0A%09%09ListenableFuture%3C? extends Node%3E node = torqueNodeManager.addNode(nodeConfiguration);%0A%09%09System.out.println(%22driver = %22 + node);%0A%09%09
 //        tr
@@ -2005,16 +2005,18 @@
   try %7B%0A
+%09%09
 //      
@@ -2041,16 +2041,18 @@
 (5000);%0A
+%09%09
 //      
@@ -2096,24 +2096,26 @@
 Driver()%22);%0A
+%09%09
 //          
@@ -2133,24 +2133,26 @@
 opDriver();%0A
+%09%09
 //        %7D 
@@ -2181,24 +2181,26 @@
 ption ex) %7B%0A
+%09%09
 //          
@@ -2284,16 +2284,18 @@
 l, ex);%0A
+%09%09
 //      
@@ -2343,209 +2343,8 @@
 %7B%0A%09%09
-//        System.out.println(%22Start %22+NUMBER_OF_NODES+%22 nodes.%22);%0A%09%09//        new TorqueJPPFNodeDeployer().execute(NUMBER_OF_NODES);%0A%09%09TorqueNodeManager torqueNodeManager = new TorqueNodeManager(null);
 %0A%09%09t

use TorqueNodeManager to start driver