From ef857d60c1db1756bf1c2f81a8d9fa86d5ea2bdf Mon Sep 17 00:00:00 2001 From: ajh123 Date: Thu, 1 Sep 2022 11:15:38 +0100 Subject: [PATCH] Connect to remote subdata server if we are a plugin --- .../ME1312/SubServers/Web/JettyServer.java | 58 ++++++++++--------- .../SubServers/Web/Library/ConfigUpdater.java | 1 - 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/SubServers.Web/src/net/ME1312/SubServers/Web/JettyServer.java b/SubServers.Web/src/net/ME1312/SubServers/Web/JettyServer.java index 432c88d8..02bdc37e 100644 --- a/SubServers.Web/src/net/ME1312/SubServers/Web/JettyServer.java +++ b/SubServers.Web/src/net/ME1312/SubServers/Web/JettyServer.java @@ -51,6 +51,7 @@ public class JettyServer { public YAMLConfig config; public ObjectMap host = null; public SubProtocol subprotocol; + public SubProxy proxy = null; public final SubAPI api = new SubAPI(this); @@ -62,6 +63,7 @@ public class JettyServer { if (proxy == null){ isPlugin = false; } + this.proxy = proxy; log = new Logger("SubServers"); info = PluginInfo.load(this); @@ -77,7 +79,6 @@ public class JettyServer { engine.getPluginManager().loadPlugins(new File(engine.getRuntimeDirectory(), "Plugins")); running = true; - this.isPlugin = isPlugin; reload(false); subdata.put(0, null); @@ -134,32 +135,37 @@ public class JettyServer { } private void connect(final java.util.logging.Logger log, Pair disconnect) throws IOException { - if (!isPlugin) { - final int reconnect = config.get().getMap("Settings").getMap("SubData").getInt("Reconnect", 60); - if (disconnect == null || (this.reconnect && reconnect > 0 && disconnect.key() != DisconnectReason.PROTOCOL_MISMATCH && disconnect.key() != DisconnectReason.ENCRYPTION_MISMATCH)) { - final long reset = resetDate; - final Timer timer = new Timer(SubAPI.getInstance().getAppInfo().getName() + "::SubData_Reconnect_Handler"); - if (disconnect != null) log.info("Attempting reconnect in " + reconnect + " seconds"); - timer.scheduleAtFixedRate(new TimerTask() { - @Override - public void run() { - try { - if (reset == resetDate && (subdata.getOrDefault(0, null) == null || subdata.get(0).isClosed())) { - SubDataClient open = subprotocol.open(InetAddress.getByName(config.get().getMap("Settings").getMap("SubData").getString("Address", "127.0.0.1:4391").split(":")[0]), - Integer.parseInt(config.get().getMap("Settings").getMap("SubData").getString("Address", "127.0.0.1:4391").split(":")[1])); - - if (subdata.getOrDefault(0, null) != null) subdata.get(0).reconnect(open); - subdata.put(0, open); - } - timer.cancel(); - } catch (IOException e) { - log.info("Connection was unsuccessful, retrying in " + reconnect + " seconds"); - } - } - }, (disconnect == null) ? 0 : TimeUnit.SECONDS.toMillis(reconnect), TimeUnit.SECONDS.toMillis(reconnect)); - } + int port = Integer.parseInt(config.get().getMap("Settings").getMap("SubData").getString("Address", "127.0.0.1:4391").split(":")[1]); + String address = config.get().getMap("Settings").getMap("SubData").getString("Address", "127.0.0.1:4391").split(":")[0]; + if (isPlugin){ + address = proxy.subdata.getSocket().getInetAddress().getHostName(); + port = proxy.subdata.getSocket().getLocalPort(); + } + + final int reconnect = config.get().getMap("Settings").getMap("SubData").getInt("Reconnect", 60); + if (disconnect == null || (this.reconnect && reconnect > 0 && disconnect.key() != DisconnectReason.PROTOCOL_MISMATCH && disconnect.key() != DisconnectReason.ENCRYPTION_MISMATCH)) { + final long reset = resetDate; + final Timer timer = new Timer(SubAPI.getInstance().getAppInfo().getName() + "::SubData_Reconnect_Handler"); + if (disconnect != null) log.info("Attempting reconnect in " + reconnect + " seconds"); + String finalAddress = address; + int finalPort = port; + timer.scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + try { + if (reset == resetDate && (subdata.getOrDefault(0, null) == null || subdata.get(0).isClosed())) { + SubDataClient open = subprotocol.open(InetAddress.getByName(finalAddress), finalPort); + + if (subdata.getOrDefault(0, null) != null) subdata.get(0).reconnect(open); + subdata.put(0, open); + } + timer.cancel(); + } catch (IOException e) { + log.info("Connection was unsuccessful, retrying in " + reconnect + " seconds"); + } + } + }, (disconnect == null) ? 0 : TimeUnit.SECONDS.toMillis(reconnect), TimeUnit.SECONDS.toMillis(reconnect)); } - //TODO: do stuff if we are a plugin } public void stop() throws Exception { diff --git a/SubServers.Web/src/net/ME1312/SubServers/Web/Library/ConfigUpdater.java b/SubServers.Web/src/net/ME1312/SubServers/Web/Library/ConfigUpdater.java index 3fc3b662..2d7c5d7a 100644 --- a/SubServers.Web/src/net/ME1312/SubServers/Web/Library/ConfigUpdater.java +++ b/SubServers.Web/src/net/ME1312/SubServers/Web/Library/ConfigUpdater.java @@ -69,7 +69,6 @@ public class ConfigUpdater { if (!isPlugin) { YAMLSection subdata = new YAMLSection(); - subdata.set("Name", updated.getMap("Settings", new YAMLSection()).getMap("SubData", new YAMLSection()).getString("Name", "undefined")); subdata.set("Address", updated.getMap("Settings", new YAMLSection()).getMap("SubData", new YAMLSection()).getString("Address", "127.0.0.1:4391")); if (updated.getMap("Settings", new YAMLSection()).getMap("SubData", new YAMLSection()).contains("Password")) subdata.set("Password", updated.getMap("Settings").getMap("SubData").getString("Password")); if (updated.getMap("Settings", new YAMLSection()).getMap("SubData", new YAMLSection()).contains("Reconnect")) subdata.set("Reconnect", updated.getMap("Settings").getMap("SubData").getInt("Reconnect"));