16 Commits

Author SHA1 Message Date
haoeliu@foxmail.com
0d7424cd7f merge newest origin/master 2023-03-15 15:39:05 +08:00
haoeliu@foxmail.com
849e8b6601 add packing logic of heaader.response 2023-03-15 15:29:56 +08:00
haoeliu@foxmail.com
1a8cb9c2a7 move router logic from router.json to create params, from YJSCompile to ContractProcess 2023-03-12 20:54:17 +08:00
haoeliu@foxmail.com
18b01e3ab0 add DO operation and multiple functions mapping relations 2023-03-12 20:12:55 +08:00
haoeliu@foxmail.com
d040028463 simplify the doip afterExec packing logic 2023-03-09 21:16:37 +08:00
haoeliu@foxmail.com
fb8305031e add JoinInfo support, the DOOPAfterExecHandler has Todo to deal with 2023-03-02 18:53:40 +08:00
haoeliu@foxmail.com
a117ef4a41 fix: add attributes and requestID to doipMsg to return 2023-02-20 20:44:46 +08:00
CaiHQ
0daa4b6d8c Merge branch 'pr_1' 2023-02-20 16:02:42 +08:00
haoeliu@foxmail.com
fdb652880b feat: change the invokeonStartingDoipServer function name 2023-02-20 15:59:36 +08:00
haoeliu@foxmail.com
838b2e77c8 add DoipMessage to JsonObject conversion in JSONTool, to deal with the ret of engine is DoipMessage case 2023-02-18 20:02:29 +08:00
haoeliu@foxmail.com
098854fd46 add joinCountFunc to functions saved to router and add DOOPAfterExecHandler 2023-02-18 19:47:07 +08:00
haoeliu@foxmail.com
29f81e582a feat: add joinInfo update logic 2023-02-14 20:57:55 +08:00
CaiHQ
7f81dba895 update yjscompiler 2023-02-08 14:05:31 +08:00
haoeliu@foxmail.com
6e25abbef0 add doip cluster server logic 2023-01-25 21:01:19 +08:00
haoeliu@foxmail.com
1a4bf502bb fix the wrapperException bug in DesktopEngine's executeContract and finish the reponse check 2022-12-11 12:02:27 +08:00
haoeliu@foxmail.com
51ee18928b finish the http input validation 2022-12-06 21:31:08 +08:00
19 changed files with 763 additions and 281 deletions

View File

