/*******************************************************************************
 * 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:
 *    Guru Nagarajan,Intel - Initial API and implementation
 *
 * $Id: TPTPAgent.java,v 1.1 2005/11/10 00:08:36 kcallaghan Exp $ 
 *******************************************************************************/ 
package org.eclipse.tptp.platform.execution.samples;

import org.eclipse.tptp.platform.execution.client.agent.*;
import org.eclipse.tptp.platform.execution.client.core.*;
import org.eclipse.tptp.platform.execution.util.*;
import org.w3c.dom.*;
/**
 * This sample connects to an Agent Controller running on the local host
 * and performs a series of agent related operations
 * <Pre>
 * 		i) Query Available Agents
 * 	   ii) Query Running Agents
 * 	   iii)Obtain Agent Reference with Controller Access
 * 	   iv) Release Agent Control
 * </Pre>
 * @author gnagaraj
 *
 */

public class TPTPAgent
{
	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);
				boolean _done = false;
			 do{
				System.out.println("---------------------------------------");
				System.out.println("Querying Available Agents ...");
				String[] AvailAgents = ac.queryAvailableAgents();
				if(AvailAgents != null) {
				for(int i = 0; i < AvailAgents.length; i++)
				{
					System.out.println("Available Agents ::" + AvailAgents[i]);
				}
				}
				System.out.println("---------------------------------------");
				System.out.println("Querying Running Agents...");
				//String[] RunningAgents = {"org.eclipse.tptp.agentManager"};
				//IAgent[] RetAgentArr = ac.queryRunningAgentsByType(RunningAgents);
				IAgent[] RetAgentArr = ac.queryRunningAgents(null,null,0);
				
				if (RetAgentArr != null)
				{
					System.out.println("# of Running Agents ::" + RetAgentArr.length);
					for(int i = 0; i < RetAgentArr.length; i++)
					{
						System.out.println("Running Agents ::" + RetAgentArr[i].getName());
					}
				}
				
				System.out.println("---------------------------------------");
				System.out.println("Get Agent MetaData ...");
				//String agentMetadata = ac.getAgentMetadata("ProcessController");
				// System.out.println("Agent MetaData ::" + agentMetadata);
				System.out.println("---------------------------------------");
				
				IAgent agent = ac.getAgent("org.eclipse.tptp.TimeCollector",TPTPAgentAccess.TPTP_CONTROLLER_ACCESS);
				//IAgent agent = ac.getAgent("org.eclipse.tptp.TimeCollector",4932);
				System.out.println("Agent ID for the agent ac " + agent.getAgentMode());
				if( agent.requestControl())
				System.out.println("Agent ID for the agent ac true ");
				agent.releaseControl();
				System.out.println("Agent ID for the agent ac true " +agent.getAgentMode());
				agent.releaseAccess();
				
				_done = true;
				
			 }while(!_done);
				
			}
			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);
		}		
	}
}

