From 1d1831cc94adc712776f890fdba51df13b78e24c Mon Sep 17 00:00:00 2001 From: sirjonasxx <36828922+sirjonasxx@users.noreply.github.com> Date: Sat, 3 Oct 2020 21:56:26 +0200 Subject: [PATCH] Expose packet<->string functionality to (network) extension interface preparation for python extensions --- .../extensionhandler/ExtensionHandler.java | 21 +++++++++++++++++ .../extensions/ExtensionListener.java | 3 +++ .../extensions/GEarthExtension.java | 17 ++++++++++++++ .../network/NetworkExtension.java | 23 +++++++++++++++++++ .../network/NetworkExtensionInfo.java | 7 ++++++ .../simple/ExampleExtension.java | 15 ++++++++++++ 6 files changed, 86 insertions(+) diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/ExtensionHandler.java b/G-Earth/src/main/java/gearth/services/extensionhandler/ExtensionHandler.java index 4296bb7..575f93c 100644 --- a/G-Earth/src/main/java/gearth/services/extensionhandler/ExtensionHandler.java +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/ExtensionHandler.java @@ -212,6 +212,27 @@ public class ExtensionHandler { protected void manipulatedPacket(HMessage hMessage) { onExtensionRespond(extension, hMessage); } + + @Override + protected void packetToStringRequest(HPacket packet) { + String s = ""; + String expression = "s"; + try { + s = packet.toString(); + if (packet.length() > 3000) { + expression = packet.toExpression(); + } + } + finally { + extension.packetToStringResponse(s, expression); + } + } + + @Override + protected void stringToPacketRequest(String string) { + HPacket packet = new HPacket(string); + extension.stringToPacketResponse(packet); + } }; extension.getExtensionObservable().addListener(listener); diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/ExtensionListener.java b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/ExtensionListener.java index f7120ca..080c7c9 100644 --- a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/ExtensionListener.java +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/ExtensionListener.java @@ -12,4 +12,7 @@ public abstract class ExtensionListener { protected void log(String text) {} protected void hasClosed() {} + protected void packetToStringRequest(HPacket packet) {} + protected void stringToPacketRequest(String string) {} + } diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/GEarthExtension.java b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/GEarthExtension.java index c38e428..61bfc32 100644 --- a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/GEarthExtension.java +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/GEarthExtension.java @@ -8,6 +8,8 @@ import gearth.services.extensionhandler.extensions.listeners.OmRemoveClickListen import gearth.services.extensionhandler.extensions.listeners.OnClickListener; import gearth.services.extensionhandler.extensions.listeners.OnDeleteListener; +import java.util.function.Consumer; + public abstract class GEarthExtension { @@ -40,6 +42,8 @@ public abstract class GEarthExtension { public abstract void connectionEnd(); public abstract void init(); public abstract void close(); + public abstract void packetToStringResponse(String string, String expression); + public abstract void stringToPacketResponse(HPacket packet); // --------------------------------------------------------------- @@ -78,6 +82,19 @@ public abstract class GEarthExtension { protected void hasClosed() { extensionObservable.fireEvent(ExtensionListener::hasClosed); } + + protected void packetToStringRequest(HPacket packet) { + int orgIndex = packet.getReadIndex(); + extensionObservable.fireEvent(listener -> { + packet.setReadIndex(6); + listener.packetToStringRequest(packet); + }); + packet.setReadIndex(orgIndex); + } + protected void stringToPacketRequest(String string) { + extensionObservable.fireEvent(l -> l.stringToPacketRequest(string)); + } + // -------------------------------------------------------------------- diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/NetworkExtension.java b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/NetworkExtension.java index f40b790..2539d1c 100644 --- a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/NetworkExtension.java +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/NetworkExtension.java @@ -88,6 +88,14 @@ public class NetworkExtension extends GEarthExtension { else if (message.headerId() == NetworkExtensionInfo.INCOMING_MESSAGES_IDS.EXTENSIONCONSOLELOG) { log(message.readString()); } + else if (message.headerId() == NetworkExtensionInfo.INCOMING_MESSAGES_IDS.PACKETTOSTRING_REQUEST) { + HPacket p = new HPacket(new byte[0]); + p.constructFromString(message.readLongString()); + packetToStringRequest(p); + } + else if (message.headerId() == NetworkExtensionInfo.INCOMING_MESSAGES_IDS.STRINGTOPACKET_REQUEST) { + stringToPacketRequest(message.readLongString()); + } } @@ -211,4 +219,19 @@ public class NetworkExtension extends GEarthExtension { connection.close(); } catch (IOException ignored) { } } + + @Override + public void packetToStringResponse(String string, String expression) { + HPacket packet = new HPacket(NetworkExtensionInfo.OUTGOING_MESSAGES_IDS.PACKETTOSTRING_RESPONSE); + packet.appendLongString(string); + packet.appendLongString(expression); + sendMessage(packet); + } + + @Override + public void stringToPacketResponse(HPacket packetFromString) { + HPacket packet = new HPacket(NetworkExtensionInfo.OUTGOING_MESSAGES_IDS.STRINGTOPACKET_RESPONSE); + packet.appendLongString(packet.stringify()); + sendMessage(packet); + } } \ No newline at end of file diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/NetworkExtensionInfo.java b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/NetworkExtensionInfo.java index 17d9f2e..9ea10e1 100644 --- a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/NetworkExtensionInfo.java +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/network/NetworkExtensionInfo.java @@ -87,6 +87,9 @@ public class NetworkExtensionInfo { public static final int CONNECTIONSTART = 5; public static final int CONNECTIONEND = 6; public static final int INIT = 7; + + public static final int PACKETTOSTRING_RESPONSE = 20; + public static final int STRINGTOPACKET_RESPONSE = 21; } @@ -95,6 +98,10 @@ public class NetworkExtensionInfo { public static final int MANIPULATEDPACKET = 2; public static final int REQUESTFLAGS = 3; public static final int SENDMESSAGE = 4; + + public static final int PACKETTOSTRING_REQUEST = 20; + public static final int STRINGTOPACKET_REQUEST = 21; + public static final int EXTENSIONCONSOLELOG = 98; } diff --git a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/simple/ExampleExtension.java b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/simple/ExampleExtension.java index 98752f2..e04b043 100644 --- a/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/simple/ExampleExtension.java +++ b/G-Earth/src/main/java/gearth/services/extensionhandler/extensions/implementations/simple/ExampleExtension.java @@ -1,6 +1,7 @@ package gearth.services.extensionhandler.extensions.implementations.simple; import gearth.protocol.HMessage; +import gearth.protocol.HPacket; import gearth.services.extensionhandler.extensions.GEarthExtension; public class ExampleExtension extends GEarthExtension { @@ -105,4 +106,18 @@ public class ExampleExtension extends GEarthExtension { // finish up and call "hasClosed()" hasClosed(); } + + + + + // ignore these + @Override + public void packetToStringResponse(String string, String expression) { + + } + + @Override + public void stringToPacketResponse(HPacket packet) { + + } }