@@ -6,7 +6,7 @@ plugins {
}
group = "org.bdware.sc"
version = "2.0.0"
version = "1.8.0"
repositories {
mavenCentral()
@@ -60,7 +60,7 @@ jar {
}
from {
// uncomment this when publish,
//while develop at local use "false"
// while develop at local use "false"
configurations.runtimeClasspath.filter {
// it.getAbsolutePath().contains("/lib/")
false
@@ -173,18 +173,18 @@ publishing {
maven {
name 'bdwareSnapshotRepository'
url 'https://oss.sonatype.org/content/repositories/snapshots'
// credentials {
// username = "${NEXUS_USERNAME}"
// password = "${NEXUS_PASSWORD}"
// }
credentials {
username = "${NEXUS_USERNAME}"
password = "${NEXUS_PASSWORD}"
}
}
maven {
name 'bdwareRepository'
url 'https://oss.sonatype.org/service/local/staging/deploy/maven2'
// credentials {
// username = "${NEXUS_USERNAME}"
// password = "${NEXUS_PASSWORD}"
// }
credentials {
username = "${NEXUS_USERNAME}"
password = "${NEXUS_PASSWORD}"
}
}
}
}

View File

@@ -1,6 +1,8 @@
{
"agentHttpAddr": "127.0.0.1:18000",
"script": "/Users/huaqiancai/BDWare/datanet/datanet-gateway-bundle/datanet-gateway-backend/build/gateway.ypk",
"pubKey": "04d1924329f72ced148f6f333fb985ccbaa31b1e3aacf10be5f43d4a4ff5ad88899a005e79e37fc06993e1d66ada8cf8b711cb36f59538bb7d3e39e70fa9360ddd",
"privKey": "589d94ee5688358a1c5c18430dd9c75097ddddebf769f139da36a807911d20f8"
"agentAddress": "127.0.0.1:18000",
"ypkPath": "/Users/huaqiancai/BDWare/datanet/datanet-gateway-bundle/datanet-gateway-backend/build/gateway.ypk",
"privateKey": "04d1924329f72ced148f6f333fb985ccbaa31b1e3aacf10be5f43d4a4ff5ad88899a005e79e37fc06993e1d66ada8cf8b711cb36f59538bb7d3e39e70fa9360ddd",
"publicKey": "589d94ee5688358a1c5c18430dd9c75097ddddebf769f139da36a807911d20f8",
"killBeforeStart":"ContractName",
"createParam":{}
}

View File

@@ -63,7 +63,9 @@ public class DebugMain {
= new ResultCallback() {
@Override
public void onResult(String str) {
if(str.contains("Error")) {
LOGGER.error("Some error happens: " + str);
}
LOGGER.info("[PrintCB] " + str);
}
};

View File

@@ -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;
@@ -17,6 +16,7 @@ import org.bdware.analysis.gas.PPCount;
import org.bdware.analysis.taint.TaintBB;
import org.bdware.analysis.taint.TaintCFG;
import org.bdware.analysis.taint.TaintResult;
import org.bdware.doip.audit.EndpointConfig;
import org.bdware.sc.ContractResult.Status;
import org.bdware.sc.bean.*;
import org.bdware.sc.boundry.JavaScriptEntry;
@@ -33,7 +33,7 @@ import org.bdware.sc.engine.hook.*;
import org.bdware.sc.handler.ContractHandler;
import org.bdware.sc.index.TimeSerialIndex;
import org.bdware.sc.node.*;
import org.bdware.sc.server.DoipServerTest;
import org.bdware.sc.server.DoipClusterServer;
import org.bdware.sc.trace.ProgramPointCounter;
import org.bdware.sc.util.FileUtil;
import org.bdware.sc.util.HashUtil;
@@ -50,6 +50,7 @@ import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ContractProcess {
@@ -520,9 +521,11 @@ public class ContractProcess {
jo.addProperty("status", result.status.merge(onCreate.status).toString());
LOGGER.debug("result: " + jo.toString());
// doipModule的话拉起DoipServer服务端口
if(cn.getYjsType() == YjsType.DoipModule) {
LOGGER.info("the doipServer has started");
DoipServerTest.main(new String[]{"8080"});
// 只有一台机器去更新Router中的repoInfo就可以了
updateRepoInfo(contract.getCreateParam());
invokeOnStartingDoipServer(cn, contract.getCreateParam());
}
return jo.toString();
@@ -566,10 +569,6 @@ public class ContractProcess {
fun.appendBeforeInvokeHandler(argSchemaHandler);
}
if (fun.isDoipOperation()) {
fun.appendBeforeInvokeHandler(DOOPHandler.createDOOPHandler());
}
if (fun.isExport()) {
//if(fun.annotations...)
AccessHandler accessHandler = createHandlerIfExist(fun,fun.annotations,AccessHandler.class);
@@ -596,6 +595,10 @@ public class ContractProcess {
fun.appendAfterInvokeHandler(new HomomorphicDecryptHandler(fun));
}
if (fun.isDoipOperation()) {
fun.appendBeforeInvokeHandler(DOOPBeforeExecHandler.createDOOPHandler());
fun.appendAfterInvokeHandler(DOOPAfterExecHandler.createDOOPHandler());
}
}
}
@@ -661,8 +664,8 @@ public class ContractProcess {
// doipModule的话拉起DoipServer服务端口
if(cn.getYjsType() == YjsType.DoipModule) {
LOGGER.info("the doipServer has started");
DoipServerTest.main(new String[]{"8080"});
updateRepoInfo(contract.getCreateParam());
invokeOnStartingDoipServer(cn, contract.getCreateParam());
}
return jo.toString();
@@ -674,6 +677,102 @@ public class ContractProcess {
}
}
public void updateRepoInfo(JsonElement arg) throws Exception {
// 只有0号节点需要初始化IRP连接去updateRepoInfo
if(JavaScriptEntry.shardingID == 0) {
// DOOP relevant logic
DoipClusterServer server = DoipClusterServer.getDOOPServerInstance();
if(server == null) {
JsonObject createParams = arg.getAsJsonObject();
if(createParams.has("router")) {
JsonElement routerInfo = createParams.get("router");
if(!routerInfo.isJsonObject()) throw new Exception("Provide wrong router info in create params to DoipModule");
else {
EndpointConfig endpointConfig = JsonUtil.GSON.fromJson(routerInfo.getAsJsonObject(), EndpointConfig.class);
DoipClusterServer.createDOOPServerInstance(endpointConfig);
}
} else {
throw new Exception("DoipModule should provide router info in create params");
}
server = DoipClusterServer.getDOOPServerInstance();
}
// 只有一台机器去更新Router中的repoInfo就可以了
server.updateRepoInfo(contract, cn);
}
}
public void invokeOnStartingDoipServer(ContractNode cn, JsonElement arg) {
ContractRequest onStartingDoipServer = new ContractRequest();
onStartingDoipServer.setAction("onServerStart");
if (arg == null) {
if (engine != null && engine.getManifest() != null && engine.getManifest().createParam != null)
arg = engine.getManifest().createParam;
else
arg = new JsonPrimitive("");
}
onStartingDoipServer.setArg(arg);
LOGGER.debug("invoke onStartingDoipServer, param:" + onStartingDoipServer.getArg().toString());
onStartingDoipServer.setRequester(contract.getOwner());
if (contract.getDoipFlag() && null != contract.getDOI() && !contract.getDOI().isEmpty()) {
onStartingDoipServer.setRequesterDOI(contract.getDOI());
} else {
onStartingDoipServer.setRequesterDOI("empty");
}
FunctionNode funNode = cn.getFunction("onServerStart");
try {
JsonElement onStartingDoipServerRes = invoke(onStartingDoipServer, funNode).result;
LOGGER.info("Fetch the onStartingDoipServerRes from router successfully, the result is " + onStartingDoipServerRes);
if(onStartingDoipServerRes.isJsonObject()) {
JsonObject onStartingDoipServerJO = onStartingDoipServerRes.getAsJsonObject();
if (!onStartingDoipServerJO.has("doipAddr")) {
throw new Exception("the doipAddr is improper");
} else {
JsonElement doipAddrJE = onStartingDoipServerJO.get("doipAddr");
if(doipAddrJE.isJsonArray()) {
JsonArray doipAddrJA = doipAddrJE.getAsJsonArray();
for(int i = 0 ; i < doipAddrJA.size() ; i++) {
DoipClusterServer.startDoipServer(doipAddrJA.get(i).getAsString());
}
} else {
DoipClusterServer.startDoipServer(doipAddrJE.getAsString());
}
}
} else {
throw new Exception("the onStartingDoipServerRes doesn't return the correct json result");
}
} catch (Exception e) {
LOGGER.error("DoipLocalSingleton cannot starts properly, plz check the onServerStart function");
e.printStackTrace();
}
}
public void invokeOnStartingDoipServer2(JsonElement arg) {
Object[] funcArgs = new Object[3];
funcArgs[0] = JSONTool.convertJsonElementToMirror(arg);
funcArgs[1] = contract.getOwner();
if (contract.getDoipFlag() && null != contract.getDOI() && !contract.getDOI().isEmpty()) {
funcArgs[2] = contract.getDOI();
} else {
funcArgs[2] = "empty";
}
Object result = engine.invokeFunction("onServerStart", funcArgs);
Map<String, String> resMap = (Map<String, String>) result;
try {
if (!resMap.containsKey("doipAddr")) {
throw new Exception("the doipAddr is improper");
} else {
DoipClusterServer.startDoipServer(resMap.get("doipAddr"));
}
} catch (Exception e) {
LOGGER.error("DoipLocalSingleton cannot starts properly, plz check the onServerStart function");
e.printStackTrace();
}
}
private void handleLog() {
for (FunctionNode fun : cn.getFunctions()) {
StringBuilder detail = new StringBuilder();

View File

@@ -1,23 +1,9 @@
package org.bdware.sc.boundry.utils;
import com.google.gson.JsonArray;
import org.bdware.sc.boundry.JavaScriptEntry;
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.internal.objects.NativeArray;
import wrp.jdk.nashorn.internal.runtime.PropertyMap;
import wrp.jdk.nashorn.internal.runtime.ScriptObject;
import wrp.jdk.nashorn.internal.scripts.JO;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@PermissionStub(permission = Permission.Cluster)
public class ClusterUtil {

View File

@@ -10,7 +10,7 @@ public abstract class AnnotationProcessor {
}
public void processFunction(
AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) {
AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) throws Exception {
return;
}
}

View File

@@ -5,11 +5,13 @@ import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DiagnosticErrorListener;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.doip.audit.EndpointConfig;
import org.bdware.sc.engine.YJSFilter;
import org.bdware.sc.node.*;
import org.bdware.sc.parser.JavaScriptLexer;
import org.bdware.sc.parser.YJSParser;
import org.bdware.sc.parser.YJSParser.ProgramContext;
import org.bdware.sc.server.DoipClusterServer;
import org.bdware.sc.util.JsonUtil;
import org.bdware.sc.visitor.ContractReader;
import wrp.jdk.nashorn.internal.objects.Global;
@@ -117,7 +119,6 @@ public class YJSCompiler {
ContractManifest cm =
JsonUtil.GSON.fromJson(
new InputStreamReader(manifestInput), ContractManifest.class);
// 如果没有就不限制根据gas进行插装
if (0L != cm.getInsnLimit()) {
LOGGER.info("++++++++++++++++++++++true");
@@ -131,7 +132,8 @@ public class YJSCompiler {
if (czb.containsPath(str)) {
continue;
}
ZipEntry entry = zf.getEntry("/" + str);
ZipEntry entry = zf.getEntry(str.startsWith("/") ? str : "/" + str);
if (null == entry) {
throw new IllegalStateException("missing import:" + str);
}
@@ -175,7 +177,7 @@ public class YJSCompiler {
return czb;
}
public ContractNode compile(InputStream input, String fileName) throws IOException {
public ContractNode compile(InputStream input, String fileName) throws Exception {
// 词法分析
JavaScriptLexer lexer = new JavaScriptLexer(new ANTLRInputStream(input));
lexer.setUseStrictDefault(true);
@@ -195,7 +197,7 @@ public class YJSCompiler {
return contract;
}
private void handleAnnotation(ContractNode contractNode) {
private void handleAnnotation(ContractNode contractNode) throws Exception {
for (AnnotationNode node : contract.annotations) {
AnnotationProcessor processor = findProcessor(node);
if (processor != null) {

View File

@@ -1,21 +1,19 @@
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.engine.hook.DOOPAfterExecHandler;
import org.bdware.sc.engine.hook.DOOPBeforeExecHandler;
import org.bdware.sc.handler.DOOPRequestHandler;
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 {
@Override
public void processFunction(AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) {
public void processFunction(AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) throws Exception {
// 通过DOOP注解解析对应的值并放进对应的FunctionNode中
// 注解必须暴露出来昂!!!
functionNode.setIsExport(true);
@@ -28,10 +26,12 @@ public class DOOP extends AnnotationProcessor {
DOOPRequestHandler.instance.addDoipOperation(functionNode);
// 维护DOOPHandler
DOOPHandler.createDOOPHandler();
DOOPHandler.instance.putFuncNameAndDoipOperationsMapping(functionNode);
DOOPBeforeExecHandler.createDOOPHandler();
DOOPBeforeExecHandler.instance.putFuncNameAndDoipOperationsMapping(functionNode);
DOOPAfterExecHandler.createDOOPHandler();
DOOPAfterExecHandler.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

@@ -526,7 +526,7 @@ public class DesktopEngine extends JSEngine {
}
}
//System.out.println("[DesktopEngine MaskConfig]"+ContractProcess.instance.getProjectConfig().getMaskConfig().config.toString());
ContractResult contractRes = new ContractResult(Status.Success, (JsonElement) ret);
ContractResult contractRes = new ContractResult(Status.Success, JSONTool.convertMirrorToJson(ret));
if (ppc != null) {
contractRes.extraGas = ppc.extraGas;
contractRes.executionGas = ppc.cost;
@@ -581,9 +581,7 @@ public class DesktopEngine extends JSEngine {
} catch (ScriptException e) {
Throwable cause = e.getCause();
e.printStackTrace();
return new ContractResult(
Status.Exception,
new JsonPrimitive(extractException(e.getMessage(), extract(cn, cause))));
return wrapperException(e, fun);
} catch (Throwable e) {
ByteArrayOutputStream bo1 = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(bo1);

View File

@@ -1,14 +1,17 @@
package org.bdware.sc.engine;
import com.google.gson.*;
import jdk.nashorn.internal.runtime.Context;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.sc.util.JsonUtil;
import wrp.jdk.nashorn.api.scripting.ScriptObjectMirror;
import wrp.jdk.nashorn.internal.objects.NativeArray;
import wrp.jdk.nashorn.internal.runtime.PropertyMap;
import wrp.jdk.nashorn.internal.runtime.ScriptObject;
import wrp.jdk.nashorn.internal.scripts.JO;
import java.util.*;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class JSONTool {
public static JsonElement convertMirrorToJson(Object ret2) {
@@ -146,17 +149,19 @@ public class JSONTool {
return jsonObject;
} else if (obj.getClass() == jdk.internal.dynalink.beans.StaticClass.class) {
return JsonNull.INSTANCE;
} else if (obj instanceof Number) {
} else if (obj.getClass() == DoipMessage.class) {
return JsonUtil.fromJson(JsonUtil.toJson(obj), JsonObject.class);
}else if (obj instanceof Number) {
return new JsonPrimitive((Number) obj);
} else if (obj instanceof String) {
return new JsonPrimitive((String) obj);
} else if (obj instanceof Character) {
return new JsonPrimitive((Character) obj);
}
if (obj instanceof Boolean) {
} else if (obj instanceof Boolean) {
return new JsonPrimitive((Boolean) obj);
} else if (obj.getClass() == Boolean.TYPE) {
return new JsonPrimitive((boolean) obj);
}
return JsonNull.INSTANCE;
}

View File

@@ -3,6 +3,8 @@ package org.bdware.sc.engine.hook;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.sc.JSEngine;
import org.bdware.sc.bean.ContractRequest;
import org.bdware.sc.boundry.ScriptReturnException;
@@ -12,13 +14,12 @@ 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 wrp.jdk.nashorn.internal.objects.Global;
import wrp.jdk.nashorn.internal.runtime.Context;
import wrp.jdk.nashorn.internal.runtime.PropertyMap;
import wrp.jdk.nashorn.internal.scripts.JO;
@YJSAnnotation(name = "Access")
public class AccessHandler implements AnnotationHook {
static Logger LOGGER = LogManager.getLogger(AccessHandler.class);
public String acFunction;
public boolean requireSign;
@@ -46,15 +47,18 @@ public class AccessHandler implements AnnotationHook {
}
return argPacks;
}
if (!argPacks.request.verifySignature()) {
LOGGER.info("verify failed! clear requester," + argPacks.request.getContentStr() +
" -> " + argPacks.request.getPublicKey() + "sign:" + argPacks.request.getSignature());
//TODO
//FIXME here!
// argPacks.request.setRequester(null);
} else
LOGGER.info("verify success!" + argPacks.request.getRequester());
if (acFunction == null) return argPacks;
DesktopEngine de = (DesktopEngine) desktopEngine;
Global oldGlobal = Context.getGlobal();
Global newGlobal = de.getDesktopGlobal();
boolean globalChanged = (oldGlobal != newGlobal);
try {
if (globalChanged) {
Context.setGlobal(newGlobal);
}
ContractRequest input = argPacks.request;
JO jo = new JO(PropertyMap.newMap());
jo.put("requester", input.getRequester(), false);
@@ -69,9 +73,6 @@ public class AccessHandler implements AnnotationHook {
jo.addProperty("code", "401");
jo.addProperty("msg", "access check meets exception! " + e);
throw new ScriptReturnException(jo);
} finally {
if (globalChanged) Context.setGlobal(oldGlobal);
}
}

View File

@@ -0,0 +1,157 @@
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.*;
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.engine.JSONTool;
import org.bdware.sc.entity.DoipMessagePacker;
import org.bdware.sc.node.AnnotationHook;
import org.bdware.sc.node.ArgPacks;
import org.bdware.sc.node.FunctionNode;
import org.bdware.sc.util.JsonUtil;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class DOOPAfterExecHandler implements AnnotationHook {
public static Map<String, BasicOperations> funcNameToDoipOperations;
public static DOOPAfterExecHandler instance;
public DOOPAfterExecHandler() {
funcNameToDoipOperations = new HashMap<>();
}
public static DOOPAfterExecHandler createDOOPHandler() {
if(instance == null) {
instance = new DOOPAfterExecHandler();
}
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
public ArgPacks handle(JSEngine desktopEngine, ArgPacks argPacks) {
BasicOperations curOp = funcNameToDoipOperations.get(argPacks.request.getAction());
Object originDoipMsgPacker = argPacks.arg;
DoipMessage originDoipMsg = null;
if (originDoipMsgPacker instanceof DoipMessagePacker) {
DoipMessagePacker doipMessagePacker = (DoipMessagePacker) originDoipMsgPacker;
originDoipMsg = doipMessagePacker.rawDoipMsg;
// if http, directly return
if (doipMessagePacker.source.equals("http")) {
return argPacks;
} else {
// pack
JsonObject jsonObjectRes = JSONTool.convertMirrorToJson(argPacks.ret).getAsJsonObject();
// validate json response
JsonElement jsonResponseRules = getRulesForJsonResponse(curOp);
ArgSchemaVisitor visitor = new ArgSchemaVisitor(jsonObjectRes);
validateJsonElementRulesByArgSchemaVisitor(jsonResponseRules, visitor);
JsonObject header = jsonObjectRes.get("header") != null ? jsonObjectRes.get("header").getAsJsonObject() : null;
String body = jsonObjectRes.get("body") != null ? jsonObjectRes.get("body").getAsString() : null;
// 和HTTP一样所有需要的字段自己封装校验规则也比较简单这里只做简单的包装返回即可
if(header != null) {
originDoipMsg.header = JsonUtil.fromJson(header, MessageHeader.class);
// response字段根据白皮书上的规定处于header下人为包装到parameters的response中
String headerRespCode = header.get("response") != null ? header.get("response").getAsString() : null;
if(headerRespCode != null) {
if (originDoipMsg.header.parameters == null) originDoipMsg.header.parameters = new HeaderParameter(null, null);
originDoipMsg.header.parameters.response = DoipResponseCode.valueOf(headerRespCode);
}
}
if(body != null) {
originDoipMsg.body.encodedData = body.getBytes(StandardCharsets.UTF_8);
}
argPacks.ret = originDoipMsg;
return argPacks;
}
} else {
return argPacks;
}
}
public static JsonElement getRulesForJsonResponse(BasicOperations basicOperations) {
switch (basicOperations) {
case Hello:
case Retrieve:
case Create:
case Update:
case Search:
case ListOps:
return JsonParser.parseString("{\"!header\":{\"!response\":\"string\"}, \"!body\":\"string\"}");
case Delete:
return JsonParser.parseString("{\"!header\":{\"!response\":\"string\"}, \"body\":\"string\"}");
case Extension:
case Unknown:
default:
return null;
}
}
// old convert jsonResponse from argPack's ret to Doip response in doip chain logic
public static DoipMessage convertJsonResponseToDoipMessage(FunctionNode fn, JsonElement jsonResponse, DoipMessage msg) {
BasicOperations curOp = funcNameToDoipOperations.get(fn.getFunctionName());
JsonObject jsonParams = jsonResponse.getAsJsonObject();
// validate json response
JsonElement jsonResponseRules = getRulesForJsonResponse(curOp);
ArgSchemaVisitor visitor = new ArgSchemaVisitor(jsonResponse);
validateJsonElementRulesByArgSchemaVisitor(jsonResponseRules, visitor);
JsonObject header = jsonParams.get("header") != null ? jsonParams.get("header").getAsJsonObject() : null;
String body = jsonParams.get("body") != null ? jsonParams.get("body").getAsString() : null;
if(header != null) {
String headerRespCode = header.get("response") != null ? header.get("response").getAsString() : null;
if(headerRespCode != null) {
for (DoipResponseCode responseCode : DoipResponseCode.values()) {
if(responseCode.toString().equals(headerRespCode)) {
msg.header.parameters.response = responseCode;
break;
}
}
}
}
if(body != null) {
msg.body.encodedData = body.getBytes(StandardCharsets.UTF_8);
}
return msg;
}
public static void validateJsonElementRulesByArgSchemaVisitor(JsonElement jsonElement, ArgSchemaVisitor visitor) {
visitor.visit(jsonElement);
if (!visitor.getStatus()) {
JsonObject jo = new JsonObject();
jo.addProperty("msg", visitor.getException());
jo.addProperty("code", visitor.errorCode);
throw new ScriptReturnException(jo);
}
}
}

View File

@@ -0,0 +1,179 @@
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;
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.ArgPacks;
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 DOOPBeforeExecHandler implements AnnotationHook {
public static Map<String, BasicOperations> funcNameToDoipOperations;
public static DOOPBeforeExecHandler instance;
public DOOPBeforeExecHandler() {
funcNameToDoipOperations = new HashMap<>();
}
public static DOOPBeforeExecHandler createDOOPHandler() {
if(instance == null) {
instance = new DOOPBeforeExecHandler();
}
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
public ArgPacks handle(JSEngine desktopEngine, ArgPacks argPacks) {
Object arg = argPacks.arg;
DoipMessagePacker doipMsgPackerArg = new DoipMessagePacker();
if (arg instanceof DoipMessagePacker){
doipMsgPackerArg = (DoipMessagePacker)arg;
} else {
// validate http request's params
ContractRequest httpReq = argPacks.request;
validateHTTPRequestArgs(httpReq);
// set doipMsgPackerArg struct's params
doipMsgPackerArg.setSource("http");
doipMsgPackerArg.rawDoipMsg = convertHttpRequestToDoipMessage(httpReq);
}
argPacks.arg = doipMsgPackerArg;
return argPacks;
}
public static void validateHTTPRequestArgs(ContractRequest httpReq) {
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 = getRulesForHTTPRequest(curOp);
ArgSchemaVisitor visitor = new ArgSchemaVisitor(httpArgs);
validateJsonElementRulesByArgSchemaVisitor(httpArgsRules, visitor);
}
public static JsonElement getRulesForHTTPRequest(BasicOperations basicOperation) {
switch(basicOperation) {
case Hello:
case Delete:
case ListOps:
return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\"}}");
case Create:
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 Retrieve:
return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\", \"attributes\":{\"element\":\"string\", \"includeElementData\":\"boolean\"}}}");
case Extension:
case Unknown:
default:
return null;
}
}
public static DoipMessage convertHttpRequestToDoipMessage(ContractRequest httpReq) {
BasicOperations httpOperation = funcNameToDoipOperations.get(httpReq.getAction());
JsonObject jsonParams = JsonParser.parseString(httpReq.getArg().getAsString()).getAsJsonObject();
// taking Extension into consideration
JsonObject header = jsonParams.get("header") != null ? jsonParams.get("header").getAsJsonObject() : null;
String body = jsonParams.get("body") != null ? jsonParams.get("body").getAsString() : null;
DoipMessage doipMessage = null;
switch(httpOperation) {
case Hello:
case Delete:
case ListOps:
doipMessage = new DoipMessageFactory.DoipMessageBuilder().createRequest(header.get("identifier").getAsString(), httpOperation.getName()).create();
break;
case Create:
case Update:
doipMessage = new DoipMessageFactory.DoipMessageBuilder()
.createRequest(header.get("identifier").getAsString(), httpOperation.getName())
.setBody(body.getBytes(StandardCharsets.UTF_8))
.create();
break;
case Search:
DoipMessageFactory.DoipMessageBuilder searchBuilder = new DoipMessageFactory.DoipMessageBuilder()
.createRequest(header.get("identifier").getAsString(), httpOperation.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 Retrieve:
doipMessage = new DoipMessageFactory.DoipMessageBuilder().createRequest(header.get("identifier").getAsString(), httpOperation.getName()).create();
JsonElement element = header.get("element");
JsonElement includeElementData = header.get("includeElementData");
if(element != null) doipMessage.header.parameters.addAttribute("element", element.getAsString());
if(includeElementData != null && includeElementData.getAsBoolean()) doipMessage.header.parameters.addAttribute("includeElementData", "true");
break;
case Extension:
DoipMessageFactory.DoipMessageBuilder extensionBuilder = new DoipMessageFactory.DoipMessageBuilder();
if(header != null) {
if(header.get("identifier") != null) {
extensionBuilder = extensionBuilder
.createRequest(header.get("identifier").getAsString(), httpOperation.getName());
}
Set<Map.Entry<String, JsonElement>> entries = header.entrySet();
for (Map.Entry<String, JsonElement> entry : entries) {
extensionBuilder.addAttributes(entry.getKey(), entry.getValue());
}
}
if(body != null) {
extensionBuilder.setBody(body.getBytes(StandardCharsets.UTF_8));
}
doipMessage = extensionBuilder.create();
break;
case Unknown:
default:
break;
}
return doipMessage;
}
public static void validateJsonElementRulesByArgSchemaVisitor(JsonElement jsonElement, ArgSchemaVisitor visitor) {
visitor.visit(jsonElement);
if (!visitor.getStatus()) {
JsonObject jo = new JsonObject();
jo.addProperty("msg", visitor.getException());
jo.addProperty("code", visitor.errorCode);
throw new ScriptReturnException(jo);
}
}
}

View File

@@ -1,129 +0,0 @@
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;
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.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 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
public ArgPacks handle(JSEngine desktopEngine, ArgPacks argPacks) {
Object arg = argPacks.arg;
DoipMessagePacker doipMsgPackerArg = new DoipMessagePacker();
if (arg instanceof DoipMessagePacker){
doipMsgPackerArg = (DoipMessagePacker)arg;
} else {
// validate http request's params
ContractRequest httpReq = argPacks.request;
validateHTTPRequestArgs(httpReq);
// set doipMsgPackerArg struct's params
doipMsgPackerArg.setSource("http");
doipMsgPackerArg.rawDoipMsg = convertContractRequestToDoipMessage(httpReq);
}
argPacks.arg = doipMsgPackerArg;
return argPacks;
}
public static void validateHTTPRequestArgs(ContractRequest httpReq) {
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) {
switch(basicOperation) {
case Hello:
return JsonParser.parseString("{\"!doid\":\"string\"}");
case Retrieve:
return JsonParser.parseString("{\"!doid\":\"string\", \"!element\":\"boolean\"}");
default:
return null;
}
}
public static DoipMessage convertContractRequestToDoipMessage(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();
switch(httpOperation) {
case Hello:
doipMessage = new DoipMessageFactory.DoipMessageBuilder().createRequest(jsonParams.get("doid").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();
break;
}
return doipMessage;
}
public static DoipMessage convertJsonElementToDoipMessage(JsonElement jsonElementRet, DoipMessage msg) {
DoipMessageFactory.DoipMessageBuilder builder = new DoipMessageFactory.DoipMessageBuilder();
String responseCodeStr = jsonElementRet.getAsJsonObject().get("doipResponseCode").getAsString();
DoipResponseCode responseCode = DoipResponseCode.UnKnownError;
for (DoipResponseCode resp : DoipResponseCode.values()) {
if (resp.toString().equals(responseCodeStr)) responseCode = resp;
}
DoipMessage doipRet = builder.createResponse(responseCode, msg).create();;
doipRet.body.encodedData = jsonElementRet.getAsJsonObject().get("body").getAsJsonObject().get("encodedData").getAsString().getBytes(StandardCharsets.UTF_8);
return doipRet;
}
}

View File

@@ -1,18 +1,19 @@
package org.bdware.sc.handler;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.netty.channel.ChannelHandlerContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bdware.doip.codec.doipMessage.DoipMessage;
import org.bdware.doip.codec.doipMessage.HeaderParameter;
import org.bdware.doip.codec.operations.BasicOperations;
import org.bdware.doip.endpoint.server.DoipRequestHandler;
import org.bdware.doip.endpoint.server.NettyServerHandler;
import org.bdware.doip.endpoint.server.RepositoryHandler;
import org.bdware.sc.ContractProcess;
import org.bdware.sc.bean.ContractRequest;
import org.bdware.sc.engine.hook.DOOPHandler;
import org.bdware.sc.boundry.JavaScriptEntry;
import org.bdware.sc.entity.DoipMessagePacker;
import org.bdware.sc.node.FunctionNode;
@@ -20,14 +21,14 @@ import java.util.HashMap;
import java.util.Map;
public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler {
public Map<String, FunctionNode> doipOperationsMap;
public Map<String, FunctionNode> doipFunctionNodeMap;
static Logger logger = LogManager.getLogger(NettyServerHandler.class);
static Gson gson;
public static DOOPRequestHandler instance;
public DOOPRequestHandler() {
doipOperationsMap = new HashMap<>();
doipFunctionNodeMap = new HashMap<>();
gson = new Gson();
}
@@ -39,8 +40,12 @@ public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler
return instance;
}
public void addDoipOperation(FunctionNode function) {
doipOperationsMap.put(function.getDoipOperationInfo().operationName, function);
public void addDoipOperation(FunctionNode function) throws Exception {
String operationName = function.getDoipOperationInfo().operationName;
if (doipFunctionNodeMap.containsKey(operationName)) {
throw new Exception("Contract is wrong: One DO operation maps multiple functions");
}
doipFunctionNodeMap.put(operationName, function);
}
@Override
@@ -49,8 +54,8 @@ public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler
logger.debug("[Call operation] name: " + str);
if (str != null) {
FunctionNode fn;
fn = doipOperationsMap.get(str);
if (fn == null) fn = doipOperationsMap.get(BasicOperations.Unknown.getName());
fn = doipFunctionNodeMap.get(str);
if (fn == null) fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName());
if (fn != null) {
return buildRequestAndInvokeEngine(fn, msg);
}
@@ -60,61 +65,76 @@ public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler
@Override
public DoipMessage handleHello(DoipMessage request) {
FunctionNode fn = doipOperationsMap.get(BasicOperations.Hello.getName());
if (fn == null) fn = doipOperationsMap.get(BasicOperations.Unknown.getName());
FunctionNode fn = doipFunctionNodeMap.get(BasicOperations.Hello.getName());
if (fn == null) fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName());
if (fn != null) {
return buildRequestAndInvokeEngine(fn, request);
} else {
logger.error("DoipOperation Hello is not provided");
}
return null;
}
@Override
public DoipMessage handleListOps(DoipMessage request) {
FunctionNode fn = doipOperationsMap.get(BasicOperations.ListOps.getName());
if (fn == null) fn = doipOperationsMap.get(BasicOperations.Unknown.getName());
FunctionNode fn = doipFunctionNodeMap.get(BasicOperations.ListOps.getName());
if (fn == null) fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName());
if (fn != null) {
return buildRequestAndInvokeEngine(fn, request);
} else {
logger.error("DoipOperation ListOps is not provided");
}
return null;
}
@Override
public DoipMessage handleCreate(DoipMessage request) {
FunctionNode fn = doipOperationsMap.get(BasicOperations.Create.getName());
if (fn == null) fn = doipOperationsMap.get(BasicOperations.Unknown.getName());
FunctionNode fn = doipFunctionNodeMap.get(BasicOperations.Create.getName());
if (fn == null) fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName());
if (fn != null) {
return buildRequestAndInvokeEngine(fn, request);
} else {
logger.error("DoipOperation Create is not provided");
}
return null;
}
@Override
public DoipMessage handleUpdate(DoipMessage request) {
FunctionNode fn = doipOperationsMap.get(BasicOperations.Update.getName());
if (fn == null) fn = doipOperationsMap.get(BasicOperations.Unknown.getName());
FunctionNode fn = doipFunctionNodeMap.get(BasicOperations.Update.getName());
if (fn == null) fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName());
if (fn != null) {
return buildRequestAndInvokeEngine(fn, request);
} else {
logger.error("DoipOperation Update is not provided");
}
return null;
}
@Override
public DoipMessage handleDelete(DoipMessage request) {
FunctionNode fn = doipOperationsMap.get(BasicOperations.Delete.getName());
if (fn == null) fn = doipOperationsMap.get(BasicOperations.Unknown.getName());
FunctionNode fn = doipFunctionNodeMap.get(BasicOperations.Delete.getName());
if (fn == null) fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName());
if (fn != null) {
return buildRequestAndInvokeEngine(fn, request);
} else {
logger.error("DoipOperation Delete is not provided");
}
return null;
}
@Override
public DoipMessage handleRetrieve(DoipMessage request) {
FunctionNode fn = doipOperationsMap.get(BasicOperations.Retrieve.getName());
if (fn == null) fn = doipOperationsMap.get(BasicOperations.Unknown.getName());
FunctionNode fn = doipFunctionNodeMap.get(BasicOperations.Retrieve.getName());
if (fn == null) fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName());
if (fn != null) {
return buildRequestAndInvokeEngine(fn, request);
} else {
logger.error("DoipOperation Retrieve is not provided");
}
return null;
}
@@ -124,9 +144,19 @@ public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler
try {
// 改变调用的函数 + 构造DoipMessagePacker
Object ret = ContractProcess.instance.engine.executeWithoutLock(fn, contractRequest, arg);
return DOOPHandler.convertJsonElementToDoipMessage((JsonElement) ret, msg);
DoipMessage finalDoipMsg = (DoipMessage) ret;
finalDoipMsg.requestID = msg.requestID;
if(finalDoipMsg.header.parameters == null) {
finalDoipMsg.header.parameters = new HeaderParameter("", "");
}
if(finalDoipMsg.header.parameters.attributes == null) {
finalDoipMsg.header.parameters.attributes = new JsonObject();
}
finalDoipMsg.header.parameters.attributes.addProperty("nodeID", String.valueOf(JavaScriptEntry.shardingID));
return finalDoipMsg;
} catch (Exception e) {
e.printStackTrace();
logger.error("buildRequestAndInvokeEngine has something wrong, executeWithoutLock err or validateJsonElementRulesByArgSchemaVisitor err");
}
return null;

View File

@@ -0,0 +1,183 @@
package org.bdware.sc.server;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
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.EndpointInfo;
import org.bdware.doip.audit.client.AuditIrpClient;
import org.bdware.doip.endpoint.server.DoipListenerConfig;
import org.bdware.doip.endpoint.server.DoipServerImpl;
import org.bdware.doip.endpoint.server.DoipServiceInfo;
import org.bdware.irp.exception.IrpClientException;
import org.bdware.irp.stateinfo.StateInfoBase;
import org.bdware.sc.ContractProcess;
import org.bdware.sc.bean.Contract;
import org.bdware.sc.bean.JoinInfo;
import org.bdware.sc.bean.RouteInfo;
import org.bdware.sc.handler.DOOPRequestHandler;
import org.bdware.sc.node.ContractNode;
import org.bdware.sc.node.FunctionNode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static org.bdware.doip.audit.EndpointConfig.defaultDOIPServerPort;
import static org.bdware.doip.audit.EndpointConfig.defaultRepoType;
public class DoipClusterServer extends DoipServerImpl {
static EndpointConfig config;
static AuditIrpClient repoIrpClient;
static DoipClusterServer instance;
// LOGGER
private static final Logger LOGGER = LogManager.getLogger(ContractProcess.class);
public DoipClusterServer(EndpointConfig config) {
super(resolveInfo(config));
DoipClusterServer.config = config;
}
public static void createDOOPServerInstance(EndpointConfig config) {
instance = new DoipClusterServer(config);
}
public static DoipClusterServer getDOOPServerInstance() {
return instance;
}
public static void startDoipServer(String arg) throws InterruptedException {
final String doipAddr = arg;
Thread doipServerThread = new Thread(){
@Override
public void run() {
try {
DoipLocalSingleton.run(doipAddr);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
doipServerThread.start();
}
private static DoipServiceInfo resolveInfo(EndpointConfig config) {
repoIrpClient = new AuditIrpClient(config);
EndpointInfo info = repoIrpClient.getEndpointInfo();
if (info == null) {
String content = "{\"date\":\"2022-1-13\",\"name\":\"testrepoforaibd\",\"doId\":\"bdware.test.local/Repo\",\"address\":\"tcp://127.0.0.1:" + defaultDOIPServerPort + "\",\"status\":\"已审核\",\"protocol\":\"DOIP\",\"pubKey\":\"empty\",\"version\":\"2.1\"}";
info = EndpointInfo.fromJson(content);
}
List<DoipListenerConfig> infos = new ArrayList<>();
try {
infos.add(new DoipListenerConfig(info.getURI(), info.getVersion()));
} catch (Exception e) {
e.printStackTrace();
}
DoipServiceInfo ret = new DoipServiceInfo(info.getDoId(), info.getPubKey(), defaultRepoType, infos);
return ret;
}
public void updateRepoInfo(Contract contract, ContractNode cn) throws IrpClientException {
String repoIdentifier = "bdtest/" + config.repoName;
StateInfoBase repoInfo = new StateInfoBase();
repoInfo.identifier = repoIdentifier;
repoInfo.handleValues = new JsonObject();
JsonObject repoHandleValues = new JsonObject();
JsonObject createParams = contract.getCreateParam().getAsJsonObject();
// 放置集群信息
JsonElement clusterInfo = createParams.get("clusterInfo");
// doipOperationName和对应的routeFunctionName的对应关系存储方式为doipOperationName: routeFunctionName
JsonObject methodRouteInfoMap = new JsonObject();
// doipOperationName和对应的routeFunctionName的对应关系存储方式为doipOperationName: routeFunctionName
JsonObject methodJoinInfoMap = new JsonObject();
// 所有Router中用得到的函数例如Route函数和Route函数依赖的相关函数
JsonObject functions = new JsonObject();
// 维护RouteInfo将RouteInfo和doipOperationName的映射关系以及所有Router中用得到的函数都维护好
maintainRouteJoinInfo(cn, methodRouteInfoMap, methodJoinInfoMap, functions);
if(clusterInfo != null) repoHandleValues.add("clusterInfo", clusterInfo);
if(!functions.equals(new JsonObject())) repoHandleValues.add("functions", functions);
if(!methodRouteInfoMap.equals(new JsonObject())) repoHandleValues.add("routeInfo", methodRouteInfoMap);
if(!methodJoinInfoMap.equals(new JsonObject())) repoHandleValues.add("joinInfo", methodJoinInfoMap);
repoInfo.handleValues.addProperty("cluster", repoHandleValues.toString());
String updateRepoInfoRes = repoIrpClient.reRegister(repoInfo);
if (updateRepoInfoRes.equals("success")) {
LOGGER.info("Update cluster info to router successfully");
} else if (updateRepoInfoRes.equals("failed")) {
LOGGER.error("Failed to update cluster info to router");
} else {
LOGGER.warn("Oops...The result of updating clusterInfo to the router is " + updateRepoInfoRes);
}
}
public void maintainRouteJoinInfo(ContractNode cn, JsonObject methodRouteInfoMap, JsonObject methodJoinInfoMap, JsonObject functions) {
// all functions存了ContractNode中所有的FunctionNode
List<FunctionNode> allFunctions = cn.getFunctions();
// gson是Gson工具类昂
Gson gson = new Gson();
// 遍历所有的 doipOperationName 和其对应的 doipFunctionNode
for (Map.Entry<String, FunctionNode> doipFunctionNodes : DOOPRequestHandler.instance.doipFunctionNodeMap.entrySet()) {
String doipOperationName = doipFunctionNodes.getKey();
FunctionNode doipFunctionNode = doipFunctionNodes.getValue();
RouteInfo doipFunctionRouteInfo = doipFunctionNode.getRouteInfo();
JoinInfo doipFunctionJoinInfo = doipFunctionNode.getJoinInfo();
// 对于RouterInfo进行维护
if(doipFunctionRouteInfo != null) {
// 建立method和RouteInfo的映射
methodRouteInfoMap.addProperty(doipOperationName, gson.toJson(doipFunctionRouteInfo));
if(doipFunctionRouteInfo.funcName != null) {
String routeFunctionName = doipFunctionRouteInfo.funcName;
packSourceFunctionAndDependentFunctions(allFunctions, routeFunctionName, functions);
}
}
// 对于JoinInfo进行维护
if(doipFunctionJoinInfo != null) {
// 建立method和JoinInfo的映射
methodJoinInfoMap.addProperty(doipOperationName, gson.toJson(doipFunctionJoinInfo));
// 包装JoinInfo中用到的JoinFunction
if(doipFunctionJoinInfo.joinFuncName != null) {
String joinFunctionName = doipFunctionJoinInfo.joinFuncName;
packSourceFunctionAndDependentFunctions(allFunctions, joinFunctionName, functions);
}
// 包装JoinInfo中用到的JoinCountFunction
if(doipFunctionJoinInfo.joinCountFuncName != null) {
String joinFunctionCountName = doipFunctionJoinInfo.joinCountFuncName;
packSourceFunctionAndDependentFunctions(allFunctions, joinFunctionCountName, functions);
}
}
}
}
public void packSourceFunctionAndDependentFunctions(List<FunctionNode> allFunctions, String sourceFunctionName, JsonObject functions) {
for(FunctionNode functionNode : allFunctions) {
// add sourceFunction
if(functionNode.functionName.equals(sourceFunctionName)) {
functions.addProperty(functionNode.functionName, functionNode.plainText());
// find all dependent functions to the "functions" struct
for (String dependentFunctionName : functionNode.getDependentFunctions()) {
for (FunctionNode f : allFunctions) {
if(f.functionName.equals(dependentFunctionName) && !functions.has(dependentFunctionName)) {
functions.addProperty(dependentFunctionName, f.plainText());
}
}
}
}
}
}
}

View File

@@ -11,8 +11,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class DoipServerTest {
static Logger LOGGER = LogManager.getLogger(DoipServerTest.class);
public class DoipLocalSingleton {
static Logger LOGGER = LogManager.getLogger(DoipLocalSingleton.class);
public static void main(String[] arg) throws InterruptedException {
final int port = (arg.length == 0 ? 21042 : Integer.parseInt(arg[0]));
@@ -21,7 +21,7 @@ public class DoipServerTest {
@Override
public void run() {
try {
DoipServerTest.run(port);
DoipLocalSingleton.run(port);
} catch (InterruptedException e) {
e.printStackTrace();
}
@@ -49,4 +49,23 @@ public class DoipServerTest {
Thread.sleep(10000);
}
}
public static void run(String doipAddr) throws InterruptedException {
List<DoipListenerConfig> infos = new ArrayList<>();
try {
infos.add(new DoipListenerConfig(doipAddr, "2.1"));
} catch (Exception e) {
e.printStackTrace();
}
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 = DOOPRequestHandler.createHandler();
server.setRepositoryHandler(handler);
server.start();
for (; ; ) {
LOGGER.info("Count:" + count.get());
Thread.sleep(10000);
}
}
}

View File

@@ -1,52 +0,0 @@
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();
}
}
}
}
}

View File

@@ -67,7 +67,7 @@ public class YJSParserTest {
ContractNode cn = compiler.compile(resource, "rrr.yjs");
new DesktopEngine();
System.out.println(new Gson().toJson(cn));
} catch (IOException e) {
} catch (Exception e) {
e.printStackTrace();
}
}