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

you wanna fuckin kiddin me eclipse?!

parent 6d32e933
Branches
No related tags found
No related merge requests found
Showing
with 30 additions and 23 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
No preview for this file type
...@@ -329,6 +329,7 @@ public class MainApp extends Application { ...@@ -329,6 +329,7 @@ public class MainApp extends Application {
out = client.getOutputStream(); out = client.getOutputStream();
connectionEstablished = true; connectionEstablished = true;
running = true; running = true;
(new Thread(new UpdateFunctionality(CONTROLLER_INSTANCE, in))).start();
Platform.runLater(new Runnable() { Platform.runLater(new Runnable() {
@Override @Override
...@@ -338,9 +339,7 @@ public class MainApp extends Application { ...@@ -338,9 +339,7 @@ public class MainApp extends Application {
} }
}); });
(new Thread(new UpdateFunctionality(CONTROLLER_INSTANCE, in))).start();
while (connectionEstablished) { while (connectionEstablished) {
} }
} }
catch (Exception e) { catch (Exception e) {
...@@ -369,6 +368,7 @@ public class MainApp extends Application { ...@@ -369,6 +368,7 @@ public class MainApp extends Application {
} }
public void parseDatagram (byte[] datagram){ public void parseDatagram (byte[] datagram){
System.out.println("HALLLLLOOOOO!!!");
if (datagram.length < 5) { if (datagram.length < 5) {
setStatus("Error while updating!"); setStatus("Error while updating!");
return; return;
...@@ -396,24 +396,28 @@ public class MainApp extends Application { ...@@ -396,24 +396,28 @@ public class MainApp extends Application {
byte datagram[] = new byte[Properties.IN_BUFFER_SIZE]; byte datagram[] = new byte[Properties.IN_BUFFER_SIZE];
boolean startByte; boolean startByte;
try { try {
while (connectionEstablished) {
stopCounter = 0; stopCounter = 0;
dataCounter = 0; dataCounter = 0;
counter = 0; counter = 0;
startByte = false; startByte = false;
while (connectionEstablished) {
while (INPUT_STREAM.available() > 0 && stopCounter < 3) { while (INPUT_STREAM.available() > 0 && stopCounter < 3) {
buffer[counter%Properties.OUT_BUFFER_SIZE] = (byte) INPUT_STREAM.read(); buffer[counter%Properties.OUT_BUFFER_SIZE] = (byte) INPUT_STREAM.read();
if (stopCounter == 1) { if (stopCounter == 1) {
System.out.println("startbyte");
startByte = true; startByte = true;
} }
if (buffer[counter%Properties.OUT_BUFFER_SIZE] == Properties.SEPERATOR && dataCounter != 3) { if (buffer[counter%Properties.OUT_BUFFER_SIZE] == Properties.SEPERATOR && dataCounter != 3) {
System.out.println("stop++");
dataCounter = 0; dataCounter = 0;
stopCounter++; stopCounter++;
} }
else { else {
System.out.println("data++");
stopCounter = 0; stopCounter = 0;
dataCounter++; dataCounter++;
if (dataCounter == 4 && startByte) { if (dataCounter == 4 && startByte) {
System.out.println("parse");
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]; datagram[i] = buffer[counter-Properties.IN_BUFFER_SIZE+i];
} }
......
...@@ -150,8 +150,10 @@ public class MaerklinServer{ ...@@ -150,8 +150,10 @@ public class MaerklinServer{
// Starts the thread to broadcast the current status // Starts the thread to broadcast the current status
public void statusUpdate(){ public void statusUpdate(){
System.out.println("Anzahl verbundene Clients: "+clients.size());
for (Socket clientSocket : clients) { for (Socket clientSocket : clients) {
try { try {
System.out.println("Updating client: "+clientSocket.getRemoteSocketAddress());
(new Thread(new UpdateThread(clientSocket.getOutputStream(), Properties.OUT_BUFFER_SIZE))).start(); (new Thread(new UpdateThread(clientSocket.getOutputStream(), Properties.OUT_BUFFER_SIZE))).start();
} catch (IOException e) { } catch (IOException e) {
System.out.println("Failed to receive output-stream of one of the clients!"); System.out.println("Failed to receive output-stream of one of the clients!");
...@@ -194,13 +196,12 @@ class ClientThread implements Runnable { ...@@ -194,13 +196,12 @@ class ClientThread implements Runnable {
return; return;
} }
else if ((byte) buffer == (byte) Properties.SEPERATOR) { else if ((byte) buffer == (byte) Properties.SEPERATOR) {
System.out.println("New datagram starts!");
for (int i = 0; i < IN_BUFFER_SIZE; i++) { for (int i = 0; i < IN_BUFFER_SIZE; i++) {
buffer = IN_STREAM.read(); buffer = IN_STREAM.read();
if ((byte) buffer == (byte) Properties.SESSION_ABORT) { if ((byte) buffer == (byte) Properties.SESSION_ABORT) {
SERVER_INSTANCE.clients.remove(CLIENT); SERVER_INSTANCE.clients.remove(CLIENT);
CLIENT.close(); CLIENT.close();
System.out.println("Conenction to client "+CLIENT.getRemoteSocketAddress()+" aborted!"); System.out.println("Connection to client "+CLIENT.getRemoteSocketAddress()+" aborted!");
return; return;
} }
else { else {
...@@ -237,21 +238,21 @@ class HandleThread implements Runnable { ...@@ -237,21 +238,21 @@ class HandleThread implements Runnable {
try { try {
switch (data[2]) { switch (data[2]) {
case Properties.SYSTEM_STOP: case Properties.SYSTEM_STOP:
System.out.println("System stop"); //System.out.println("System stop");
serverInstance.emergencyStop(); serverInstance.emergencyStop();
break; break;
case Properties.SYSTEM_GO: case Properties.SYSTEM_GO:
System.out.println("System go"); //System.out.println("System go");
serverInstance.start(); serverInstance.start();
break; break;
case Properties.GET_STATUS: case Properties.GET_STATUS:
System.out.println("Status update"); //System.out.println("Status update");
serverInstance.statusUpdate(); serverInstance.statusUpdate();
break; break;
case Properties.ENGINE_SET_SPEED: case Properties.ENGINE_SET_SPEED:
for (Engine train : Engine.engines) { for (Engine train : Engine.engines) {
if (train.getEngineID() == ((data[0]&0xFF)*(1<<8)+(data[1]&0xFF))) { if (train.getEngineID() == ((data[0]&0xFF)*(1<<8)+(data[1]&0xFF))) {
System.out.println("Set engine speed"); //System.out.println("Set engine speed");
train.setEngineSpeed((data[3]&0xFF)*(1<<8)+(data[4]&0xFF)); train.setEngineSpeed((data[3]&0xFF)*(1<<8)+(data[4]&0xFF));
break; break;
} }
...@@ -260,7 +261,7 @@ class HandleThread implements Runnable { ...@@ -260,7 +261,7 @@ class HandleThread implements Runnable {
case Properties.ENGINE_INCREASE_SPEED: case Properties.ENGINE_INCREASE_SPEED:
for (Engine train : Engine.engines) { for (Engine train : Engine.engines) {
if (train.getEngineID() == ((data[0]&0xFF)*(1<<8)+(data[1]&0xFF))) { if (train.getEngineID() == ((data[0]&0xFF)*(1<<8)+(data[1]&0xFF))) {
System.out.println("Increase engine speed"); //System.out.println("Increase engine speed");
train.increaseEngineSpeed(); train.increaseEngineSpeed();
break; break;
} }
...@@ -269,7 +270,7 @@ class HandleThread implements Runnable { ...@@ -269,7 +270,7 @@ class HandleThread implements Runnable {
case Properties.ENGINE_DECREASE_SPEED: case Properties.ENGINE_DECREASE_SPEED:
for (Engine train : Engine.engines) { for (Engine train : Engine.engines) {
if (train.getEngineID() == ((data[0]&0xFF)*(1<<8)+(data[1]&0xFF))) { if (train.getEngineID() == ((data[0]&0xFF)*(1<<8)+(data[1]&0xFF))) {
System.out.println("Decrease engine speed"); //System.out.println("Decrease engine speed");
train.decreaseEngineSpeed(); train.decreaseEngineSpeed();
break; break;
} }
...@@ -278,7 +279,7 @@ class HandleThread implements Runnable { ...@@ -278,7 +279,7 @@ class HandleThread implements Runnable {
case Properties.ENGINE_SET_DIRECTION: case Properties.ENGINE_SET_DIRECTION:
for (Engine train : Engine.engines) { for (Engine train : Engine.engines) {
if (train.getEngineID() == ((data[0]&0xFF)*(1<<8)+(data[1]&0xFF))) { if (train.getEngineID() == ((data[0]&0xFF)*(1<<8)+(data[1]&0xFF))) {
System.out.println("Set engine direction"); //System.out.println("Set engine direction");
train.setEngineDirection(data[3]); train.setEngineDirection(data[3]);
break; break;
} }
...@@ -287,7 +288,7 @@ class HandleThread implements Runnable { ...@@ -287,7 +288,7 @@ class HandleThread implements Runnable {
case Properties.ENGINE_SWITCH_DIRECTION: case Properties.ENGINE_SWITCH_DIRECTION:
for (Engine train : Engine.engines) { for (Engine train : Engine.engines) {
if (train.getEngineID() == ((data[0]&0xFF)*(1<<8)+(data[1]&0xFF))) { if (train.getEngineID() == ((data[0]&0xFF)*(1<<8)+(data[1]&0xFF))) {
System.out.println("Switch engine direction"); //System.out.println("Switch engine direction");
train.setEngineDirection(3); train.setEngineDirection(3);
break; break;
} }
...@@ -296,7 +297,7 @@ class HandleThread implements Runnable { ...@@ -296,7 +297,7 @@ class HandleThread implements Runnable {
case Properties.SWITCH_SET_DIRECTION: case Properties.SWITCH_SET_DIRECTION:
for (Switch sw : Switch.switches) { for (Switch sw : Switch.switches) {
if (sw.getSwitchID() == ((data[0]&0xFF)*(1<<8)+(data[1]&0xFF))) { if (sw.getSwitchID() == ((data[0]&0xFF)*(1<<8)+(data[1]&0xFF))) {
System.out.println("Set switch direction"); //System.out.println("Set switch direction");
sw.setSwitchDirection(data[3]); sw.setSwitchDirection(data[3]);
break; break;
} }
...@@ -305,7 +306,7 @@ class HandleThread implements Runnable { ...@@ -305,7 +306,7 @@ class HandleThread implements Runnable {
case Properties.SWITCH_SWITCH_DIRECTION: case Properties.SWITCH_SWITCH_DIRECTION:
for (Switch sw : Switch.switches) { for (Switch sw : Switch.switches) {
if (sw.getSwitchID() == ((data[0]&0xFF)*(1<<8)+(data[1]&0xFF))) { if (sw.getSwitchID() == ((data[0]&0xFF)*(1<<8)+(data[1]&0xFF))) {
System.out.println("Switch switch direction"); //System.out.println("Switch switch direction");
sw.changeSwitchDirection(); sw.changeSwitchDirection();
break; break;
} }
......
...@@ -13,14 +13,15 @@ public class MaerklinServerApplication { ...@@ -13,14 +13,15 @@ public class MaerklinServerApplication {
// Calling constructor // Calling constructor
MaerklinServer server = new MaerklinServer(); MaerklinServer server = new MaerklinServer();
// Starting the client-handling
server.listen();
// Starting listener-thread // Starting listener-thread
server.startListener(); server.startListener();
// Starting the client-handling
server.listen();
while (true){ while (true){
if (System.currentTimeMillis()-timestamp >= updateThreshold) { if (System.currentTimeMillis()-timestamp >= updateThreshold) {
System.exit(0);
server.statusUpdate(); server.statusUpdate();
timestamp = System.currentTimeMillis(); timestamp = System.currentTimeMillis();
} }
......
...@@ -48,7 +48,7 @@ public class UDPListener implements Runnable{ ...@@ -48,7 +48,7 @@ public class UDPListener implements Runnable{
} }
public int getCommand(){ public int getCommand(){
return (in[1]&0xFF); return ((in[1]&0xFF)&0xFE);
} }
public boolean isAnswer(){ public boolean isAnswer(){
...@@ -68,7 +68,7 @@ public class UDPListener implements Runnable{ ...@@ -68,7 +68,7 @@ public class UDPListener implements Runnable{
System.arraycopy(data, 0, kennung, 0, 4); System.arraycopy(data, 0, kennung, 0, 4);
System.arraycopy(data, 4, laenge, 0, 1); System.arraycopy(data, 4, laenge, 0, 1);
System.arraycopy(data, 5, nutzdaten, 0, 8); System.arraycopy(data, 5, nutzdaten, 0, 8);
//System.out.print("\n\nKennung DLC Nutzdaten\n" + DatatypeConverter.printHexBinary(kennung) + " " + DatatypeConverter.printHexBinary(laenge) + " " + DatatypeConverter.printHexBinary(nutzdaten)); System.out.print("\n\nKennung DLC Nutzdaten\n" + DatatypeConverter.printHexBinary(kennung) + " " + DatatypeConverter.printHexBinary(laenge) + " " + DatatypeConverter.printHexBinary(nutzdaten));
} }
catch(Exception e){ catch(Exception e){
e.printStackTrace(); e.printStackTrace();
...@@ -85,6 +85,7 @@ public class UDPListener implements Runnable{ ...@@ -85,6 +85,7 @@ public class UDPListener implements Runnable{
@Override @Override
public void run() { public void run() {
//System.out.println("UDPListener start!");
try(DatagramSocket socket = new DatagramSocket(port)){ try(DatagramSocket socket = new DatagramSocket(port)){
socket.setReuseAddress(true); socket.setReuseAddress(true);
while (true){ while (true){
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment