"redo" task11 (black screen)

This commit is contained in:
Luca Puderbach 2024-10-14 04:17:43 +02:00
parent 86a3d90bf8
commit c525a8994c
5 changed files with 44 additions and 47 deletions

View File

@ -9,6 +9,9 @@ dependencies {
implementation project(":battleship:model")
implementation libs.jme3.desktop
implementation project(path: ':battleship:server')
implementation project(path: ':battleship:server')
runtimeOnly libs.jme3.awt.dialogs
runtimeOnly libs.jme3.plugins

View File

@ -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.client;
import java.lang.System.Logger;
@ -19,7 +12,7 @@ import com.simsilica.lemur.TextField;
import com.simsilica.lemur.component.SpringGridLayout;
import static pp.battleship.Resources.lookup;
import pp.battleship.client.server.BattleshipSelfStart;
import pp.battleship.client.server.BattleshipSelfhostServer;
import pp.dialog.Dialog;
import pp.dialog.DialogBuilder;
import pp.dialog.SimpleDialog;
@ -30,16 +23,16 @@ import pp.dialog.SimpleDialog;
*/
class NetworkDialog extends SimpleDialog {
private static final Logger LOGGER = System.getLogger(NetworkDialog.class.getName());
private static final String LOCALHOST = "localhorst"; //NON-NLS
private static final String DEFAULT_PORT = "4321"; //NON-NLS
private static final String LOCALHOST = "localhost"; //NON-NLS
private static final String DEFAULT_PORT = "1234"; //NON-NLS
private final NetworkSupport network;
private final TextField host = new TextField(LOCALHOST);
private final TextField port = new TextField(DEFAULT_PORT);
private static final Checkbox HOST = new Checkbox(lookup("server.host"));
private String hostname;
private int portNumber;
private Future<Object> connectionFuture;
private Dialog progressDialog;
private static final Checkbox ServerStart = new Checkbox(lookup("server.start"));
/**
* Constructs a new NetworkDialog.
@ -59,7 +52,7 @@ class NetworkDialog extends SimpleDialog {
input.addChild(host, 1);
input.addChild(new Label(lookup("port.number") + ": "));
input.addChild(port, 1);
input.addChild(ServerStart).addClickCommands(s -> ifTopDialog(this::startClientServer));
input.addChild(HOST).addClickCommands(s -> ifTopDialog(this::startClientServer));
DialogBuilder.simple(app.getDialogManager())
.setTitle(lookup("server.dialog"))
@ -154,13 +147,17 @@ class NetworkDialog extends SimpleDialog {
network.getApp().errorDialog(lookup("server.connection.failed"));
network.getApp().setInfoText(e.getLocalizedMessage());
}
/**
* Starts the server of the client in a new thread and catches connectivity issues.
*/
private void startClientServer() {
ServerStart.setEnabled(false);
HOST.setEnabled(false);
Thread serverThread = new Thread(() -> {
try {
BattleshipSelfStart.main(null);
BattleshipSelfhostServer.main(null);
} catch (Exception e) {
ServerStart.setEnabled(true);
HOST.setEnabled(true);
LOGGER.log(Level.ERROR, "Server could not be started", e);
network.getApp().errorDialog("Could not start server: " + e.getMessage());
}
@ -168,3 +165,4 @@ class NetworkDialog extends SimpleDialog {
serverThread.start();
}
}

View File

@ -7,6 +7,15 @@
package pp.battleship.client.server;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.LogManager;
import com.jme3.network.ConnectionListener;
import com.jme3.network.HostedConnection;
import com.jme3.network.Message;
@ -14,6 +23,7 @@ import com.jme3.network.MessageListener;
import com.jme3.network.Network;
import com.jme3.network.Server;
import com.jme3.network.serializing.Serializer;
import pp.battleship.BattleshipConfig;
import pp.battleship.game.server.Player;
import pp.battleship.game.server.ServerGameLogic;
@ -29,26 +39,17 @@ import pp.battleship.model.Battleship;
import pp.battleship.model.IntPoint;
import pp.battleship.model.Shot;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.LogManager;
/**
* Server implementing the visitor pattern as MessageReceiver for ClientMessages
*/
public class BattleshipSelfStart implements MessageListener<HostedConnection>, ConnectionListener, ServerSender {
private static final Logger LOGGER = System.getLogger(BattleshipSelfStart.class.getName());
public class BattleshipSelfhostServer implements MessageListener<HostedConnection>, ConnectionListener, ServerSender {
private static final Logger LOGGER = System.getLogger(BattleshipSelfhostServer.class.getName());
private static final File CONFIG_FILE = new File("server.properties");
private final BattleshipConfig config = new BattleshipConfig();
private Server myServer;
private final ServerGameLogic logic;
private final BlockingQueue<ReceivedMessage> pendingMessages = new LinkedBlockingQueue<>();
private final BlockingQueue<ReceivedMessageSelfhost> pendingMessages = new LinkedBlockingQueue<>();
static {
// Configure logging
@ -66,13 +67,13 @@ public class BattleshipSelfStart implements MessageListener<HostedConnection>, C
* Starts the server.
*/
public static void main(String[] args) {
new BattleshipSelfStart().run();
new BattleshipSelfhostServer().run();
}
/**
* Creates the server.
*/
BattleshipSelfStart() {
BattleshipSelfhostServer() {
config.readFromIfExists(CONFIG_FILE);
LOGGER.log(Level.INFO, "Configuration: {0}", config); //NON-NLS
logic = new ServerGameLogic(this, config);
@ -130,7 +131,7 @@ public class BattleshipSelfStart implements MessageListener<HostedConnection>, C
public void messageReceived(HostedConnection source, Message message) {
LOGGER.log(Level.INFO, "message received from {0}: {1}", source.getId(), message); //NON-NLS
if (message instanceof ClientMessage clientMessage)
pendingMessages.add(new ReceivedMessage(clientMessage, source.getId()));
pendingMessages.add(new ReceivedMessageSelfhost(clientMessage, source.getId()));
}
@Override

View File

@ -1,17 +0,0 @@
////////////////////////////////////////
// Programming project code
// UniBw M, 2022, 2023, 2024
// www.unibw.de/inf2
// (c) Mark Minas (mark.minas@unibw.de)
////////////////////////////////////////
package pp.battleship.client.server;
import pp.battleship.message.client.ClientInterpreter;
import pp.battleship.message.client.ClientMessage;
record ReceivedMessage(ClientMessage message, int from) {
void process(ClientInterpreter interpreter) {
message.accept(interpreter, from);
}
}

View File

@ -0,0 +1,12 @@
package pp.battleship.client.server;
import pp.battleship.message.client.ClientInterpreter;
import pp.battleship.message.client.ClientMessage;
record ReceivedMessageSelfhost(ClientMessage message, int from) {
void process(ClientInterpreter interpreter) {
message.accept(interpreter, from);
}
}