//  package Java.TicTacToe.Game;

import RemoteInterface.*;

import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;

public abstract class Game
    extends _RemoteGameImplBase
{
    abstract public void setMove(RemoteInterface.Move newMove);
    abstract public void resetGame();

    static NamingContext ncRef = null;  // move to game

    
    // PreCondition: This should be one of the first methods
    //               called
    //               ORBArgs = options for initializing the Corba ORB
    //               i.e. "-ORBInitialPort 1050" or null
    // PostCondition: The ORB is initialized, the Game is made servent
    //                on the ORB,  and the NamingContect, ncRef,
    //                is set inorder to obtain ORB servents. 
    public void connect(String ORBArgs[])
    {

	try { 
	    // create and initialize the ORB
	    ORB orb = ORB.init(ORBArgs, null);
	    
	    // register game with ORB
	    orb.connect(this);
	    
	    // get the root naming context
	    org.omg.CORBA.Object objRef = 
		orb.resolve_initial_references("NameService");
	    
	    ncRef = NamingContextHelper.narrow(objRef);
	    
	    // bind the Object Reference in Naming
	    NameComponent nc = new NameComponent("Game", "");
	    NameComponent path[] = {nc};
	    ncRef.rebind(path, this);
	}
	catch (Exception e) {
	    System.err.println("Error in game.connect" + e);
	    e.printStackTrace();
	    System.exit(1); // we should throw an exception here - jlr
	}


    }
  
    // PreCondition: The Game.connect() method has been
    //               succesfully executed
    // PostCondition: returns a "RemotePlayer" with the
    //                name "PlayerName" from the orb
    public RemotePlayer getRemotePlayer(String playerName)
    {
	
	RemotePlayer remotePlayer = null;
	
	while( true){ // remotePlayer == null 
	    try {
		
		// resolve the Object Reference in Naming
		NameComponent nc = new NameComponent(playerName, "");
		NameComponent path[] = {nc};
		remotePlayer = RemotePlayerHelper.narrow(ncRef.resolve(path));
		
		return remotePlayer;
	    }
	    catch (Exception e) {
		System.out.println("waiting for player = " + playerName);
	    }
	}
    }
	

}
