Skip to content
Snippets Groups Projects
Commit ee57b8a4 authored by Philipp Stenkamp's avatar Philipp Stenkamp
Browse files

Merge branch 'master' of https://gitlab.cvh-server.de/lf.ps/VInf

parents 05cad7ae 51c48d0b
No related branches found
No related tags found
No related merge requests found
Showing
with 116 additions and 71 deletions
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -7,7 +7,7 @@ public class Properties {
// Setting ports and defining size of the buffers
public final static int IN_BUFFER_SIZE = 5; // First and second byte: accessory-ID, third byte: command, fourth and fifth byte: potential parameters
public final static int OUT_BUFFER_SIZE = 1024;
public final static int OUT_BUFFER_SIZE = 128;
// Defining client-commands
public final static byte SEPERATOR = (byte) 0xFE;
......
......@@ -328,6 +328,7 @@ public class MainApp extends Application {
@Override
public void run() {
Thread.yield();
if (!connectionEstablished) {
try (Socket socket = new Socket(configuration.getSocketAddress().get().getHostName(), configuration.getSocketAddress().get().getPort())) {
client = socket;
......@@ -348,6 +349,7 @@ public class MainApp extends Application {
});
while (connectionEstablished) {
Thread.sleep(100);
}
}
catch (Exception e) {
......@@ -361,6 +363,9 @@ public class MainApp extends Application {
});
e.printStackTrace();
}
finally {
CONTROLLER_INSTANCE.UTILITY_THREAD_EXECUTOR.shutdownNow();
}
}
}
}
......@@ -376,28 +381,43 @@ public class MainApp extends Application {
}
public void parseDatagram (byte[] datagram) {
System.out.println("2: "+DatatypeConverter.printHexBinary(datagram));
if (datagram.length < 5) {
setStatus("Error while updating!");
return;
}
for (Engine eng : engines){
if(eng.getMaerklinID().get()==((datagram[0]&0xFF)*(1<<8)+(datagram[1]&0xFF))){
eng.setSpeed((datagram[2]&0xFF)*(1<<8)+(datagram[3]&0xFF));
int engSpeed = (datagram[2]&0xFF)*(1<<8)+(datagram[3]&0xFF);
boolean engDirection = (datagram[4] == (0x01));
if (engSpeed != eng.getSpeed().get()) {
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)+".");
eng.setDirection(datagram[4] == (0x01));
}
if (engDirection != eng.getDirection().get()) {
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()))
engineController.updateEngineStatus();
}
if (eng.equals(engineController.getSelectedEngine())){}
}
}
for (Switch sw : switches){
if(sw.getMaerklinID().get()==((datagram[0]&0xFF)*(1<<8)+(datagram[1]&0xFF))){
sw.setState(!(datagram[4] == (byte) 0x01));
boolean swState = !(datagram[4] == (byte) 0x01);
if (swState != sw.getState().get()) {
sw.setState(swState);
//System.out.println("Setting direction of switch "+(datagram[0]&0xFF)*(1<<8)+(datagram[1]&0xFF)+" to "+ datagram[4]+".");
sw.getController().get().updateSwitchStatus();
}
}
}
}
public void updateGUI () {
engineController.updateEngineStatus();
for (Switch sw : switches){
sw.getController().get().updateSwitchStatus();
}
}
@Override
public void run() {
......@@ -428,22 +448,27 @@ public class MainApp extends Application {
for (int i = 0; i < Properties.IN_BUFFER_SIZE; i++) {
datagram[i] = buffer[counter-Properties.IN_BUFFER_SIZE+i+1];
}
//System.out.println(DatatypeConverter.printHexBinary(datagram));
Platform.runLater(new Runnable() {
@Override
public void run() {
System.out.println("1: "+DatatypeConverter.printHexBinary(datagram));
parseDatagram(datagram);
}
});
}
else if (dataCounter > 5 && startByte) {
throw (new Exception("Wrong data-update-format!"));
}
}
counter++;
}
while (INPUT_STREAM.available() != 0) {
INPUT_STREAM.read();
}
Platform.runLater(new Runnable() {
@Override
public void run() {
updateGUI();
}
});
Thread.sleep(50);
}
}
catch (Exception e) {
......
......@@ -23,7 +23,7 @@ public class MaerklinServer{
private UDPListener overwatch;
private ServerThread serverThread;
final private ExecutorService SERVER_THREAD_EXECUTOR;
final ExecutorService SERVER_THREAD_EXECUTOR;
final ExecutorService CLIENT_THREAD_EXECUTOR;
final ExecutorService UTILITY_THREAD_EXECUTOR;
......@@ -154,7 +154,8 @@ public class MaerklinServer{
try {
//System.out.println("Updating client: "+clientSocket.getRemoteSocketAddress());
UTILITY_THREAD_EXECUTOR.submit(new UpdateThread(clientSocket.getOutputStream()));
} catch (IOException e) {
}
catch (IOException e) {
System.out.println("Failed to receive output-stream of one of the clients!");
}
}
......@@ -188,6 +189,11 @@ class ServerThread implements Runnable {
System.out.println("Accessing user-interaction-port failed! Trying again!");
run();
}
finally {
SERVER_INSTANCE.SERVER_THREAD_EXECUTOR.shutdownNow();
SERVER_INSTANCE.CLIENT_THREAD_EXECUTOR.shutdownNow();
SERVER_INSTANCE.UTILITY_THREAD_EXECUTOR.shutdownNow();
}
}
}
......@@ -215,6 +221,7 @@ class ClientThread implements Runnable {
int buffer;
while (true) {
try {
if (IN_STREAM.available() != 0) {
buffer = IN_STREAM.read();
if ((byte) buffer == (byte) Properties.SESSION_ABORT) {
SERVER_INSTANCE.clients.remove(CLIENT);
......@@ -238,6 +245,10 @@ class ClientThread implements Runnable {
SERVER_INSTANCE.UTILITY_THREAD_EXECUTOR.submit(new HandleThread(SERVER_INSTANCE, data));
}
}
else {
Thread.sleep(10);
}
}
catch (Exception e) {
System.out.println("An error occured while reading the clients input-stream!");
SERVER_INSTANCE.clients.remove(CLIENT);
......@@ -377,7 +388,7 @@ class UpdateThread implements Runnable {
outgoingData[position++] = Properties.SEPERATOR;
outgoingData[position++] = Properties.SEPERATOR;
for (Engine train : Engine.engines) {
if (position >= 1016) {
if (position >= Properties.OUT_BUFFER_SIZE-9) {
Exception e = new Exception("Overload of registered elements! Update-buffer too small!");
throw e;
}
......@@ -392,7 +403,7 @@ class UpdateThread implements Runnable {
outgoingData[position++] = Properties.SEPERATOR;
outgoingData[position++] = Properties.SEPERATOR;
for (Switch sw : Switch.switches) {
if (position >= 1018) {
if (position >= Properties.OUT_BUFFER_SIZE-10) {
Exception e = new Exception("Overload of registered elements! Update-buffer too small!");
throw e;
}
......
......@@ -3,7 +3,7 @@ package server;
public class MaerklinServerApplication {
// Defining a threshold value for when the status-update is to be executed (in milliseconds)
private final static long updateThreshold = 1000;
private final static long updateThreshold = 500;
public static void main(String[] args) {
......@@ -24,6 +24,13 @@ public class MaerklinServerApplication {
server.statusUpdate();
timestamp = System.currentTimeMillis();
}
else {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
......
common/qr.repo.VInf.png

837 B

Student Repositiory for Advanced Computer Science (Vertiefung Informatik)
<img src="/common/qr.repo.VInf.png" alt="QR-Code which takes you to this repository" align="right">
Student Repositiory for Computer Science (Vertiefung Informatik)
----------------------------------------------------------------------
Copyright (C) 2016, 2017 Lukas Friedrichsen & Philipp Stenkamp
{ [lukas.friedrichsen](mailto:lukas.friedrichsen@hs-bochum.de), [philipp.stenkamp](mailto:philipp.stenkamp@hs-bochum.de) }@hs-bochum.de
---
These documents are free software; you can redistribute them and/or
modify them under the terms and conditions of the following licenses:
......@@ -10,7 +12,7 @@ modify them under the terms and conditions of the following licenses:
- either the GNU General Public License, version 3 or, at your option, any later version,
- or the Creative Commons Attribution-ShareAlike 3.0 Unported License
- \*.c, \*.cpp, \*.h, Makefile*: programmes
- \*.java, \*.fxml: programmes and Java FX 8 layout files
- either the Modified BSD License,
- or the Creative Commons Attribution-ShareAlike 3.0 Unported License
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment