import dbg.*; import org.omg.CORBA.*; import java.util.*; import java.text.SimpleDateFormat; import org.omg.CosNaming.NamingContextPackage.*; import org.omg.CosNaming.*; public class GameserverImpl extends GameserverPOA { public GameserverImpl (ORB orbit,String testing) { orb = orbit; System.out.println(testing); currentGameImpl = new GameImpl(); currentGame = currentGameImpl._this(orb); nextPlayerIsWhite=true; // numLaunched = 0; // antPlayers = 0; } public Game register_player(Player player, colorHolder colorholder) { /* System.err.println("register_player, registering "+ ((nextPlayerIsWhite) ? "white": "black")+ " player."); */ //player_helper = player; // Release game and create a new one if current has two players if (currentGameImpl.getNumPlayers()==2){ currentGameImpl = new GameImpl(); currentGame = currentGameImpl._this(orb); } // Add the player to the game if (currentGameImpl.getNumPlayers()==0) { colorholder.value = color.white; currentGameImpl.setPlayer1(player); start_timer(player); }else if (player._is_equivalent(currentGameImpl.getPlayer1())) { colorholder.value = color.white; currentGameImpl.setPlayer1(player); }else if (currentGameImpl.getNumPlayers()==1){ colorholder.value = color.black; currentGameImpl.setPlayer2(player); } else { // ERROR! // Throw some exception colorholder.value = null; } return currentGame; } public void player_done(Player player) { // no need to do anything here... We have no reference to the // games after they are stared, and keeping a reference to // all the games would inmply an enormous overhead in the case of, say, // 15,000 simultaneous games. Should have had this method in the Game object // instead, then the player exiting could have called this method, and the other // player wouldn't be left hanging and waiting for the other player. } public boolean has_waiting_player() { return waiting_player; } public Game register_ongoing_game(Player player1, Player player2, position currentPosition){ System.out.println("Registering ongoing game"); GameImpl gi = new GameImpl(player1, player2); gi.setPosition(currentPosition); return gi._this(orb); } /** * To be used when called from thread in start_timer. */ private Player playerWaiting = null; public void start_timer(Player player){ this.playerWaiting = player; waiting_player = true; date = new Date(); //Only for debugging System.out.println("Time is : "+ new SimpleDateFormat("HH:mm:ss:SSS").format(date)); new Thread(){ public void run(){ while (waiting_player) { try{ // Wait for 5 secs to avoid stressing the server Thread.sleep(5000); } catch(InterruptedException ie){} //Checks if the player has pushed the Exit-button //while server was sleeping if (!waiting_player) {break;} date_helper = new Date(); // a second player has registered on this server while this thread // was sleeping. The waiting player allready has a reference to the // Game-object. if (currentGameImpl.getNumPlayers()==2){ waiting_player = false; playerWaiting = null; } else //Find out if the player has waited too long (10 secs) if (date_helper.getTime() - date.getTime() >= 10000) if (find_player(playerWaiting)) { waiting_player = false; playerWaiting = null; } } // end of while } // end of run() }.start(); } // end of start_timer() public boolean find_player(Player player) { // For testing only System.out.println("Finding a player on another server."); //List all available servers (use Naming Service), and check one by one //if any of them has a player waiting for an opponent. //If so send forward player-reference to that server. //*********************** // // Get naming service // org.omg.CORBA.Object obj = null; try { obj = orb.resolve_initial_references("NameService"); } catch(org.omg.CORBA.ORBPackage.InvalidName ex) { System.err.println("Can't resolve `NameService'"); return false; } if(obj == null) { System.err.println("`NameService' is a nil object reference"); return false; } NamingContext nc = null; try { nc = NamingContextHelper.narrow(obj); } catch(org.omg.CORBA.BAD_PARAM ex) { System.err.println("`NameService' is not " + "a NamingContext object reference"); return false; } try { int i; BindingListHolder bl = new BindingListHolder(); BindingIteratorHolder bi = new BindingIteratorHolder(); nc.list(999999, bl, bi); for(i = 0 ; i < bl.value.length ; i++) { System.out.print(i+1+")"); printBinding(bl.value[i]); System.out.println(""); String s = bl.value[i].binding_name[0].id; // // Resolve names with the Naming Service // NameComponent[] aName = new NameComponent[1]; aName[0] = new NameComponent(); aName[0].id = s; aName[0].kind = ""; org.omg.CORBA.Object aObj = nc.resolve(aName); gameserver_helper = GameserverHelper.narrow(aObj); if (!_this()._is_equivalent(gameserver_helper)){ boolean testing = gameserver_helper.has_waiting_player(); if (testing) { System.out.println("Jippiiii!!"); //remove the player from currentgame currentGameImpl.setPlayer1(null); //transfer the player to the other server player.setGame(gameserver_helper.register_remote_player(player)); // currentGame = gameserver_helper.register_remote_player(player); return true; } } } // for return false; } catch(NotFound ex) { System.err.print("Got a `NotFound' exception ("); switch(ex.why.value()) { case NotFoundReason._missing_node: System.err.print("missing node"); break; case NotFoundReason._not_context: System.err.print("not context"); break; case NotFoundReason._not_object: System.err.print("not object"); break; } System.err.println(")"); ex.printStackTrace(); return false; } catch(CannotProceed ex) { System.err.println("Got a `CannotProceed' exception"); ex.printStackTrace(); return false; } catch(InvalidName ex) { System.err.println("Got an `InvalidName' exception"); ex.printStackTrace(); return false; } //*********************** }// end of find_player static void printBinding(Binding b) { int i; for(i = 0 ; i < b.binding_name.length ; i++) { if(i > 0) System.out.print(" ; "); System.out.print(b.binding_name[i].id); } } public Game register_remote_player (Player player){ waiting_player = false; System.out.println("Registration in progress."); player.onRemoteServer(true); // // }else if (currentGameImpl.getNumPlayers()==1){ currentGameImpl.setPlayer2(player); // // } else { // // ERROR! // // Throw some exception // // } return currentGame; } public void removeGame (Game game){ if (game._is_equivalent(currentGame)){ currentGameImpl = new GameImpl(); currentGame = currentGameImpl._this(orb); }//end if }//end of removeGame public void checkWaitingPlayer(Player player){ if (player._is_equivalent(playerWaiting)){ playerWaiting = null; waiting_player = false; } } /* Protected variables */ protected ORB orb; protected GameImpl currentGameImpl; protected Game currentGame; protected boolean nextPlayerIsWhite; protected boolean waiting_player = false; protected Date date, date_helper; protected Thread pingThread = null; protected Gameserver gameserver_helper; // protected Player player_helper; // protected int antPlayers; // protected static int numLaunched; }//end of class