|
|
|
Advertisement
|
In my example, the game itself isn't very interesting. But you're probably curious about how the game is implemented and what it does, so let's take a quick peek. Here is the code for the remote game interface (defined in Game.java):
import java.rmi.*;
public interface Game extends Remote {
public void play(String playerName) throws RemoteException;
}
Here is the code for the GameImpl implementation of that interface (defined in GameImpl.java):
import java.rmi.*;
import java.rmi.server.*;
public class GameImpl extends UnicastRemoteObject implements Game {
private String name;
public GameImpl(String name) throws RemoteException {
super();
this.name = name;
// Create and install a security manager
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
}
// play just prints out messages & sleeps
public void play(String playerName) {
for (int i = 0; i < 5; i++) {
System.out.println(playerName + " is playing...");
try {
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
If you're not familiar with the basics of RMI and remote objects, you may find Resources helpful. This is an extremely minimal game: the game implementation only supplies a play method, which simply loops, printing messages and sleeping for a bit each time. Of course, there are more elaborate games that supply additional methods and do very interesting things in play. In particular, it would make sense for the play method of such a distributed multiplayer game to communicate information to other game players, and a JavaSpace would be a natural way to accomplish this.
In this example, though, your focus isn't on playing an interesting game but on how to coordinate players' access to it. With this in mind, let's turn to the picture's one missing piece: the implementation of the game service proxy that players use to join and leave the game.
|
Sponsored Links
|