finish the local version

This commit is contained in:
haoeliu@foxmail.com 2022-12-02 16:39:11 +08:00
parent 76a8e3ca58
commit a1b59cfbfd
6 changed files with 135 additions and 25 deletions

View File

@ -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"});
}

View File

@ -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);
// 维护ContractNodefunctionName is useless, use BasicOperation to map the corresponding functionNode
contractNode.updateFunctionMap(functionNode.functionName, functionNode.getDoipOperationInfo().operationName);
// contractNode.updateFunctionMap(functionNode.functionName, functionNode.getDoipOperationInfo().operationName);
}
}

View File

@ -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<String, BasicOperations> 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;
}

View File

@ -22,16 +22,21 @@ import java.util.Map;
public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler {
public Map<String, FunctionNode> 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;
}

View File

@ -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 (; ; ) {

View File

@ -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();
}
}
}
}
}