diff --git a/src/main/java/org/bdware/sc/ContractProcess.java b/src/main/java/org/bdware/sc/ContractProcess.java index b0a8703..4aeb587 100644 --- a/src/main/java/org/bdware/sc/ContractProcess.java +++ b/src/main/java/org/bdware/sc/ContractProcess.java @@ -3,7 +3,6 @@ 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; 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 d5e5037..4f67f9c 100644 --- a/src/main/java/org/bdware/sc/compiler/ap/DOOP.java +++ b/src/main/java/org/bdware/sc/compiler/ap/DOOP.java @@ -1,6 +1,5 @@ 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; @@ -9,8 +8,6 @@ import org.bdware.sc.node.AnnotationNode; import org.bdware.sc.node.ContractNode; import org.bdware.sc.node.FunctionNode; -import java.util.List; - // DOOP is designed for DoipModule which contains specific functions for RepositoryHandler public class DOOP extends AnnotationProcessor { 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 b9fbb70..4064e95 100644 --- a/src/main/java/org/bdware/sc/engine/hook/DOOPHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/DOOPHandler.java @@ -18,6 +18,7 @@ import org.bdware.sc.node.FunctionNode; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; +import java.util.Set; public class DOOPHandler implements AnnotationHook { public static Map funcNameToDoipOperations; @@ -59,7 +60,7 @@ public class DOOPHandler implements AnnotationHook { // set doipMsgPackerArg struct's params doipMsgPackerArg.setSource("http"); - doipMsgPackerArg.rawDoipMsg = convertContractRequestToDoipMessage(httpReq); + doipMsgPackerArg.rawDoipMsg = httpInputConvertContractRequestToDoipMessage(httpReq); } argPacks.arg = doipMsgPackerArg; @@ -72,7 +73,7 @@ public class DOOPHandler implements AnnotationHook { BasicOperations curOp = funcNameToDoipOperations.get(httpReq.getAction()); // get args rules and validate http args - JsonElement httpArgsRules = getRulesForBasicOperation(curOp); + JsonElement httpArgsRules = getRulesForHTTPRequest(curOp); ArgSchemaVisitor visitor = new ArgSchemaVisitor(httpArgs); visitor.visit(httpArgsRules); if (!visitor.getStatus()) { @@ -83,37 +84,102 @@ public class DOOPHandler implements AnnotationHook { } } - public static JsonElement getRulesForBasicOperation(BasicOperations basicOperation) { - + public static JsonElement getRulesForHTTPRequest(BasicOperations basicOperation) { switch(basicOperation) { case Hello: - return JsonParser.parseString("{\"!doid\":\"string\"}"); + case Delete: + case ListOps: + return JsonParser.parseString("{\"!header\":{{\"!identifier\":\"string\"}}}"); case Retrieve: - return JsonParser.parseString("{\"!doid\":\"string\", \"!element\":\"boolean\"}"); + return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\", \"attributes\":{\"element\":\"string\", \"includeElementData\":\"boolean\"}}}"); + case Create: + return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\", \"attributes\":{\"element\":\"string\", \"includeElementData\":\"boolean\"}}, \"!body\":\"string\"}"); + case Update: + return JsonParser.parseString("{\"!header\":{{\"!identifier\":\"string\"}}, \"!body\":\"string\"}"); + case Search: + return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\", \"attributes\":{\"query\":\"string\", \"pageNum\":\"int\", \"pageSize\":\"int\", \"type\":\"string\"}}}"); + case Extension: + case Unknown: default: return null; } } - public static DoipMessage convertContractRequestToDoipMessage(ContractRequest httpReq) { + public static DoipMessage httpInputConvertContractRequestToDoipMessage(ContractRequest httpReq) { BasicOperations httpOperation = funcNameToDoipOperations.get(httpReq.getAction()); JsonObject jsonParams = JsonParser.parseString(httpReq.getArg().getAsString()).getAsJsonObject(); - DoipMessage doipMessage = new DoipMessageFactory.DoipMessageBuilder().createRequest("", httpReq.getAction()).create(); + // taking Extension into consideration + JsonObject header = jsonParams.get("header") != null ? jsonParams.get("header").getAsJsonObject() : null; + JsonObject body = jsonParams.get("body") != null ? jsonParams.get("body").getAsJsonObject() : null; + DoipMessage doipMessage = null; switch(httpOperation) { case Hello: - doipMessage = new DoipMessageFactory.DoipMessageBuilder().createRequest(jsonParams.get("doid").getAsString(), httpReq.getAction()).create(); + case Delete: + case ListOps: + doipMessage = new DoipMessageFactory.DoipMessageBuilder().createRequest(header.get("identifier").getAsString(), httpReq.getAction()).create(); break; case Retrieve: - DoipMessageFactory.DoipMessageBuilder msgBuilder = new DoipMessageFactory.DoipMessageBuilder().createRequest(jsonParams.get("doid").getAsString(), httpReq.getAction()); - msgBuilder = msgBuilder.addAttributes("element", jsonParams.get("element").getAsString()); - doipMessage = msgBuilder.create(); + doipMessage = new DoipMessageFactory.DoipMessageBuilder().createRequest(header.get("identifier").getAsString(), httpReq.getAction()).create(); + JsonElement element = header.get("element"); + JsonElement includeElementData = header.get("includeElementData"); + if(element != null) doipMessage.header.parameters.addAttribute("element", element.getAsString()); + if(includeElementData != null) doipMessage.header.parameters.addAttribute("includeElementData", includeElementData.getAsBoolean()); + break; + case Create: + doipMessage = new DoipMessageFactory.DoipMessageBuilder() + .createRequest(header.get("identifier").getAsString(), BasicOperations.Create.getName()) + .setBody(body.getAsString().getBytes(StandardCharsets.UTF_8)) + .create(); + break; + case Update: + doipMessage = new DoipMessageFactory.DoipMessageBuilder() + .createRequest(header.get("identifier").getAsString(), BasicOperations.Update.getName()) + .setBody(body.getAsString().getBytes(StandardCharsets.UTF_8)) + .create(); + break; + case Search: + DoipMessageFactory.DoipMessageBuilder searchBuilder = new DoipMessageFactory.DoipMessageBuilder() + .createRequest(header.get("identifier").getAsString(), BasicOperations.Search.getName()); + JsonElement query = header.get("query"); + if(query != null) searchBuilder.addAttributes("query", query.getAsString()); + JsonElement pageNum = header.get("pageNum"); + if(pageNum != null) searchBuilder.addAttributes("pageNum", pageNum.getAsInt()); + JsonElement pageSize = header.get("pageSize"); + if(pageSize != null) searchBuilder.addAttributes("pageSize", pageSize.getAsInt()); + JsonElement type = header.get("type"); + if(type != null) searchBuilder.addAttributes("type", type.getAsString()); + + doipMessage = searchBuilder.create(); + break; + case Extension: + DoipMessageFactory.DoipMessageBuilder extensionBuilder = new DoipMessageFactory.DoipMessageBuilder(); + if(header != null) { + if(header.get("identifier") != null) { + extensionBuilder = extensionBuilder + .createRequest(header.get("identifier").getAsString(), BasicOperations.Extension.getName()); + } + + Set> entries = header.entrySet(); + for (Map.Entry entry : entries) { + extensionBuilder.addAttributes(entry.getKey(), entry.getValue()); + } + } + + if(body != null) { + extensionBuilder.setBody(body.getAsString().getBytes(StandardCharsets.UTF_8)); + } + + doipMessage = extensionBuilder.create(); + break; + case Unknown: + default: break; } return doipMessage; } - public static DoipMessage convertJsonElementToDoipMessage(JsonElement jsonElementRet, DoipMessage msg) { + public static DoipMessage doipOuputConvertJsonElementToDoipMessage(JsonElement jsonElementRet, DoipMessage msg) { DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder(); String responseCodeStr = jsonElementRet.getAsJsonObject().get("doipResponseCode").getAsString(); diff --git a/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java b/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java index bb00c37..59096de 100644 --- a/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java +++ b/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java @@ -124,7 +124,7 @@ public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler try { // 改变调用的函数 + 构造DoipMessagePacker Object ret = ContractProcess.instance.engine.executeWithoutLock(fn, contractRequest, arg); - return DOOPHandler.convertJsonElementToDoipMessage((JsonElement) ret, msg); + return DOOPHandler.doipOuputConvertJsonElementToDoipMessage((JsonElement) ret, msg); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/test/java/org/bdware/doip/DoipClientTest.java b/src/test/java/org/bdware/doip/DoipClientTest.java index da9ae56..0e9ed0f 100644 --- a/src/test/java/org/bdware/doip/DoipClientTest.java +++ b/src/test/java/org/bdware/doip/DoipClientTest.java @@ -10,14 +10,8 @@ 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(); + public void doipClientRetrieveTest(){ final AtomicInteger total = new AtomicInteger(0); final AtomicInteger correct = new AtomicInteger(0); int totalCount = 1; @@ -49,4 +43,38 @@ public class DoipClientTest { } } } + + @Test + public void doipClientHelloTest(){ + 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.hello("aibd/do.e626924a-3b1c-492f-9a41-59179bfe0361", 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(); + } + } + } + } }