Skip to content
Snippets Groups Projects
Commit 11f96b72 authored by Lukas Friedrichsen's avatar Lukas Friedrichsen
Browse files

started commenting the source code

parent 78281ab8
Branches
No related tags found
No related merge requests found
package common; package common;
/*
* In dieser Klasse werden alle klassenübergreifen verwendeten Werte einmalig deklariert,
* so dass potentielle Änderungen lediglich einmalig an dieser Stelle durchgeführt werden
* müssen und Code-Vervielfachung vermieden wird.
*/
public class Properties { public class Properties {
// Defining server-ip and port // Defining server-ip and port
......
package gui; package gui;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
...@@ -28,6 +29,10 @@ import javafx.scene.layout.BorderPane; ...@@ -28,6 +29,10 @@ import javafx.scene.layout.BorderPane;
import javafx.stage.Stage; import javafx.stage.Stage;
import common.Properties; import common.Properties;
/*
* Die Klasse MainApp stellt die Hauptklasse der Clients dar. In ihr wird die GUI initialisiert
* und geladen sowie sämtliche "nach außen" (zum Server) gerichteten Funktionalitäten implementiert.
*/
public class MainApp extends Application { public class MainApp extends Application {
private Stage primaryStage; private Stage primaryStage;
...@@ -56,9 +61,7 @@ public class MainApp extends Application { ...@@ -56,9 +61,7 @@ public class MainApp extends Application {
private Boolean connectionEstablished = false; private Boolean connectionEstablished = false;
private Boolean running = false; private Boolean running = false;
/** // Constructor
* Constructor
*/
public MainApp() { public MainApp() {
UTILITY_THREAD_EXECUTOR = Executors.newFixedThreadPool(2); UTILITY_THREAD_EXECUTOR = Executors.newFixedThreadPool(2);
...@@ -69,7 +72,7 @@ public class MainApp extends Application { ...@@ -69,7 +72,7 @@ public class MainApp extends Application {
engines.add(new Engine("Dampflok", Properties.LOK_ID, "res/BR 86.png")); engines.add(new Engine("Dampflok", Properties.LOK_ID, "res/BR 86.png"));
engines.add(new Engine("Reichsbahn", Properties.REICHSBAHN_ID, "res/E 50.png")); engines.add(new Engine("Reichsbahn", Properties.REICHSBAHN_ID, "res/E 50.png"));
// Add the already known switches // yes, lazy, i know // Add the already known switches
for (int i=4;i<=15;i++){ for (int i=4;i<=15;i++){
if (i != 11 && i != 7) { if (i != 11 && i != 7) {
switches.add(new Switch("Weiche "+i, Properties.SWITCH4_ID+i-4)); switches.add(new Switch("Weiche "+i, Properties.SWITCH4_ID+i-4));
...@@ -80,23 +83,7 @@ public class MainApp extends Application { ...@@ -80,23 +83,7 @@ public class MainApp extends Application {
} }
/** // Initializes the loading of the GUI
* Returns the objects as an observable list.
* @return
*/
public ObservableList<Tab> getTabs() {
return tabs;
}
public ObservableList<Engine> getEngines() {
return engines;
}
public ObservableList<Switch> getSwitches() {
return switches;
}
@Override @Override
public void start(Stage primaryStage) { public void start(Stage primaryStage) {
this.primaryStage = primaryStage; this.primaryStage = primaryStage;
...@@ -106,16 +93,17 @@ public class MainApp extends Application { ...@@ -106,16 +93,17 @@ public class MainApp extends Application {
initTabs(); initTabs();
} }
// Loads and binds the root layouut
public void initRootLayout() { public void initRootLayout() {
try { try {
// Load root layout from fxml file. // Load root layout from fxml file
FXMLLoader loader = new FXMLLoader(); FXMLLoader loader = new FXMLLoader();
loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml")); loader.setLocation(MainApp.class.getResource("view/RootLayout.fxml"));
rootLayout = (BorderPane) loader.load(); rootLayout = (BorderPane) loader.load();
rootController = loader.getController(); // only works if loader.load() has been called already rootController = loader.getController(); // only works if loader.load() has been called already
rootController.setMainApp(this); rootController.setMainApp(this);
// Show the scene containing the root layout. // Show the scene containing the root layout
Scene scene = new Scene(rootLayout); Scene scene = new Scene(rootLayout);
primaryStage.setScene(scene); primaryStage.setScene(scene);
primaryStage.show(); primaryStage.show();
...@@ -124,9 +112,7 @@ public class MainApp extends Application { ...@@ -124,9 +112,7 @@ public class MainApp extends Application {
} }
} }
/** // Shows the control tabs inside the root layout.
* Shows the control tabs inside the root layout.
*/
public void initTabs() { public void initTabs() {
try { try {
FXMLLoader loader = new FXMLLoader(); FXMLLoader loader = new FXMLLoader();
...@@ -169,23 +155,41 @@ public class MainApp extends Application { ...@@ -169,23 +155,41 @@ public class MainApp extends Application {
} }
/** public static void main(String[] args) {
* Returns the main stage. launch(args);
* @return }
*/
// Calls the root-controllers function setStatus to set the statusbar to the given string
public void setStatus(String status){ public void setStatus(String status){
rootController.setStatus(status); rootController.setStatus(status);
} }
// Returns the primary stage
public Stage getPrimaryStage() { public Stage getPrimaryStage() {
return primaryStage; return primaryStage;
} }
public static void main(String[] args) { // Returns the tabs as an observable list
launch(args); public ObservableList<Tab> getTabs() {
return tabs;
} }
// Returns the engines as an observable list
public ObservableList<Engine> getEngines() {
return engines;
}
// Returns the switches as an observable list
public ObservableList<Switch> getSwitches() {
return switches;
}
// Returns the value of connectionEstablished
public Boolean getConnectionEstablished() {
return connectionEstablished;
}
// Called, when the emegency-stop-button is pressed; calls emergencyStop and sets every engines speed to 0
public void emergencyStopHandler() { public void emergencyStopHandler() {
emergencyStop(); emergencyStop();
for(Engine eng:engines){ for(Engine eng:engines){
...@@ -196,6 +200,7 @@ public class MainApp extends Application { ...@@ -196,6 +200,7 @@ public class MainApp extends Application {
} }
// Signalizes the server to cut the train-controls power or to re-enable it depending on its current state (running == false means the emergency-stop is active)
public void emergencyStop () { public void emergencyStop () {
if (connectionEstablished) { if (connectionEstablished) {
try { try {
...@@ -216,10 +221,7 @@ public class MainApp extends Application { ...@@ -216,10 +221,7 @@ public class MainApp extends Application {
} }
} }
public Boolean getConnectionEstablished() { // Signalizes the server to set the given engines direction to the client-models direction-value
return connectionEstablished;
}
public void setEngineDirection (Engine eng) { public void setEngineDirection (Engine eng) {
if (connectionEstablished) { if (connectionEstablished) {
try { try {
...@@ -238,6 +240,7 @@ public class MainApp extends Application { ...@@ -238,6 +240,7 @@ public class MainApp extends Application {
} }
} }
// Signalizes the server to set the given engines direction to the client-models speed-value
public void setEngineSpeed (Engine eng) { public void setEngineSpeed (Engine eng) {
if (connectionEstablished) { if (connectionEstablished) {
try { try {
...@@ -251,13 +254,14 @@ public class MainApp extends Application { ...@@ -251,13 +254,14 @@ public class MainApp extends Application {
} }
} }
// Signalizes the server to set the given switch state to the client-models state-value
public void setSwitchState (Switch sw) { public void setSwitchState (Switch sw) {
if (connectionEstablished) { if (connectionEstablished) {
try { try {
byte dir; byte dir;
if (!sw.getState().get()) //gerade if (!sw.getState().get()) // Straight
dir = (byte) 0x01; dir = (byte) 0x01;
else //krumm else // Curved
dir = (byte) 0x00; dir = (byte) 0x00;
byte[] datagram = {Properties.SEPERATOR, (byte) ((sw.getMaerklinID().get()/(1<<8))%(1<<8)), (byte) (sw.getMaerklinID().get()%(1<<8)), (byte) Properties.SWITCH_SET_DIRECTION, dir, (byte) 0x00}; byte[] datagram = {Properties.SEPERATOR, (byte) ((sw.getMaerklinID().get()/(1<<8))%(1<<8)), (byte) (sw.getMaerklinID().get()%(1<<8)), (byte) Properties.SWITCH_SET_DIRECTION, dir, (byte) 0x00};
...@@ -269,6 +273,8 @@ public class MainApp extends Application { ...@@ -269,6 +273,8 @@ public class MainApp extends Application {
} }
} }
// Signalizes the server to move the turntable one step in clockwise direction
public void turntableTurnCW () { public void turntableTurnCW () {
if (connectionEstablished) { if (connectionEstablished) {
try { try {
...@@ -281,6 +287,7 @@ public class MainApp extends Application { ...@@ -281,6 +287,7 @@ public class MainApp extends Application {
} }
} }
// Signalizes the server to move the turntable one step in counterclockwise direction
public void turntableTurnCCW () { public void turntableTurnCCW () {
if (connectionEstablished) { if (connectionEstablished) {
try { try {
...@@ -293,6 +300,7 @@ public class MainApp extends Application { ...@@ -293,6 +300,7 @@ public class MainApp extends Application {
} }
} }
// Submits a thread to the UTILITY_THREAD_EXECUTOR to establish the connection to the server or closes the active connection depending on the value of connectionEstablished
public void establishConnetion () { public void establishConnetion () {
if (connectionEstablished) { if (connectionEstablished) {
try { try {
...@@ -313,14 +321,21 @@ public class MainApp extends Application { ...@@ -313,14 +321,21 @@ public class MainApp extends Application {
} }
} }
/*
* Diese Klasse stellt die Verbindung zum Server her und hält die Input- und Output-Streams
* offen. Weiterhin updated sie bei bestehender Verbindung zyklisch die GUI-Elemente mit den
* Werten der (clientseitig) zugehörigen Modelle.
*/
class EstablishConnection implements Runnable { class EstablishConnection implements Runnable {
// Serves to solve relations to the calling instance
private final MainApp CONTROLLER_INSTANCE; private final MainApp CONTROLLER_INSTANCE;
EstablishConnection (MainApp controller) { EstablishConnection (MainApp controller) {
CONTROLLER_INSTANCE = controller; CONTROLLER_INSTANCE = controller;
} }
// Updates all GUI-elements to match the models state
public void updateGUI () { public void updateGUI () {
engineController.updateEngineStatus(); engineController.updateEngineStatus();
for (Switch sw : switches){ for (Switch sw : switches){
...@@ -328,6 +343,7 @@ public class MainApp extends Application { ...@@ -328,6 +343,7 @@ public class MainApp extends Application {
} }
} }
// Establishes the connection to the server if no connection is established yet, submits a update-thread to the UTILITY_THREAD_EXECUTOR (meaning that updates from the server are processed from now on) and cyclically updates all GUI-elements through updateGUI
@Override @Override
public void run() { public void run() {
if (!connectionEstablished) { if (!connectionEstablished) {
...@@ -342,6 +358,7 @@ public class MainApp extends Application { ...@@ -342,6 +358,7 @@ public class MainApp extends Application {
UTILITY_THREAD_EXECUTOR.submit((new UpdateFunctionality(CONTROLLER_INSTANCE, in))); UTILITY_THREAD_EXECUTOR.submit((new UpdateFunctionality(CONTROLLER_INSTANCE, in)));
Platform.runLater(new Runnable() { Platform.runLater(new Runnable() {
// Sets the connection-esttablishing-buttons text to "Verbindung trennen"
@Override @Override
public void run() { public void run() {
CONTROLLER_INSTANCE.setStatus("Verbindung zu "+client.getRemoteSocketAddress()+" aufgebaut!"); CONTROLLER_INSTANCE.setStatus("Verbindung zu "+client.getRemoteSocketAddress()+" aufgebaut!");
...@@ -358,14 +375,15 @@ public class MainApp extends Application { ...@@ -358,14 +375,15 @@ public class MainApp extends Application {
} }
}); });
Thread.sleep(50); Thread.sleep(50); // Frees the resources between the GUI-updates
} }
} }
catch (Exception e) { catch (Exception e) {
connectionEstablished = false; connectionEstablished = false; // Interrupts the connection to the server in case of an error
running = false; running = false;
Platform.runLater(new Runnable() { Platform.runLater(new Runnable() {
// Sets the connection-esttablishing-buttons text to "Verbindung herstellen"
@Override @Override
public void run() { public void run() {
CONTROLLER_INSTANCE.setStatus("Error at: establishConnection (establishing connection)"); CONTROLLER_INSTANCE.setStatus("Error at: establishConnection (establishing connection)");
...@@ -377,6 +395,7 @@ public class MainApp extends Application { ...@@ -377,6 +395,7 @@ public class MainApp extends Application {
} }
finally { finally {
try { try {
// Properly shuts down the connection to the server in case of an unexpected termination of the client-instance
byte[] datagram = {Properties.SEPERATOR, (byte) 0x00, (byte) 0x00, (byte) Properties.SESSION_ABORT, (byte) 0x00, (byte) 0x00}; byte[] datagram = {Properties.SEPERATOR, (byte) 0x00, (byte) 0x00, (byte) Properties.SESSION_ABORT, (byte) 0x00, (byte) 0x00};
out.write(datagram); out.write(datagram);
client.close(); client.close();
...@@ -393,9 +412,18 @@ public class MainApp extends Application { ...@@ -393,9 +412,18 @@ public class MainApp extends Application {
} }
} }
/*
* Diese Runnable-Klasse lauscht auf dem Update-Port des Servers auf Status-Updates,
* decodiert diese bei Empfang und updatet die (clientseitigen) Modelle auf die erhaltenen
* Werte. Achtung: die Modelle werden geupdatet, nicht die GUI, da sich ansonsten Nebenläufigkeits-
* Problematiken ergeben können.
*/
class UpdateFunctionality implements Runnable { class UpdateFunctionality implements Runnable {
// Serves to solve relations to the calling instance
private final MainApp CONTROLLER_INSTANCE; private final MainApp CONTROLLER_INSTANCE;
// Due to its length, the incoming datagram can't be handled in one step. Therefore a BufferedInputStream is chosen to buffer it until everything is properly processed.
private final BufferedInputStream INPUT_STREAM; private final BufferedInputStream INPUT_STREAM;
UpdateFunctionality (MainApp controller, InputStream in) { UpdateFunctionality (MainApp controller, InputStream in) {
...@@ -403,38 +431,37 @@ public class MainApp extends Application { ...@@ -403,38 +431,37 @@ public class MainApp extends Application {
INPUT_STREAM = new BufferedInputStream(in, Properties.OUT_BUFFER_SIZE); INPUT_STREAM = new BufferedInputStream(in, Properties.OUT_BUFFER_SIZE);
} }
// Looks up the correct model related to the datagram and updates it to the received values
public void parseDatagram (byte[] datagram) { public void parseDatagram (byte[] datagram) {
//System.out.println("2: "+DatatypeConverter.printHexBinary(datagram)); if (datagram.length < 5) { // Checks the given datagrams format
if (datagram.length < 5) {
setStatus("Error while updating!"); setStatus("Error while updating!");
return; return;
} }
for (Engine eng : engines){ for (Engine eng : engines){ // Checks all engines
if(eng.getMaerklinID().get()==((datagram[0]&0xFF)*(1<<8)+(datagram[1]&0xFF))){ if(eng.getMaerklinID().get()==((datagram[0]&0xFF)*(1<<8)+(datagram[1]&0xFF))){
int engSpeed = (datagram[2]&0xFF)*(1<<8)+(datagram[3]&0xFF); int engSpeed = (datagram[2]&0xFF)*(1<<8)+(datagram[3]&0xFF);
boolean engDirection = (datagram[4] == (0x01)); boolean engDirection = (datagram[4] == (0x01));
if (engSpeed != eng.getSpeed().get()) { if (engSpeed != eng.getSpeed().get()) {
eng.setSpeed(engSpeed); eng.setSpeed(engSpeed);
//System.out.println("Setting speed of engine "+(datagram[0]&0xFF)*(1<<8)+(datagram[1]&0xFF)+" to "+(datagram[2]&0xFF)*(1<<8)+(datagram[3]&0xFF)+".");
} }
if (engDirection != eng.getDirection().get()) { if (engDirection != eng.getDirection().get()) {
eng.setDirection(engDirection); eng.setDirection(engDirection);
//System.out.println("Setting direction of engine "+(datagram[0]&0xFF)*(1<<8)+(datagram[1]&0xFF)+" to "+datagram[4]+".");
} }
if (eng.equals(engineController.getSelectedEngine())){} if (eng.equals(engineController.getSelectedEngine())){}
} }
} }
for (Switch sw : switches){ for (Switch sw : switches){ // Checks all switches
if(sw.getMaerklinID().get()==((datagram[0]&0xFF)*(1<<8)+(datagram[1]&0xFF))){ if(sw.getMaerklinID().get()==((datagram[0]&0xFF)*(1<<8)+(datagram[1]&0xFF))){
boolean swState = !(datagram[4] == (byte) 0x01); boolean swState = !(datagram[4] == (byte) 0x01);
if (swState != sw.getState().get()) { if (swState != sw.getState().get()) {
sw.setState(swState); sw.setState(swState);
//System.out.println("Setting direction of switch "+(datagram[0]&0xFF)*(1<<8)+(datagram[1]&0xFF)+" to "+ datagram[4]+".");
} }
} }
} }
} }
// If a datagram is available, it will be scanned for its start (marked through two seperator-bytes right after another). Afterwards the data is read until the next seperator-byte or the datagram ends, the datas format is checked and, if everything is okay, the (clients) models are updated through parseDatagram.
@Override @Override
public void run() { public void run() {
int stopCounter, dataCounter, counter; int stopCounter, dataCounter, counter;
...@@ -447,36 +474,35 @@ public class MainApp extends Application { ...@@ -447,36 +474,35 @@ public class MainApp extends Application {
dataCounter = 0; dataCounter = 0;
counter = 0; counter = 0;
startByte = false; startByte = false;
while (INPUT_STREAM.available() > 0 && stopCounter < 5) { while (INPUT_STREAM.available() > 0 && stopCounter < 5) { // Checks if a datagram is available and not processed yet
buffer[counter%Properties.OUT_BUFFER_SIZE] = (byte) INPUT_STREAM.read(); buffer[counter%Properties.OUT_BUFFER_SIZE] = (byte) INPUT_STREAM.read();
if (stopCounter == 2) { if (stopCounter == 2) { // Checks for the datagrams start
startByte = true; startByte = true;
} }
if (buffer[counter%Properties.OUT_BUFFER_SIZE] == Properties.SEPERATOR && dataCounter != 4) { if (buffer[counter%Properties.OUT_BUFFER_SIZE] == Properties.SEPERATOR && dataCounter != 4) { // Handles seperator-bytes
dataCounter = 0; dataCounter = 0;
stopCounter++; stopCounter++;
} }
else { else { // Handles data-bytes
stopCounter = 0; stopCounter = 0;
dataCounter++; dataCounter++;
if (dataCounter == 5 && startByte) { if (dataCounter == 5 && startByte) { // If a full datagram consisting of five bytes is read, parseDatagram is called to handle the data
dataCounter = 0; dataCounter = 0;
for (int i = 0; i < Properties.IN_BUFFER_SIZE; i++) { for (int i = 0; i < Properties.IN_BUFFER_SIZE; i++) {
datagram[i] = buffer[counter-Properties.IN_BUFFER_SIZE+i+1]; datagram[i] = buffer[counter-Properties.IN_BUFFER_SIZE+i+1];
} }
//System.out.println("1: "+DatatypeConverter.printHexBinary(datagram));
parseDatagram(datagram); parseDatagram(datagram);
} }
else if (dataCounter > 5 && startByte) { else if (dataCounter > 5 && startByte) { // Checks if the datagrams have the correct format
throw (new Exception("Wrong data-update-format!")); throw (new Exception("Wrong data-update-format!"));
} }
} }
counter++; counter++;
} }
while (INPUT_STREAM.available() != 0) { while (INPUT_STREAM.available() != 0) { // Flushes the remaining buffer of the BufferedInputStream (doesn't contain any information), so that the next datagram will start at position 0 again (speeds up the next update-process)
INPUT_STREAM.read(); INPUT_STREAM.read();
} }
Thread.sleep(50); Thread.sleep(50); // Frees the resources between the status-updates
} }
} }
catch (Exception e) { catch (Exception e) {
......
...@@ -11,10 +11,8 @@ import javafx.beans.property.SimpleStringProperty; ...@@ -11,10 +11,8 @@ import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty; import javafx.beans.property.StringProperty;
import javafx.scene.image.Image; import javafx.scene.image.Image;
/** /*
* Model class for a Model Engine. * Model class for a Model Engine
*
* @author Philipp Stenkamp
*/ */
public class Engine { public class Engine {
...@@ -24,9 +22,7 @@ public class Engine { ...@@ -24,9 +22,7 @@ public class Engine {
private final BooleanProperty fwd; private final BooleanProperty fwd;
private final ObjectProperty<Image> img; private final ObjectProperty<Image> img;
/** // Default constructor
* Default constructor.
*/
public Engine() { public Engine() {
this(null, null, null); this(null, null, null);
} }
...@@ -35,13 +31,7 @@ public class Engine { ...@@ -35,13 +31,7 @@ public class Engine {
this(name, id, null); this(name, id, null);
} }
/** // Constructor with some initial data
* Constructor with some initial data.
*
* @param name
* @param id
*/
public Engine(String name, Integer id, String imgPath) { public Engine(String name, Integer id, String imgPath) {
this.name = new SimpleStringProperty(name); this.name = new SimpleStringProperty(name);
this.id = new SimpleIntegerProperty(id); this.id = new SimpleIntegerProperty(id);
...@@ -53,39 +43,48 @@ public class Engine { ...@@ -53,39 +43,48 @@ public class Engine {
} }
// Overrides the toString-functionality to return the instances value of name
@Override @Override
public String toString() { public String toString() {
return name.getValue(); return name.getValue();
} }
// Returns the name
public StringProperty getName() { public StringProperty getName() {
return name; return name;
} }
// Returns the maerklin-id
public IntegerProperty getMaerklinID() { public IntegerProperty getMaerklinID() {
return id; return id;
} }
// Returns the direction
public BooleanProperty getDirection() { public BooleanProperty getDirection() {
return fwd; return fwd;
} }
// Sets the direction
public void setDirection(Boolean fwd) { public void setDirection(Boolean fwd) {
this.fwd.set(fwd); this.fwd.set(fwd);
} }
// Returns the speed
public IntegerProperty getSpeed() { public IntegerProperty getSpeed() {
return speed; return speed;
} }
// Sets the speed
public void setSpeed(Integer speed) { public void setSpeed(Integer speed) {
this.speed.set(speed); this.speed.set(speed);
} }
// Returns the objects matching image
public ObjectProperty<Image> getImg(){ public ObjectProperty<Image> getImg(){
return img; return img;
} }
// Sets the objects matchin image
public void setImg(Image img){ public void setImg(Image img){
this.img.set(img); this.img.set(img);
} }
......
...@@ -6,26 +6,23 @@ import common.Properties; ...@@ -6,26 +6,23 @@ import common.Properties;
import javafx.beans.property.ObjectProperty; import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty; import javafx.beans.property.SimpleObjectProperty;
/*
/** * Model class for a Model Engine
* Model class for a Model Engine.
*
* @author Philipp Stenkamp
*/ */
public class Settings { public class Settings {
private final ObjectProperty<InetSocketAddress> server; private final ObjectProperty<InetSocketAddress> server;
/** // Default constructor
* Default constructor.
*/
public Settings() { public Settings() {
this.server = new SimpleObjectProperty<InetSocketAddress> (new InetSocketAddress("localhost", Properties.PORT)); this.server = new SimpleObjectProperty<InetSocketAddress> (new InetSocketAddress("localhost", Properties.PORT));
} }
// Returns the server (InetSocketAddress)
public ObjectProperty<InetSocketAddress> getSocketAddress() { public ObjectProperty<InetSocketAddress> getSocketAddress() {
return server; return server;
} }
// Sets the server (InetSocketAddress)
public void setSocketAddress(InetSocketAddress server) { public void setSocketAddress(InetSocketAddress server) {
this.server.set(server); this.server.set(server);
} }
......
...@@ -10,10 +10,8 @@ import javafx.beans.property.SimpleStringProperty; ...@@ -10,10 +10,8 @@ import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty; import javafx.beans.property.StringProperty;
import javafx.beans.property.ObjectProperty; import javafx.beans.property.ObjectProperty;
/** /*
* Model class for a Model Train Switch. * Model class for a Model Train Switch
*
* @author Philipp Stenkamp
*/ */
public class Switch{ public class Switch{
...@@ -22,24 +20,17 @@ public class Switch{ ...@@ -22,24 +20,17 @@ public class Switch{
private final BooleanProperty state; private final BooleanProperty state;
private final ObjectProperty<SwitchControl> controller; private final ObjectProperty<SwitchControl> controller;
/** // Default constructor
* Default constructor.
*/
public Switch() { public Switch() {
this(null, null, null); this(null, null, null);
} }
/** // Constructor with some initial data
* Constructor with some initial data.
*
* @param name
* @param id
*/
public Switch(String name, Integer id){ public Switch(String name, Integer id){
this(name, id, null); this(name, id, null);
} }
// Constructor with some more initial data
public Switch(String name, Integer id, SwitchControl controller) { public Switch(String name, Integer id, SwitchControl controller) {
this.name = new SimpleStringProperty(name); this.name = new SimpleStringProperty(name);
this.id = new SimpleIntegerProperty(id); this.id = new SimpleIntegerProperty(id);
...@@ -50,35 +41,43 @@ public class Switch{ ...@@ -50,35 +41,43 @@ public class Switch{
} }
// Overrides the toString-functionality to return the instances value of name
@Override @Override
public String toString() { public String toString() {
return name.getValue(); return name.getValue();
} }
// Returns the name
public StringProperty getName() { public StringProperty getName() {
return name; return name;
} }
// Sets the name
public void setName(String name) { public void setName(String name) {
this.name.set(name); this.name.set(name);
} }
// Returns the maerklin-id
public IntegerProperty getMaerklinID() { public IntegerProperty getMaerklinID() {
return id; return id;
} }
// Returns the state
public BooleanProperty getState() { public BooleanProperty getState() {
return state; return state;
} }
// Sets the state
public void setState(Boolean state) { public void setState(Boolean state) {
this.state.set(state); this.state.set(state);
} }
// Returns the objects matching controller-instance
public ObjectProperty<SwitchControl> getController(){ public ObjectProperty<SwitchControl> getController(){
return controller; return controller;
} }
// Sets the objects matching controller-instance
public void setController(SwitchControl controller){ public void setController(SwitchControl controller){
this.controller.setValue(controller); this.controller.setValue(controller);
} }
......
package server; package server;
import java.net.DatagramPacket; import java.net.DatagramPacket;
import java.net.DatagramSocket; import java.net.DatagramSocket;
import javax.xml.bind.DatatypeConverter; import javax.xml.bind.DatatypeConverter;
...@@ -101,7 +100,7 @@ public class UDPListener implements Runnable{ ...@@ -101,7 +100,7 @@ public class UDPListener implements Runnable{
for (Engine train : Engine.engines){ for (Engine train : Engine.engines){
if (train.getEngineID() == getAddress()){ if (train.getEngineID() == getAddress()){
train.engineSpeed = getEngineSpeed(); train.engineSpeed = getEngineSpeed();
System.out.println("Speed of Engine "+train.getEngineID()+" changed to "+train.getEngineSpeed()); // System.out.println("Speed of Engine "+train.getEngineID()+" changed to "+train.getEngineSpeed());
break; break;
} }
} }
...@@ -111,7 +110,7 @@ public class UDPListener implements Runnable{ ...@@ -111,7 +110,7 @@ public class UDPListener implements Runnable{
if (train.getEngineID() == getAddress()){ if (train.getEngineID() == getAddress()){
train.engineDirection = getEngineDirection(); train.engineDirection = getEngineDirection();
train.engineSpeed = 0; train.engineSpeed = 0;
System.out.println("Direction of Engine "+train.getEngineID()+" changed to "+train.getEngineDirection()); // System.out.println("Direction of Engine "+train.getEngineID()+" changed to "+train.getEngineDirection());
break; break;
} }
} }
...@@ -120,7 +119,7 @@ public class UDPListener implements Runnable{ ...@@ -120,7 +119,7 @@ public class UDPListener implements Runnable{
for (Switch sw : Switch.switches){ for (Switch sw : Switch.switches){
if (sw.getSwitchID() == getAddress()){ if (sw.getSwitchID() == getAddress()){
sw.switchDirection = getEngineDirection(); sw.switchDirection = getEngineDirection();
System.out.println("Direction of Switch "+sw.getSwitchID()+" changed to "+sw.getSwitchDirection()); // System.out.println("Direction of Switch "+sw.getSwitchID()+" changed to "+sw.getSwitchDirection());
break; break;
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment