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

IT LIVES

parent 6144e418
Branches
No related tags found
No related merge requests found
Showing
with 108 additions and 66 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
......@@ -4,6 +4,10 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.xml.bind.DatatypeConverter;
import gui.model.Engine;
import gui.model.Switch;
......@@ -49,6 +53,8 @@ public class MainApp extends Application {
private InputStream in;
private OutputStream out;
final ExecutorService UTILITY_THREAD_EXECUTOR;
private Boolean connectionEstablished = false;
private Boolean running = false;
......@@ -56,6 +62,8 @@ public class MainApp extends Application {
* Constructor
*/
public MainApp() {
UTILITY_THREAD_EXECUTOR = Executors.newFixedThreadPool(2);
configuration = new Settings();
// Add the already known engines
......@@ -306,7 +314,7 @@ public class MainApp extends Application {
}
}
else {
(new Thread(new EstablishConnection(this))).start();;
UTILITY_THREAD_EXECUTOR.submit((new EstablishConnection(this)));
}
}
......@@ -329,7 +337,7 @@ public class MainApp extends Application {
out = client.getOutputStream();
connectionEstablished = true;
running = true;
(new Thread(new UpdateFunctionality(CONTROLLER_INSTANCE, in))).start();
UTILITY_THREAD_EXECUTOR.submit((new UpdateFunctionality(CONTROLLER_INSTANCE, in)));
Platform.runLater(new Runnable() {
@Override
......@@ -368,7 +376,6 @@ public class MainApp extends Application {
}
public void parseDatagram (byte[] datagram) {
System.out.println("HALLLLLOOOOO!!!");
if (datagram.length < 5) {
setStatus("Error while updating!");
return;
......@@ -376,7 +383,9 @@ public class MainApp extends Application {
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));
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));
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();
}
......@@ -384,6 +393,7 @@ public class MainApp extends Application {
for (Switch sw : switches){
if(sw.getMaerklinID().get()==((datagram[0]&0xFF)*(1<<8)+(datagram[1]&0xFF))){
sw.setState(datagram[4] == (byte) 0x01);
System.out.println("Setting direction of switch "+(datagram[0]&0xFF)*(1<<8)+(datagram[1]&0xFF)+" to "+ datagram[4]+".");
sw.getController().get().updateSwitchStatus();
}
}
......@@ -401,26 +411,24 @@ public class MainApp extends Application {
dataCounter = 0;
counter = 0;
startByte = false;
while (INPUT_STREAM.available() > 0 && stopCounter < 3) {
while (INPUT_STREAM.available() > 0 && stopCounter < 5) {
buffer[counter%Properties.OUT_BUFFER_SIZE] = (byte) INPUT_STREAM.read();
if (stopCounter == 1) {
System.out.println("startbyte");
if (stopCounter == 2) {
startByte = true;
}
if (buffer[counter%Properties.OUT_BUFFER_SIZE] == Properties.SEPERATOR && dataCounter != 3) {
System.out.println("stop++");
if (buffer[counter%Properties.OUT_BUFFER_SIZE] == Properties.SEPERATOR && dataCounter != 4) {
dataCounter = 0;
stopCounter++;
}
else {
System.out.println("data++");
stopCounter = 0;
dataCounter++;
if (dataCounter == 4 && startByte) {
System.out.println("parse");
if (dataCounter == 5 && startByte) {
dataCounter = 0;
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+1];
}
//System.out.println(DatatypeConverter.printHexBinary(datagram));
Platform.runLater(new Runnable() {
@Override
......@@ -430,15 +438,16 @@ public class MainApp extends Application {
});
}
else if (dataCounter > 4 && startByte) {
else if (dataCounter > 5 && startByte) {
throw (new Exception("Wrong data-update-format!"));
}
}
counter++;
}
Thread.yield();
}
}
catch (Exception e) {
e.printStackTrace();
Platform.runLater(new Runnable() {
@Override
......
......@@ -15,12 +15,16 @@ public class Engine {
private UDPListener listener;
public Engine(){
//this(0, 1, 0);
if (engines == null) {
engines = new ArrayList<Engine>();
}
udpProtocol = new MaerklinProtocol();
listener = new UDPListener();
engines.add(this);
engineID = 0;
engineDirection = 1;
engineSpeed = 0;
}
public Engine(int id, int direction, int speed){
......
......@@ -7,6 +7,8 @@ import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.xml.bind.DatatypeConverter;
......@@ -18,7 +20,12 @@ public class MaerklinServer{
ArrayList<Socket> clients;
private MaerklinProtocol protocol;
private Thread overwatch;
private UDPListener overwatch;
private ServerThread serverThread;
final private ExecutorService SERVER_THREAD_EXECUTOR;
final ExecutorService CLIENT_THREAD_EXECUTOR;
final ExecutorService UTILITY_THREAD_EXECUTOR;
private Engine ice;
private Engine lok;
......@@ -44,7 +51,12 @@ public class MaerklinServer{
clients = new ArrayList<Socket>();
protocol = new MaerklinProtocol();
overwatch = new Thread(new UDPListener());
overwatch = new UDPListener();
serverThread = new ServerThread(this);
SERVER_THREAD_EXECUTOR = Executors.newFixedThreadPool(2);
CLIENT_THREAD_EXECUTOR = Executors.newCachedThreadPool();
UTILITY_THREAD_EXECUTOR = Executors.newCachedThreadPool();
ice = new Engine();
lok = new Engine();
......@@ -81,8 +93,8 @@ public class MaerklinServer{
// Starting the system and setting ("safe") values
try {
start();
initialize();
//start();
//initialize();
}
catch (Exception e){
System.out.println(e.getMessage());
......@@ -128,36 +140,53 @@ public class MaerklinServer{
// Starts the listener-thread
public void startListener(){
overwatch.start();
SERVER_THREAD_EXECUTOR.submit(overwatch);
}
// Listens to the client-port and initializes a new Thread to handle the client-messages
public void listen(){
SERVER_THREAD_EXECUTOR.submit(serverThread);
}
// Starts the thread to broadcast the current status
public void statusUpdate(){
for (Socket clientSocket : clients) {
try {
//System.out.println("Updating client: "+clientSocket.getRemoteSocketAddress());
UTILITY_THREAD_EXECUTOR.submit(new UpdateThread(clientSocket.getOutputStream()));
} catch (IOException e) {
System.out.println("Failed to receive output-stream of one of the clients!");
}
}
}
}
//Opens a server-port and establishes a new handling-thread for every client that connects to the server
class ServerThread implements Runnable {
// Declaration of objects
private final MaerklinServer SERVER_INSTANCE;
// Constructor
ServerThread (MaerklinServer server) {
SERVER_INSTANCE = server;
}
@Override
public void run() {
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 Thread(new ClientThread(this, socket, socket.getInputStream(), Properties.IN_BUFFER_SIZE))).start();
SERVER_INSTANCE.CLIENT_THREAD_EXECUTOR.submit(new ClientThread(SERVER_INSTANCE, socket, socket.getInputStream()));
}
}
catch (Exception e) {
System.out.println("Accessing user-interaction-port failed! Trying again!");
listen();
}
}
// Starts the thread to broadcast the current status
public void statusUpdate(){
System.out.println("Anzahl verbundene Clients: "+clients.size());
for (Socket clientSocket : clients) {
try {
System.out.println("Updating client: "+clientSocket.getRemoteSocketAddress());
(new Thread(new UpdateThread(clientSocket.getOutputStream(), Properties.OUT_BUFFER_SIZE))).start();
} catch (IOException e) {
System.out.println("Failed to receive output-stream of one of the clients!");
}
run();
}
}
......@@ -169,18 +198,16 @@ class ClientThread implements Runnable {
// Declaration of objects
private final Socket CLIENT;
private final BufferedInputStream IN_STREAM;
private final int IN_BUFFER_SIZE;
private final MaerklinServer SERVER_INSTANCE;
byte[] data;
// Constructor
ClientThread (MaerklinServer server, Socket socket, InputStream in, int bufferSize) {
ClientThread (MaerklinServer server, Socket socket, InputStream in) {
CLIENT = socket;
IN_STREAM = new BufferedInputStream(in);
IN_BUFFER_SIZE = bufferSize;
SERVER_INSTANCE = server;
SERVER_INSTANCE.clients.add(socket);
data = new byte[IN_BUFFER_SIZE];
data = new byte[Properties.IN_BUFFER_SIZE];
}
@Override
......@@ -196,7 +223,7 @@ class ClientThread implements Runnable {
return;
}
else if ((byte) buffer == (byte) Properties.SEPERATOR) {
for (int i = 0; i < IN_BUFFER_SIZE; i++) {
for (int i = 0; i < Properties.IN_BUFFER_SIZE; i++) {
buffer = IN_STREAM.read();
if ((byte) buffer == (byte) Properties.SESSION_ABORT) {
SERVER_INSTANCE.clients.remove(CLIENT);
......@@ -208,11 +235,13 @@ class ClientThread implements Runnable {
data[i] = (byte) (buffer%(1<<8));
}
}
(new Thread(new HandleThread(SERVER_INSTANCE, data))).start();
SERVER_INSTANCE.UTILITY_THREAD_EXECUTOR.submit(new HandleThread(SERVER_INSTANCE, data));
}
}
catch (Exception e) {
System.out.println("An error occured while reading client-input-stream!");
System.out.println("An error occured while reading the clients input-stream!");
SERVER_INSTANCE.clients.remove(CLIENT);
System.out.println("Conenction to client "+CLIENT.getRemoteSocketAddress()+" aborted!");
return;
}
}
......@@ -224,12 +253,12 @@ class ClientThread implements Runnable {
class HandleThread implements Runnable {
// Declaration of objects
MaerklinServer serverInstance;
private final MaerklinServer SERVER_INSTANCE;
byte[] data;
// Constructor
HandleThread (MaerklinServer server, byte[] incomingData) {
serverInstance = server;
SERVER_INSTANCE = server;
data = incomingData;
}
......@@ -239,15 +268,15 @@ class HandleThread implements Runnable {
switch (data[2]) {
case Properties.SYSTEM_STOP:
//System.out.println("System stop");
serverInstance.emergencyStop();
SERVER_INSTANCE.emergencyStop();
break;
case Properties.SYSTEM_GO:
//System.out.println("System go");
serverInstance.start();
SERVER_INSTANCE.start();
break;
case Properties.GET_STATUS:
//System.out.println("Status update");
serverInstance.statusUpdate();
SERVER_INSTANCE.statusUpdate();
break;
case Properties.ENGINE_SET_SPEED:
for (Engine train : Engine.engines) {
......@@ -328,18 +357,16 @@ class UpdateThread implements Runnable {
// Declaration of objects
private final OutputStream OUT_STREAM;
private final int OUT_BUFFER_SIZE;
private byte[] outgoingData;
// Constructor
UpdateThread (OutputStream out, int bufferSize) {
UpdateThread (OutputStream out) {
// Setting the broadcast-port and the outgoing-data-buffer-size
// Setting the broadcast-port
OUT_STREAM = out;
OUT_BUFFER_SIZE = bufferSize;
// Initializing
outgoingData = new byte[OUT_BUFFER_SIZE];
outgoingData = new byte[Properties.OUT_BUFFER_SIZE];
}
// Reads every registered objects data into outgoingData and broadcasts the status
......@@ -350,15 +377,15 @@ class UpdateThread implements Runnable {
outgoingData[position++] = Properties.SEPERATOR;
outgoingData[position++] = Properties.SEPERATOR;
for (Engine train : Engine.engines) {
if (position <= 1016) {
if (position >= 1016) {
Exception e = new Exception("Overload of registered elements! Update-buffer too small!");
throw e;
}
outgoingData[position++] = Byte.parseByte(""+Math.floor((train.getEngineID()/(1<<8))%(1<<8)));
outgoingData[position++] = Byte.parseByte(""+Math.floor(train.getEngineID()%(1<<8)));
outgoingData[position++] = Byte.parseByte(""+Math.floor((train.getEngineSpeed()/(1<<8))%(1<<8)));
outgoingData[position++] = Byte.parseByte(""+Math.floor(train.getEngineSpeed()%(1<<8)));
outgoingData[position++] = Byte.parseByte(""+Math.floor(train.getEngineDirection()%(1<<24)));
outgoingData[position++] = (byte) ((train.getEngineID()/(1<<8))%(1<<8));
outgoingData[position++] = (byte) ((train.getEngineID()%(1<<8)));
outgoingData[position++] = (byte) ((train.getEngineSpeed()/(1<<8))%(1<<8));
outgoingData[position++] = (byte) ((train.getEngineSpeed()%(1<<8)));
outgoingData[position++] = (byte) ((train.getEngineDirection()%(1<<24)));
outgoingData[position++] = Properties.SEPERATOR;
}
outgoingData[position++] = Properties.SEPERATOR;
......@@ -369,11 +396,11 @@ class UpdateThread implements Runnable {
Exception e = new Exception("Overload of registered elements! Update-buffer too small!");
throw e;
}
outgoingData[position++] = Byte.parseByte(""+Math.floor((sw.getSwitchID()/(1<<8))%(1<<8)));
outgoingData[position++] = Byte.parseByte(""+Math.floor(sw.getSwitchID()%(1<<8)));
outgoingData[position++] = Properties.SEPERATOR;
outgoingData[position++] = Properties.SEPERATOR;
outgoingData[position++] = Byte.parseByte(""+Math.floor(sw.getSwitchDirection()%(1<<24)));
outgoingData[position++] = (byte) ((sw.getSwitchID()/(1<<8))%(1<<8));
outgoingData[position++] = (byte) ((sw.getSwitchID()%(1<<8)));
outgoingData[position++] = (byte) 0x00;
outgoingData[position++] = (byte) 0x00;
outgoingData[position++] = (byte) ((sw.getSwitchDirection()%(1<<24)));
outgoingData[position++] = Properties.SEPERATOR;
}
outgoingData[position++] = Properties.SEPERATOR;
......@@ -385,9 +412,9 @@ class UpdateThread implements Runnable {
System.out.println(e.getMessage());
e.printStackTrace();
}
System.out.printf("\nSende: " + DatatypeConverter.printHexBinary(outgoingData));
//System.out.printf("\nSende: " + DatatypeConverter.printHexBinary(outgoingData));
try {
OUT_STREAM.write(outgoingData, 0, OUT_BUFFER_SIZE);
OUT_STREAM.write(outgoingData, 0, Properties.OUT_BUFFER_SIZE);
}
catch (Exception e) {
System.out.println("Communication-error whilst status-update!");
......
......@@ -21,7 +21,6 @@ public class MaerklinServerApplication {
while (true){
if (System.currentTimeMillis()-timestamp >= updateThreshold) {
System.exit(0);
server.statusUpdate();
timestamp = System.currentTimeMillis();
}
......
......@@ -13,12 +13,15 @@ public class Switch {
private UDPListener listener;
public Switch(){
//this(0, 1);
if (switches == null) {
switches = new ArrayList<Switch>();
}
udpProtocol = new MaerklinProtocol();
listener = new UDPListener();
switches.add(this);
switchID = 0;
switchDirection = 1;
}
public Switch(int id, int direction){
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment