From d843c009227299bc983655f4245138b38341019f Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Fri, 18 May 2018 11:48:48 +0200 Subject: [PATCH] fehlerhafter code --- .../praktikum/praktikum4/KlausurenServer.java | 79 +++++++++++++++++-- .../resources/praktikum4/{data => data.txt} | 0 .../hs/java2/tests/KlausurenServerTest.java | 2 - 3 files changed, 74 insertions(+), 7 deletions(-) rename src/main/resources/praktikum4/{data => data.txt} (100%) diff --git a/src/main/java/de/joethei/hs/java2/praktikum/praktikum4/KlausurenServer.java b/src/main/java/de/joethei/hs/java2/praktikum/praktikum4/KlausurenServer.java index 419d46d..e31f24c 100644 --- a/src/main/java/de/joethei/hs/java2/praktikum/praktikum4/KlausurenServer.java +++ b/src/main/java/de/joethei/hs/java2/praktikum/praktikum4/KlausurenServer.java @@ -1,24 +1,67 @@ package de.joethei.hs.java2.praktikum.praktikum4; -import java.io.IOException; +import java.io.*; import java.net.ServerSocket; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; -import java.util.TreeSet; +import java.net.URISyntaxException; +import java.util.*; import java.util.concurrent.*; +import java.util.logging.Logger; public class KlausurenServer { private ConcurrentMap> data = new ConcurrentHashMap<>(); private static ExecutorService executorService = Executors.newCachedThreadPool(); private static ServerSocket serverSocket; + private Logger logger = Logger.getLogger(this.getClass().getName()); private static Map commands = new HashMap<>(); private boolean running; public KlausurenServer(int port) { + BufferedReader bufferedReader = null; + File file = null; + try { + logger.info(getClass().getResource("praktikum4/data.txt").toString()); + logger.info(new File(getClass().getResource("praktikum4/data.txt").toString()).toString()); + file = new File(getClass().getResource("praktikum4/data.txt").toURI()); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + + if(file.exists()) { + try { + bufferedReader = new BufferedReader(new FileReader(file)); + for(String line = bufferedReader.readLine(); line != null; line = bufferedReader.readLine()) { + String[] array = line.split(":"); + TreeSet set = new TreeSet<>(); + for (int i = 1; i < array.length - 1; i++) { + set.add(Integer.parseInt(array[i])); + } + data.put(array[0], set); + } + + } catch (IOException e) { + e.printStackTrace(); + }finally { + if (bufferedReader != null) { + try { + bufferedReader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + }else { + try { + file.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + + commands.put("test", ((writer, args) -> writer.write("Hallo Welt"))); commands.put("put", (writer, args) -> { if (args.size() >= 3) { @@ -35,6 +78,7 @@ public class KlausurenServer { set.addAll(data.get(args.get(1).toLowerCase())); } data.put(args.get(1).toLowerCase(), set); + save(); } else { writer.write("0"); } @@ -60,6 +104,7 @@ public class KlausurenServer { if(args.size() == 2) { if(data.containsKey(args.get(1).toLowerCase())) { writer.write("1" + data.remove(args.get(1).toLowerCase()).toString().replace('[', ' ').replace(']', ' ').replaceFirst(".$","")); + save(); }else writer.write("0"); }else writer.write("0"); }); @@ -99,6 +144,30 @@ public class KlausurenServer { return commands; } + synchronized void save() { + BufferedWriter bufferedWriter = null; + try { + bufferedWriter = new BufferedWriter(new FileWriter(getClass().getClassLoader().getResource("praktikum4/data.txt").toString())); + for(Map.Entry> entry : data.entrySet()) { + bufferedWriter.write(entry.getKey()); + for(int integer : entry.getValue()) { + bufferedWriter.write(":" + integer); + } + bufferedWriter.newLine(); + bufferedWriter.flush(); + } + bufferedWriter.close(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + bufferedWriter.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + public static void main(String[] args) { new KlausurenServer(Integer.parseInt(args[0])); } diff --git a/src/main/resources/praktikum4/data b/src/main/resources/praktikum4/data.txt similarity index 100% rename from src/main/resources/praktikum4/data rename to src/main/resources/praktikum4/data.txt diff --git a/src/test/java/de/joethei/hs/java2/tests/KlausurenServerTest.java b/src/test/java/de/joethei/hs/java2/tests/KlausurenServerTest.java index 638ca94..e8700c3 100644 --- a/src/test/java/de/joethei/hs/java2/tests/KlausurenServerTest.java +++ b/src/test/java/de/joethei/hs/java2/tests/KlausurenServerTest.java @@ -55,7 +55,6 @@ public class KlausurenServerTest { assertEquals("0", sendRequest("get max@maier")); } - /* @Test(timeout = 700L) @Order(order = 6) public void saved() throws InterruptedException { @@ -64,7 +63,6 @@ public class KlausurenServerTest { server = new KlausurenServer(6767); assertEquals("1 [5, 6, 7, 8]", sendRequest("getall")); } - */ private String sendRequest(String command) { try {