feat: subdivide clienthandler into messageprocessor
This commit is contained in:
@@ -9,24 +9,16 @@ public class ClientHandler implements Runnable {
|
||||
private final DatagramSocket clientHandlerSocket;
|
||||
private final Client client;
|
||||
private boolean running = true;
|
||||
private final MessageProcessor messageProcessor;
|
||||
|
||||
public ClientHandler(DatagramSocket socket, Client client) {
|
||||
this.clientHandlerSocket = socket;
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
private void sendConnectedClients(String message) {
|
||||
if (message.equals("/list")) {
|
||||
Server.sendMessage(clientHandlerSocket, Server.getPseudos(), client.getAddress(), client.getPort());
|
||||
}
|
||||
}
|
||||
|
||||
private void handleDeconnexion(String message) {
|
||||
if (message.equals("DISCONNECT")) {
|
||||
System.out.println("Client deconnection : " + prettyPrint(client));
|
||||
Server.removeClientHandler(client.getPseudo());
|
||||
stop();
|
||||
}
|
||||
this.messageProcessor = new MessageProcessor(
|
||||
client,
|
||||
clientHandlerSocket,
|
||||
this
|
||||
);
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
@@ -36,43 +28,39 @@ public class ClientHandler implements Runnable {
|
||||
}
|
||||
}
|
||||
|
||||
public String prettyPrint(Client client) {
|
||||
public static String prettyPrint(Client client) {
|
||||
StringBuilder str = new StringBuilder();
|
||||
str.append(client.getPseudo() +
|
||||
str.append(
|
||||
client.getPseudo() +
|
||||
" (" +
|
||||
client.getAddress() +
|
||||
":" +
|
||||
client.getPort() +
|
||||
")");
|
||||
")"
|
||||
);
|
||||
return str.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println(
|
||||
"Started handler for client " +
|
||||
prettyPrint(client));
|
||||
System.out.println("Started handler for client " + prettyPrint(client));
|
||||
|
||||
while (running && !clientHandlerSocket.isClosed()) {
|
||||
DatagramPacket packet = Server.receivedPacket(clientHandlerSocket);
|
||||
if (packet == null)
|
||||
continue;
|
||||
if (packet == null) continue;
|
||||
|
||||
String message = new String(
|
||||
packet.getData(),
|
||||
0,
|
||||
packet.getLength());
|
||||
System.out.println(
|
||||
prettyPrint(client) + " : " +
|
||||
message);
|
||||
packet.getLength()
|
||||
);
|
||||
System.out.println(prettyPrint(client) + " : " + message);
|
||||
|
||||
// Possibilies
|
||||
sendConnectedClients(message); // /list command
|
||||
handleDeconnexion(message); // exit command or DISCONNECT
|
||||
messageProcessor.processMessage(message);
|
||||
}
|
||||
|
||||
System.out.println(
|
||||
"Client handler terminated for " +
|
||||
prettyPrint(client));
|
||||
"Client handler terminated for " + prettyPrint(client)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
51
app/src/main/java/clientserver/server/MessageProcessor.java
Normal file
51
app/src/main/java/clientserver/server/MessageProcessor.java
Normal file
@@ -0,0 +1,51 @@
|
||||
package clientserver.server;
|
||||
|
||||
import clientserver.client.Client;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
|
||||
public class MessageProcessor {
|
||||
|
||||
private final Client client;
|
||||
private final DatagramSocket clientHandlerSocket;
|
||||
private final ClientHandler clientHandler;
|
||||
|
||||
public MessageProcessor(
|
||||
Client client,
|
||||
DatagramSocket clientHandlerSocket,
|
||||
ClientHandler clientHandler
|
||||
) {
|
||||
this.client = client;
|
||||
this.clientHandlerSocket = clientHandlerSocket;
|
||||
this.clientHandler = clientHandler;
|
||||
}
|
||||
|
||||
public void processMessage(String message) {
|
||||
if (message == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (message.equals("/list")) {
|
||||
handleListCommand();
|
||||
} else if (message.equals("DISCONNECT")) {
|
||||
handleDisconnect();
|
||||
}
|
||||
}
|
||||
|
||||
private void handleListCommand() {
|
||||
Server.sendMessage(
|
||||
clientHandlerSocket,
|
||||
Server.getPseudos(),
|
||||
client.getAddress(),
|
||||
client.getPort()
|
||||
);
|
||||
}
|
||||
|
||||
private void handleDisconnect() {
|
||||
System.out.println(
|
||||
"Client deconnection : " + ClientHandler.prettyPrint(client)
|
||||
);
|
||||
Server.removeClientHandler(client.getPseudo());
|
||||
clientHandler.stop();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user