Connect to remote subdata server if we are a plugin

This commit is contained in:
ajh123 2022-09-01 11:15:38 +01:00
parent 81d9ecd52f
commit ef857d60c1
2 changed files with 32 additions and 27 deletions

View File

@ -51,6 +51,7 @@ public class JettyServer {
public YAMLConfig config; public YAMLConfig config;
public ObjectMap<String> host = null; public ObjectMap<String> host = null;
public SubProtocol subprotocol; public SubProtocol subprotocol;
public SubProxy proxy = null;
public final SubAPI api = new SubAPI(this); public final SubAPI api = new SubAPI(this);
@ -62,6 +63,7 @@ public class JettyServer {
if (proxy == null){ if (proxy == null){
isPlugin = false; isPlugin = false;
} }
this.proxy = proxy;
log = new Logger("SubServers"); log = new Logger("SubServers");
info = PluginInfo.load(this); info = PluginInfo.load(this);
@ -77,7 +79,6 @@ public class JettyServer {
engine.getPluginManager().loadPlugins(new File(engine.getRuntimeDirectory(), "Plugins")); engine.getPluginManager().loadPlugins(new File(engine.getRuntimeDirectory(), "Plugins"));
running = true; running = true;
this.isPlugin = isPlugin;
reload(false); reload(false);
subdata.put(0, null); subdata.put(0, null);
@ -134,32 +135,37 @@ public class JettyServer {
} }
private void connect(final java.util.logging.Logger log, Pair<DisconnectReason, DataClient> disconnect) throws IOException { private void connect(final java.util.logging.Logger log, Pair<DisconnectReason, DataClient> disconnect) throws IOException {
if (!isPlugin) { int port = Integer.parseInt(config.get().getMap("Settings").getMap("SubData").getString("Address", "127.0.0.1:4391").split(":")[1]);
final int reconnect = config.get().getMap("Settings").getMap("SubData").getInt("Reconnect", 60); String address = config.get().getMap("Settings").getMap("SubData").getString("Address", "127.0.0.1:4391").split(":")[0];
if (disconnect == null || (this.reconnect && reconnect > 0 && disconnect.key() != DisconnectReason.PROTOCOL_MISMATCH && disconnect.key() != DisconnectReason.ENCRYPTION_MISMATCH)) { if (isPlugin){
final long reset = resetDate; address = proxy.subdata.getSocket().getInetAddress().getHostName();
final Timer timer = new Timer(SubAPI.getInstance().getAppInfo().getName() + "::SubData_Reconnect_Handler"); port = proxy.subdata.getSocket().getLocalPort();
if (disconnect != null) log.info("Attempting reconnect in " + reconnect + " seconds"); }
timer.scheduleAtFixedRate(new TimerTask() {
@Override final int reconnect = config.get().getMap("Settings").getMap("SubData").getInt("Reconnect", 60);
public void run() { if (disconnect == null || (this.reconnect && reconnect > 0 && disconnect.key() != DisconnectReason.PROTOCOL_MISMATCH && disconnect.key() != DisconnectReason.ENCRYPTION_MISMATCH)) {
try { final long reset = resetDate;
if (reset == resetDate && (subdata.getOrDefault(0, null) == null || subdata.get(0).isClosed())) { final Timer timer = new Timer(SubAPI.getInstance().getAppInfo().getName() + "::SubData_Reconnect_Handler");
SubDataClient open = subprotocol.open(InetAddress.getByName(config.get().getMap("Settings").getMap("SubData").getString("Address", "127.0.0.1:4391").split(":")[0]), if (disconnect != null) log.info("Attempting reconnect in " + reconnect + " seconds");
Integer.parseInt(config.get().getMap("Settings").getMap("SubData").getString("Address", "127.0.0.1:4391").split(":")[1])); String finalAddress = address;
int finalPort = port;
if (subdata.getOrDefault(0, null) != null) subdata.get(0).reconnect(open); timer.scheduleAtFixedRate(new TimerTask() {
subdata.put(0, open); @Override
} public void run() {
timer.cancel(); try {
} catch (IOException e) { if (reset == resetDate && (subdata.getOrDefault(0, null) == null || subdata.get(0).isClosed())) {
log.info("Connection was unsuccessful, retrying in " + reconnect + " seconds"); SubDataClient open = subprotocol.open(InetAddress.getByName(finalAddress), finalPort);
}
} if (subdata.getOrDefault(0, null) != null) subdata.get(0).reconnect(open);
}, (disconnect == null) ? 0 : TimeUnit.SECONDS.toMillis(reconnect), TimeUnit.SECONDS.toMillis(reconnect)); 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 { public void stop() throws Exception {

View File

@ -69,7 +69,6 @@ public class ConfigUpdater {
if (!isPlugin) { if (!isPlugin) {
YAMLSection subdata = new YAMLSection(); 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")); 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("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")); if (updated.getMap("Settings", new YAMLSection()).getMap("SubData", new YAMLSection()).contains("Reconnect")) subdata.set("Reconnect", updated.getMap("Settings").getMap("SubData").getInt("Reconnect"));