From bb6c8161192ad218823b529e5db391ab33189f70 Mon Sep 17 00:00:00 2001 From: CaiHQ Date: Tue, 2 Nov 2021 12:58:35 +0800 Subject: [PATCH] support debug run --- .gitignore | 6 +- build.gradle | 2 +- debugconf.json.template | 6 ++ .../org/bdware/sc/debugger/DebugMain.java | 93 +++++++++++++++++++ .../java/org/bdware/sc/ContractProcess.java | 9 +- .../org/bdware/sc/compiler/YJSCompiler.java | 9 +- 6 files changed, 118 insertions(+), 7 deletions(-) create mode 100644 debugconf.json.template create mode 100644 src/main/debugger/org/bdware/sc/debugger/DebugMain.java diff --git a/.gitignore b/.gitignore index 02f3d23..f8982e8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ /recoverTestFiles/ -/build/ \ No newline at end of file +/build/ +debugconf.json +/ContractDB/ +/defaultLog/ +/log/ \ No newline at end of file diff --git a/build.gradle b/build.gradle index 190d9fb..82f22b2 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ mainClassName = 'org.bdware.sc.ContractProcess' sourceSets { main { java { - srcDirs 'src/main/java', 'src/main/analysis', 'src/main/data-mask' + srcDirs 'src/main/java', 'src/main/debugger', 'src/main/data-mask' } resources { srcDir 'src/main/resources' diff --git a/debugconf.json.template b/debugconf.json.template new file mode 100644 index 0000000..b2a0c04 --- /dev/null +++ b/debugconf.json.template @@ -0,0 +1,6 @@ +{ + "agentHttpAddr": "127.0.0.1:18000", + "script": "/Users/huaqiancai/BDWare/datanet/datanet-gateway-bundle/datanet-gateway-backend/build/gateway.ypk", + "pubKey": "04d1924329f72ced148f6f333fb985ccbaa31b1e3aacf10be5f43d4a4ff5ad88899a005e79e37fc06993e1d66ada8cf8b711cb36f59538bb7d3e39e70fa9360ddd", + "privKey": "589d94ee5688358a1c5c18430dd9c75097ddddebf769f139da36a807911d20f8" +} \ No newline at end of file diff --git a/src/main/debugger/org/bdware/sc/debugger/DebugMain.java b/src/main/debugger/org/bdware/sc/debugger/DebugMain.java new file mode 100644 index 0000000..bc0e999 --- /dev/null +++ b/src/main/debugger/org/bdware/sc/debugger/DebugMain.java @@ -0,0 +1,93 @@ +package org.bdware.sc.debugger; + +import com.google.gson.JsonObject; +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; +import org.bdware.sc.ContractProcess; +import org.bdware.sc.YJSPacker; +import org.bdware.sc.bean.Contract; +import org.bdware.sc.bean.ContractExecType; +import org.bdware.sc.conn.ResultCallback; +import org.bdware.sc.get.GetMessage; +import org.bdware.sc.http.HttpUtil; +import org.bdware.sc.util.FileUtil; +import org.bdware.sc.util.JsonUtil; + +import java.io.File; +import java.util.Map; + +public class DebugMain { + static class DebugConfig { + String script; + String agentHttpAddr; + String pubKey; + String privKey; + //AutoAppend + + int port; + String cmi; + String dbPath; + int cPort; + Contract contract; + } + + static Logger LOGGER = LogManager.getLogger(DebugMain.class); + + public static void main(String[] args) { + String content = FileUtil.getFileContent("./debugconf.json"); + DebugConfig config = JsonUtil.fromJson(content, DebugConfig.class); + inject(config); + ContractProcess.main(new String[]{"-port=" + config.port, "-cmi=" + config.cmi, "-disablePID"}); + ResultCallback printCallback + = new ResultCallback() { + @Override + public void onResult(String str) { + LOGGER.info(str); + } + }; + ContractProcess.instance.handler.setDBInfo(wrap("", config.dbPath), printCallback); + ContractProcess.instance.handler.registerMangerPort(wrap("", Integer.valueOf(config.cPort)), printCallback); + ContractProcess.instance.handler.setContractBundle(wrap("", config.contract), printCallback); + String urlFormat = ("http://%s/SCIDE/SCManager?action=reconnectAll&owner=%s"); + + String url = String.format(urlFormat, config.agentHttpAddr, config.pubKey); + Map resp = HttpUtil.httpGet(url); + String data = (String)resp.get("response"); + LOGGER.info(JsonUtil.toPrettyJson(JsonUtil.parseString(data))); + LOGGER.info("start done!"); + } + + private static void inject(DebugConfig config) { + String urlFormat = ("http://%s/SCIDE/SCManager?action=%s&arg=%s"); + String url = String.format(urlFormat, config.agentHttpAddr, "getAgentConfig", ""); + Map resp = HttpUtil.httpGet(url); + String data = (String + ) resp.get("response"); + JsonObject jsonObject = JsonUtil.parseString(data); + config.cmi = jsonObject.get("cmi").getAsString(); + config.dbPath = jsonObject.get("dbPath").getAsString(); + config.cPort = jsonObject.get("cPort").getAsInt(); + config.port = jsonObject.get("port").getAsInt(); + JsonObject ownerAndScript = new JsonObject(); + String arg = "abc&owner=" + config.pubKey + "&script=" + config.script; + url = String.format(urlFormat, config.agentHttpAddr, "allocateKeyPair", arg); + resp = HttpUtil.httpGet(url); + LOGGER.info(url); + String contractStr = (String) resp.get("response"); + LOGGER.info("[ContratStr] " + contractStr); + Contract contract = JsonUtil.fromJson(contractStr, Contract.class); + config.contract = contract; + contract.setType(ContractExecType.Sole); + } + + private static GetMessage wrap(String operation, Object arg) { + return wrap(operation, JsonUtil.toJson(arg)); + } + + private static GetMessage wrap(String operation, String arg) { + GetMessage msg = new GetMessage(); + msg.method = operation; + msg.arg = arg; + return msg; + } +} diff --git a/src/main/java/org/bdware/sc/ContractProcess.java b/src/main/java/org/bdware/sc/ContractProcess.java index 0be6ca3..e3cf07a 100644 --- a/src/main/java/org/bdware/sc/ContractProcess.java +++ b/src/main/java/org/bdware/sc/ContractProcess.java @@ -47,6 +47,7 @@ import javax.script.ScriptContext; import javax.script.ScriptEngine; import javax.script.ScriptException; import java.io.*; +import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.util.*; import java.util.zip.ZipFile; @@ -60,6 +61,7 @@ public class ContractProcess { public final String cmi; private final Set cachedRequests = new HashSet<>(); + public final ContractHandler handler; public ServiceServer server; public DesktopEngine engine; String dbPath; @@ -81,7 +83,7 @@ public class ContractProcess { private String pid; public ContractProcess(int port, String cmi) { - ContractHandler handler = new ContractHandler(this); + handler = new ContractHandler(this); this.server = new ServiceServer(handler, port); this.cmi = cmi; } @@ -89,6 +91,7 @@ public class ContractProcess { public static void main(String[] args) { int port = 1616; String cmi = ""; + InputStream pidInput = System.in; for (String arg : args) { if (arg.startsWith("-port")) { String portStr = arg.substring(6); @@ -99,9 +102,11 @@ public class ContractProcess { cmi = arg.substring(5); } else if (arg.startsWith("-debug")) { Configurator.setRootLevel(Level.DEBUG); + } else if (arg.startsWith("-disablePID")) { + pidInput = new ByteArrayInputStream("CP PID:-1".getBytes(StandardCharsets.UTF_8)); } } - Scanner sc = new Scanner(System.in); + Scanner sc = new Scanner(pidInput); for (String str; sc.hasNextLine(); ) { str = sc.nextLine(); System.out.println("[CP From STDIN] " + str); diff --git a/src/main/java/org/bdware/sc/compiler/YJSCompiler.java b/src/main/java/org/bdware/sc/compiler/YJSCompiler.java index 0f96338..2c5dd13 100644 --- a/src/main/java/org/bdware/sc/compiler/YJSCompiler.java +++ b/src/main/java/org/bdware/sc/compiler/YJSCompiler.java @@ -67,7 +67,7 @@ public class YJSCompiler { final ErrorManager errors = new ErrorManager(werr); // Set up options. final Options options = new Options("nashorn", werr); - options.process(new String[] {}); + options.process(new String[]{}); // detect scripting mode by any source's first character being '#' options.set("persistent.code.cache", true); options.set("print.code", "true"); @@ -105,7 +105,10 @@ public class YJSCompiler { ContractZipBundle czb = new ContractZipBundle(); ZipEntry manifest = zf.getEntry("/manifest.json"); if (null == manifest) { - throw new IllegalStateException("manifest.json is not exists!"); + manifest = zf.getEntry("manifest.json"); + if (null == manifest) { + throw new IllegalStateException("manifest.json is not exists!"); + } } InputStream manifestInput = zf.getInputStream(manifest); // Gson gson = new GsonBuilder().registerTypeAdapter(Contract.Type.class, @@ -213,7 +216,7 @@ public class YJSCompiler { Class clz = Class.forName(clzName); return (AnnotationProcessor) clz.getConstructor().newInstance(); } catch (Exception e) { - // e.printStackTrace(); + // e.printStackTrace(); } return null; }