From ce4de99fcc5cb33d2995b94aa3df80ff2403e384 Mon Sep 17 00:00:00 2001 From: Alyssa Date: Sun, 13 Oct 2024 10:51:05 +0100 Subject: [PATCH] tidy up abstract client stuff -> ClientUtil --- client/src/main/java/EntropyMain.java | 18 ++-- .../main/java/online/screen/EntropyLobby.java | 2 +- .../online/util/DesktopEntropyClient.java | 88 ------------------- .../java/online/util/HeartbeatRunnable.java | 4 +- .../java/online/util/XmlBuilderClient.java | 8 -- client/src/main/java/screen/MainScreen.java | 7 +- .../java/util/ClientNotificationRunnable.java | 10 +-- .../{AbstractClient.java => ClientUtil.java} | 87 ++++++++---------- client/src/main/java/util/MessageSender.java | 37 ++++---- client/src/main/java/util/MessageUtil.java | 5 +- client/src/main/kotlin/MainUtil.kt | 6 +- client/src/main/kotlin/util/DialogUtilNew.kt | 1 + client/src/main/kotlin/util/UpdateManager.kt | 2 +- .../src/test/kotlin/util/UpdateManagerTest.kt | 6 +- 14 files changed, 80 insertions(+), 201 deletions(-) delete mode 100644 client/src/main/java/online/util/DesktopEntropyClient.java rename client/src/main/java/util/{AbstractClient.java => ClientUtil.java} (56%) diff --git a/client/src/main/java/EntropyMain.java b/client/src/main/java/EntropyMain.java index c30db2e..effd3e3 100644 --- a/client/src/main/java/EntropyMain.java +++ b/client/src/main/java/EntropyMain.java @@ -5,7 +5,6 @@ import javax.swing.WindowConstants; import logging.LoggerUncaughtExceptionHandler; -import online.util.DesktopEntropyClient; import screen.MainScreen; import screen.ScreenCache; import util.*; @@ -24,14 +23,13 @@ public static void main(String[] args) Debug.initialise(new DebugOutputSystemOut()); Thread.setDefaultUncaughtExceptionHandler(new LoggerUncaughtExceptionHandler()); MainUtilKt.configureLogging(); - AbstractClient.setInstance(new DesktopEntropyClient()); //Dev mode - AbstractClient.parseProgramArguments(args); + ClientUtil.parseProgramArguments(args); setLookAndFeel(); - if (AbstractClient.devMode) + if (ClientUtil.devMode) { setInstanceNumber(); } @@ -56,13 +54,13 @@ else if (!bindOnPort(BIND_PORT_NUMBER)) private static void setLookAndFeel() { - logger.info("laf.init", "Initialising Look & Feel - Operating System: " + AbstractClient.operatingSystem); + logger.info("laf.init", "Initialising Look & Feel - Operating System: " + ClientUtil.operatingSystem); String lookAndFeel = null; try { lookAndFeel = prefs.get(PREFERENCES_STRING_LOOK_AND_FEEL, "Metal"); - if (AbstractClient.isAppleOs() + if (ClientUtil.isAppleOs() && lookAndFeel.equals("Metal")) { //This doesn't seem to work on macs... @@ -94,11 +92,11 @@ private static void setInstanceNumber() while (!boundSuccessfully) { startingPortNumber++; - AbstractClient.instanceNumber++; + ClientUtil.instanceNumber++; boundSuccessfully = bindOnPort(startingPortNumber); } - logger.info("instanceCheck", "I am instance number " + AbstractClient.instanceNumber); + logger.info("instanceCheck", "I am instance number " + ClientUtil.instanceNumber); } private static boolean bindOnPort(int portNumber) @@ -120,12 +118,12 @@ private static void checkForUpdatesIfRequired() { boolean checkForUpdates = Registry.prefs.getBoolean(PREFERENCES_BOOLEAN_CHECK_FOR_UPDATES, true); if (!checkForUpdates - || AbstractClient.devMode) + || ClientUtil.devMode) { logger.info("updateCheck", "Not checking for updates as preference is disabled or I'm in dev mode"); return; } - AbstractClient.getInstance().checkForUpdatesIfRequired(); + ClientUtil.checkForUpdatesIfRequired(); } } diff --git a/client/src/main/java/online/screen/EntropyLobby.java b/client/src/main/java/online/screen/EntropyLobby.java index b193040..a990361 100644 --- a/client/src/main/java/online/screen/EntropyLobby.java +++ b/client/src/main/java/online/screen/EntropyLobby.java @@ -164,7 +164,7 @@ public void run() AchievementsUtil.unlockConnected(); //Start the notification thread, this is how the server will send us unsolicited messages - AbstractClient.getInstance().startNotificationThreads(); + ClientUtil.startNotificationThreads(); AchievementsDialog achievementsDialog = ScreenCache.getAchievementsDialog(); achievementsDialog.refresh(false); diff --git a/client/src/main/java/online/util/DesktopEntropyClient.java b/client/src/main/java/online/util/DesktopEntropyClient.java deleted file mode 100644 index 0ae487a..0000000 --- a/client/src/main/java/online/util/DesktopEntropyClient.java +++ /dev/null @@ -1,88 +0,0 @@ -package online.util; - -import online.screen.ConnectingDialog; -import online.screen.EntropyLobby; -import screen.ScreenCache; -import util.*; - -public class DesktopEntropyClient extends AbstractClient -{ - @Override - public void init() - { - } - - @Override - public String getUsername() - { - return ScreenCache.getEntropyLobby().getUsername(); - } - - @Override - public boolean isOnline() - { - return ScreenCache.getEntropyLobby().isVisible(); - } - - @Override - public void handleResponse(String message, String encryptedResponse) throws Throwable - { - ResponseHandler.handleResponse(message, encryptedResponse); - } - - @Override - public boolean isCommunicatingWithServer() - { - ConnectingDialog dialog = ScreenCache.getConnectingDialog(); - return dialog.isVisible(); - } - - @Override - public void finishServerCommunication() - { - ScreenCache.dismissConnectingDialog(); - } - - @Override - public void unableToConnect() - { - ConnectingDialog dialog = ScreenCache.getConnectingDialog(); - dialog.dismissDialog(); - DialogUtil.showErrorLater("Unable to connect."); - } - - @Override - public void connectionLost() - { - DialogUtil.showConnectionLost(); - } - - @Override - public void goOffline() - { - EntropyLobby lobby = ScreenCache.getEntropyLobby(); - lobby.exit(true); - } - - @Override - public void sendAsyncInSingleThread(MessageSenderParams message) - { - addToPendingMessages(message); - - MessageSender senderRunnable = new MessageSender(this); - Thread senderThread = new Thread(senderRunnable, "MessageSender-" + System.currentTimeMillis()); - senderThread.start(); - } - - @Override - public String sendSyncOnDevice(MessageSender runnable) - { - return runnable.sendMessage(); - } - - @Override - public void checkForUpdates() - { - ClientGlobals.INSTANCE.getUpdateManager().checkForUpdates(OnlineConstants.ENTROPY_VERSION_NUMBER); - } -} diff --git a/client/src/main/java/online/util/HeartbeatRunnable.java b/client/src/main/java/online/util/HeartbeatRunnable.java index 7841afb..a23396d 100644 --- a/client/src/main/java/online/util/HeartbeatRunnable.java +++ b/client/src/main/java/online/util/HeartbeatRunnable.java @@ -4,7 +4,7 @@ import org.w3c.dom.Document; -import util.AbstractClient; +import util.ClientUtil; import util.MessageUtil; import static utils.CoreGlobals.logger; @@ -26,7 +26,7 @@ public void run() { while (lobby.isVisible()) { - long lastSentMessageMillis = AbstractClient.getInstance().getLastSentMessageMillis(); + long lastSentMessageMillis = ClientUtil.getLastSentMessageMillis(); long currentMillis = System.currentTimeMillis(); long difference = currentMillis - lastSentMessageMillis; if (difference >= HEARTBEAT_THREADHOLD) diff --git a/client/src/main/java/online/util/XmlBuilderClient.java b/client/src/main/java/online/util/XmlBuilderClient.java index 63d22f6..3bc2226 100644 --- a/client/src/main/java/online/util/XmlBuilderClient.java +++ b/client/src/main/java/online/util/XmlBuilderClient.java @@ -1,18 +1,10 @@ package online.util; -import javax.crypto.SecretKey; - import object.Bid; import org.w3c.dom.Document; import org.w3c.dom.Element; -import util.AbstractClient; -import util.Debug; -import util.EncryptionUtil; -import util.KeyGeneratorUtil; -import util.MessageUtil; -import util.OnlineConstants; import util.XmlConstants; import util.XmlUtil; diff --git a/client/src/main/java/screen/MainScreen.java b/client/src/main/java/screen/MainScreen.java index 6207990..a338069 100644 --- a/client/src/main/java/screen/MainScreen.java +++ b/client/src/main/java/screen/MainScreen.java @@ -9,7 +9,6 @@ import online.util.XmlBuilderDesktop; import org.w3c.dom.Document; import util.*; -import utils.CoreGlobals; import javax.swing.*; import javax.swing.border.BevelBorder; @@ -659,7 +658,7 @@ private void selectGameScreen(int gameMode) @Override public boolean commandsEnabled() { - return AbstractClient.devMode + return ClientUtil.devMode || rewards.getBoolean(Registry.REWARDS_BOOLEAN_CHEATS, false); } @@ -667,7 +666,7 @@ public boolean commandsEnabled() public String processCommand(String command) { String textToShow = ""; - if (AbstractClient.devMode) + if (ClientUtil.devMode) { boolean processed = processDevModeCommand(command); if (processed) @@ -748,7 +747,7 @@ public void onStart() leftPanel.add(gamePanel, BorderLayout.CENTER); //If we've just updated, show the change log automatically - if (AbstractClient.justUpdated) + if (ClientUtil.justUpdated) { ChangeLog dialog = new ChangeLog(); dialog.setLocationRelativeTo(null); diff --git a/client/src/main/java/util/ClientNotificationRunnable.java b/client/src/main/java/util/ClientNotificationRunnable.java index 13f717e..6bb2691 100644 --- a/client/src/main/java/util/ClientNotificationRunnable.java +++ b/client/src/main/java/util/ClientNotificationRunnable.java @@ -8,11 +8,9 @@ public class ClientNotificationRunnable implements Runnable private static final int SO_TIMEOUT_MILLIS = 120000; //2 minutes private String socketType = null; - private AbstractClient client = null; - public ClientNotificationRunnable(AbstractClient client, String socketType) + public ClientNotificationRunnable(String socketType) { - this.client = client; this.socketType = socketType; } @@ -23,16 +21,16 @@ public void run() String messageStr = null; String response = null; - while (client.isOnline()) + while (ClientUtil.isOnline()) { try { - String username = client.getUsername(); + String username = ClientUtil.getUsername(); Document notificationXml = XmlUtil.factorySimpleMessage(username, socketType); messageStr = XmlUtil.getStringFromDocument(notificationXml); //Send encrypted, with a 1 minute timeout - response = client.sendSync(notificationXml, true, SO_TIMEOUT_MILLIS, true); + response = ClientUtil.sendSync(notificationXml, true, SO_TIMEOUT_MILLIS, true); //If the thread has stopped due to a d/c, we'll get a null response. if (response == null) diff --git a/client/src/main/java/util/AbstractClient.java b/client/src/main/java/util/ClientUtil.java similarity index 56% rename from client/src/main/java/util/AbstractClient.java rename to client/src/main/java/util/ClientUtil.java index ce6dbfd..5cc3074 100644 --- a/client/src/main/java/util/AbstractClient.java +++ b/client/src/main/java/util/ClientUtil.java @@ -1,6 +1,7 @@ package util; import org.w3c.dom.Document; +import screen.ScreenCache; import java.util.ArrayList; import java.util.Locale; @@ -10,39 +11,31 @@ /** * Interface used by Entropy Android & Desktop for anything to do with the online session */ -public abstract class AbstractClient implements OnlineConstants +public class ClientUtil { public static boolean devMode = false; public static String operatingSystem = System.getProperty("os.name").toLowerCase(Locale.ENGLISH); public static boolean justUpdated = false; public static int instanceNumber = 1; - //Instance - private static AbstractClient client = null; - //Properties on the instance - private long lastSentMessageMillis = -1; - private ArrayList pendingMessages = new ArrayList<>(); - - /** - * Abstract methods - */ - public abstract void init(); - public abstract String getUsername(); - public abstract boolean isOnline(); - public abstract void sendAsyncInSingleThread(MessageSenderParams message); - public abstract String sendSyncOnDevice(MessageSender runnable); - public abstract void handleResponse(String message, String encryptedResponse) throws Throwable; - public abstract void checkForUpdates(); - - /** - * Use these to show waiting dialogs/info to the user - */ - public abstract boolean isCommunicatingWithServer(); - public abstract void finishServerCommunication(); - public abstract void unableToConnect(); - public abstract void connectionLost(); - public abstract void goOffline(); + private static long lastSentMessageMillis = -1; + private final static ArrayList pendingMessages = new ArrayList<>(); + + + public static String getUsername() { + return ScreenCache.getEntropyLobby().getUsername(); + } + public static boolean isOnline() { + return ScreenCache.getEntropyLobby().isVisible(); + } + public static void sendAsyncInSingleThread(MessageSenderParams message) { + addToPendingMessages(message); + + MessageSender senderRunnable = new MessageSender(); + Thread senderThread = new Thread(senderRunnable, "MessageSender-" + System.currentTimeMillis()); + senderThread.start(); + } /** * Helpers during startup @@ -81,37 +74,27 @@ public static boolean isWindowsOs() return operatingSystem.contains("windows"); } - public void checkForUpdatesIfRequired() + public static void checkForUpdatesIfRequired() { if (justUpdated) { logger.info("justUpdated", "Just updated - not checking for updates"); return; } - - checkForUpdates(); + + ClientGlobals.INSTANCE.getUpdateManager().checkForUpdates(OnlineConstants.ENTROPY_VERSION_NUMBER); } - public long getLastSentMessageMillis() + public static long getLastSentMessageMillis() { return lastSentMessageMillis; } - public void setLastSentMessageMillis(long lastSentMessageMillis) - { - this.lastSentMessageMillis = lastSentMessageMillis; - } - - public static AbstractClient getInstance() + public static void setLastSentMessageMillis(long newValue) { - return client; - } - public static void setInstance(AbstractClient client) - { - AbstractClient.client = client; - client.init(); + lastSentMessageMillis = newValue; } - public String sendSync(Document message, boolean encrypt, int readTimeOut, boolean alwaysRetryOnSoTimeout) + public static String sendSync(Document message, boolean encrypt, int readTimeOut, boolean alwaysRetryOnSoTimeout) { String messageString = XmlUtil.getStringFromDocument(message); String encryptedMessageString = messageString; @@ -126,30 +109,32 @@ public String sendSync(Document message, boolean encrypt, int readTimeOut, boole wrapper.setReadTimeOut(readTimeOut); wrapper.setAlwaysRetryOnSoTimeout(alwaysRetryOnSoTimeout); - MessageSender sender = new MessageSender(this, wrapper); - return sendSyncOnDevice(sender); + MessageSender sender = new MessageSender(wrapper); + return sender.sendMessage(); } - public void startNotificationThreads() + public static void startNotificationThreads() { startNotificationThread(XmlConstants.SOCKET_NAME_GAME); startNotificationThread(XmlConstants.SOCKET_NAME_CHAT); startNotificationThread(XmlConstants.SOCKET_NAME_LOBBY); } - private void startNotificationThread(String socketType) + private static void startNotificationThread(String socketType) { - ClientNotificationRunnable runnable = new ClientNotificationRunnable(this, socketType); + ClientNotificationRunnable runnable = new ClientNotificationRunnable(socketType); Thread notificationThread = new Thread(runnable, socketType + "Thread"); notificationThread.start(); } - public void addToPendingMessages(MessageSenderParams message) + public static void addToPendingMessages(MessageSenderParams message) { pendingMessages.add(message); } - public MessageSenderParams getNextMessageToSend() + public static MessageSenderParams getNextMessageToSend() { - return pendingMessages.remove(0); + synchronized (pendingMessages) { + return pendingMessages.remove(0); + } } } diff --git a/client/src/main/java/util/MessageSender.java b/client/src/main/java/util/MessageSender.java index 551a0ec..66e53cb 100644 --- a/client/src/main/java/util/MessageSender.java +++ b/client/src/main/java/util/MessageSender.java @@ -9,8 +9,10 @@ import java.net.SocketTimeoutException; import kotlin.Pair; +import online.util.ResponseHandler; import org.w3c.dom.Document; import org.w3c.dom.Element; +import screen.ScreenCache; import static utils.CoreGlobals.logger; @@ -19,14 +21,12 @@ public class MessageSender implements Runnable private MessageSenderParams messageParms = null; private String encryptedResponseString = null; private int currentRetries = 0; - private AbstractClient client = null; /** * Constructor where message params are passed in directly. Used when sending synchronously. */ - public MessageSender(AbstractClient client, MessageSenderParams messageWrapper) + public MessageSender(MessageSenderParams messageWrapper) { - this.client = client; this.messageParms = messageWrapper; } @@ -34,22 +34,15 @@ public MessageSender(AbstractClient client, MessageSenderParams messageWrapper) * Constructor just containing the client. When this runnable gets kicked off, we'll get the next * messageWrapper to send off of the client. */ - public MessageSender(AbstractClient client) - { - this.client = client; - } + public MessageSender() {} @Override public void run() { if (messageParms == null) { - //We're picking up off the queue, so we should synchronise - synchronized (client) - { - this.messageParms = client.getNextMessageToSend(); - sendMessage(); - } + this.messageParms = ClientUtil.getNextMessageToSend(); + sendMessage(); } else { @@ -70,7 +63,7 @@ public String sendMessage() try (Socket socket = new Socket(address, portNumber); PrintWriter out = new PrintWriter(socket.getOutputStream(), true);) { - client.setLastSentMessageMillis(System.currentTimeMillis()); + ClientUtil.setLastSentMessageMillis(System.currentTimeMillis()); int soTimeOut = messageParms.getReadTimeOut(); socket.setSoTimeout(soTimeOut); @@ -95,7 +88,7 @@ public String sendMessage() //Handle the response if we're not ignoring it if (!messageParms.getIgnoreResponse()) { - client.handleResponse(messageString, encryptedResponseString); + ResponseHandler.handleResponse(messageString, encryptedResponseString); } return encryptedResponseString; @@ -145,7 +138,7 @@ private void sleepWithCatch() private String retryOrStackTrace(Throwable t) { - if (!AbstractClient.getInstance().isOnline()) + if (!ClientUtil.isOnline()) { return null; } @@ -175,18 +168,18 @@ private String retryOrStackTrace(Throwable t) new Pair<>("message", messageParms.getMessageString()), new Pair<>("previousStack", messageParms.getCreationStack())); - if (client.isCommunicatingWithServer()) + if (ScreenCache.getConnectingDialog().isVisible()) { - client.unableToConnect(); + ScreenCache.getConnectingDialog().dismissDialog(); + DialogUtilNew.showErrorLater("Unable to connect."); } else { - client.finishServerCommunication(); - client.goOffline(); - + ScreenCache.getEntropyLobby().exit(true); + if (!messageParms.getIgnoreResponse()) { - client.connectionLost(); + DialogUtil.showConnectionLost(); } } diff --git a/client/src/main/java/util/MessageUtil.java b/client/src/main/java/util/MessageUtil.java index d0eab84..db10a13 100644 --- a/client/src/main/java/util/MessageUtil.java +++ b/client/src/main/java/util/MessageUtil.java @@ -4,6 +4,7 @@ import http.LegacyConstants; import kotlin.Pair; import org.w3c.dom.Document; +import screen.ScreenCache; import javax.crypto.SecretKey; import java.net.InetAddress; @@ -51,7 +52,7 @@ public static void sendMessage(MessageSenderParams message, boolean encrypt) message.setEncryptedMessageString(encryptedMessageString); - AbstractClient.getInstance().sendAsyncInSingleThread(message); + ClientUtil.sendAsyncInSingleThread(message); } public static int getRandomPortNumber() @@ -78,7 +79,7 @@ public static InetAddress factoryInetAddress(String ipAddress) public static void stackTraceAndDumpMessages(Throwable t, Throwable clientStackTrace, String messageStr, String encryptedResponseStr) { - AbstractClient.getInstance().finishServerCommunication(); + ScreenCache.dismissConnectingDialog(); logger.error("messageFailure", "Failed to send message: " + messageStr, diff --git a/client/src/main/kotlin/MainUtil.kt b/client/src/main/kotlin/MainUtil.kt index e0cbf1b..c7bc5cc 100644 --- a/client/src/main/kotlin/MainUtil.kt +++ b/client/src/main/kotlin/MainUtil.kt @@ -4,8 +4,8 @@ import logging.KEY_DEVICE_ID import logging.KEY_DEV_MODE import logging.KEY_OPERATING_SYSTEM import logging.KEY_USERNAME -import util.AbstractClient import util.ClientGlobals +import util.ClientUtil import util.CoreRegistry.INSTANCE_STRING_DEVICE_ID import util.CoreRegistry.instance import util.OnlineConstants @@ -20,9 +20,9 @@ fun configureLogging() { logger.addToContext(KEY_USERNAME, getUsername()) logger.addToContext(KEY_APP_VERSION, OnlineConstants.ENTROPY_VERSION_NUMBER) - logger.addToContext(KEY_OPERATING_SYSTEM, AbstractClient.operatingSystem) + logger.addToContext(KEY_OPERATING_SYSTEM, ClientUtil.operatingSystem) logger.addToContext(KEY_DEVICE_ID, getDeviceId()) - logger.addToContext(KEY_DEV_MODE, AbstractClient.devMode.toString()) + logger.addToContext(KEY_DEV_MODE, ClientUtil.devMode.toString()) } fun getDeviceId() = instance.get(INSTANCE_STRING_DEVICE_ID, null) ?: setDeviceId() diff --git a/client/src/main/kotlin/util/DialogUtilNew.kt b/client/src/main/kotlin/util/DialogUtilNew.kt index 7200f43..f2a8c0b 100644 --- a/client/src/main/kotlin/util/DialogUtilNew.kt +++ b/client/src/main/kotlin/util/DialogUtilNew.kt @@ -42,6 +42,7 @@ object DialogUtilNew { logDialogClosed("Error", null) } + @JvmStatic fun showErrorLater(errorText: String) { SwingUtilities.invokeLater { showError(errorText) } } diff --git a/client/src/main/kotlin/util/UpdateManager.kt b/client/src/main/kotlin/util/UpdateManager.kt index 65d2f20..5127810 100644 --- a/client/src/main/kotlin/util/UpdateManager.kt +++ b/client/src/main/kotlin/util/UpdateManager.kt @@ -67,7 +67,7 @@ class UpdateManager { // An update is available logger.info("updateAvailable", "Newer release available - $newVersion") - if (!AbstractClient.isWindowsOs()) { + if (!ClientUtil.isWindowsOs()) { showManualDownloadMessage(newVersion) return false } diff --git a/client/src/test/kotlin/util/UpdateManagerTest.kt b/client/src/test/kotlin/util/UpdateManagerTest.kt index 4faf3ab..f88e32d 100644 --- a/client/src/test/kotlin/util/UpdateManagerTest.kt +++ b/client/src/test/kotlin/util/UpdateManagerTest.kt @@ -165,7 +165,7 @@ class UpdateManagerTest : AbstractTest() { @Test fun `Should show an info and not proceed to auto update if OS is not windows`() { - AbstractClient.operatingSystem = "foo" + ClientUtil.operatingSystem = "foo" val metadata = UpdateMetadata("v100", 123456, "Dartzee_x_y.jar", 100) shouldUpdateAsync(OnlineConstants.ENTROPY_VERSION_NUMBER, metadata).get() shouldBe false @@ -181,7 +181,7 @@ class UpdateManagerTest : AbstractTest() { @Test fun `Should not proceed with the update if user selects 'No'`() { - AbstractClient.operatingSystem = "windows" + ClientUtil.operatingSystem = "windows" val metadata = UpdateMetadata("foo", 123456, "Dartzee_x_y.jar", 100) val result = shouldUpdateAsync("bar", metadata) @@ -197,7 +197,7 @@ class UpdateManagerTest : AbstractTest() { @Test fun `Should proceed with the update if user selects 'Yes'`() { - AbstractClient.operatingSystem = "windows" + ClientUtil.operatingSystem = "windows" val metadata = UpdateMetadata("foo", 123456, "Dartzee_x_y.jar", 100) val result = shouldUpdateAsync("bar", metadata)