package dcj.examples.messageV2;
import java.util.Vector;
import java.io.*;
/**
* Source code from "Java Distributed Computing", by Jim Farley.
*
* Class: BasicMessage
* Example: 6-6
* Description: An updated version of the BasicMessage that handles
* heterogeneous argument lists.
*/
abstract class BasicMessage
{
protected String id;
protected Vector argList;
String endToken = "END";
public BasicMessage() {
argList = new Vector();
}
public BasicMessage(String mid) {
id = mid;
argList = new Vector();
}
protected void setId(String mid) {
id = mid;
}
public void addArg(Object arg) {
argList.addElement(arg);
}
public String messageID() {
return id;
}
public Vector argList() {
Vector listCopy = (Vector)argList.clone();
return listCopy;
}
public boolean readArgs(InputStream ins) {
boolean success = true;
DataInputStream din = new DataInputStream(ins);
// Read tokens until the "end-of-message" token is seen.
try {
String token = din.readUTF();
while (token.compareTo(endToken) != 0) {
addArg(token);
token = din.readUTF();
}
}
catch (IOException e) {
// Failed to read complete argument list.
success = false;
}
return success;
}
public boolean writeArgs(OutputStream outs) {
int len = argList.size();
boolean success = true;
DataOutputStream dout = new DataOutputStream(outs);
// Write each argument in order
try {
for (int i = 0; i < len; i++) {
String arg = (String)argList.elementAt(i);
dout.writeUTF(arg);
}
// Finish with the end-of-message token
dout.writeUTF(endToken);
}
catch (IOException e) {
success = false;
}
return success;
}
public abstract boolean Do();
}
|