added logic to the 'MdgaServer' class

This commit is contained in:
Daniel Grigencha
2024-12-01 16:57:48 +01:00
parent efc7a2f09d
commit a29e942191

View File

@@ -13,6 +13,7 @@
import java.io.IOException;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.sql.Connection;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
@@ -154,8 +155,30 @@ private void registerListeners() {
myServer.addConnectionListener(this);
}
/**
* This method will be used to receive network messages from the given source parameter.
* It will check if the given message parameter is a ClientMessage object. If yes it will call the messageReceived
* method with the casted ClientMessage object.
*
* @param source as the connection which sends the message as a HostedConnection object.
* @param message as the received message as a Message object.
*/
@Override
public void messageReceived(HostedConnection source, Message message) {
if (message instanceof ClientMessage) {
this.messageReceived(source, (ClientMessage) message);
}
}
public void messageReceived(HostedConnection source, ClientMessage message) {
/**
* This method will be used to received network messages from the given source parameter.
* It will add the given message parameter to the pendingMessage attribute of MdgaServer after creating
* a ReceivedMessage object with it and its id.
*
* @param source as the connection which sends the message as a HostedConnection object.
* @param message as the received message as a Message object.
*/
private void messageReceived(HostedConnection source, ClientMessage message) {
LOGGER.log(Level.INFO, "message received from {0}: {1}", source.getId(), message); //NON-NLS
pendingMessages.add(new ReceivedMessage(message, source.getId()));
}
@@ -203,6 +226,7 @@ public void exit(int exitValue) { //NON-NLS
* @param id the connection id
* @param message the message
*/
@Override
public void send(int id, ServerMessage message) {
if (myServer == null || !myServer.isRunning()) {
LOGGER.log(Level.ERROR, "no server running when trying to send {0}", message); //NON-NLS
@@ -221,15 +245,20 @@ public void send(int id, ServerMessage message) {
*
* @param message as the message which will be sent to all players as a ServerMessage.
*/
@Override
public void broadcast(ServerMessage message) {
for (Map.Entry<Integer, Player> entry: this.logic.getGame().getPlayers().entrySet()) {
this.send(entry.getKey(), message);
}
}
//TODO:
/**
* This method will be used to diconenect the client depending on the given id parameter.
*
* @param id as the connection id of the client as an Integer.
*/
@Override
public void messageReceived(HostedConnection source, Message m) {
public void disconnectClient(int id) {
this.myServer.getConnection(id).close("");
}
}