package nxm.train.prim;

import nxm.sys.inc.*;
import nxm.sys.lib.*;

/** Passes messages onto other commands. */
public final class primlab8 extends Primitive {
  private MessageHandler[] msgHandlers;  // The message handlers to send the messages to
  
  public int open() {
    // Gets the message handlers to send the messages to
    msgHandlers = new MessageHandler[MA.getNArgs()];
    for (int i = 0; i < msgHandlers.length; i++) {
      // getU(..) is 1-based since getU(0) gets the entire command line
      String regName = MA.getU(i+1);
      msgHandlers[i] = M.registry.getMessageHandler(regName,5);
    }
    return NORMAL;
  }
  
  public int process() {
    return NOOP;
  }
  
  public int close() {
    return NORMAL;
  }
  
  public int processMessage(Message msg) {
    // All of your code goes in this method.
    for (int i = 0; i < msgHandlers.length; i++) {
      MQ.put(msg.getName(), msg.getInfo(), msg.getData(), msgHandlers[i]);
    }
    return super.processMessage(msg);
  }
}
