Java Reference
In-Depth Information
Example 16−4: MudServer.java (continued)
if (places.containsKey(name)) throw new PlaceAlreadyExists();
places.put(name, place);
}
/**
* This remote method serializes and compresses the state of the MUD
* to a named file, if the specified password matches the one specified
* when the MUD was initially created. Note that the state of a MUD
* consists of all places in the MUD, with all things and exits in those
* places. The people in the MUD are not part of the state that is saved.
**/
public void dump(String password, String f)
throws RemoteException, BadPassword, IOException
{
if ((this.password != null) && !this.password.equals(password))
throw new BadPassword();
ObjectOutputStream out = new ObjectOutputStream(
new GZIPOutputStream(new FileOutputStream(f)));
out.writeObject(this);
out.close();
}
/**
* This main() method defines the standalone program that starts up a MUD
* server. If invoked with a single argument, it treats that argument as
* the name of a file containing the serialized and compressed state of an
* existing MUD, and recreates it. Otherwise, it expects four command-line
* arguments: the name of the MUD, the password, the name of the entrance
* place for the MUD, and a description of that entrance place.
* Besides creating the MudServer object, this program sets an appropriate
* security manager, and uses the default rmiregistry to register the
* the MudServer under its given name.
**/
public static void main(String[] args) {
try {
MudServer server;
if (args.length == 1) {
// Read the MUD state in from a file
FileInputStream f = new FileInputStream(args[0]);
ObjectInputStream in =
new ObjectInputStream(new GZIPInputStream(f));
server = (MudServer) in.readObject();
}
// Otherwise, create an initial MUD from scratch
else server = new MudServer(args[0], args[1], args[2], args[3]);
Naming.rebind(Mud.mudPrefix + server.mudname, server);
}
// Display an error message if anything goes wrong.
catch (Exception e) {
System.out.println(e);
System.out.println("Usage: java MudServer <savefile>\n" +
" or: java MudServer <mudname> <password> " +
"<placename> <description>");
System.exit(1);
}
}
Search WWH ::




Custom Search