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

debugging in progress

parent caf6c8fc
Branches
No related tags found
No related merge requests found
Showing
with 69 additions and 54 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
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -3,7 +3,7 @@ package common;
public class Properties {
// Defining server-ip and port
public final static int PORT = 1234567;
public final static int PORT = 23456;
// 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 fifth byte: potential parameters
......
......@@ -323,16 +323,15 @@ public class MainApp extends Application {
}
}
else {
try (Socket socket = new Socket()) {
socket.bind(configuration.getSocketAddress().get());
try (Socket socket = new Socket(configuration.getSocketAddress().get().getHostName(), configuration.getSocketAddress().get().getPort())) {
client = socket;
client.setKeepAlive(true);
client.setReuseAddress(true);
in = client.getInputStream();
out = client.getOutputStream();
client.setSoTimeout(200);
connectionEstablished = true;
setStatus("Verbindung zu "+client.getRemoteSocketAddress()+" aufgebaut!");
Platform.runLater(new updateThread(this));
(new updateThread(this, in)).run();
}
catch (Exception e) {
setStatus("Error at: establishConnection (establishing connection");
......@@ -343,10 +342,12 @@ public class MainApp extends Application {
class updateThread implements Runnable {
MainApp controllerInstance;
private final MainApp CONTROLLER_INSTANCE;
private final BufferedInputStream INPUT_STREAM;
updateThread (MainApp controller) {
controllerInstance = controller;
updateThread (MainApp controller, InputStream in) {
CONTROLLER_INSTANCE = controller;
INPUT_STREAM = new BufferedInputStream(in, Properties.OUT_BUFFER_SIZE);
}
@Override
......@@ -361,8 +362,8 @@ public class MainApp extends Application {
counter = 0;
startByte = false;
while (connectionEstablished) {
while (in.available() > 0 && stopCounter < 3) {
buffer[counter%Properties.OUT_BUFFER_SIZE] = (byte) in.read();
while (INPUT_STREAM.available() > 0 && stopCounter < 3) {
buffer[counter%Properties.OUT_BUFFER_SIZE] = (byte) INPUT_STREAM.read();
if (stopCounter == 1) {
startByte = true;
}
......@@ -377,7 +378,7 @@ public class MainApp extends Application {
for (int i = 0; i < Properties.IN_BUFFER_SIZE; i++) {
datagram[i] = buffer[counter-Properties.IN_BUFFER_SIZE+i];
}
controllerInstance.parseDatagram(datagram);
CONTROLLER_INSTANCE.parseDatagram(datagram);
}
else if (dataCounter > 4 && startByte) {
throw (new Exception("Wrong data-update-format!"));
......@@ -387,7 +388,7 @@ public class MainApp extends Application {
}
}
catch (Exception e) {
controllerInstance.setStatus("Error while updating!");
CONTROLLER_INSTANCE.setStatus("Error while updating!");
}
}
}
......
......@@ -58,10 +58,10 @@ public class Engine {
}
udpProtocol.changeEngineDirection(engineID, engineDirection);
listener.listen();
if (engineDirection != listener.getEngineDirection()){
/*if (engineDirection != listener.getEngineDirection()){
Exception e = new Exception("Communication Error!");
throw e;
}
}*/
}
public void setEngineSpeed(int newEngineSpeed) throws Exception {
......@@ -74,30 +74,30 @@ public class Engine {
}
udpProtocol.changeEngineSpeed(engineID, engineSpeed);
listener.listen();
if (engineSpeed != listener.getEngineSpeed()){
/*if (engineSpeed != listener.getEngineSpeed()){
Exception e = new Exception("Communication Error!");
throw e;
}
}*/
}
public void increaseEngineSpeed() throws Exception {
engineSpeed = engineSpeed+50;
udpProtocol.changeEngineSpeed(engineID, engineSpeed);
listener.listen();
if (engineSpeed != listener.getEngineSpeed()){
/*if (engineSpeed != listener.getEngineSpeed()){
Exception e = new Exception("Communication Error!");
throw e;
}
}*/
}
public void decreaseEngineSpeed() throws Exception{
engineSpeed = engineSpeed-50;
udpProtocol.changeEngineSpeed(engineID, engineSpeed);
listener.listen();
if (engineSpeed != listener.getEngineSpeed()){
/*if (engineSpeed != listener.getEngineSpeed()){
Exception e = new Exception("Communication Error!");
throw e;
}
}*/
}
public int getEngineID(){
......
package server;
import java.net.*;
import javax.xml.bind.DatatypeConverter;
public class MaerklinProtocol {
private final static int port = 15731;
......@@ -15,8 +12,9 @@ public class MaerklinProtocol {
}
private void writeDatagram(String ip, int port, byte[] data){
System.out.printf("\nGesendet: " + DatatypeConverter.printHexBinary(data));
//System.out.printf("\nGesendet: " + DatatypeConverter.printHexBinary(data));
try(DatagramSocket socket = new DatagramSocket()){
socket.setReuseAddress(true);
InetAddress address = InetAddress.getByName(ip);
DatagramPacket outgoingData = new DatagramPacket(data, data.length, address, port);
socket.send(outgoingData);
......
package server;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
......@@ -38,6 +39,8 @@ public class MaerklinServer{
public MaerklinServer(){
// Initializing
System.out.println("Initializing...");
clients = new ArrayList<Socket>();
protocol = new MaerklinProtocol();
......@@ -102,25 +105,25 @@ public class MaerklinServer{
// Initialize; setting ("safe") values
public void initialize() throws Exception {
ice.setEngineDirection(0);
ice.setEngineDirection(1);
ice.setEngineSpeed(0);
lok.setEngineDirection(0);
lok.setEngineDirection(1);
lok.setEngineSpeed(0);
reichsbahn.setEngineDirection(0);
reichsbahn.setEngineDirection(1);
reichsbahn.setEngineSpeed(0);
switch4.setSwitchDirection(0);
switch5.setSwitchDirection(0);
switch6.setSwitchDirection(0);
switch8.setSwitchDirection(0);
switch9.setSwitchDirection(0);
switch10.setSwitchDirection(0);
switch12.setSwitchDirection(0);
switch13.setSwitchDirection(0);
switch14.setSwitchDirection(0);
switch15.setSwitchDirection(0);
switch4.setSwitchDirection(1);
switch5.setSwitchDirection(1);
switch6.setSwitchDirection(1);
switch8.setSwitchDirection(1);
switch9.setSwitchDirection(1);
switch10.setSwitchDirection(1);
switch12.setSwitchDirection(1);
switch13.setSwitchDirection(1);
switch14.setSwitchDirection(1);
switch15.setSwitchDirection(1);
}
// Starts the listener-thread
......@@ -132,11 +135,16 @@ public class MaerklinServer{
public void listen(){
try(ServerSocket serverSocket = new ServerSocket(Properties.PORT)){
serverSocket.setReuseAddress(true);
System.out.println("Server-socket established!");
while (true) {
Socket socket = serverSocket.accept();
System.out.println("Connection to client "+socket.getRemoteSocketAddress()+" established!");
(new ClientThread(this, socket, socket.getInputStream(), Properties.IN_BUFFER_SIZE)).run();
}
}
catch (Exception e) {
System.out.println("Accessing user-interaction-port failed! Trying again!");
listen();
}
}
......@@ -158,7 +166,7 @@ class ClientThread implements Runnable {
// Declaration of objects
private final Socket CLIENT;
private final InputStream IN_STREAM;
private final BufferedInputStream IN_STREAM;
private final int IN_BUFFER_SIZE;
private final MaerklinServer SERVER_INSTANCE;
byte[] data;
......@@ -168,7 +176,7 @@ class ClientThread implements Runnable {
// Constructor
ClientThread (MaerklinServer server, Socket socket, InputStream in, int bufferSize) {
CLIENT = socket;
IN_STREAM = in;
IN_STREAM = new BufferedInputStream(in);
IN_BUFFER_SIZE = bufferSize;
SERVER_INSTANCE = server;
SERVER_INSTANCE.clients.add(socket);
......@@ -179,24 +187,30 @@ class ClientThread implements Runnable {
@Override
public void run() {
int buffer;
while (true) {
try {
dataRaw[counter] = IN_STREAM.read();
if (dataRaw[counter++] == Properties.SESSION_ABORT) {
buffer = IN_STREAM.read();
if (buffer == Properties.SESSION_ABORT) {
SERVER_INSTANCE.clients.remove(CLIENT);
CLIENT.close();
System.out.println("Conenction to client "+CLIENT.getRemoteSocketAddress()+" aborted!");
return;
}
else if (counter == IN_BUFFER_SIZE) {
else {
dataRaw[counter++] = buffer;
}
if (counter == IN_BUFFER_SIZE) {
for (int i = 0; i < IN_BUFFER_SIZE; i++){
data[i] = (byte) (dataRaw[i]%(1<<8));
}
(new HandleThread(SERVER_INSTANCE, data)).run();
counter = -1;
counter = 0;
}
}
catch (Exception e) {
System.out.println("An error occured while reading client-input-stream!");
return;
}
}
}
......
......@@ -13,15 +13,17 @@ public class MaerklinServerApplication {
// Calling constructor
MaerklinServer server = new MaerklinServer();
// Starting the client-handling
server.listen();
// Starting listener-thread
server.startListener();
while (true){
if (System.currentTimeMillis()-timestamp >= updateThreshold) {
server.statusUpdate();
//server.statusUpdate();
timestamp = System.currentTimeMillis();
}
server.listen();
}
}
......
......@@ -47,10 +47,10 @@ public class Switch {
}
udpProtocol.changeSwitchDirection(switchID, switchDirection);
listener.listen();
if (switchDirection != listener.getSwitchDirection()){
/*if (switchDirection != listener.getSwitchDirection()){
Exception e = new Exception("Communication Error!");
throw e;
}
}*/
}
public void changeSwitchDirection() throws Exception {
......
......@@ -69,7 +69,7 @@ public class UDPListener implements Runnable{
System.arraycopy(data, 0, kennung, 0, 4);
System.arraycopy(data, 4, laenge, 0, 1);
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){
e.printStackTrace();
......@@ -93,7 +93,7 @@ public class UDPListener implements Runnable{
for (Engine train : Engine.engines){
if (train.getEngineID() == getAddress()){
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;
}
}
......@@ -102,7 +102,7 @@ public class UDPListener implements Runnable{
for (Engine train : Engine.engines){
if (train.getEngineID() == getAddress()){
train.engineDirection = getEngineDirection();
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;
}
}
......@@ -111,7 +111,7 @@ public class UDPListener implements Runnable{
for (Switch sw : Switch.switches){
if (sw.getSwitchID() == getAddress()){
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;
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment