The Artima Developer Community
Sponsored Link

Make Room for JavaSpaces, Part III
Coordinate your Jini Apps with JavaSpaces
by Susan Hupfer
First Published in JavaWorld, March 2000

<<  Page 4 of 10  >>

Advertisement

The Player

Let's start by looking at the implementation of a player. Here is the basic skeleton of the player code, excluding some of the Jini lookup and discovery details (you can find the full code in Player.java from the code.zip file in Resources):


public class Player implements Runnable {
    protected String gameName;
    protected String myName;
      
    ...
    
    public Player(String gameName, String myName) throws IOException {
        this.gameName = gameName;
        this.myName = myName;

        ...
        // create a template for locating a GameServiceInterface, set a
        // security manager, & set up a listener for discovery events
        // in the Jini public group
        ...
    }
    
    // This method is called whenever a new lookup service is found
    protected void lookForService(ServiceRegistrar lookupService) {
        ...
        // use a template to search for proxies that implement GameServiceInterface
        ...
      
        GameServiceInterface gameInterface = (GameServiceInterface)proxy;
        
        while (true) {
            Game game = gameInterface.joinGame(gameName);
            if (game != null) {
                try {
                    System.out.println("Playing game " + gameName);
                    game.play(myName);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
                gameInterface.leaveGame();
            } else {
                System.out.println("Couldn't obtain game " + gameName);
            }
            
            // sleep for 10 seconds before trying to join the game again
            try {
                Thread.sleep(10000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
      
    // Create a Player and start its thread
    public static void main(String args[]) {
        if (args.length < 2) {
            System.out.println("Usage: Player gameName playerName");
            System.exit(1);
        }  
        
        try {
            Player player = new Player(args[0], args[1]);
            new Thread(player).start(); // start a thread that sleeps
        } catch (IOException e) {
            System.out.println("Couldn't create player: " + e.getMessage());
        }
    }
}

As you can see in the player's main method, you instantiate a new Player object, passing it the command-line arguments that hold the name of the game that the player wishes to join and the player's name, which the constructor then assigns to member variables. The player constructor also takes care of Jini details, such as creating a template for locating a GameServiceInterface, setting a security manager, and registering a listener to listen for Jini discovery events.

After the constructor returns, you start a thread that will sleep indefinitely with the help of a loop. This might seem a bit peculiar, but if you don't start the thread, the constructor will finish and the player will simply exit. The thread keeps the player alive and listening for Jini discovery events, which is what you want.

Whenever a lookup service is found, the registered listener (an inner class of player that I am not showing here) is contacted with the event and, in turn, calls the lookForService method that you see above. Since the player wants to find a game service that provides immediate access to a game, this method uses a template to search for proxies registered with the lookup service that implement the GameServiceInterface.

Once a proxy has been located, the player enters a loop. First, the player calls the proxy's joinGame method -- waiting as long as necessary to obtain admission to the game with the given name. Once the player has been admitted, the method returns a remote object that gets assigned to the game. Assuming that the game isn't null, the player calls the remote object's play method to start playing the game. When finished playing, the player calls the game proxy's leaveGame method, which essentially relinquishes the player's slot in the game, making it available to other potential players. The player then sleeps for 10 seconds (apparently, playing games is tiring!) before beginning the loop anew and trying to join the game again.

<<  Page 4 of 10  >>


Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2017 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us