Praktikum 4: multithreaded Server, Command Handling
This commit is contained in:
parent
fe22278fe6
commit
81d472b661
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package de.joethei.hs.java2.praktikum.praktikum4;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
|
||||
public interface CommandHandler {
|
||||
|
||||
boolean handle(BufferedWriter writer, String[] args);
|
||||
|
||||
}
|
|
@ -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<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();
|
||||
|
||||
}
|
||||
}
|
|
@ -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<String, Integer> data = new HashMap<>();
|
||||
private ExecutorService executorService = Executors.newCachedThreadPool();
|
||||
private Map<String, Set<Integer>> 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]));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue