diff --git a/src/main/java/de/joethei/hs/java2/praktikum/praktikum4/ClientRunnable.java b/src/main/java/de/joethei/hs/java2/praktikum/praktikum4/ClientRunnable.java new file mode 100644 index 0000000..4bb195e --- /dev/null +++ b/src/main/java/de/joethei/hs/java2/praktikum/praktikum4/ClientRunnable.java @@ -0,0 +1,31 @@ +package de.joethei.hs.java2.praktikum.praktikum4; + +import java.io.*; +import java.net.Socket; + +public class ClientRunnable implements Runnable{ + + private Socket socket; + private BufferedReader bufferedReader; + + public ClientRunnable(Socket socket) { + this.socket = socket; + try { + bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Override + public void run() { + System.out.println("client connected"); + try { + String input = bufferedReader.readLine(); + KlausurenServer.getCommandManager().handle(socket, input); + + } catch (IOException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/main/java/de/joethei/hs/java2/praktikum/praktikum4/CommandHandler.java b/src/main/java/de/joethei/hs/java2/praktikum/praktikum4/CommandHandler.java new file mode 100644 index 0000000..3f4331b --- /dev/null +++ b/src/main/java/de/joethei/hs/java2/praktikum/praktikum4/CommandHandler.java @@ -0,0 +1,9 @@ +package de.joethei.hs.java2.praktikum.praktikum4; + +import java.io.BufferedWriter; + +public interface CommandHandler { + + boolean handle(BufferedWriter writer, String[] args); + +} \ No newline at end of file diff --git a/src/main/java/de/joethei/hs/java2/praktikum/praktikum4/CommandManager.java b/src/main/java/de/joethei/hs/java2/praktikum/praktikum4/CommandManager.java new file mode 100644 index 0000000..e1c97ea --- /dev/null +++ b/src/main/java/de/joethei/hs/java2/praktikum/praktikum4/CommandManager.java @@ -0,0 +1,32 @@ +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 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(); + + } +} \ No newline at end of file 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 5da57ab..131a7b8 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 @@ -5,28 +5,47 @@ import java.net.ServerSocket; import java.net.Socket; import java.util.HashMap; import java.util.Map; +import java.util.Set; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class KlausurenServer { - private int port; - private Map data = new HashMap<>(); - private ExecutorService executorService = Executors.newCachedThreadPool(); + private Map> data = new HashMap<>(); + private static ExecutorService executorService = Executors.newCachedThreadPool(); + private static CommandManager commandManager = new CommandManager(); public KlausurenServer(int port) { - this.port = port; + commandManager.register("stop", new Commands.Stop()); + commandManager.register("test", new Commands.Test()); - try (ServerSocket serverSocket = new ServerSocket(port)) { - Socket socket = serverSocket.accept(); + Runnable runnable = () -> { + try { + ServerSocket serverSocket = new ServerSocket(port); + System.out.println("waiting for clients"); + while (true) { + Socket clientSocket = serverSocket.accept(); + executorService.submit(new ClientRunnable(clientSocket)); + } + } catch (IOException e) { + e.printStackTrace(); + } - } catch (IOException e) { - e.printStackTrace(); - System.exit(-1); - } + }; + + Thread thread = new Thread(runnable); + thread.start(); + } + + public static ExecutorService getExecutorService() { + return executorService; + } + + public static CommandManager getCommandManager() { + return commandManager; } public static void main(String[] args) { - + new KlausurenServer(Integer.parseInt(args[0])); } } \ No newline at end of file