FileDocCategorySizeDatePackage
BasicMsgHandler.javaAPI DocExample2318Tue Jan 20 21:09:12 GMT 1998dcj.examples.messageV1

BasicMsgHandler

public abstract class BasicMsgHandler extends Object implements Runnable
Source 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
Constructors Summary
public BasicMsgHandler(InputStream in, OutputStream out)


       
    setStreams(in, out);
    current = this;
  
public BasicMsgHandler(InputStream in, OutputStream out, String endToken)

    msgEndToken = endToken;
    setStreams(in, out);
    current = this;
  
Methods Summary
protected abstract BasicMessagebuildMessage(java.lang.String msgId)

public BasicMessagereadMsg()

    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 voidrun()

    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 voidsendMsg(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 voidsetStreams(java.io.InputStream in, java.io.OutputStream out)

    msgIn = in;
    msgOut = out;