From ad411e5e6c731830c9678462d2ccaf5fb651fdec Mon Sep 17 00:00:00 2001 From: CaiHQ Date: Sat, 27 May 2023 13:16:22 +0800 Subject: [PATCH] update http server --- build.gradle | 2 +- .../sdk/org/bdware/client/ContractResult.java | 13 +++++++ .../bdware/client/SmartContractClient.java | 5 ++- .../client/SmartContractHttpClient.java | 31 +++++++++++++-- .../sdk/org/bdware/client/TimeoutUtil.java | 39 +++++++++++++++++++ 5 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 src/main/sdk/org/bdware/client/ContractResult.java create mode 100644 src/main/sdk/org/bdware/client/TimeoutUtil.java diff --git a/build.gradle b/build.gradle index 8b9eb99..eeab735 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { id 'signing' } group 'org.bdware.bdcontract' -version '1.0.1' +version '1.0.2' sourceCompatibility = 1.8 diff --git a/src/main/sdk/org/bdware/client/ContractResult.java b/src/main/sdk/org/bdware/client/ContractResult.java new file mode 100644 index 0000000..f15bc27 --- /dev/null +++ b/src/main/sdk/org/bdware/client/ContractResult.java @@ -0,0 +1,13 @@ +package org.bdware.client; + +import com.google.gson.JsonElement; + +public class ContractResult { + public Status status; + public JsonElement result; + + public enum Status { + Success, Exception, Error, Executing + } +} + diff --git a/src/main/sdk/org/bdware/client/SmartContractClient.java b/src/main/sdk/org/bdware/client/SmartContractClient.java index 5c898ae..c68a158 100644 --- a/src/main/sdk/org/bdware/client/SmartContractClient.java +++ b/src/main/sdk/org/bdware/client/SmartContractClient.java @@ -227,7 +227,10 @@ public class SmartContractClient extends SmartContractHandler { if (null == cr.getArg()) { req.put("arg", "undefined"); } else { - req.put("arg", cr.getArg().toString()); + if (cr.getArg().isJsonPrimitive()) + req.put("arg", cr.getArg().getAsString()); + else + req.put("arg", cr.getArg().toString()); } return req; } diff --git a/src/main/sdk/org/bdware/client/SmartContractHttpClient.java b/src/main/sdk/org/bdware/client/SmartContractHttpClient.java index f05b9a5..d641e64 100644 --- a/src/main/sdk/org/bdware/client/SmartContractHttpClient.java +++ b/src/main/sdk/org/bdware/client/SmartContractHttpClient.java @@ -1,5 +1,6 @@ package org.bdware.client; +import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.zz.gmhelper.SM2KeyPair; @@ -12,6 +13,7 @@ import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import java.util.Scanner; +import java.util.concurrent.Callable; public class SmartContractHttpClient { private final SM2KeyPair keyPair; @@ -85,21 +87,34 @@ public class SmartContractHttpClient { ret.put("responseCode", 505); ByteArrayOutputStream bo = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(bo)); - ret.put("response", bo.toString()); + ContractResult cr = new ContractResult(); + JsonObject jo = new JsonObject(); + jo.addProperty("msg", bo.toString()); + jo.addProperty("code", 1); + cr.result = jo; + cr.status = ContractResult.Status.Exception; + ret.put("response", new Gson().toJson(cr)); } return ret; } + public static Map httpGet(String str) { Map ret = new HashMap<>(); - try { URL url = new URL(str); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(40000); connection.setReadTimeout(40000); + Callable callable = new Callable() { + @Override + public InputStream call() throws Exception { + return connection.getInputStream(); + } + }; + InputStream input = TimeoutUtil.runWithTimeout(callable, 2000); + assert input != null; ret.put("responseCode", connection.getResponseCode()); - InputStream input = connection.getInputStream(); Scanner sc = new Scanner(input); StringBuilder sb = new StringBuilder(); while (sc.hasNextLine()) { @@ -111,11 +126,18 @@ public class SmartContractHttpClient { ret.put("responseCode", 505); ByteArrayOutputStream bo = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(bo)); - ret.put("response", bo.toString()); + ContractResult cr = new ContractResult(); + JsonObject jo = new JsonObject(); + jo.addProperty("msg", bo.toString()); + jo.addProperty("code", 1); + cr.result = jo; + cr.status = ContractResult.Status.Exception; + ret.put("response", new Gson().toJson(cr)); } return ret; } + public JsonObject executeContract(String id, String operation, String arg) { ContractRequest cr = new ContractRequest(); cr.setContractID(id); @@ -124,6 +146,7 @@ public class SmartContractHttpClient { cr.doSignature(keyPair); String url = getExecuteUrl(ip, port, cr); String str = (String) httpGet(url).get("response"); + return JsonParser.parseString(str).getAsJsonObject(); } diff --git a/src/main/sdk/org/bdware/client/TimeoutUtil.java b/src/main/sdk/org/bdware/client/TimeoutUtil.java new file mode 100644 index 0000000..f88e3e6 --- /dev/null +++ b/src/main/sdk/org/bdware/client/TimeoutUtil.java @@ -0,0 +1,39 @@ +package org.bdware.client; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +public class TimeoutUtil { + static ExecutorService executor = Executors.newCachedThreadPool(); + + public static Callable wrapFunction(Object callback, Object arg) { + Callable callable = new Callable() { + @Override + public Object call() throws Exception { + Class clz = Class.forName("org.bdware.sc.boundry.JavaScriptEntry"); + Class scriptFunction = Class.forName("wrp.jdk.nashorn.internal.runtime.ScriptFunction"); + Class sco = Class.forName("wrp.jdk.nashorn.api.scripting.ScriptObjectMirror"); + Field field = sco.getDeclaredField("sobj"); + field.setAccessible(true); + Method method = clz.getDeclaredMethod("executeFunction", scriptFunction, Object.class); + return method.invoke(null, field.get(callback), arg); + } + }; + return callable; + } + + public static T runWithTimeout(Callable callable, int timeout) { + T ret; + try { + ret = executor.submit(callable).get(timeout, TimeUnit.MILLISECONDS); + } catch (Exception e) { + e.printStackTrace(); + ret = null; + } + return ret; + } +}