content: /*
   * Copyright (c) 2001 Boulder Real Time Technologies, Inc.
   * All rights reserved
   *
   * This software may be used freely in any way as long as
   * the copyright statement above is not removed.
   *
   * com.brtt.antelope.OrbThread.java
   *
   * Created on March 2, 2001, 2:18 PM
   */
  
  package com.brtt.antelope;
  
  import java.io.*;
  import java.net.*;
  import java.util.*;
  import java.lang.*;
  
  /**
   * This class implements a Runnable class and extends the com.brtt.antelope.Orb
   * class to provide an Orb object that is running on a separate thread.
   *
   * @author  Danny Harvey, BRTT
   * @version 1.0
   */
  public class OrbThread extends com.brtt.antelope.Orb implements Runnable {
      private Thread runner;
      private Mtfifo inQueue;
      private Mtfifo outQueue;
  
      /**
       * This creates a new OrbThread.
       * @param name The orbserver name, of the form hostname:portno. If
       *              hostname is omitted, then localhost is used. If 
       *              :portno is ommited, then the default orbserver port
       *              number is used.
       * @param perm Permission string. Must be one of "r", for read, "r&",
       *              for read with automatic reconnect, "w", for write, or
       *              "w&", for write with automatic reconnect.
       * @exception com.brtt.antelope.OrbErrorException
       *              An error was generated by the orbserver.
       * @exception java.net.UnknownHostException
       *              hostname not found.
       * @exception java.io.IOException
       *              IO error during communication with orbserver.
       */
      public OrbThread(String name, String perm) throws OrbErrorException, UnknownHostException, IOException {
          super(name, perm);
          runner = new Thread(this);
          inQueue = new Mtfifo();
          outQueue = new Mtfifo();
          runner.start();
      }
      
      public void stop() throws OrbErrorException {
          Object rsp = null;
          
          try { rsp = sendcmd (ORBSTOP, null); }
          catch (OrbErrorException e) {
              System.out.println("Error Closing ORB");
              throw e;
          }
          System.out.println("ORB Closed");
      }
      
      public OrbStat stat() throws OrbErrorException {
          Object rsp = null;
          
          try { rsp = sendcmd (ORBSTAT, null); }
          catch (OrbErrorException e) {
              System.out.println("Error Stating ORB");
              throw e;
          }
          System.out.println("ORB Stat");
          return (OrbStat) rsp;
      }
          
  
      public void run() {
          Command cmd;
          OrbStat stat=null;
          
          while (true) {
              cmd = (Command) inQueue.pop();
              switch (cmd.cmd) {
                  case ORBSTOP:
                      System.out.println("Closing ORB");
                      try { close(); }
                      catch (Throwable x) {
                          outQueue.push (new Command(ORBERR, x));
                          return;
                      }
                      outQueue.push (new Command(ORBSTOP));
                      return;
                  case ORBSTAT:
                      System.out.println("Stating ORB");
                      try { stat = super.stat(); }
                      catch (Throwable x) {
                          outQueue.push (new Command(ORBERR, x));
                          break;
                      }
                      outQueue.push (new Command(ORBSTAT, stat));
                      break;
              }
          }
      }
      
      private Object sendcmd(int cmd, Object args) throws OrbErrorException {
          inQueue.push (new Command(cmd, args));
          Command command = (Command) outQueue.pop();
          if (command.cmd == ORBERR) {
              throw (OrbErrorException) command.args;
          }
          return command.args;
      }
      
      static private final int ORBERR  = -1;
      static private final int ORBSTOP = 0;
      static private final int ORBSTAT = 1;
      
      private class Command extends java.lang.Object {
          int cmd;
          Object args=null;
          
          Command (int cmd) {
              this.cmd = cmd;
          }
          
          Command (int cmd, Object args) {
              this.cmd = cmd;
              if (args != null) this.args = args;
          }
      }
      
  }
  