FileDocCategorySizeDatePackage
BetterMsgHandler.javaAPI DocExample1974Sat Jan 31 23:36:44 GMT 1998dcj.examples.messageV2

BasicMsgHandler

public abstract class BasicMsgHandler extends Object implements Runnable

Fields Summary
InputStream
msgIn
OutputStream
msgOut
StreamTokenizer
tokenizer
String
msgEndToken
String
tokenDelim
static BasicMsgHandler
current
Constructors Summary
public BasicMsgHandler(InputStream in, OutputStream out, String endToken, String delim)


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

public BasicMessagereadMsg()

    BasicMessage msg;

    if (tokenizer.nextToken() == StreamTokenizer.TT_WORD) {
      String msgId = tokenizer.sval;
      msg = buildMessage(msgId);
      if (msg.readArgs(msgIn)) {
        return msg;
      }
      else {
        msg = null;
      }
    }
    else {
      return null;
    }
    return msg;
  
public voidrun()

    try {
      while (true) {
        BasicMessage msg = readMsg();
        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);

    char delim = tokenDelim.charAt(0);
    dout.writeUTF(msg.messageID());
    msg.writeArgs(msgOut);
  
protected voidsetStreams(java.io.InputStream in, java.io.OutputStream out)

    msgIn = in;
    msgOut = out;

    // Wrap the input stream with a tokenizer
    tokenizer = new StreamTokenizer(msgIn);

    // Set the token delimiting characters on the tokenizer
    int len = tokenDelim.length();
    for (int i = 0; i < len; i++)
      tokenizer.whitespaceChars(tokenDelim.charAt(i), tokenDelim.charAt(i));