diff --git a/Projekte/mdga/client/src/main/java/pp/mdga/client/server/MdgaServer.java b/Projekte/mdga/client/src/main/java/pp/mdga/client/server/MdgaServer.java index 20264ef9..a23348ee 100644 --- a/Projekte/mdga/client/src/main/java/pp/mdga/client/server/MdgaServer.java +++ b/Projekte/mdga/client/src/main/java/pp/mdga/client/server/MdgaServer.java @@ -2,6 +2,7 @@ import com.jme3.network.*; import com.jme3.network.serializing.Serializer; +import pp.mdga.Resources; import pp.mdga.game.*; import pp.mdga.message.client.*; import pp.mdga.message.server.*; @@ -195,12 +196,29 @@ private void messageReceived(HostedConnection source, ClientMessage message) { pendingMessages.add(new ReceivedMessage(message, source.getId())); } + /** + * This method will be used to handle all connections which are connected to the server. + * It will check if the maximum number of connected clients are already reached. If yes it will send a + * LobbyDenyMessage to the given hostedConnection parameter and close it, otherwise it will send a + * LobbyAcceptMessage to the given hostedConnection parameter. In Addition, if the number of connected clients is + * equal to 1 it will set the host of the game to the id of the given hostedConnection parameter. + * + * @param server as the server which is contains all connections as a Server object. + * @param hostedConnection as the connection which is added to the server as a HostedConnection object. + */ @Override public void connectionAdded(Server server, HostedConnection hostedConnection) { System.out.println("new connection " + hostedConnection); //NON-NLS LOGGER.log(Level.DEBUG, "new connection {0}", hostedConnection); //NON-NLS - if (this.myServer.getConnections().size() == 1) { - this.logic.getGame().setHost(hostedConnection.getId()); + + if (this.myServer.getConnections().size() > Resources.MAX_PLAYERS) { + this.logic.getServerSender().send(hostedConnection.getId(), new LobbyDenyMessage()); + hostedConnection.close(""); + } else { + if (this.myServer.getConnections().size() == 1) { + this.logic.getGame().setHost(hostedConnection.getId()); + } + this.logic.getServerSender().send(hostedConnection.getId(), new LobbyAcceptMessage()); } }