Add: CoreClient and ChatClient

This commit is contained in:
Mathis Ginkel
2026-04-02 11:16:40 +02:00
parent 93e39c3962
commit f5138503cb
4 changed files with 73 additions and 54 deletions
@@ -1,5 +1,6 @@
package ch.unibas.dmi.dbis.cs108.casono.client.chat; package ch.unibas.dmi.dbis.cs108.casono.client.chat;
import ch.unibas.dmi.dbis.cs108.casono.client.network.ChatClient;
import ch.unibas.dmi.dbis.cs108.casono.client.network.ClientService; import ch.unibas.dmi.dbis.cs108.casono.client.network.ClientService;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -14,12 +15,14 @@ public class ChatController {
private final ClientService clientService; private final ClientService clientService;
private final ArrayList<ChatModel> chatModelArrayList; private final ArrayList<ChatModel> chatModelArrayList;
private final ChatClient chatClient;
public ChatController(String username, ClientService clientService) { public ChatController(String username, ClientService clientService) {
this.username = username; this.username = username;
this.clientService = clientService; this.clientService = clientService;
chatModelArrayList = new ArrayList<>(); chatModelArrayList = new ArrayList<>();
chatModelArrayList.add(new ChatModel(ChatType.GLOBAL, username)); chatModelArrayList.add(new ChatModel(ChatType.GLOBAL, username));
chatClient = new ChatClient(clientService);
} }
public void createLobbyChat(int lobbyId, ChatType chatType) { public void createLobbyChat(int lobbyId, ChatType chatType) {
@@ -29,7 +32,7 @@ public class ChatController {
/** method to get all messages from the server */ /** method to get all messages from the server */
public Boolean receiveMessage() { public Boolean receiveMessage() {
List<Message> newMessages = clientService.getMessages(); List<Message> newMessages = chatClient.getMessages();
if (!newMessages.isEmpty()) { if (!newMessages.isEmpty()) {
for (Message msg : newMessages) { for (Message msg : newMessages) {
switch (msg.getMessageType()) { switch (msg.getMessageType()) {
@@ -51,8 +54,11 @@ public class ChatController {
} }
} }
/** method to send a message to the server */ /**
public void sendMessageToNetwork(Message message) { * method to send a message to the server
clientService.sendMessage(message); * @param message
*/
public void onSendToNetwork(Message message) {
chatClient.sendMessage(message);
} }
} }
@@ -0,0 +1,45 @@
package ch.unibas.dmi.dbis.cs108.casono.client.network;
import ch.unibas.dmi.dbis.cs108.casono.client.chat.Message;
import java.util.ArrayList;
import java.util.List;
public class ChatClient {
private ClientService clientService;
public ChatClient(ClientService clientService) {
this.clientService = clientService;
}
/**
* sends a message to the server
* @param message
*/
public void sendMessage(Message message) {
String request = "SEND_MESSAGE " + message.toArgsString();
clientService.processCommand(request);
}
/**
* Send a Request to get the number of Messages currently in the Queue for the client.
* Then proceeds, if needed, to get the messages by sending
*/
public List<Message> getMessages() {
String countStr = clientService.processCommand("GET_MESSAGE_COUNT");
int count = Integer.parseInt(countStr);
System.out.println("Got " + count + " messages");
List<Message> messages = new ArrayList<>();
for (int i = 0; i < count; i++) {
String message = clientService.processCommand("GET_NEXT_MESSAGE");
if (message != null) {
Message message1 = Message.toMessage(message);
messages.add(message1);
}
}
return messages;
}
}
@@ -8,7 +8,6 @@ import org.apache.logging.log4j.Logger;
import java.io.IOException; import java.io.IOException;
import java.net.Socket; import java.net.Socket;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@@ -56,54 +55,6 @@ public class ClientService {
} }
/**
* Sends a ping to the server Returns nothing, the method processMessage() already handles the
* cases "+OK" or "-ERROR"
*/
public void ping() {
processMessage("PING");
}
/**
* Sends a login request to the server to create a new user on the server
*
* @param username
* @return - a new username, if the same username is already used by someone else
*/
public String login(String username) {
String msg = "LOGIN USERNAME=" + username;
return processMessage(msg);
}
/**
* Send a Request to get the number of Messages currently in the Queue for the client. Then
* proceeds, if needed, to get the messages by sending
*/
public List<Message> getMessages() {
String countStr = processMessage("GET_MESSAGE_COUNT");
int count = Integer.parseInt(countStr);
logger.info("Got " + count + " messages");
List<Message> messages = new ArrayList<>();
for (int i = 0; i < count; i++) {
String message = processMessage("GET_NEXT_MESSAGE");
if (message != null) {
Message message1 = Message.toMessage(message);
messages.add(message1);
}
}
return messages;
}
/**
* Sends a Request to the Server containing all relevant information of the message the client
* wrote.
*
* @param message
*/
public void sendMessage(Message message) {
String request = "SEND_MESSAGE " + message.toArgsString();
processMessage(request);
}
/** /**
* Sends the Requests to the server and waits for the response If the response is "+OK" it * Sends the Requests to the server and waits for the response If the response is "+OK" it
@@ -112,7 +63,7 @@ public class ClientService {
* @param message * @param message
* @return - The response as a string, if it has to be returned (+OK will not be returned) * @return - The response as a string, if it has to be returned (+OK will not be returned)
*/ */
private String processMessage(String message) { protected String processCommand(String message) {
AtomicReference<String> response = new AtomicReference<>(); AtomicReference<String> response = new AtomicReference<>();
sendRequest( sendRequest(
() -> { () -> {
@@ -0,0 +1,17 @@
package ch.unibas.dmi.dbis.cs108.casono.client.network;
public class CoreClient {
private final ClientService clientService;
public CoreClient(ClientService clientservice) {
this.clientService = clientservice;
}
public void ping() {
clientService.processCommand("PING");
}
public void login(String user) {
clientService.processCommand("LOGIN USERNAME=" + user);
}
}