FileDocCategorySizeDatePackage
MessageHandler.javaAPI DocExample2055Tue Jan 20 21:39:44 GMT 1998dcj.util.message

MessageHandler

public class MessageHandler extends Object implements Runnable
Source code from "Java Distributed Computing", by Jim Farley. Class: MessageHandler Example: 6-10 Description: The final version of the basic message handler.

Fields Summary
public static MessageHandler
current
InputStream
msgIn
OutputStream
msgOut
Vector
msgPrototypes
Constructors Summary
public MessageHandler()


    
public MessageHandler(InputStream in, OutputStream out)

    setStreams(in, out);
  
Methods Summary
public voidaddMessageType(Message prototype)

    msgPrototypes.addElement(prototype);
  
protected MessagebuildMessage(java.lang.String msgId)

    Message msg = null;
    int numMTypes = msgPrototypes.size();
    for (int i = 0; i < numMTypes; i++) {
      Message m = (Message)msgPrototypes.elementAt(i);
      if (m.handles(msgId)) {
        msg = m.newCopy();
        break;
      }
    }
    return msg;
  
public MessagereadMsg()

    Message msg = null;
    DataInputStream din = new DataInputStream(msgIn);

    String msgId = din.readUTF();
    msg = buildMessage(msgId);
    if (msg != null && msg.readArgs(msgIn)) {
      return msg;
    }
    else {
      return null;
    }
  
public voidrun()

    try {
      while (true) {
        Message 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(Message msg)

    boolean success = true;
    DataOutputStream dout = new DataOutputStream(msgOut);

    dout.writeUTF(msg.messageID());
    msg.writeArgs(msgOut);
  
public voidsetStreams(java.io.InputStream in, java.io.OutputStream out)

    msgIn = in;
    msgOut = out;