Java Reference
In-Depth Information
Example 16−7: MudClient.java (continued)
*
* It uses the Naming.lookup() method to obtain a RemoteMudServer object
* for the named MUD on the specified host. Then it uses the getEntrance()
* or getNamedPlace() method of RemoteMudServer to obtain the starting
* RemoteMudPlace object. It prompts the user for a their name and
* description, and creates a MudPerson object. Finally, it passes
* the person and the place to runMud() to begin interaction with the MUD.
**/
public static void main(String[] args) {
try {
String hostname = args[0]; // Each MUD is uniquely identified by a
String mudname = args[1]; // host and a MUD name.
String placename = null; // Each place in a MUD has a unique name
if (args.length > 2) placename = args[2];
// Look up the RemoteMudServer object for the named MUD using
// the default registry on the specified host. Note the use of
// the Mud.mudPrefix constant to help prevent naming conflicts
// in the registry.
RemoteMudServer server =
(RemoteMudServer)Naming.lookup("rmi://" + hostname + "/" +
Mud.mudPrefix + mudname);
// If the user did not specify a place in the mud, use
// getEntrance() to get the initial place. Otherwise, call
// getNamedPlace() to find the initial place.
RemoteMudPlace location = null;
if (placename == null) location = server.getEntrance();
else location = (RemoteMudPlace) server.getNamedPlace(placename);
// Greet the user and ask for their name and description.
// This relies on getLine() and getMultiLine() defined below.
System.out.println("Welcome to " + mudname);
String name = getLine("Enter your name: ");
String description = getMultiLine("Please describe what " +
"people see when they look at you:");
// Define an output stream that the MudPerson object will use to
// display messages sent to it to the user. We'll use the console.
PrintWriter myout = new PrintWriter(System.out);
// Create a MudPerson object to represent the user in the MUD.
// Use the specified name and description, and the output stream.
MudPerson me = new MudPerson(name, description, myout);
// Lower this thread's priority one notch so that broadcast
// messages can appear even when we're blocking for I/O. This is
// necessary on the Linux platform, but may not be necessary on all
// platforms.
int pri = Thread.currentThread().getPriority();
Thread.currentThread().setPriority(pri-1);
// Finally, put the MudPerson into the RemoteMudPlace, and start
// prompting the user for commands.
runMud(location, me);
}
// If anything goes wrong, print a message and exit.
catch (Exception e) {
Search WWH ::




Custom Search