package tmcsim.client;

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import javax.swing.table.DefaultTableModel;
import tmcsim.client.cadclientgui.enums.CADDataEnums;
import tmcsim.common.SimulationException;
import tmcsim.interfaces.CADClientInterface;
import tmcsim.interfaces.CoordinatorInterface;

/**
 * Test Driver for getCadDataTable(CADDataEnums.TABLE.UNIT_STATUS);
 * Usage: Start the CADServer, Sim Mgr, and CADClient. Be Unit Status window is visible.
 *        Start this program and observe CADClient output window.
 * @author jdalbey
 */
public class CadDataTableTest extends UnicastRemoteObject implements
        CADClientInterface {

    private CoordinatorInterface theCoorInt;
    /** reference to itself to be used for disconnecting from CADSimulator   */
    private CADClientInterface myself = this;
    private int kIterationLimit = 500;
    public static void main(String[] args) throws SimulationException, RemoteException
    {
        String host = "127.0.0.1";
        if (args.length > 0)
        {
            host = args[0]; //192.168.251.116 for production machine
        }
        CadDataTableTest app = new CadDataTableTest();
        app.start(host);
        System.exit(0);
    }
    public CadDataTableTest() throws RemoteException { }
    
    public void start(String hostname) throws SimulationException, RemoteException {
        String coorIntURL = "";

        try {
            coorIntURL = "rmi://" + hostname + ":" + "4446"
                    + "/coordinator";
            theCoorInt = (CoordinatorInterface) Naming.lookup(coorIntURL);
            theCoorInt.registerForCallback(this);
        } catch (Exception e) {
            throw new SimulationException(SimulationException.CAD_SIM_CONNECT,"127.0.0.1",
                    e);
        }
        try
        {
            // Call the Coordinator
            DefaultTableModel mdl = theCoorInt.getCadDataTable(CADDataEnums.TABLE.UNIT_STATUS);
            // Loop many times to simulate many clients accessing the CAD Data at once
            System.out.println("Starting CAD Data Table test for "+kIterationLimit+" iterations.");
            for (int count = 0; count < kIterationLimit; count++)
            {
                mdl = theCoorInt.getCadDataTable(CADDataEnums.TABLE.UNIT_STATUS);
                try
                {
                    Thread.sleep(100); // pause briefly between accesses
                }
                catch(InterruptedException e)
                { ; } 
            }
        } catch (RemoteException ex)
        {
            System.out.println("RemoteException in CADClientTest.java");
            System.out.println(ex.getMessage());
        }
        System.out.println("Test Completed.");
        ensureProperShutdown();        
    }

    @Override
    public void refresh() throws RemoteException {
        throw new UnsupportedOperationException("Not supported yet."); 
    }

    public void ensureProperShutdown()
    {
        Runtime.getRuntime().addShutdownHook(new Thread()
        {
            public void run()
            {
                try
                {
                    theCoorInt.unregisterForCallback(myself);
                } catch (RemoteException e)
                {
                    e.printStackTrace();
                }
            }
        });
    }   
}
