Java Reference
In-Depth Information
Example 16−5: MudPlace.java (continued)
/**
* This remote method moves the specified RemoteMudPerson from this place
* in the named direction (i.e. through the named exit) to whatever place
* is there. It throws exceptions if the specified person isn't in this
* place to begin with, or if they are already in the place through the
* exit or if the exit doesn't exist, or if the exit links to another MUD
* server and the server is not functioning.
**/
public RemoteMudPlace go(RemoteMudPerson who, String direction)
throws RemoteException, NotThere, AlreadyThere, NoSuchExit, LinkFailed
{
// Make sure the direction is valid, and get destination if it is
Object destination;
synchronized(exits) {
int i = exits.indexOf(direction);
if (i == -1) throw new NoSuchExit();
destination = destinations.elementAt(i);
}
// If destination is a string, it is a place on another server, so
// connect to that server. Otherwise, it is a place already on this
// server. Throw an exception if we can't connect to the server.
RemoteMudPlace newplace;
if (destination instanceof String) {
try {
String t = (String) destination;
int pos = t.indexOf('@');
String url = t.substring(0, pos);
String placename = t.substring(pos+1);
RemoteMudServer s = (RemoteMudServer) Naming.lookup(url);
newplace = s.getNamedPlace(placename);
}
catch (Exception e) { throw new LinkFailed(); }
}
// If the destination is not a string, then it is a Place
else newplace = (RemoteMudPlace) destination;
// Make sure the person is here and get their name.
// Throw an exception if they are not here
String name = verifyPresence(who);
// Move the person out of here, and tell everyone who remains about it.
this.exit(who, name + " has gone " + direction);
// Put the person into the new place.
// Send a message to everyone already in that new place
String fromwhere;
if (newplace instanceof MudPlace) // going to a local place
fromwhere = placename;
else
fromwhere = server.getMudName() + "." + placename;
newplace.enter(who, name, name + " has arrived from: " + fromwhere);
// Return the new RemoteMudPlace object to the client so they
// know where they are now at.
return newplace;
}
Search WWH ::




Custom Search