Java Reference
In-Depth Information
Example 16−5: MudPlace.java (continued)
names.removeElementAt(i);
people.removeElementAt(i);
}
if (message != null) tellEveryone(message);
}
/**
* This method puts a person into this place, assigning them the
* specified name, and displaying a message to anyone else who is in
* that place. This method is called by go(), and the client should
* call it to initially place a person into the MUD. Once the person
* is in the MUD, however, the client should restrict them to using go()
* and should not allow them to call this method directly.
* If there have been networking problems, a client might call this method
* to restore a person to this place, in case they've been bumped out.
* (A person will be bumped out of a place if the server tries to send
* a message to them and gets a RemoteException.)
**/
public void enter(RemoteMudPerson who, String name, String message)
throws RemoteException, AlreadyThere
{
// Send the message to everyone who is already here.
if (message != null) tellEveryone(message);
// Add the person to this place.
synchronized (names) {
if (people.indexOf(who) != -1) throw new AlreadyThere();
names.addElement(name);
people.addElement(who);
}
}
/**
* This final remote method returns the server object for the MUD in which
* this place exists. The client should not allow the user to invoke this
* method.
**/
public RemoteMudServer getServer() throws RemoteException {
return server;
}
/**
* Create and start a thread that sends out a message everyone in this
* place. If it gets a RemoteException talking to a person, it silently
* removes that person from this place. This is not a remote method, but
* is used internally by a number of remote methods.
**/
protected void tellEveryone(final String message) {
// If there is no-one here, don't bother sending the message!
if (people.size() == 0) return;
// Make a copy of the people here now. The message is sent
// asynchronously and the list of people in the room may change before
// the message is sent to everyone.
final Vector recipients = (Vector) people.clone();
// Create and start a thread to send the message, using an anonymous
// class. We do this because sending the message to everyone in this
// place might take some time, (particularly on a slow or flaky
// network) and we don't want to wait.
Search WWH ::




Custom Search