diff --git a/build.gradle b/build.gradle index 40daaf7..a72d0dc 100644 --- a/build.gradle +++ b/build.gradle @@ -5,10 +5,11 @@ plugins { id 'signing' } - +group = "org.bdware.sc" +version = "1.5.7" repositories { - mavenLocal() mavenCentral() + mavenLocal() } sourceSets { main { @@ -57,7 +58,7 @@ jar { } from { // uncomment this when publish! - // configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } + configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } manifest { attributes 'Manifest-Version': project.version diff --git a/lib/bdledger-java-sdk-dev-201104.2ce17584.jar b/lib/bdledger-java-sdk-dev-201104.2ce17584.jar new file mode 100644 index 0000000..f0a4ba0 Binary files /dev/null and b/lib/bdledger-java-sdk-dev-201104.2ce17584.jar differ diff --git a/src/main/debugger/org/bdware/sc/debugger/DebugMain.java b/src/main/debugger/org/bdware/sc/debugger/DebugMain.java index c7792f3..934aa88 100644 --- a/src/main/debugger/org/bdware/sc/debugger/DebugMain.java +++ b/src/main/debugger/org/bdware/sc/debugger/DebugMain.java @@ -16,27 +16,46 @@ import org.bdware.sc.util.JsonUtil; import org.zz.gmhelper.SM2KeyPair; import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; public class DebugMain { static Logger LOGGER = LogManager.getLogger(DebugMain.class); - public static void main(String[] args) { - String content = FileUtil.getFileContent("./debugconf.json"); + public static void runWithConf(String configPath) { + String content = FileUtil.getFileContent(configPath); DebugConfig config = JsonUtil.fromJson(content, DebugConfig.class); inject(config); String keyPairStr = "{\"publicKey\":\"%s\",\"privateKey\":\"%s\"}"; - SM2KeyPair pair = SM2KeyPair.fromJson(String.format(keyPairStr, config.pubKey, config.privKey)); + SM2KeyPair pair = SM2KeyPair.fromJson(String.format(keyPairStr, config.publicKey, config.privateKey)); String uriFormat = "ws://%s/SCIDE/SCExecutor"; - if (config.killContract != null && config.killContract.length() > 0) { - SmartContractClient client = new SmartContractClient(String.format(uriFormat, config.agentHttpAddr), pair); + if (config.killBeforeStart != null && config.killBeforeStart.length() > 0) { + AtomicInteger counter = new AtomicInteger(0); + + SmartContractClient client = new SmartContractClient(String.format(uriFormat, config.agentAddress), pair) { + public void onLogin(JsonObject obj) { + counter.incrementAndGet(); + } + + public void onKillContractProcess(JsonObject obj) { + counter.incrementAndGet(); + } + + }; client.waitForConnect(); client.login(); try { - Thread.sleep(2000); + for (; counter.get() == 0; ) + Thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } + client.sendMsg("{\"action\":\"killContractProcess\",\"name\":\"" + config.killBeforeStart + "\"}"); + try { + for (; counter.get() == 1; ) + Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } - client.sendMsg("{\"action\":\"killContractProcess\",\"contractName\":\"" + config.killContract + "\"}"); } config.contract.setCreateParam(config.createParam); ContractProcess.main(new String[]{"-port=" + config.port, "-cmi=" + config.cmi, "-disablePID"}); @@ -51,10 +70,13 @@ public class DebugMain { 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=reconnectPort&owner=%s&port=%d"); - String url = String.format(urlFormat, config.agentHttpAddr, - config.pubKey, ContractProcess.instance.server.mainPort.get()); + String urlFormat = "http://%s/SCIDE/SCManager?action=reconnectPort&owner=%s&port=%d%s"; + String cpHost = ""; + if (config.cpHost != null && config.cpHost.length() > 0) + cpHost = "&host=" + config.cpHost; + String url = String.format(urlFormat, config.agentAddress, + config.publicKey, ContractProcess.instance.server.mainPort.get(), cpHost); Map resp = HttpUtil.httpGet(url); String data = (String) resp.get("response"); @@ -62,9 +84,13 @@ public class DebugMain { LOGGER.info("start done!"); } + public static void main(String[] args) { + runWithConf("./debugconf.json"); + } + private static void inject(DebugConfig config) { String urlFormat = ("http://%s/SCIDE/SCManager?action=%s&arg=%s"); - String url = String.format(urlFormat, config.agentHttpAddr, "getAgentConfig", ""); + String url = String.format(urlFormat, config.agentAddress, "getAgentConfig", ""); Map resp = HttpUtil.httpGet(url); String data = (String ) resp.get("response"); @@ -74,8 +100,8 @@ public class DebugMain { 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); + String arg = "abc&owner=" + config.publicKey + "&script=" + config.ypkPath; + url = String.format(urlFormat, config.agentAddress, "allocateKeyPair", arg); resp = HttpUtil.httpGet(url); LOGGER.info(url); String contractStr = (String) resp.get("response"); @@ -97,12 +123,13 @@ public class DebugMain { } static class DebugConfig { - String killContract; + String agentAddress; public JsonElement createParam; - String script; - String agentHttpAddr; - String pubKey; - String privKey; + String publicKey; + String privateKey; + String killBeforeStart; + String ypkPath; + String cpHost; //AutoAppend int port; String cmi; diff --git a/src/main/java/org/bdware/sc/boundry/utils/IRPUtil.java b/src/main/java/org/bdware/sc/boundry/utils/IRPUtil.java new file mode 100644 index 0000000..d7230ba --- /dev/null +++ b/src/main/java/org/bdware/sc/boundry/utils/IRPUtil.java @@ -0,0 +1,94 @@ +package org.bdware.sc.boundry.utils; + +import com.google.api.client.json.Json; +import com.google.gson.JsonObject; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.bdware.doip.audit.EndpointConfig; +import org.bdware.doip.audit.client.AuditIrpClient; +import org.bdware.doip.audit.config.TempConfigStorage; +import org.bdware.doip.codec.digitalObject.DigitalObject; +import org.bdware.doip.codec.digitalObject.Element; +import org.bdware.doip.codec.doipMessage.DoipMessage; +import org.bdware.doip.codec.doipMessage.DoipMessageFactory; +import org.bdware.doip.codec.operations.BasicOperations; +import org.bdware.doip.endpoint.client.ClientConfig; +import org.bdware.doip.endpoint.client.DoipClientImpl; +import org.bdware.doip.endpoint.client.DoipMessageCallback; +import org.bdware.irp.exception.IrpClientException; +import org.bdware.irp.stateinfo.StateInfoBase; +import org.bdware.sc.compiler.PermissionStub; +import org.bdware.sc.engine.JSONTool; +import org.bdware.sc.node.Permission; +import org.bdware.sc.util.JsonUtil; +import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + +@PermissionStub(permission = Permission.IRP) +public class IRPUtil { + private static final Logger LOGGER = LogManager.getLogger(IRPUtil.class); + private final AuditIrpClient auditIrpClient; + + private IRPUtil(String config) { + TempConfigStorage configStorage = new TempConfigStorage(config); + JsonObject jo = configStorage.load(); + EndpointConfig endpointConfig = configStorage.loadAsEndpointConfig(); + + if (jo.has("clientDoId")) { + this.auditIrpClient = new AuditIrpClient(jo.get("clientDoId").getAsString(), endpointConfig); + } else { + this.auditIrpClient = new AuditIrpClient(endpointConfig); + } + } + + public static IRPUtil createClient(String jsonObject) { + return new IRPUtil(jsonObject); + } + + public String test(String doi) { + return "create DOClient And hello " + doi + " World"; + } + + public Object resolve(String doId) { + try { + StateInfoBase result = auditIrpClient.resolve(doId); + JsonObject r = new JsonObject(); + r.addProperty("code", 0); + r.add("handleValues", result.handleValues); + return JSONTool.convertJsonElementToMirror(result.handleValues); + } catch (Exception e) { + JsonObject r = new JsonObject(); + r.addProperty("code", 1); + r.addProperty("msg", e.getMessage()); + return JSONTool.convertJsonElementToMirror(r); + } + } + + public Object register(ScriptObjectMirror mirror) { + JsonObject jo = JSONTool.convertMirrorToJson(mirror).getAsJsonObject(); + StateInfoBase base = new StateInfoBase(); + JsonObject result = new JsonObject(); + if (jo.has("identifier")) + base.identifier = jo.get("identifier").getAsString(); + base.handleValues = jo.get("handleValues").getAsJsonObject(); + try { + String val = auditIrpClient.register(base); + result.addProperty("code", 0); + result.addProperty("doId", val); + if (val == null || val.length() == 0) { + result.addProperty("code", 1); + result.addProperty("msg", auditIrpClient.getLastResponse().getResponseMessage()); + } + return JSONTool.convertJsonElementToMirror(result); + } catch (Exception e) { + result.addProperty("code", 1); + result.addProperty("msg", e.getMessage()); + return JSONTool.convertJsonElementToMirror(result); + } + } +}