Assignment 1 --- Trains - T-106.5600 Concurrent programming
Assignment 1 --- Trains - T-106.5600 Concurrent programming
Assignment 1 --- Trains - T-106.5600 Concurrent programming
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Slides by Jan Lönnberg and Teemu Kiviniemi<br />
tsim<br />
<strong>Assignment</strong><br />
<strong>Assignment</strong> 1 — <strong>Trains</strong><br />
T-<strong>106.5600</strong> <strong>Concurrent</strong> <strong>programming</strong><br />
Jan Lönnberg<br />
Laboratory of Software Technology<br />
Helsinki University of Technology<br />
2nd October 2007<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
Introduction<br />
The Story<br />
tsim<br />
<strong>Assignment</strong><br />
A small railway company wants to automate their train<br />
control.<br />
You have been tasked with implementing the train control<br />
software.<br />
Proof of concept: write control software for two trains on a<br />
simulated track.<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
Introduction<br />
Goal<br />
tsim<br />
<strong>Assignment</strong><br />
Write a control program for the trains that lets them travel<br />
between two stations safely.<br />
<strong>Trains</strong> must be independent; each train is controlled by a<br />
separate process.<br />
You will practise mutual exclusion using semaphores and<br />
communicating with other concurrently executing programs<br />
through a prewritten interface.<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
Introduction<br />
You get:<br />
tsim<br />
<strong>Assignment</strong><br />
A simulator called tsim.<br />
A track with two trains.<br />
Java interface code for tsim.<br />
Example control code that drives a train from one station to<br />
another and back.<br />
Your task is to:<br />
Add sensors to the track.<br />
Write control code that drives two trains back and forth<br />
between two stations.<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
Introduction<br />
You get:<br />
tsim<br />
<strong>Assignment</strong><br />
A simulator called tsim.<br />
A track with two trains.<br />
Java interface code for tsim.<br />
Example control code that drives a train from one station to<br />
another and back.<br />
Your task is to:<br />
Add sensors to the track.<br />
Write control code that drives two trains back and forth<br />
between two stations.<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
tsim<br />
tsim<br />
<strong>Assignment</strong><br />
Controlling tsim<br />
Running tsim<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
tsim<br />
tsim<br />
<strong>Assignment</strong><br />
The tsim simulator<br />
tsim is a simple train simulator.<br />
GUI allows you to:<br />
Edit tracks and trains.<br />
Control trains.<br />
View simulation.<br />
Controlling tsim<br />
Running tsim<br />
<strong>Trains</strong> can be controlled through an external program.<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
tsim<br />
Getting tsim<br />
tsim<br />
<strong>Assignment</strong><br />
Controlling tsim<br />
Running tsim<br />
tsim (and associated code) can be downloaded from the<br />
assignment page.<br />
tsim requires a Unix-like system with X (e.g. Linux,<br />
Windows with Cygwin).<br />
tsim is preinstalled in Niksula and HUT Computing Centre.<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
Controlling tsim<br />
tsim<br />
<strong>Assignment</strong><br />
Controlling tsim with another program<br />
Controlling tsim<br />
Running tsim<br />
External programs can control tsim through a TCP/IP<br />
socket.<br />
You write the control code in Java.<br />
You access tsim through the Java tsim interface (TSim.*),<br />
which handles TCP/IP communication with tsim.<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
Controlling tsim<br />
tsim<br />
<strong>Assignment</strong><br />
Your train controller code (1/2)<br />
Controlling tsim<br />
Running tsim<br />
Control code for each train runs in a separate Java virtual<br />
machine.<br />
Your control code communicates with other trains using<br />
semaphores, which are provided by the Java tsim interface.<br />
Both trains must be controlled by the same code.<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
Controlling tsim<br />
tsim<br />
<strong>Assignment</strong><br />
Your train controller code (2/2)<br />
Controlling tsim<br />
Running tsim<br />
Your program must contain a classtrains.Train, an<br />
instance of which controls one train.<br />
Two instances oftrains.Train run in two separate Java<br />
virtual machines, one instance in each virtual machine.<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
Controlling tsim<br />
tsim (simulator)<br />
GUI<br />
User<br />
Commands<br />
Events, command results<br />
Commands<br />
Events, command results<br />
tsim<br />
<strong>Assignment</strong><br />
Java virtual machine<br />
Java virtual machine<br />
Controlling tsim<br />
Running tsim<br />
tsim Java interface<br />
TSim.*<br />
tsim Java interface<br />
TSim.*<br />
Wait for events, change switches, set speeds<br />
Student’s train controller code<br />
trains.*<br />
Wait for events, change switches, set speeds<br />
Student’s train controller code<br />
trains.*<br />
Figure: Structure Jan Lönnberg of tsim runtime <strong>Assignment</strong> 1 configuration<br />
— <strong>Trains</strong><br />
AB
Controlling tsim<br />
tsim<br />
<strong>Assignment</strong><br />
Controlling tsim<br />
Running tsim<br />
Java interface for tsim<br />
Each train is controlled through class<br />
TSim.TSimInterface.<br />
An instance ofTSim.TSimInterface is passed to the<br />
constructor oftrains.Train.<br />
The classTSim.TSimInterface provides methods to:<br />
set the train speed<br />
set the state of track switches<br />
load and unload passengers and goods<br />
get sensor events<br />
get semaphores<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
Controlling tsim<br />
tsim<br />
<strong>Assignment</strong><br />
Controlling tsim<br />
Running tsim<br />
Methods of classTSimInterface (1/2)<br />
public void setSpeed(int speed)<br />
Set train speed (measured in pixels/second).<br />
public void setSwitch(int xPos, int yPos,<br />
int switchPos)<br />
Set the state of the switch at(xPos, yPos) to<br />
switchPos (TSimInterface.SWITCH_LEFT or<br />
TSimInterface.SWITCH_RIGHT).<br />
public void loadUnload()<br />
Load and unload passengers and goods.<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
Controlling tsim<br />
tsim<br />
<strong>Assignment</strong><br />
Controlling tsim<br />
Running tsim<br />
Methods of classTSimInterface (2/2)<br />
public SensorEvent getSensor()<br />
Waits for a sensor event from the train.<br />
public Semaphore getSemaphore(int s)<br />
Create a semaphore with starting values.<br />
The semaphore is shared with all other trains on the track;<br />
the nthSemaphore object created by each train<br />
corresponds to the same semaphore.<br />
Parametersspecifies the the initial value of the semaphore<br />
(if the semaphore is not already created)<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
Controlling tsim<br />
tsim<br />
<strong>Assignment</strong><br />
Controlling tsim<br />
Running tsim<br />
ClassSensorEvent<br />
ASensorEvent is generated when the train enters or<br />
exits a sensor square. It has the following methods:<br />
public int getXpos()<br />
Return the X position of the sensor.<br />
public int getYpos()<br />
Return the Y position of the sensor.<br />
public int getStatus()<br />
Return the status of the sensor:<br />
SensorEvent.ACTIVE if the train has entered the sensor<br />
square or<br />
SensorEvent.INACTIVE if the train has left the sensor<br />
square<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
Controlling tsim<br />
tsim<br />
<strong>Assignment</strong><br />
Controlling tsim<br />
Running tsim<br />
ClassSemaphore<br />
public void acquire()<br />
Acquires the semaphore (P operation).<br />
public void release()<br />
Releases the semaphore (V operation).<br />
public boolean tryAcquire()<br />
Acquires the semaphore if possible.<br />
Returnstrue if the semaphore was acquired.<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
Running tsim<br />
tsim<br />
<strong>Assignment</strong><br />
Controlling tsim<br />
Running tsim<br />
Starting tsim with the controller code (1/2)<br />
tsim/run.sh<br />
˜jlonnber/tsim/run.sh in Niksula or HUT-CC.<br />
The command starts tsim and two Java virtual machines to<br />
control the trains.<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
Running tsim<br />
tsim<br />
<strong>Assignment</strong><br />
Controlling tsim<br />
Running tsim<br />
Starting tsim with the controller code (2/2)<br />
Option-speedupFactor can be used to<br />
speed up the simulation (default is 1.0). Your controller<br />
code must behave correctly when the default acceleration<br />
is used and with any speed-up factor up to 15.0.<br />
Alternative scripttsim/run-log.sh activates logging in<br />
both simulator and Java interface, producing a lot of<br />
information that can aid testing and debugging.<br />
More command-line options are described on the<br />
assignment web page.<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
Running tsim<br />
Editing a track with tsim<br />
tsim<br />
<strong>Assignment</strong><br />
tsim/tsim.sh <br />
Controlling tsim<br />
Running tsim<br />
˜jlonnber/tsim/tsim.sh in Niksula or<br />
HUT-CC.<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
<strong>Assignment</strong><br />
Doing the assignment<br />
tsim<br />
<strong>Assignment</strong><br />
Group size: 1–2 students.<br />
Grading does not depend on group size.<br />
Grade: 0 (fail), 3 (pass), 5 (pass with honours).<br />
Submission<br />
Deadline is 2007-10-22 23:59.<br />
Submit a tar.gz archive containing:<br />
Control code source.<br />
Track with added sensors.<br />
There will be one extra round for re-submissions or late<br />
submissions (grade: 0 (fail) or 1 (pass)).<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
<strong>Assignment</strong><br />
Coding style<br />
tsim<br />
<strong>Assignment</strong><br />
The program must be written in Java, and should have a<br />
clear and simple structure.<br />
Explanatory comments are required for code that is not<br />
self-explanatory to a programmer fluent in Java.<br />
English for variable names, method names, comments and<br />
reports is recommended.<br />
Cryptic method and variable names may not be used.<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
<strong>Assignment</strong><br />
tsim<br />
<strong>Assignment</strong><br />
Instructions and requirements<br />
Full instructions will be on the course home page.<br />
Include a report explaining:<br />
The reasoning behind your solution.<br />
Measures you have taken to ensure your solution is correct<br />
Any unexpected behaviour you may have encountered<br />
during testing and how you have investigated and resolved<br />
it.<br />
The instructions on the course web page are the<br />
authoritative description of the assignment.<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
<strong>Assignment</strong><br />
tsim<br />
<strong>Assignment</strong><br />
Questions and clarifications<br />
Technical questions and clarification requests should be<br />
sent to the course newsgroup:<br />
opinnot.tik.rinnakkaisohjelmointi<br />
Clarifications (if any) will be posted to the newsgroup.<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
<strong>Assignment</strong><br />
Tips<br />
tsim<br />
<strong>Assignment</strong><br />
Read the instructions thoroughly.<br />
Test your code well, with different speed-up factors.<br />
Follow the newsgroup.<br />
Start working now.<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB
Conclusion<br />
tsim<br />
<strong>Assignment</strong><br />
Conclusion<br />
<strong>Assignment</strong> is intended to help learn mutual exclusion<br />
using semaphores.<br />
These slides and the full assignment instructions are<br />
available on the course web page.<br />
Questions?<br />
Jan Lönnberg <strong>Assignment</strong> 1 — <strong>Trains</strong><br />
AB