Expose packet<->string functionality to (network) extension interface

preparation for python extensions
This commit is contained in:
sirjonasxx 2020-10-03 21:56:26 +02:00
parent 4b54af927d
commit 1d1831cc94
6 changed files with 86 additions and 0 deletions

View File

@ -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);

View File

@ -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) {}
}

View File

@ -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));
}
// --------------------------------------------------------------------

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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) {
}
}