Stop using a temp file and feed the data directly to the stdin of the kbinxml call, removes race conditions for multiple simultaneous requests and speeds things up a bit

This commit is contained in:
skogaby 2019-01-09 23:42:17 -06:00
parent 927129cfd5
commit acd9b752fb
10 changed files with 75 additions and 67 deletions

View File

@ -41,15 +41,13 @@ public class EventLogRequestHandler extends BaseRequestHandler {
* @return A response object for Spark
*/
private Object handleWriteRequest(final Request request, final Response response) {
LOG.debug("Handling the eventlog.write request");
// TODO: actually store the events coming in from the client
KXmlBuilder respBuilder = KXmlBuilder.create("response")
.e("eventlog")
.writeS64("gamesession", 0).up()
.writeS32("logsendflg", 0).up()
.writeS32("logerrlevel", 0).up()
.writeS32("evtidnosendflg", 0);
.s64("gamesession", 0).up()
.s32("logsendflg", 0).up()
.s32("logerrlevel", 0).up()
.s32("evtidnosendflg", 0);
return this.sendResponse(request, response, respBuilder);
}

View File

@ -41,42 +41,40 @@ public class FacilityRequestHandler extends BaseRequestHandler {
* @return A response object for Spark
*/
private Object handleGetRequest(final Request request, final Response response) {
LOG.debug("Handling the facility.get request");
// TODO: Remove all the hardcoded stuff
KXmlBuilder respBuilder = KXmlBuilder.create("response")
.e("facility")
.e("location")
.writeStr("id", "US-01").up()
.writeStr("country", "US").up()
.writeStr("region", "TX").up()
.writeStr("name", "BUTTERFLY").up()
.writeU8("type", 0).up().up()
.str("id", "US-01").up()
.str("country", "US").up()
.str("region", "TX").up()
.str("name", "BUTTERFLY").up()
.u8("type", 0).up().up()
.e("line")
.writeStr("id", "3").up()
.writeU8("class", 8).up()
.writeU8("upclass", 8).up()
.writeU16("rtt", 40).up().up()
.str("id", "3").up()
.u8("class", 8).up()
.u8("upclass", 8).up()
.u16("rtt", 40).up().up()
.e("public")
.writeU8("flag", 1).up()
.writeStr("name", "BUTTERFLY").up()
.writeStr("lattitude", "0").up()
.writeStr("longitude", "0").up().up()
.u8("flag", 1).up()
.str("name", "BUTTERFLY").up()
.str("lattitude", "0").up()
.str("longitude", "0").up().up()
.e("share")
.e("eacoin")
.writeS32("notchamount", 0).up()
.writeS32("notchcount", 0).up()
.writeS32("supplylimit", 1000000).up().up()
.s32("notchamount", 0).up()
.s32("notchcount", 0).up()
.s32("supplylimit", 1000000).up().up()
.e("url")
.writeStr("eapass", "http://eagate.573.jp/").up()
.writeStr("arcadefan", "http://eagate.573.jp/").up()
.writeStr("konaminetdx", "http://eagate.573.jp/").up()
.writeStr("konamiid", "http://eagate.573.jp/").up()
.writeStr("eagate", "http://eagate.573.jp/").up().up().up()
.str("eapass", "http://eagate.573.jp/").up()
.str("arcadefan", "http://eagate.573.jp/").up()
.str("konaminetdx", "http://eagate.573.jp/").up()
.str("konamiid", "http://eagate.573.jp/").up()
.str("eagate", "http://eagate.573.jp/").up().up().up()
.e("portfw")
.writeIp("globalip", "1.0.0.127").up()
.writeU16("globalport", 8888).up()
.writeU16("privateport", 8888);
.ip("globalip", "1.0.0.127").up()
.u16("globalport", 8888).up()
.u16("privateport", 8888);
return this.sendResponse(request, response, respBuilder);
}

View File

@ -41,8 +41,6 @@ public class MessageRequestHandler extends BaseRequestHandler {
* @return A response object for Spark
*/
private Object handleGetRequest(final Request request, final Response response) {
LOG.debug("Handling the message.get request");
// TODO: Remove all the hardcoded stuff and actually do something with the input
KXmlBuilder respBuilder = KXmlBuilder.create("response")
.e("message").a("expire", "1800").a("status", "0")

View File

@ -41,8 +41,6 @@ public class PackageRequestHandler extends BaseRequestHandler {
* @return A response object for Spark
*/
private Object handleListRequest(final Request request, final Response response) {
LOG.debug("Handling the package.list request");
KXmlBuilder respBuilder = KXmlBuilder.create("response")
.e("package");

View File

@ -41,8 +41,6 @@ public class PcbEventRequestHandler extends BaseRequestHandler {
* @return A response object for Spark
*/
private Object handlePutRequest(final Request request, final Response response) {
LOG.debug("Handling the pcbevent.put request");
// TODO: Remove all the hardcoded stuff and actually do something with the input
KXmlBuilder respBuilder = KXmlBuilder.create("response")
.e("pcbevent");

View File

@ -41,8 +41,6 @@ public class PcbTrackerRequestHandler extends BaseRequestHandler {
* @return A response object for Spark
*/
private Object handleAliveRequest(final Request request, final Response response) {
LOG.debug("Handling the pcbtracker.alive request");
// TODO: Remove all the hardcoded stuff and actually do something with the input
KXmlBuilder respBuilder = KXmlBuilder.create("response")
.e("pcbtracker").a("ecenable", "1").a("eclimit", "0").a("expire", "0").a("limit", "0").a("status", "0");

View File

@ -77,8 +77,6 @@ public class ServicesRequestHandler extends BaseRequestHandler {
* @return A response object for Spark
*/
private Object handleGetRequest(final Request request, final Response response) {
LOG.debug("Handling the services.get request");
// TODO: Remove all the hardcoded stuff
KXmlBuilder respBuilder = KXmlBuilder.create("response")
.e("services").a("expire", "600").a("method", "get").a("mode", "operation").a("status", "0");

View File

@ -41,12 +41,10 @@ public class TaxRequestHandler extends BaseRequestHandler {
* @return A response object for Spark
*/
private Object handleGetPhaseRequest(final Request request, final Response response) {
LOG.debug("Handling the tax.get_phase request");
// TODO: remove the hardcoded value, actually store phase per PCBID
KXmlBuilder respBuilder = KXmlBuilder.create("response")
.e("tax")
.writeS32("phase", 0);
.s32("phase", 0);
return this.sendResponse(request, response, respBuilder);
}

View File

@ -2,9 +2,9 @@ package com.buttongames.butterfly.xml;
import com.google.common.io.ByteStreams;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
/**
@ -31,16 +31,21 @@ public class BinaryXmlUtils {
*/
public static byte[] binaryToXml(final byte[] input) {
try {
final String tmpPath = System.getProperty("user.home") + "\\tmpkbin";
final DataOutputStream dos = new DataOutputStream(new FileOutputStream(tmpPath));
dos.write(input, 0, input.length);
dos.flush();
dos.close();
final ProcessBuilder builder = new ProcessBuilder("python",
"-c",
"import sys; " +
"from kbinxml import KBinXML; " +
"the_bytes = sys.stdin.buffer.read(); "+
"print(KBinXML(the_bytes).to_text())");
final Process process = builder.start();
final OutputStream stdin = process.getOutputStream();
final InputStream stdout = process.getInputStream();
// shell out to mon's implementation for now
final Process child = Runtime.getRuntime().exec("kbinxml " + tmpPath);
final byte[] output = ByteStreams.toByteArray(child.getInputStream());
return output;
stdin.write(input, 0, input.length);
stdin.flush();
stdin.close();
return ByteStreams.toByteArray(stdout);
} catch (Exception e) {
e.printStackTrace();
return null;
@ -54,7 +59,26 @@ public class BinaryXmlUtils {
* @throws IOException
*/
public static byte[] xmlToBinary(final byte[] input) {
return binaryToXml(input);
try {
final ProcessBuilder builder = new ProcessBuilder("python",
"-c",
"import sys; " +
"from kbinxml import KBinXML; " +
"the_bytes = sys.stdin.buffer.read(); " +
"sys.stdout.buffer.write(KBinXML(the_bytes).to_binary())");
final Process process = builder.start();
final OutputStream stdin = process.getOutputStream();
final InputStream stdout = process.getInputStream();
stdin.write(input, 0, input.length);
stdin.flush();
stdin.close();
return ByteStreams.toByteArray(stdout);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**

View File

@ -708,43 +708,43 @@ public final class KXmlBuilder extends BaseXMLBuilder {
}
}
public KXmlBuilder writeStr(final String name, final String value) {
public KXmlBuilder str(final String name, final String value) {
return e(name).a("__type", "str").t(value);
}
public KXmlBuilder writeU8(final String name, final int value) {
public KXmlBuilder u8(final String name, final int value) {
return e(name).a("__type", "u8").t(String.valueOf(value));
}
public KXmlBuilder writeU16(final String name, final int value) {
public KXmlBuilder u16(final String name, final int value) {
return e(name).a("__type", "u16").t(String.valueOf(value));
}
public KXmlBuilder writeU32(final String name, final long value) {
public KXmlBuilder u32(final String name, final long value) {
return e(name).a("__type", "u32").t(String.valueOf(value));
}
public KXmlBuilder writeU64(final String name, final long value) {
public KXmlBuilder u64(final String name, final long value) {
return e(name).a("__type", "u64").t(String.valueOf(value));
}
public KXmlBuilder writeS8(final String name, final int value) {
public KXmlBuilder s8(final String name, final int value) {
return e(name).a("__type", "s8").t(String.valueOf(value));
}
public KXmlBuilder writeS16(final String name, final int value) {
public KXmlBuilder s16(final String name, final int value) {
return e(name).a("__type", "s16").t(String.valueOf(value));
}
public KXmlBuilder writeS32(final String name, final int value) {
public KXmlBuilder s32(final String name, final int value) {
return e(name).a("__type", "s32").t(String.valueOf(value));
}
public KXmlBuilder writeS64(final String name, final long value) {
public KXmlBuilder s64(final String name, final long value) {
return e(name).a("__type", "s64").t(String.valueOf(value));
}
public KXmlBuilder writeIp(final String name, final String value) {
public KXmlBuilder ip(final String name, final String value) {
return e(name).a("__type", "ip4").t(value);
}
}