fehlerhafter code
This commit is contained in:
parent
9f41c48100
commit
d843c00922
|
@ -1,24 +1,67 @@
|
||||||
package de.joethei.hs.java2.praktikum.praktikum4;
|
package de.joethei.hs.java2.praktikum.praktikum4;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.*;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.util.HashMap;
|
import java.net.URISyntaxException;
|
||||||
import java.util.Map;
|
import java.util.*;
|
||||||
import java.util.Set;
|
|
||||||
import java.util.TreeSet;
|
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
public class KlausurenServer {
|
public class KlausurenServer {
|
||||||
|
|
||||||
private ConcurrentMap<String, TreeSet<Integer>> data = new ConcurrentHashMap<>();
|
private ConcurrentMap<String, TreeSet<Integer>> data = new ConcurrentHashMap<>();
|
||||||
private static ExecutorService executorService = Executors.newCachedThreadPool();
|
private static ExecutorService executorService = Executors.newCachedThreadPool();
|
||||||
private static ServerSocket serverSocket;
|
private static ServerSocket serverSocket;
|
||||||
|
private Logger logger = Logger.getLogger(this.getClass().getName());
|
||||||
|
|
||||||
private static Map<String, CommandHandler> commands = new HashMap<>();
|
private static Map<String, CommandHandler> commands = new HashMap<>();
|
||||||
|
|
||||||
private boolean running;
|
private boolean running;
|
||||||
|
|
||||||
public KlausurenServer(int port) {
|
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<Integer> 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("test", ((writer, args) -> writer.write("Hallo Welt")));
|
||||||
commands.put("put", (writer, args) -> {
|
commands.put("put", (writer, args) -> {
|
||||||
if (args.size() >= 3) {
|
if (args.size() >= 3) {
|
||||||
|
@ -35,6 +78,7 @@ public class KlausurenServer {
|
||||||
set.addAll(data.get(args.get(1).toLowerCase()));
|
set.addAll(data.get(args.get(1).toLowerCase()));
|
||||||
}
|
}
|
||||||
data.put(args.get(1).toLowerCase(), set);
|
data.put(args.get(1).toLowerCase(), set);
|
||||||
|
save();
|
||||||
} else {
|
} else {
|
||||||
writer.write("0");
|
writer.write("0");
|
||||||
}
|
}
|
||||||
|
@ -60,6 +104,7 @@ public class KlausurenServer {
|
||||||
if(args.size() == 2) {
|
if(args.size() == 2) {
|
||||||
if(data.containsKey(args.get(1).toLowerCase())) {
|
if(data.containsKey(args.get(1).toLowerCase())) {
|
||||||
writer.write("1" + data.remove(args.get(1).toLowerCase()).toString().replace('[', ' ').replace(']', ' ').replaceFirst(".$",""));
|
writer.write("1" + data.remove(args.get(1).toLowerCase()).toString().replace('[', ' ').replace(']', ' ').replaceFirst(".$",""));
|
||||||
|
save();
|
||||||
}else writer.write("0");
|
}else writer.write("0");
|
||||||
}else writer.write("0");
|
}else writer.write("0");
|
||||||
});
|
});
|
||||||
|
@ -99,6 +144,30 @@ public class KlausurenServer {
|
||||||
return commands;
|
return commands;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
synchronized void save() {
|
||||||
|
BufferedWriter bufferedWriter = null;
|
||||||
|
try {
|
||||||
|
bufferedWriter = new BufferedWriter(new FileWriter(getClass().getClassLoader().getResource("praktikum4/data.txt").toString()));
|
||||||
|
for(Map.Entry<String, TreeSet<Integer>> 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) {
|
public static void main(String[] args) {
|
||||||
new KlausurenServer(Integer.parseInt(args[0]));
|
new KlausurenServer(Integer.parseInt(args[0]));
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,7 +55,6 @@ public class KlausurenServerTest {
|
||||||
assertEquals("0", sendRequest("get max@maier"));
|
assertEquals("0", sendRequest("get max@maier"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
@Test(timeout = 700L)
|
@Test(timeout = 700L)
|
||||||
@Order(order = 6)
|
@Order(order = 6)
|
||||||
public void saved() throws InterruptedException {
|
public void saved() throws InterruptedException {
|
||||||
|
@ -64,7 +63,6 @@ public class KlausurenServerTest {
|
||||||
server = new KlausurenServer(6767);
|
server = new KlausurenServer(6767);
|
||||||
assertEquals("1 [5, 6, 7, 8]", sendRequest("getall"));
|
assertEquals("1 [5, 6, 7, 8]", sendRequest("getall"));
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
private String sendRequest(String command) {
|
private String sendRequest(String command) {
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in New Issue