Code cleanup
added JavaDocs removed unnecessary lines
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
########################################
|
||||
## Programming project code
|
||||
########################################## Programming project code
|
||||
## UniBw M, 2022, 2023, 2024
|
||||
## www.unibw.de/inf2
|
||||
## (c) Mark Minas (mark.minas@unibw.de)
|
||||
|
||||
@@ -1,10 +1,3 @@
|
||||
////////////////////////////////////////
|
||||
// Programming project code
|
||||
// UniBw M, 2022, 2023, 2024
|
||||
// www.unibw.de/inf2
|
||||
// (c) Mark Minas (mark.minas@unibw.de)
|
||||
////////////////////////////////////////
|
||||
|
||||
package pp.battleship.server;
|
||||
|
||||
import com.jme3.network.ConnectionListener;
|
||||
@@ -81,12 +74,18 @@ public static void main(String[] args) {
|
||||
logic = new ServerGameLogic(this, config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a server
|
||||
*/
|
||||
public void run() {
|
||||
startServer();
|
||||
while (true)
|
||||
processNextMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts a server
|
||||
*/
|
||||
private void startServer() {
|
||||
try {
|
||||
LOGGER.log(Level.INFO, "Starting server..."); //NON-NLS
|
||||
@@ -102,6 +101,9 @@ private void startServer() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Processes next received message
|
||||
*/
|
||||
private void processNextMessage() {
|
||||
try {
|
||||
pendingMessages.take().process(logic);
|
||||
@@ -112,6 +114,9 @@ private void processNextMessage() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers all serializable classes
|
||||
*/
|
||||
private void initializeSerializables() {
|
||||
Serializer.registerClass(GameDetails.class);
|
||||
Serializer.registerClass(StartBattleMessage.class);
|
||||
@@ -126,6 +131,9 @@ private void initializeSerializables() {
|
||||
Serializer.registerClass(SwitchToBattleState.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers all listeners
|
||||
*/
|
||||
private void registerListeners() {
|
||||
myServer.addMessageListener(this, MapMessage.class);
|
||||
myServer.addMessageListener(this, ShootMessage.class);
|
||||
@@ -133,6 +141,11 @@ private void registerListeners() {
|
||||
myServer.addConnectionListener(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a received message
|
||||
* @param source the connection the message comes from
|
||||
* @param message the received message
|
||||
*/
|
||||
@Override
|
||||
public void messageReceived(HostedConnection source, Message message) {
|
||||
LOGGER.log(Level.INFO, "message received from {0}: {1}", source.getId(), message); //NON-NLS
|
||||
@@ -140,12 +153,22 @@ public void messageReceived(HostedConnection source, Message message) {
|
||||
pendingMessages.add(new ReceivedMessage(clientMessage, source.getId()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new connection to a server
|
||||
* @param server the server to add the connection to
|
||||
* @param hostedConnection the connection to be added
|
||||
*/
|
||||
@Override
|
||||
public void connectionAdded(Server server, HostedConnection hostedConnection) {
|
||||
LOGGER.log(Level.INFO, "new connection {0}", hostedConnection); //NON-NLS
|
||||
logic.addPlayer(hostedConnection.getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a standing connection from a server
|
||||
* @param server the server to add the connection to
|
||||
* @param hostedConnection the connection to be added
|
||||
*/
|
||||
@Override
|
||||
public void connectionRemoved(Server server, HostedConnection hostedConnection) {
|
||||
LOGGER.log(Level.INFO, "connection closed: {0}", hostedConnection); //NON-NLS
|
||||
@@ -158,6 +181,10 @@ public void connectionRemoved(Server server, HostedConnection hostedConnection)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Shuts down the server and terminates the application with the given exit code
|
||||
* @param exitValue the exit status code
|
||||
*/
|
||||
private void exit(int exitValue) { //NON-NLS
|
||||
LOGGER.log(Level.INFO, "close request"); //NON-NLS
|
||||
if (myServer != null)
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
////////////////////////////////////////
|
||||
// Programming project code
|
||||
// UniBw M, 2022, 2023, 2024
|
||||
// www.unibw.de/inf2
|
||||
// (c) Mark Minas (mark.minas@unibw.de)
|
||||
////////////////////////////////////////
|
||||
|
||||
package pp.battleship.server;
|
||||
|
||||
import pp.battleship.message.client.ClientInterpreter;
|
||||
import pp.battleship.message.client.ClientMessage;
|
||||
|
||||
/**
|
||||
* Represents a message received from a client
|
||||
* @param message the received message
|
||||
* @param from the ID of the client that sent the message
|
||||
*/
|
||||
record ReceivedMessage(ClientMessage message, int from) {
|
||||
void process(ClientInterpreter interpreter) {
|
||||
message.accept(interpreter, from);
|
||||
|
||||
Reference in New Issue
Block a user