package JavaAgent.agent;

import JavaAgent.resource.KQMLmessage;

/**
 * Thread which pulls messages off the incoming buffer and passes them
 * to the specified Interpreter for interpretation.<p>
 * 
 * <hr>
 * Copyright (c) 1995, H. Robert Frost, Stanford University.
 * All rights reserved.<p>
 * Copyright (c) 1996, H. Robert Frost, Enterprise Integration Technologies,
 * Inc. All rights reserved.<p>
 *
 * RESTRICTED RIGHTS LEGEND: Use, duplication or disclosure by the 
 * Government is subject to restrictions as set forth in 
 * subparagraph(c)(1)(ii) of the Rights in Technical Data and Computer
 * Software clause at DFARS 252.227-7013 and in similar clauses in the
 * FAR and NASA FAR supplement.<p>
 * 
 * This software is bound by the terms and conditions listed in the
 * attached <a href="../LICENSE">LICENSE file</A>.
 * <hr>
 * @author <A HREF="http://cdr.stanford.edu/html/people/frost-bio.html"> Rob Frost</A>
 * @version 0.3 5/21/96 for Java(tm) Language Version 1.0.2
 */

public class ReceiverThread extends Thread{

  protected MessageHandler parent;

  /**
   * Creates a new ReceiverThread.
   * @param parent MessageHandler which created this thread.
   */

  public ReceiverThread(MessageHandler parent){
    super("Receiver Thread");
    this.parent = parent;
  }

  /**
   * Starts message interpretation.
   */

  public void run(){
    while(true){
      KQMLmessage msg = parent.incoming.get();
      InterpretThread it = new InterpretThread(parent,msg);
      it.start();
    }
  }
}

class InterpretThread extends Thread{

  private MessageHandler parent;
  private KQMLmessage msg;

  /**
   * Creates a new InterpretThread.
   * @param parent MessageHandler which created this thread.
   */

  public InterpretThread(MessageHandler parent, KQMLmessage msg){
    super("Interpret Thread");
    this.msg = msg;
    this.parent = parent;
  }

  /**
   * Starts message interpretation.
   */

  public void run(){
    parent.interpretMessage(msg);
    parent.addSystemMessage("System stat:processing finished");
  }
}
