From c525a8994c7adf377a0df5b4af6fe641359d7c91 Mon Sep 17 00:00:00 2001 From: Luca Puderbach Date: Mon, 14 Oct 2024 04:17:43 +0200 Subject: [PATCH] "redo" task11 (black screen) --- Projekte/battleship/client/build.gradle | 3 ++ .../pp/battleship/client/NetworkDialog.java | 28 ++++++++--------- ...art.java => BattleshipSelfhostServer.java} | 31 ++++++++++--------- .../client/server/ReceiveMessage.java | 17 ---------- .../server/ReceivedMessageSelfhost.java | 12 +++++++ 5 files changed, 44 insertions(+), 47 deletions(-) rename Projekte/battleship/client/src/main/java/pp/battleship/client/server/{BattleshipSelfStart.java => BattleshipSelfhostServer.java} (93%) delete mode 100644 Projekte/battleship/client/src/main/java/pp/battleship/client/server/ReceiveMessage.java create mode 100644 Projekte/battleship/client/src/main/java/pp/battleship/client/server/ReceivedMessageSelfhost.java diff --git a/Projekte/battleship/client/build.gradle b/Projekte/battleship/client/build.gradle index a8aeff3..03c3222 100644 --- a/Projekte/battleship/client/build.gradle +++ b/Projekte/battleship/client/build.gradle @@ -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 diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/NetworkDialog.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/NetworkDialog.java index 3523492..8008024 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/NetworkDialog.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/NetworkDialog.java @@ -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 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(); } } + diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/server/BattleshipSelfStart.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/server/BattleshipSelfhostServer.java similarity index 93% rename from Projekte/battleship/client/src/main/java/pp/battleship/client/server/BattleshipSelfStart.java rename to Projekte/battleship/client/src/main/java/pp/battleship/client/server/BattleshipSelfhostServer.java index eb9bb92..9114d73 100644 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/server/BattleshipSelfStart.java +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/server/BattleshipSelfhostServer.java @@ -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, ConnectionListener, ServerSender { - private static final Logger LOGGER = System.getLogger(BattleshipSelfStart.class.getName()); +public class BattleshipSelfhostServer implements MessageListener, 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 pendingMessages = new LinkedBlockingQueue<>(); + private final BlockingQueue pendingMessages = new LinkedBlockingQueue<>(); static { // Configure logging @@ -66,13 +67,13 @@ public class BattleshipSelfStart implements MessageListener, 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, 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 diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/server/ReceiveMessage.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/server/ReceiveMessage.java deleted file mode 100644 index a688fe9..0000000 --- a/Projekte/battleship/client/src/main/java/pp/battleship/client/server/ReceiveMessage.java +++ /dev/null @@ -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); - } -} diff --git a/Projekte/battleship/client/src/main/java/pp/battleship/client/server/ReceivedMessageSelfhost.java b/Projekte/battleship/client/src/main/java/pp/battleship/client/server/ReceivedMessageSelfhost.java new file mode 100644 index 0000000..fe15420 --- /dev/null +++ b/Projekte/battleship/client/src/main/java/pp/battleship/client/server/ReceivedMessageSelfhost.java @@ -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); + } +} +