diff --git a/Praktikum/VINF_MaerklinControl/bin/common/Properties.class b/Praktikum/VINF_MaerklinControl/bin/common/Properties.class index 0ad72d6f399c153f83c5b503be9cfdf78d744216..680801dcf289b5287de748be0b58330b5fee9d44 100644 Binary files a/Praktikum/VINF_MaerklinControl/bin/common/Properties.class and b/Praktikum/VINF_MaerklinControl/bin/common/Properties.class differ diff --git a/Praktikum/VINF_MaerklinControl/bin/server/ClientThread.class b/Praktikum/VINF_MaerklinControl/bin/server/ClientThread.class index 5d3616db25ff8bc9dbc5d9343b6572629702cb3b..87ad4494d5fb304483eaa093de83bbcc1a5259c5 100644 Binary files a/Praktikum/VINF_MaerklinControl/bin/server/ClientThread.class and b/Praktikum/VINF_MaerklinControl/bin/server/ClientThread.class differ diff --git a/Praktikum/VINF_MaerklinControl/bin/server/HandleThread.class b/Praktikum/VINF_MaerklinControl/bin/server/HandleThread.class index d2298463a67cbf6dbd2ac7b4ffc99c87a9072803..b716bf9e72a489e3a5572a1845d5821058fad1b0 100644 Binary files a/Praktikum/VINF_MaerklinControl/bin/server/HandleThread.class and b/Praktikum/VINF_MaerklinControl/bin/server/HandleThread.class differ diff --git a/Praktikum/VINF_MaerklinControl/bin/server/MaerklinServer.class b/Praktikum/VINF_MaerklinControl/bin/server/MaerklinServer.class index ea870768d451df53ae1eed282a28cb1fd520b6b6..afe7a4d2062fca83c9266f25ce3c80bae44dadef 100644 Binary files a/Praktikum/VINF_MaerklinControl/bin/server/MaerklinServer.class and b/Praktikum/VINF_MaerklinControl/bin/server/MaerklinServer.class differ diff --git a/Praktikum/VINF_MaerklinControl/bin/server/MaerklinServerApplication.class b/Praktikum/VINF_MaerklinControl/bin/server/MaerklinServerApplication.class index 928a0d1854323be899f5f217b60f64bf64e71f6a..49cade3d83850e8a50e66f49229e30d5f0feb2be 100644 Binary files a/Praktikum/VINF_MaerklinControl/bin/server/MaerklinServerApplication.class and b/Praktikum/VINF_MaerklinControl/bin/server/MaerklinServerApplication.class differ diff --git a/Praktikum/VINF_MaerklinControl/bin/server/UpdateThread.class b/Praktikum/VINF_MaerklinControl/bin/server/UpdateThread.class index 748d228df76e0616d0dd2d720e6f22161b40d336..baf588be8710d5a1c39e4f5038ce009fd276d972 100644 Binary files a/Praktikum/VINF_MaerklinControl/bin/server/UpdateThread.class and b/Praktikum/VINF_MaerklinControl/bin/server/UpdateThread.class differ diff --git a/Praktikum/VINF_MaerklinControl/src/common/Properties.java b/Praktikum/VINF_MaerklinControl/src/common/Properties.java index a6e8b86dd74c79f479528dd76630103e0bd71d2e..144b96d85315ffd068b2e2bba3fc0cd2b7b83e47 100644 --- a/Praktikum/VINF_MaerklinControl/src/common/Properties.java +++ b/Praktikum/VINF_MaerklinControl/src/common/Properties.java @@ -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; diff --git a/Praktikum/VINF_MaerklinControl/src/gui/MainApp.java b/Praktikum/VINF_MaerklinControl/src/gui/MainApp.java index 191cd21b987a058b05df43d62f8fc9ca98eaf2a5..153ae1b97ce955bce02a7e273b687456656b4eaa 100644 --- a/Praktikum/VINF_MaerklinControl/src/gui/MainApp.java +++ b/Praktikum/VINF_MaerklinControl/src/gui/MainApp.java @@ -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,29 +381,44 @@ 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)); - //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(); + if(eng.getMaerklinID().get()==((datagram[0]&0xFF)*(1<<8)+(datagram[1]&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)+"."); + } + 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())){} } } 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(); + 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]+"."); + } } } } + public void updateGUI () { + engineController.updateEngineStatus(); + for (Switch sw : switches){ + sw.getController().get().updateSwitchStatus(); + } + } + @Override public void run() { int stopCounter, dataCounter, counter; @@ -428,15 +448,8 @@ 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() { - parseDatagram(datagram); - } - - }); + System.out.println("1: "+DatatypeConverter.printHexBinary(datagram)); + parseDatagram(datagram); } else if (dataCounter > 5 && startByte) { throw (new Exception("Wrong data-update-format!")); @@ -444,6 +457,18 @@ public class MainApp extends Application { } counter++; } + while (INPUT_STREAM.available() != 0) { + INPUT_STREAM.read(); + } + Platform.runLater(new Runnable() { + + @Override + public void run() { + updateGUI(); + } + + }); + Thread.sleep(50); } } catch (Exception e) { diff --git a/Praktikum/VINF_MaerklinControl/src/server/MaerklinServer.java b/Praktikum/VINF_MaerklinControl/src/server/MaerklinServer.java index 88464ca63b87d5fd4831a83c463acf19039e65d5..c2774bd217d4ce717b1fc7c705dc880e43323880 100644 --- a/Praktikum/VINF_MaerklinControl/src/server/MaerklinServer.java +++ b/Praktikum/VINF_MaerklinControl/src/server/MaerklinServer.java @@ -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,27 +221,32 @@ class ClientThread implements Runnable { int buffer; while (true) { try { - buffer = IN_STREAM.read(); - if ((byte) buffer == (byte) Properties.SESSION_ABORT) { - SERVER_INSTANCE.clients.remove(CLIENT); - CLIENT.close(); - System.out.println("Conenction to client "+CLIENT.getRemoteSocketAddress()+" aborted!"); - return; - } - else if ((byte) buffer == (byte) Properties.SEPERATOR) { - 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); - CLIENT.close(); - System.out.println("Connection to client "+CLIENT.getRemoteSocketAddress()+" aborted!"); - return; - } - else { - data[i] = (byte) (buffer%(1<<8)); + if (IN_STREAM.available() != 0) { + buffer = IN_STREAM.read(); + if ((byte) buffer == (byte) Properties.SESSION_ABORT) { + SERVER_INSTANCE.clients.remove(CLIENT); + CLIENT.close(); + System.out.println("Conenction to client "+CLIENT.getRemoteSocketAddress()+" aborted!"); + return; + } + else if ((byte) buffer == (byte) Properties.SEPERATOR) { + 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); + CLIENT.close(); + System.out.println("Connection to client "+CLIENT.getRemoteSocketAddress()+" aborted!"); + return; + } + else { + data[i] = (byte) (buffer%(1<<8)); + } } + SERVER_INSTANCE.UTILITY_THREAD_EXECUTOR.submit(new HandleThread(SERVER_INSTANCE, data)); } - SERVER_INSTANCE.UTILITY_THREAD_EXECUTOR.submit(new HandleThread(SERVER_INSTANCE, data)); + } + else { + Thread.sleep(10); } } catch (Exception e) { @@ -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; } diff --git a/Praktikum/VINF_MaerklinControl/src/server/MaerklinServerApplication.java b/Praktikum/VINF_MaerklinControl/src/server/MaerklinServerApplication.java index 94a9176be2ccfb1e4ad4e167a7162fb8e046356e..7bbd8c583b8818b0232a810ef61c347e928926ab 100644 --- a/Praktikum/VINF_MaerklinControl/src/server/MaerklinServerApplication.java +++ b/Praktikum/VINF_MaerklinControl/src/server/MaerklinServerApplication.java @@ -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(); + } + } } } diff --git a/common/qr.repo.VInf.png b/common/qr.repo.VInf.png new file mode 100644 index 0000000000000000000000000000000000000000..ef270903265776923dfed71a564acb2a0c7a532a Binary files /dev/null and b/common/qr.repo.VInf.png differ diff --git a/readme.md b/readme.md index aba540b8058b4fea756c1d9ae05b8f3d98bca06b..103b61667defa82a36491572d3376f429e73a3d9 100644 --- a/readme.md +++ b/readme.md @@ -1,27 +1,29 @@ -Student Repositiory for Advanced 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: -- \*.tex, \*.pdf: documentation and slides - - 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 - - either the Modified BSD License, - - or the Creative Commons Attribution-ShareAlike 3.0 Unported License - -These documents are distributed in the hope that they will be -useful, but WITHOUT ANY WARRANTY; without even the implied warranty -of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -licenses for more details. - -You should have received a [copy](https://gitlab.cvh-server.de/lf.ps/vbls/blob/master/common/GNU-GPL-3.txt) of the GNU General Public License along with this program; see the file GNU-GPL-3. If not, see <http://www.gnu.org/licenses/>. - -You should have received a [copy](https://gitlab.cvh-server.de/lf.ps/vbls/blob/master/common/BSD-MODIFIED.txt) of the Modified BSD License along with this document; see the file BSD-MODIFIED. If not, see http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5. - -You should have received a [copy](https://gitlab.cvh-server.de/lf.ps/vbls/blob/master/common/CC-BY-SA-3.0.txt) of the Creative Commons Attribution-ShareAlike 3.0 Unported License along with this document; see the file CC-BY-SA-3.0. If not, see -http://creativecommons.org/licenses/. +<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: +- \*.tex, \*.pdf: documentation and slides + - 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 + +- \*.java, \*.fxml: programmes and Java FX 8 layout files + - either the Modified BSD License, + - or the Creative Commons Attribution-ShareAlike 3.0 Unported License + +These documents are distributed in the hope that they will be +useful, but WITHOUT ANY WARRANTY; without even the implied warranty +of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +licenses for more details. + +You should have received a [copy](https://gitlab.cvh-server.de/lf.ps/vbls/blob/master/common/GNU-GPL-3.txt) of the GNU General Public License along with this program; see the file GNU-GPL-3. If not, see <http://www.gnu.org/licenses/>. + +You should have received a [copy](https://gitlab.cvh-server.de/lf.ps/vbls/blob/master/common/BSD-MODIFIED.txt) of the Modified BSD License along with this document; see the file BSD-MODIFIED. If not, see http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5. + +You should have received a [copy](https://gitlab.cvh-server.de/lf.ps/vbls/blob/master/common/CC-BY-SA-3.0.txt) of the Creative Commons Attribution-ShareAlike 3.0 Unported License along with this document; see the file CC-BY-SA-3.0. If not, see +http://creativecommons.org/licenses/.