TSim
Class TSimInterface

java.lang.Object
  extended by TSim.TSimInterface

public final class TSimInterface
extends java.lang.Object

The TSimInterface is the intended interface between TSim and the laboration. It handles the extraction of information from TSim and provides methods for manipulating trains, sensors and switches.


Field Summary
static int SWITCH_LEFT
           
static int SWITCH_RIGHT
           
 
Method Summary
static TSimInterface getInstance()
          Returns the single instance (singleton pattern).
 SensorEvent getSensor(int trainId)
          Blocks the calling thread until the specified train passes a sensor.
 void setDebug(boolean debug)
          Turns on and off printing of debug info to System.err.
 void setSpeed(int trainId, int speed)
          Sets the speed of a train.
 void setSwitch(int xPos, int yPos, int switchDir)
          Sets the direction of the specified switch.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SWITCH_LEFT

public static final int SWITCH_LEFT
See Also:
Constant Field Values

SWITCH_RIGHT

public static final int SWITCH_RIGHT
See Also:
Constant Field Values
Method Detail

getInstance

public static TSimInterface getInstance()
Returns the single instance (singleton pattern). In first call, creates an instance of this class and starts a thread executing its run() method.


setDebug

public void setDebug(boolean debug)
Turns on and off printing of debug info to System.err.


setSpeed

public void setSpeed(int trainId,
                     int speed)
              throws CommandException
Sets the speed of a train.

Parameters:
trainId - the id of the train to be affected by the command.
speed - the new speed of the train.
Throws:
CommandException - if the supplied id was false (NO_SUCH_TRAIN), if the speed was illegal (ILLEGAL_SPEED) or if the train had crashed.

setSwitch

public void setSwitch(int xPos,
                      int yPos,
                      int switchDir)
               throws CommandException
Sets the direction of the specified switch. Valid directions are SWITCH_LEFT and SWITCH_RIGHT.

Parameters:
xPos - the x coordinate of the switch.
yPos - the y coordinate of the switch.
switchDir - the new direction of the switch.
Throws:
CommandException - if the coordinates of the switch were invalid (NO_SUCH_SWITCH) or if there was a train on the switch (TRAIN_ON_SWITCH)

getSensor

public SensorEvent getSensor(int trainId)
                      throws CommandException,
                             java.lang.InterruptedException
Blocks the calling thread until the specified train passes a sensor.

Parameters:
trainId - the id of the train to wait for.
Returns:
a SensorEvent representing the information about the event
Throws:
CommandException - if the train has crashed.
java.lang.InterruptedException