
package com.sterlingcommerce.scc.agent.services.test;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;

import junit.framework.AssertionFailedError;

import com.sterlingcommerce.scc.agent.services.node.StatRecord_Helper;
import com.sterlingcommerce.scc.agent.services.nodeClient.NodeAgentServiceServiceLocator;
import com.sterlingcommerce.scc.agent.services.nodeClient.NodeAgentServiceSoapBindingStub;
import com.sterlingcommerce.scc.agent.services.node.StatRecord;

/**
 * 
 * @author MChristiansen-TW
 */
public class NodeAgentClient 
{
  String name;
  URL url;
  
  /**
   * 
   */
  public NodeAgentClient(String name, String address) throws MalformedURLException
  {
    url  = new URL(address);
    this.name = name;
  }
  
  /**
   * 
   */
  public void echoString() throws Exception
  {
    NodeAgentServiceSoapBindingStub binding;
    try {
      binding = (NodeAgentServiceSoapBindingStub) new NodeAgentServiceServiceLocator().getnodeAgentService();
    }
    catch (javax.xml.rpc.ServiceException jre) {
      if (jre.getLinkedCause() != null)
        jre.getLinkedCause().printStackTrace();
      throw new junit.framework.AssertionFailedError("JAX-RPC ServiceException caught: " + jre);
    }
    assert binding != null;

    // Time out after a minute
    binding.setTimeout(60000);

    // Test operation
    String value = null;
    value = binding.echoString(new String(), new String());
    // TBD - validate results
  }

  /**
   * 
   */
  public StatRecord[] getStats(int recordsRequested) throws Exception
  {
    NodeAgentServiceSoapBindingStub binding;
    try {
      binding = (NodeAgentServiceSoapBindingStub) new NodeAgentServiceServiceLocator().getnodeAgentService(url);
    }
    catch (javax.xml.rpc.ServiceException jre) {
      if (jre.getLinkedCause() != null)
        jre.getLinkedCause().printStackTrace();
      throw new AssertionFailedError("JAX-RPC ServiceException caught: " + jre);
    }
    assert binding != null;

    // Time out after a minute
    binding.setTimeout(60000);

    // Test operation
    StatRecord[] statRecords = null;
    long start = System.currentTimeMillis();
    System.out.println(name + " Starting Operation " + start + " Requesting " + recordsRequested);
    statRecords = binding.getStats(new Date(), recordsRequested);
    long end = System.currentTimeMillis();
    System.out.println(name + " Ending Operation "  + end + "  delta="+(end - start));
    
    return statRecords;  
  }
}