Java Reference
In-Depth Information
A MUD Client
Example 16-7 is a client program for the MUD system developed in the previous
examples. It uses the
Naming.lookup()
method to look up the
RemoteMudServer
object that represents a named MUD on a specified host. The program then calls
getEntrance()
or
getNamedPlace()
method of this
RemoteMudServer
object to
obtain an initial
MudPlace
object into which to insert the user. Next, the program
asks the user for a name and description of the
MudPerson
that will represent her
in the MUD, creates a
MudPerson
object with that name and description, and then
places it in the initial
RemoteMudPlace
. Finally, the program enters a loop that
prompts the user to enter a command and processes the command. Most of the
commands that this client supports simply invoke one of the remote methods of
the
RemoteMudPlace
that represents the user's current location in the MUD. The
end of the command loop consists of a number of
catch
clauses that handle the
large number of things that can go wrong.
In order to use the
MudClient
class, you must first have a
MudServer
up and run-
ning. You should be able to accomplish that with commands like the following:
% cd com/davidflanagan/examples/rmi
% javac Mud*.java
% rmic -d ../../../../ com.davidflanagan.examples.rmi.MudServer
% rmic -d ../../../../ com.davidflanagan.examples.rmi.MudPlace
% rmic -d ../../../../ com.davidflanagan.examples.rmi.MudPerson
% rmiregistry &
% java com.davidflanagan.examples.rmi.MudServer MyMud muddy Lobby \
'A large marble lobby with ficus trees'
Having started the server with these commands, you can then run the client with a
command like this:
% java com.davidflanagan.examples.rmi.MudClient localhost MyMud
Example 16−7: MudClient.java
package com.davidflanagan.examples.rmi;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
import java.io.*;
import java.util.*;
import com.davidflanagan.examples.rmi.Mud.*;
/**
* This class is a client program for the MUD. The main() method sets up
* a connection to a RemoteMudServer, gets the initial RemoteMudPlace object,
* and creates a MudPerson object to represent the user in the MUD. Then it
* calls runMud() to put the person in the place, begins processing
* user commands. The getLine() and getMultiLine() methods are convenience
* methods used throughout to get input from the user.
**/
public class MudClient {
/**
* The main program. It expects two or three arguments:
*
0) the name of the host on which the mud server is running
*
1) the name of the MUD on that host
*
2) the name of a place within that MUD to start at (optional).