TSim
Class TSimInterface

java.lang.Object
  extended by TSim.TSimInterface
All Implemented Interfaces:
java.lang.Runnable

public class TSimInterface
extends java.lang.Object
implements java.lang.Runnable

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
          Switch state: switch points to the left.
static int SWITCH_RIGHT
          Switch state: switch points to the right.
 
Method Summary
 Semaphore getSemaphore(int s)
          Create a semaphore with specified starting value.
 SensorEvent getSensor()
          Wait until the train passes a sensor.
 void loadUnload()
          Load and unload as necessary at station.
 void run()
           
 void setSpeed(int speed)
          Set train target speed to the given value.
 void setSwitch(int xPos, int yPos, int switchPos)
          Set the position 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
Switch state: switch points to the left.

See Also:
Constant Field Values

SWITCH_RIGHT

public static final int SWITCH_RIGHT
Switch state: switch points to the right.

See Also:
Constant Field Values
Method Detail

run

public void run()
Specified by:
run in interface java.lang.Runnable

setSpeed

public void setSpeed(int speed)
              throws CommandException
Set train target speed to the given value. The train's speed does not change immediately and the method call does not wait until the train has reached the target speed.

Parameters:
speed - the new speed of the train.
Throws:
CommandException - if the train has encountered a fatal error

loadUnload

public void loadUnload()
                throws CommandException
Load and unload as necessary at station. This should be called at stations. The train is stopped before loading/unloading starts.

Throws:
CommandException - if the train has encountered a fatal error

setSwitch

public void setSwitch(int xPos,
                      int yPos,
                      int switchPos)
               throws CommandException
Set the position of the specified switch. Valid positions are SWITCH_LEFT and SWITCH_RIGHT.

Parameters:
xPos - the x coordinate of the switch.
yPos - the y coordinate of the switch.
switchPos - the new position 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()
                      throws CommandException
Wait until the train passes a sensor.

Returns:
a SensorEvent representing the information about the event
Throws:
CommandException - if the train has encountered a fatal error.

getSemaphore

public Semaphore getSemaphore(int s)
Create a semaphore with specified starting value. The semaphore is shared with all other trains on the track; the nth Semaphore object created by each train corresponds to the same semaphore.

Parameters:
s - Initial value to give semaphore (if not already created).
Returns:
The new semaphore.