From a1b59cfbfdd771242fa0e8d899e822a7a92fd23f Mon Sep 17 00:00:00 2001 From: "haoeliu@foxmail.com" Date: Fri, 2 Dec 2022 16:39:11 +0800 Subject: [PATCH] finish the local version --- .../java/org/bdware/sc/ContractProcess.java | 10 +++- .../java/org/bdware/sc/compiler/ap/DOOP.java | 12 +++-- .../bdware/sc/engine/hook/DOOPHandler.java | 48 +++++++++++++---- .../bdware/sc/handler/DOOPRequestHandler.java | 17 ++++-- .../org/bdware/sc/server/DoipServerTest.java | 21 +++++--- .../java/org/bdware/doip/DoipClientTest.java | 52 +++++++++++++++++++ 6 files changed, 135 insertions(+), 25 deletions(-) create mode 100644 src/test/java/org/bdware/doip/DoipClientTest.java diff --git a/src/main/java/org/bdware/sc/ContractProcess.java b/src/main/java/org/bdware/sc/ContractProcess.java index c58cbbf..b0a8703 100644 --- a/src/main/java/org/bdware/sc/ContractProcess.java +++ b/src/main/java/org/bdware/sc/ContractProcess.java @@ -3,6 +3,7 @@ package org.bdware.sc; import com.google.gson.*; import com.google.gson.reflect.TypeToken; import com.google.gson.stream.MalformedJsonException; +import groovy.util.logging.Log; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.core.config.Configurator; import org.bdware.analysis.BasicBlock; @@ -518,6 +519,12 @@ public class ContractProcess { jo.add("loadContract", JsonUtil.parseObject(result)); jo.addProperty("status", result.status.merge(onCreate.status).toString()); LOGGER.debug("result: " + jo.toString()); + + if(cn.getYjsType() == YjsType.DoipModule) { + LOGGER.info("the doipServer has started"); + DoipServerTest.main(new String[]{"8080"}); + } + return jo.toString(); } else { contract.setScript(FileUtil.getFileContent(zipPath)); @@ -560,7 +567,7 @@ public class ContractProcess { } if (fun.isDoipOperation()) { - fun.appendBeforeInvokeHandler(new DOOPHandler()); + fun.appendBeforeInvokeHandler(DOOPHandler.createDOOPHandler()); } if (fun.isExport()) { @@ -654,6 +661,7 @@ public class ContractProcess { // doipModule的话,拉起DoipServer服务端口 if(cn.getYjsType() == YjsType.DoipModule) { + LOGGER.info("the doipServer has started"); DoipServerTest.main(new String[]{"8080"}); } diff --git a/src/main/java/org/bdware/sc/compiler/ap/DOOP.java b/src/main/java/org/bdware/sc/compiler/ap/DOOP.java index a24446d..d5e5037 100644 --- a/src/main/java/org/bdware/sc/compiler/ap/DOOP.java +++ b/src/main/java/org/bdware/sc/compiler/ap/DOOP.java @@ -3,6 +3,7 @@ package org.bdware.sc.compiler.ap; import org.bdware.doip.codec.operations.BasicOperations; import org.bdware.sc.bean.DoipOperationInfo; import org.bdware.sc.compiler.AnnotationProcessor; +import org.bdware.sc.engine.hook.DOOPHandler; import org.bdware.sc.handler.DOOPRequestHandler; import org.bdware.sc.node.AnnotationNode; import org.bdware.sc.node.ContractNode; @@ -20,14 +21,17 @@ public class DOOP extends AnnotationProcessor { functionNode.setIsExport(true); functionNode.setIsDoipOperation(true); functionNode.setDoipOperationInfo(DoipOperationInfo.create(anno, contractNode)); + // functionNode.setFunctionName(functionNode.getDoipOperationInfo().operationName); // 维护DOOPRequestHandler - if(DOOPRequestHandler.instance == null) { - new DOOPRequestHandler(); - } + DOOPRequestHandler.createHandler(); DOOPRequestHandler.instance.addDoipOperation(functionNode); + // 维护DOOPHandler + DOOPHandler.createDOOPHandler(); + DOOPHandler.instance.putFuncNameAndDoipOperationsMapping(functionNode); + // 维护ContractNode,functionName is useless, use BasicOperation to map the corresponding functionNode - contractNode.updateFunctionMap(functionNode.functionName, functionNode.getDoipOperationInfo().operationName); + // contractNode.updateFunctionMap(functionNode.functionName, functionNode.getDoipOperationInfo().operationName); } } diff --git a/src/main/java/org/bdware/sc/engine/hook/DOOPHandler.java b/src/main/java/org/bdware/sc/engine/hook/DOOPHandler.java index 3d0addb..b9fbb70 100644 --- a/src/main/java/org/bdware/sc/engine/hook/DOOPHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/DOOPHandler.java @@ -1,6 +1,7 @@ package org.bdware.sc.engine.hook; import com.google.gson.JsonElement; +import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.bdware.doip.codec.doipMessage.DoipMessage; import org.bdware.doip.codec.doipMessage.DoipMessageFactory; @@ -8,21 +9,40 @@ import org.bdware.doip.codec.doipMessage.DoipResponseCode; import org.bdware.doip.codec.operations.BasicOperations; import org.bdware.sc.JSEngine; import org.bdware.sc.bean.ContractRequest; +import org.bdware.sc.boundry.ScriptReturnException; import org.bdware.sc.entity.DoipMessagePacker; import org.bdware.sc.node.AnnotationHook; -import org.bdware.sc.node.AnnotationNode; import org.bdware.sc.node.ArgPacks; import org.bdware.sc.node.FunctionNode; import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; public class DOOPHandler implements AnnotationHook { + public static Map funcNameToDoipOperations; + public static DOOPHandler instance; public DOOPHandler() { - + funcNameToDoipOperations = new HashMap<>(); } - public static DOOPHandler fromAnnotationNode(FunctionNode funNode, AnnotationNode annoNode) { - return new DOOPHandler(); + public static DOOPHandler createDOOPHandler() { + if(instance == null) { + instance = new DOOPHandler(); + } + + return instance; + } + + public void putFuncNameAndDoipOperationsMapping(FunctionNode fn) { + String basicOperationsString = fn.getDoipOperationInfo().operationType; + BasicOperations operation = BasicOperations.Unknown; + for(BasicOperations basicOperation : BasicOperations.values()) { + if(basicOperation.toString().equals(basicOperationsString)) { + operation = basicOperation; + } + } + funcNameToDoipOperations.put(fn.getFunctionName(), operation); } @Override @@ -47,13 +67,20 @@ public class DOOPHandler implements AnnotationHook { } public static void validateHTTPRequestArgs(ContractRequest httpReq) { - JsonElement httpArgs = httpReq.getArg(); - BasicOperations curOp = BasicOperations.getDoOp(httpReq.getAction()); + JsonElement originArgs = httpReq.getArg(); + JsonElement httpArgs = JsonParser.parseString(originArgs.getAsString()); + BasicOperations curOp = funcNameToDoipOperations.get(httpReq.getAction()); // get args rules and validate http args JsonElement httpArgsRules = getRulesForBasicOperation(curOp); ArgSchemaVisitor visitor = new ArgSchemaVisitor(httpArgs); visitor.visit(httpArgsRules); + if (!visitor.getStatus()) { + JsonObject jo = new JsonObject(); + jo.addProperty("msg", visitor.getException()); + jo.addProperty("code", visitor.errorCode); + throw new ScriptReturnException(jo); + } } public static JsonElement getRulesForBasicOperation(BasicOperations basicOperation) { @@ -69,15 +96,16 @@ public class DOOPHandler implements AnnotationHook { } public static DoipMessage convertContractRequestToDoipMessage(ContractRequest httpReq) { - BasicOperations httpOperation = BasicOperations.getDoOp(httpReq.getAction()); + BasicOperations httpOperation = funcNameToDoipOperations.get(httpReq.getAction()); + JsonObject jsonParams = JsonParser.parseString(httpReq.getArg().getAsString()).getAsJsonObject(); DoipMessage doipMessage = new DoipMessageFactory.DoipMessageBuilder().createRequest("", httpReq.getAction()).create(); switch(httpOperation) { case Hello: - doipMessage = new DoipMessageFactory.DoipMessageBuilder().createRequest(httpReq.getArg().getAsJsonObject().get("doid").getAsString(), httpReq.getAction()).create(); + doipMessage = new DoipMessageFactory.DoipMessageBuilder().createRequest(jsonParams.get("doid").getAsString(), httpReq.getAction()).create(); break; case Retrieve: - DoipMessageFactory.DoipMessageBuilder msgBuilder = new DoipMessageFactory.DoipMessageBuilder().createRequest(httpReq.getArg().getAsJsonObject().get("doid").getAsString(), httpReq.getAction()); - msgBuilder = msgBuilder.addAttributes("element", httpReq.getArg().getAsJsonObject().get("element").getAsString()); + DoipMessageFactory.DoipMessageBuilder msgBuilder = new DoipMessageFactory.DoipMessageBuilder().createRequest(jsonParams.get("doid").getAsString(), httpReq.getAction()); + msgBuilder = msgBuilder.addAttributes("element", jsonParams.get("element").getAsString()); doipMessage = msgBuilder.create(); break; } diff --git a/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java b/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java index 72f0f97..bb00c37 100644 --- a/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java +++ b/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java @@ -22,16 +22,21 @@ import java.util.Map; public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler { public Map doipOperationsMap; static Logger logger = LogManager.getLogger(NettyServerHandler.class); - Gson gson; + static Gson gson; public static DOOPRequestHandler instance; public DOOPRequestHandler() { + doipOperationsMap = new HashMap<>(); + gson = new Gson(); + } + + public static DOOPRequestHandler createHandler() { if(instance == null) { instance = new DOOPRequestHandler(); - instance.doipOperationsMap = new HashMap<>(); - gson = new Gson(); } + + return instance; } public void addDoipOperation(FunctionNode function) { @@ -130,7 +135,11 @@ public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler public ContractRequest constructContractRequest(FunctionNode fn, DoipMessage request) { ContractRequest cr = new ContractRequest(); cr.setContractID(""); - cr.setRequester(request.credential.getSigner()); + if(request.credential == null) { + cr.setRequester(null); + } else { + cr.setRequester(request.credential.getSigner()); + } cr.setAction(fn.functionName); return cr; } diff --git a/src/main/java/org/bdware/sc/server/DoipServerTest.java b/src/main/java/org/bdware/sc/server/DoipServerTest.java index ff56911..b14ebbc 100644 --- a/src/main/java/org/bdware/sc/server/DoipServerTest.java +++ b/src/main/java/org/bdware/sc/server/DoipServerTest.java @@ -15,11 +15,20 @@ public class DoipServerTest { static Logger LOGGER = LogManager.getLogger(DoipServerTest.class); public static void main(String[] arg) throws InterruptedException { - int port = 21042; - if (arg != null && arg.length > 0) { - port = Integer.parseInt(arg[0]); - } - run(port); + final int port = (arg.length == 0 ? 21042 : Integer.parseInt(arg[0])); + + Thread doipServerThread = new Thread(){ + @Override + public void run() { + try { + DoipServerTest.run(port); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }; + + doipServerThread.start(); } public static void run(int port) throws InterruptedException { @@ -32,7 +41,7 @@ public class DoipServerTest { DoipServiceInfo info = new DoipServiceInfo("aibd.govdata.tj/do.3f9c41e6-9f8e-48a0-9220-53f438d40e43", "ownerDEF", "gateRepo", infos); DoipServerImpl server = new DoipServerImpl(info); final AtomicInteger count = new AtomicInteger(0); - DOOPRequestHandler handler = new DOOPRequestHandler(); + DOOPRequestHandler handler = DOOPRequestHandler.createHandler(); server.setRepositoryHandler(handler); server.start(); for (; ; ) { diff --git a/src/test/java/org/bdware/doip/DoipClientTest.java b/src/test/java/org/bdware/doip/DoipClientTest.java new file mode 100644 index 0000000..da9ae56 --- /dev/null +++ b/src/test/java/org/bdware/doip/DoipClientTest.java @@ -0,0 +1,52 @@ +package org.bdware.doip; + +import org.bdware.doip.codec.doipMessage.DoipMessage; +import org.bdware.doip.endpoint.client.ClientConfig; +import org.bdware.doip.endpoint.client.DoipClientImpl; +import org.bdware.doip.endpoint.client.DoipMessageCallback; +import org.junit.Test; + +import java.util.concurrent.atomic.AtomicInteger; + + +public class DoipClientTest { + long start = System.currentTimeMillis(); + final AtomicInteger total = new AtomicInteger(0); + final AtomicInteger correct = new AtomicInteger(0); + int totalCount = 10000; + + @Test + public void doipClientTest(){ + long start = System.currentTimeMillis(); + final AtomicInteger total = new AtomicInteger(0); + final AtomicInteger correct = new AtomicInteger(0); + int totalCount = 1; + for (int i = 0; i < totalCount; i++) { + final DoipClientImpl doipClient = new DoipClientImpl(); + doipClient.connect(ClientConfig.fromUrl("tcp://127.0.0.1:8080")); + doipClient.retrieve("aibd/do.e626924a-3b1c-492f-9a41-59179bfe0361", null, true, new DoipMessageCallback() { + @Override + public void onResult(DoipMessage msg) { + String str = new String(msg.body.encodedData); + System.out.println("Result is " + str); + //LOGGER.info("Retrieved:" + str + //+ " respCode:" + msg.header.parameters.response); + total.incrementAndGet(); + if (str.contains("aaa")) + correct.incrementAndGet(); + if (doipClient != null) doipClient.close(); + } + }); + } + int circle = 0; + for (; total.get() < totalCount; ) { + if (++circle % 100 == 0) { + try { + Thread.sleep(10); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + } +}