/*
 * Copyright (c) 1999 Software Architects, All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies.
 *
 * SOFTWARE ARCHITECTS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SOFTWARE 
 * ARCHITECTS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE 
 * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS 
 * DERIVATIVES.
 */

//  package Java.TicTacToe.Game;

import RemoteInterface.*;

import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;

/**
  *A framework class that provides the behavior of a Game being
  *able to conenct to the ORB.
  */
public abstract class Game
    extends _RemoteGameImplBase
{
    abstract public void setMove(RemoteInterface.Move newMove);
    abstract public void resetGame();

    static NamingContext ncRef = null;  // move to game

    /**
	*This method connects the servant to the ORB
	*@param ORBArgs A list of arguments to the ORB
	*/
    // PreCondition: This should be one of the first methods
    //               called
    //               ORBArgs = options for initializing the Corba ORB
    //               i.e. "-ORBInitialPort 1050" or null
    //Semantics:      ORB.connect(this)
    // 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
	}


    }
  
    /**
	*This method obtains a reference from the ORB to a RemotePlayer
	*@param playerName "Player" + number indicating which Player
	*@return RemotePlayer A reference to the named RemotePlayer
	*/
    // 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);
	    }
	}
    }
	

}
