diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/ConnectionHandler.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/ConnectionHandler.java index c1caafadcea8175884a736e19db31a4164e9bb08..48c31a576c546179217c67ec02df6f7041b555ea 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/ConnectionHandler.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/ConnectionHandler.java @@ -36,7 +36,6 @@ public class ConnectionHandler { ConnectionThread connectionthread = new ConnectionThread(client); connectionthread.start(); - } private static class ConnectionThread extends Thread { @@ -49,6 +48,7 @@ public class ConnectionHandler { public void run() { try { while (!Thread.interrupted()) { + System.out.println(Arrays.toString(coordinates)); int[] cords = client.sendcords(coordinates); enemyX = cords[0]; enemyY = cords[1]; diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/Server.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/Server.java index 32ee4320d12999d77fc19bb1b4deae32433d779e..946bf73ae679fd9072b5706157079a968a7c4dd8 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/Server.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Multiplayer/Server.java @@ -77,17 +77,13 @@ public class Server { this.clientSocket = socket; } - public void run() { - ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); - - executorService.scheduleAtFixedRate(() -> { + public synchronized void run() { try { BufferedReader localIn = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); PrintWriter localOut = new PrintWriter(clientSocket.getOutputStream(), true); - String inputLine = localIn.readLine(); - - if (inputLine != null) { + String inputLine; + while ((inputLine = localIn.readLine()) != null) { //only runs if there is a request from a server // Assuming that the input line is a comma-separated list of integers String[] coordinates = inputLine.split(","); cords = new int[coordinates.length]; @@ -157,27 +153,14 @@ public class Server { localOut.println(Arrays.toString(returncords)); localOut.flush(); - } - log.info("Player (X Y) : " + px + " " + py + " Attacking : " + playerAttacking + " direction : " + playerWalkDirection + " HP: " + playerHealth); - log.info("Enemy (X Y) : " + enemyx + " " + enemyy + " Attacking : " + enemyAttacking + " direction : " + enemyWalkDirection + " HP: " + enemyHealth); + log.info("Player (X Y) : " + px + " " + py + " Attacking : " + playerAttacking + " direction : " + playerWalkDirection + " HP: " + playerHealth); + log.info("Enemy (X Y) : " + enemyx + " " + enemyy + " Attacking : " + enemyAttacking + " direction : " + enemyWalkDirection + " HP: " + enemyHealth); - //check if server was shut down:W - if (!isServerRunning) { - executorService.shutdown(); - try { - if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) { - executorService.shutdownNow(); - } - } catch (InterruptedException e) { - log.error("Error waiting for executor service termination: " + e.getMessage()); - } } - } catch (IOException e) { throw new RuntimeException(e); } - }, 0, 50, TimeUnit.MILLISECONDS); } }