
import java.io.*;

import HEXTOOLS;
import Event;
import EventQueue;

public class CommandReader extends Thread {

BufferedReader in;
InputStreamReader converter;

EventQueue myQueue;
int codeBase = 256;
int codePointa = 97; 			// preset with ASCII code of 'a'
int codePointz = codePointa+25; // preset with ASCII code of 'z'

public CommandReader(EventQueue theQueue, int ccBase) {
  myQueue = theQueue;
  converter = new InputStreamReader(System.in);
  in = new BufferedReader(converter);
  codeBase = ccBase;
  codePointa = 'a';
  codePointz = 'z';
}

public void run() {
String cmd;
String cmdp[] = new String[10];
int ccode;
boolean running = true;
  while (running) {
    try {
	  System.out.print("CommandReader> ");
      cmd = in.readLine();
	  if (cmd.length()<1) { cmd = new String("help"); }
      System.out.println("Command: "+cmd);
	  if (cmd.startsWith("s")) {
	    System.out.println("will stop");
		running = false;
      } 
	  ccode = cmd.charAt(0);
	  if  ((ccode>=codePointa) && (ccode<=codePointz)){
	    ccode -= codePointa;
	    myQueue.append(codeBase,ccode,null);
		System.out.println("Valid Command code="+ccode);
	    }
		}
	  catch (Exception e) {
        System.out.println("Exception "+e+" caught in readLine");
		running = false;
      }
	}
  }
}