BasicMsgHandlerpublic abstract class BasicMsgHandler extends Object implements RunnableSource code from "Java Distributed Computing", by Jim Farley.
Class: BasicMsgHandler
Example: 6-2
Description: A base class for agents that utilize message-passing. |
Fields Summary |
---|
public static BasicMsgHandler | current | InputStream | msgIn | OutputStream | msgOut | StreamTokenizer | tokenizer | String | msgEndToken |
Methods Summary |
---|
protected abstract BasicMessage | buildMessage(java.lang.String msgId)
| public BasicMessage | readMsg()
BasicMessage msg;
String token;
DataInputStream din = new DataInputStream(msgIn);
token = din.readUTF();
msg = buildMessage(token);
if (msg != null) {
boolean msgEnd = false;
while (!msgEnd) {
token = din.readUTF();
if (token.compareTo(msgEndToken) == 0)
msgEnd = true;
else {
msg.addArg(token);
}
}
}
return msg;
| public void | run()
try {
while (true) {
BasicMessage msg = readMsg();
if (msg != null)
msg.Do();
}
}
// Treat an IOException as a termination of the message
// exchange, and let this message-processing thread die.
catch (IOException e) {}
| public void | sendMsg(BasicMessage msg)
boolean success = true;
DataOutputStream dout = new DataOutputStream(msgOut);
dout.writeUTF(msg.messageID());
Vector args = msg.argList();
int acnt = args.size();
for (int i = 0; i < acnt; i++) {
dout.writeUTF((String)args.elementAt(i));
}
dout.writeUTF(msgEndToken);
| protected void | setStreams(java.io.InputStream in, java.io.OutputStream out)
msgIn = in;
msgOut = out;
|
|