/*******************************************************************************
 * Copyright (c) 2005 Intel Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Vishnu K Naikawadi,Intel - Initial API and implementation
 *
 * $Id: TPTPProcess.java,v 1.1 2005/11/10 00:08:36 kcallaghan Exp $ 
 *******************************************************************************/ 
package org.eclipse.tptp.platform.execution.samples;
/**
 * This sample connects to an Agent Controller running on the local host
 * and performs process launch. 
 * <Pre>
 * 		i) Validate the process
 * 	   ii) Launch the process and request console for reading and writing to the application
 * 	   iii)Following the launch - retrieve data from the application using the console and write to the console.
 * </Pre>
 * @author gnagaraj
 *
 */
import org.eclipse.tptp.platform.execution.client.agent.*;
import org.eclipse.tptp.platform.execution.client.core.*;
import org.eclipse.tptp.platform.execution.client.core.impl.*;
import org.eclipse.tptp.platform.execution.util.*;

public class TPTPProcess
{
    public static void main(String[] args)
	{
//	  Connect to the host where the Agent Controller is running
	    //See the Getting started guide for TPTP 4.0 
		String localhost = "127.0.0.1";
		int port = 10002;

		// Get the Command Line Parameters
		if (args.length > 0)
		{
			if (args.length >= 1)
			{
				localhost = args[0];
			}
			if (args.length >=2)
			{
				port = (new Integer(args[1])).intValue();
			}
		}
		
		//Create a Node that represents the target agent controller
		INode SampleNode = null;
		//Create the client Agent Controller representation object
		IAgentController ac = null;
		//Pass the connection info the client
		ConnectionInfo connInfo = new ConnectionInfo();
		
		try
		{
			//Create a Node that represents the target machine and agent controller
			SampleNode = NodeFactory.createNode(localhost);
			//Set the connection parameters required 
			connInfo.setHostName(localhost);
			connInfo.setPort(port);
			//connInfo.setAgentControllerNew(true);

			if (SampleNode != null)
			{
				// Connect to the Agent Controller, a successful connection returns 
			    //a client side Agent Controller object that provides various 
			    //methods to perform TPTP tasks.
				System.out.println("Connecting to AC..");
				ac = SampleNode.connect(connInfo);

				System.out.println("Connected to " + localhost + " at port number " + port);
				//Create a process object that encapsulates process related attributes
				IProcess proc = ac.createProcess();
				//Set the attribute exe name on the process object
				proc.setExecutable("SimpleAppl.exe");
				//Set the attribute location - where the executable is located on the system where the
				//agent controller is executing
				proc.setLocation(".");
				//Validate this process to check if the executable and the directory is valid
				boolean b = proc.validateProcessToLaunch();
				if(b){System.out.println("Validate Process - True"); /*_done=false;*/}
				else{System.out.println("Validate Process - False");/*_done=false;*/}
				//For the same process object created - create a console 
				//and set the data processor required for the console.
				IConsole console = proc.getConsole();
				IDataProcessor dp = new ClientDataProcessor();
				console.setDataProcessor(dp);
				//Launch the process. At this point the process object information 
				//is sent to the Agent Controller that in turn hands it over to the process 
				//controller agent to launch the process.
				proc.launch();
				
				System.out.println("Process ID ::=>>"+ proc.getProcessId());
				//If the process launch is successful read and write to the console
				if(console.getDataProcessor() != null && proc.getProcessId() > 0)
				{
					
					for(int i = 0; i < 10; i ++)
					{
						StringBuffer buf = new StringBuffer();
						for(int ii = 0; ii < 10; ii ++)
						{
							buf.append("Write from the client ");
						}
						console.write(buf.toString());
						console.write("\n");
					}
					
				}
			while(true){}
			}
			else
			{
				System.out.println("Error occurred while creating the Node for " + localhost);
			}
		}
		catch(Exception exp)
		{
			System.out.println("Error occurred while connecting to " + localhost + ":" + exp);
		}		
	}
}
class ClientDataProcessor implements IDataProcessor 
{
	public void incommingData(byte[] buffer, int length, java.net.InetAddress peer)
	{
		System.out.println("Console Output "+ new String(buffer));
	}
	
	public void incommingData(char[] buffer, int length, java.net.InetAddress peer){
		System.out.println("Console Output "+ new String(buffer));	
	}
	
	public void invalidDataType(byte[] data, int length, java.net.InetAddress peer){}
	
	public void waitingForData(){}
}

