// FIXME: should move somewhere else
package hep.graphics.heprep.jni;

import java.io.PrintWriter;
import java.util.Vector;

/**
 * @author Tony Johnson
 * @author Mark Donszelmann
 * @version $Id: JG4CommandDriver.java,v 1.1 2000/10/19 08:25:45 duns Exp $
 */
public class JG4CommandDriver {
    
    private native void immediateCommand(String command);
    private Vector queue = new Vector();
    private PrintWriter print;

    public JG4CommandDriver(String name){
    }

    protected void fireCommand(String command) {
        System.out.println("Command Received: "+command);
        if (command.startsWith("!")) {
    	    immediateCommand(command);
        } else synchronized (queue) {
            if (queue.isEmpty()) {
                queue.notifyAll();
            }
    	    queue.add(command);
        }
    }

/*    
    public void log(String msg) {
        if (print == null) {
            print = new PrintWriter(getLogStream());
        }
        print.print(msg);
        print.flush();
    }
*/
    
    public String getCommand() { 
        try {
            synchronized (queue) {
    	        if (queue.isEmpty()) {
    	            queue.wait();
    	        }
                String command = (String) queue.firstElement();
                queue.removeElementAt(0);
                return command;
            }
        } catch (InterruptedException x) {
            return "exit";
        }
    }
}
