Changes and stuff
This commit is contained in:
parent
18cd86a15b
commit
a12982b040
|
@ -2,6 +2,8 @@ package de.joethei.hs.java2.praktikum.praktikum4;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class ClientRunnable implements Runnable{
|
public class ClientRunnable implements Runnable{
|
||||||
|
|
||||||
|
@ -22,7 +24,18 @@ public class ClientRunnable implements Runnable{
|
||||||
System.out.println("client connected");
|
System.out.println("client connected");
|
||||||
try {
|
try {
|
||||||
String input = bufferedReader.readLine();
|
String input = bufferedReader.readLine();
|
||||||
KlausurenServer.getCommandManager().handle(socket, input);
|
System.out.println(input);
|
||||||
|
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||||
|
input = input.replace(",", "");
|
||||||
|
List<String> arguments = Arrays.asList(input.split(" "));
|
||||||
|
String command = arguments.remove(0);
|
||||||
|
|
||||||
|
KlausurenServer.getCommands().get(command.toLowerCase()).handle(bufferedWriter, arguments);
|
||||||
|
|
||||||
|
bufferedWriter.flush();
|
||||||
|
bufferedWriter.close();
|
||||||
|
socket.close();
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
package de.joethei.hs.java2.praktikum.praktikum4;
|
package de.joethei.hs.java2.praktikum.praktikum4;
|
||||||
|
|
||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface CommandHandler {
|
public interface CommandHandler {
|
||||||
|
|
||||||
boolean handle(BufferedWriter writer, String[] args);
|
void handle(BufferedWriter writer, List<String> args) throws IOException;
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,32 +0,0 @@
|
||||||
package de.joethei.hs.java2.praktikum.praktikum4;
|
|
||||||
|
|
||||||
import java.io.BufferedWriter;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.OutputStreamWriter;
|
|
||||||
import java.net.Socket;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class CommandManager {
|
|
||||||
|
|
||||||
private Map<String, CommandHandler> commandHandlers = new HashMap<>();
|
|
||||||
|
|
||||||
void register(String command, CommandHandler commandHandler) {
|
|
||||||
commandHandlers.put(command, commandHandler);
|
|
||||||
}
|
|
||||||
|
|
||||||
void handle(Socket socket, String command) throws IOException {
|
|
||||||
System.out.println(command);
|
|
||||||
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
|
||||||
String[] arguments = command.split(" ");
|
|
||||||
if(commandHandlers.get(arguments[0].toLowerCase()).handle(bufferedWriter, arguments)) {
|
|
||||||
bufferedWriter.write("1\n");
|
|
||||||
}else bufferedWriter.write("0\n");
|
|
||||||
|
|
||||||
bufferedWriter.flush();
|
|
||||||
bufferedWriter.close();
|
|
||||||
socket.close();
|
|
||||||
Thread.currentThread().interrupt();
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,30 +0,0 @@
|
||||||
package de.joethei.hs.java2.praktikum.praktikum4;
|
|
||||||
|
|
||||||
import java.io.BufferedWriter;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public class Commands {
|
|
||||||
|
|
||||||
public static class Stop implements CommandHandler {
|
|
||||||
@Override
|
|
||||||
public boolean handle(BufferedWriter writer, String[] args) {
|
|
||||||
KlausurenServer.getExecutorService().shutdown();
|
|
||||||
System.exit(0);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Test implements CommandHandler {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean handle(BufferedWriter writer, String[] args) {
|
|
||||||
try {
|
|
||||||
writer.write("Hallo Welt\n");
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -3,9 +3,7 @@ package de.joethei.hs.java2.praktikum.praktikum4;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.HashMap;
|
import java.util.*;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
@ -13,11 +11,39 @@ public class KlausurenServer {
|
||||||
|
|
||||||
private Map<String, Set<Integer>> data = new HashMap<>();
|
private Map<String, Set<Integer>> data = new HashMap<>();
|
||||||
private static ExecutorService executorService = Executors.newCachedThreadPool();
|
private static ExecutorService executorService = Executors.newCachedThreadPool();
|
||||||
private static CommandManager commandManager = new CommandManager();
|
private static Map<String, CommandHandler> commands = new HashMap<>();
|
||||||
|
|
||||||
public KlausurenServer(int port) {
|
public KlausurenServer(int port) {
|
||||||
commandManager.register("stop", new Commands.Stop());
|
commands.put("put", (writer, args) -> {
|
||||||
commandManager.register("test", new Commands.Test());
|
if(args.size() >= 2) {
|
||||||
|
Set<Integer> set = new HashSet<>();
|
||||||
|
for(int i = 1; i <= args.size(); i++) {
|
||||||
|
try {
|
||||||
|
set.add(Integer.parseInt(args.get(i)));
|
||||||
|
}catch (NumberFormatException ex) {
|
||||||
|
writer.write("0");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
data.put(args.get(0), set);
|
||||||
|
}else {
|
||||||
|
writer.write("0");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
commands.put("getall", (writer, args) -> {
|
||||||
|
if(!data.isEmpty()) {
|
||||||
|
writer.write("1 ");
|
||||||
|
Set<Integer> set = new TreeSet<>();
|
||||||
|
data.forEach((s, integers) -> set.addAll(integers));
|
||||||
|
writer.write(set.toString());
|
||||||
|
}else {
|
||||||
|
writer.write("0");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
commands.put("stop", ((writer, args) -> {
|
||||||
|
writer.write("1");
|
||||||
|
executorService.shutdown();
|
||||||
|
System.exit(0);
|
||||||
|
}));
|
||||||
|
|
||||||
Runnable runnable = () -> {
|
Runnable runnable = () -> {
|
||||||
try {
|
try {
|
||||||
|
@ -37,12 +63,8 @@ public class KlausurenServer {
|
||||||
thread.start();
|
thread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ExecutorService getExecutorService() {
|
static Map<String, CommandHandler> getCommands() {
|
||||||
return executorService;
|
return commands;
|
||||||
}
|
|
||||||
|
|
||||||
public static CommandManager getCommandManager() {
|
|
||||||
return commandManager;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
Loading…
Reference in New Issue