Java Reference
In-Depth Information
Example 16−5: MudPlace.java (continued)
new Thread() {
public void run() {
// Loop through the recipients
for(int i = 0; i < recipients.size(); i++) {
RemoteMudPerson person =
(RemoteMudPerson)recipients.elementAt(i);
// Try to send the message to each one.
try { person.tell(message); }
// If it fails, assume that that person's client or
// network has failed, and silently remove them from
// this place.
catch (RemoteException e) {
try { MudPlace.this.exit(person, null); }
catch (Exception ex) {}
}
}
}
}.start();
}
/**
* This convenience method checks whether the specified person is here.
* If so, it returns their name. If not it throws a NotThere exception
**/
protected String verifyPresence(RemoteMudPerson who) throws NotThere {
int i = people.indexOf(who);
if (i == -1) throw new NotThere();
else return (String) names.elementAt(i);
}
/**
* This method is used for custom de-serialization. Since the vectors of
* people and of their names are transient, they are not serialized with
* the rest of this place. Therefore, when the place is de-serialized,
* those vectors have to be recreated (empty).
**/
private void readObject(ObjectInputStream in)
throws IOException, ClassNotFoundException {
in.defaultReadObject(); // Read most of the object as normal
names = new Vector();
// Then recreate the names vector
people = new Vector();
// and recreate the people vector
}
/** This constant is a version number for serialization */
static final long serialVersionUID = 5090967989223703026L;
}
The MudPerson Class
Example 16-6 shows the MudPerson class. This is the simplest of the remote objects
in the MUD system. It implements the two remote methods defined by the
RemoteMudPerson interface and also defines a few nonremote methods used by the
MudClient class of Example 16-7. The remote methods are quite simple: one sim-
ply returns a description string to the caller,x and the other writes a message to a
stream where the user can see it.
Search WWH ::




Custom Search