From 6e25abbef0bf0a552ca33236b6d927120fd4e1ed Mon Sep 17 00:00:00 2001 From: "haoeliu@foxmail.com" Date: Wed, 25 Jan 2023 21:01:19 +0800 Subject: [PATCH] add doip cluster server logic --- .../java/org/bdware/sc/ContractProcess.java | 89 +++++++++- .../bdware/sc/boundry/utils/ClusterUtil.java | 14 -- .../org/bdware/sc/compiler/YJSCompiler.java | 13 ++ .../java/org/bdware/sc/compiler/ap/DOOP.java | 2 +- .../org/bdware/sc/engine/DesktopEngine.java | 2 +- .../bdware/sc/engine/hook/DOOPHandler.java | 50 +++--- .../bdware/sc/handler/DOOPRequestHandler.java | 50 ++++-- .../bdware/sc/server/DoipClusterServer.java | 156 ++++++++++++++++++ ...erverTest.java => DoipLocalSingleton.java} | 25 ++- .../java/org/bdware/doip/DoipClientTest.java | 80 --------- 10 files changed, 331 insertions(+), 150 deletions(-) create mode 100644 src/main/java/org/bdware/sc/server/DoipClusterServer.java rename src/main/java/org/bdware/sc/server/{DoipServerTest.java => DoipLocalSingleton.java} (63%) delete 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 4aeb587..51be291 100644 --- a/src/main/java/org/bdware/sc/ContractProcess.java +++ b/src/main/java/org/bdware/sc/ContractProcess.java @@ -32,7 +32,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; @@ -519,9 +519,14 @@ 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就可以了 + if(JavaScriptEntry.shardingID == 0) { + DoipClusterServer server = DoipClusterServer.getDOOPServerInstance(); + server.updateRepoInfo(contract, cn); + } + invokeOnStartingDoipServer(cn, contract.getCreateParam()); } return jo.toString(); @@ -660,8 +665,12 @@ public class ContractProcess { // doipModule的话,拉起DoipServer服务端口 if(cn.getYjsType() == YjsType.DoipModule) { - LOGGER.info("the doipServer has started"); - DoipServerTest.main(new String[]{"8080"}); + // 只有一台机器去更新Router中的repoInfo就可以了 + if(JavaScriptEntry.shardingID == 0) { + DoipClusterServer server = DoipClusterServer.getDOOPServerInstance(); + server.updateRepoInfo(contract, cn); + } + invokeOnStartingDoipServer(cn, contract.getCreateParam()); } return jo.toString(); @@ -673,6 +682,76 @@ public class ContractProcess { } } + public void invokeOnStartingDoipServer(ContractNode cn, JsonElement arg) { + ContractRequest onStartingDoipServer = new ContractRequest(); + onStartingDoipServer.setAction("invokeOnStartingDoipServer"); + 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("invokeOnStartingDoipServer"); + + 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 invokeOnStartingDoipServer 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("invokeOnStartingDoipServer", funcArgs); + Map resMap = (Map) 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 invokeOnStartingDoipServer function"); + e.printStackTrace(); + } + } + private void handleLog() { for (FunctionNode fun : cn.getFunctions()) { StringBuilder detail = new StringBuilder(); diff --git a/src/main/java/org/bdware/sc/boundry/utils/ClusterUtil.java b/src/main/java/org/bdware/sc/boundry/utils/ClusterUtil.java index f6b01b4..7154e24 100644 --- a/src/main/java/org/bdware/sc/boundry/utils/ClusterUtil.java +++ b/src/main/java/org/bdware/sc/boundry/utils/ClusterUtil.java @@ -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 { diff --git a/src/main/java/org/bdware/sc/compiler/YJSCompiler.java b/src/main/java/org/bdware/sc/compiler/YJSCompiler.java index 82c203b..11cd2c6 100644 --- a/src/main/java/org/bdware/sc/compiler/YJSCompiler.java +++ b/src/main/java/org/bdware/sc/compiler/YJSCompiler.java @@ -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; @@ -118,6 +120,17 @@ public class YJSCompiler { JsonUtil.GSON.fromJson( new InputStreamReader(manifestInput), ContractManifest.class); + // DOOP relevant logic + ZipEntry routerConfig = zf.getEntry("/router.json"); + if (null == routerConfig) { + routerConfig = zf.getEntry("router.json"); + } + if(null != routerConfig) { + InputStream routerInput = zf.getInputStream(routerConfig); + EndpointConfig endpointConfig = JsonUtil.GSON.fromJson(new InputStreamReader(routerInput), EndpointConfig.class); + DoipClusterServer.createDOOPServerInstance(endpointConfig); + } + // 如果没有就不限制,根据gas进行插装 if (0L != cm.getInsnLimit()) { LOGGER.info("++++++++++++++++++++++true"); 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 4f67f9c..630c19a 100644 --- a/src/main/java/org/bdware/sc/compiler/ap/DOOP.java +++ b/src/main/java/org/bdware/sc/compiler/ap/DOOP.java @@ -29,6 +29,6 @@ public class DOOP extends AnnotationProcessor { 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/DesktopEngine.java b/src/main/java/org/bdware/sc/engine/DesktopEngine.java index 416add6..20a9636 100644 --- a/src/main/java/org/bdware/sc/engine/DesktopEngine.java +++ b/src/main/java/org/bdware/sc/engine/DesktopEngine.java @@ -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; 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 b7f1090..bbed912 100644 --- a/src/main/java/org/bdware/sc/engine/hook/DOOPHandler.java +++ b/src/main/java/org/bdware/sc/engine/hook/DOOPHandler.java @@ -84,14 +84,13 @@ public class DOOPHandler implements AnnotationHook { case Delete: case ListOps: return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\"}}"); - case Retrieve: - 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\"}}}"); + 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: @@ -128,30 +127,18 @@ public class DOOPHandler implements AnnotationHook { case Hello: case Delete: case ListOps: - doipMessage = new DoipMessageFactory.DoipMessageBuilder().createRequest(header.get("identifier").getAsString(), httpReq.getAction()).create(); - break; - case Retrieve: - 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()); + doipMessage = new DoipMessageFactory.DoipMessageBuilder().createRequest(header.get("identifier").getAsString(), httpOperation.getName()).create(); 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()) + .createRequest(header.get("identifier").getAsString(), httpOperation.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()); + .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"); @@ -163,12 +150,19 @@ public class DOOPHandler implements AnnotationHook { 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(), BasicOperations.Extension.getName()); + .createRequest(header.get("identifier").getAsString(), httpOperation.getName()); } Set> entries = header.entrySet(); @@ -201,24 +195,22 @@ public class DOOPHandler implements AnnotationHook { validateJsonElementRulesByArgSchemaVisitor(jsonResponseRules, visitor); JsonObject header = jsonParams.get("header") != null ? jsonParams.get("header").getAsJsonObject() : null; - JsonObject body = jsonParams.get("body") != null ? jsonParams.get("body").getAsJsonObject() : null; + String body = jsonParams.get("body") != null ? jsonParams.get("body").getAsString() : null; if(header != null) { - JsonObject respCodeJson = header.get("response") != null ? header.get("response").getAsJsonObject() : null; - DoipResponseCode respCode = null; - if(respCodeJson != null) { + String headerRespCode = header.get("response") != null ? header.get("response").getAsString() : null; + if(headerRespCode != null) { for (DoipResponseCode responseCode : DoipResponseCode.values()) { - if(responseCode.toString().equals(respCodeJson.toString())) { - respCode = responseCode; + if(responseCode.toString().equals(headerRespCode)) { + msg.header.parameters.response = responseCode; + break; } } - - if(respCode != null) msg.header.parameters.response = respCode; } } if(body != null) { - msg.body.encodedData = body.getAsString().getBytes(StandardCharsets.UTF_8); + msg.body.encodedData = body.getBytes(StandardCharsets.UTF_8); } return msg; diff --git a/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java b/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java index 421f4f2..c0c1c38 100644 --- a/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java +++ b/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java @@ -20,14 +20,14 @@ import java.util.HashMap; import java.util.Map; public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler { - public Map doipOperationsMap; + public Map 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(); } @@ -40,7 +40,7 @@ public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler } public void addDoipOperation(FunctionNode function) { - doipOperationsMap.put(function.getDoipOperationInfo().operationName, function); + doipFunctionNodeMap.put(function.getDoipOperationInfo().operationName, function); } @Override @@ -49,8 +49,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 +60,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; } @@ -127,6 +142,7 @@ public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler return DOOPHandler.convertJsonResponseToDoipMessage(fn, (JsonElement) ret, msg); } catch (Exception e) { e.printStackTrace(); + logger.error("buildRequestAndInvokeEngine has something wrong, executeWithoutLock err or validateJsonElementRulesByArgSchemaVisitor err"); } return null; diff --git a/src/main/java/org/bdware/sc/server/DoipClusterServer.java b/src/main/java/org/bdware/sc/server/DoipClusterServer.java new file mode 100644 index 0000000..e202388 --- /dev/null +++ b/src/main/java/org/bdware/sc/server/DoipClusterServer.java @@ -0,0 +1,156 @@ +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.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 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(); + // 所有Router中用得到的函数(例如Route函数和Route函数依赖的相关函数) + JsonObject functions = new JsonObject(); + + // 维护RouteInfo,将RouteInfo和doipOperationName的映射关系,以及所有Router中用得到的函数都维护好 + maintainRouteInfo(cn, methodRouteInfoMap, 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); + 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 maintainRouteInfo(ContractNode cn, JsonObject methodRouteInfoMap, JsonObject functions) { + // all functions存了ContractNode中,所有的FunctionNode + List allFunctions = cn.getFunctions(); + // gson是Gson工具类昂!!! + Gson gson = new Gson(); + + // 遍历所有的 doipOperationName 和其对应的 doipFunctionNode + for (Map.Entry doipFunctionNodes : DOOPRequestHandler.instance.doipFunctionNodeMap.entrySet()) { + String doipOperationName = doipFunctionNodes.getKey(); + FunctionNode doipFunctionNode = doipFunctionNodes.getValue(); + RouteInfo doipFunctionRouteInfo = doipFunctionNode.getRouteInfo(); + + // 对于routerInfo进行处理,对于routeInfo进行维护 + if(doipFunctionRouteInfo != null) { + methodRouteInfoMap.addProperty(doipOperationName, gson.toJson(doipFunctionRouteInfo)); + if(doipFunctionRouteInfo.funcName != null) { + String routeFunctionName = doipFunctionRouteInfo.funcName; + for(FunctionNode functionNode : allFunctions) { + // find Route Function + if(functionNode.functionName.equals(routeFunctionName)) { + functions.addProperty(functionNode.functionName, functionNode.plainText()); + + // add all dependentFunction to the "functions" struct + for (String dependentFunctionName : functionNode.getDependentFunctions()) { + for (FunctionNode f : cn.getFunctions()) { + if(f.functionName.equals(dependentFunctionName) && !functions.has(dependentFunctionName)) { + functions.addProperty(dependentFunctionName, f.plainText()); + } + } + } + } + } + } + } + } + } +} diff --git a/src/main/java/org/bdware/sc/server/DoipServerTest.java b/src/main/java/org/bdware/sc/server/DoipLocalSingleton.java similarity index 63% rename from src/main/java/org/bdware/sc/server/DoipServerTest.java rename to src/main/java/org/bdware/sc/server/DoipLocalSingleton.java index b14ebbc..1993032 100644 --- a/src/main/java/org/bdware/sc/server/DoipServerTest.java +++ b/src/main/java/org/bdware/sc/server/DoipLocalSingleton.java @@ -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 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); + } + } } diff --git a/src/test/java/org/bdware/doip/DoipClientTest.java b/src/test/java/org/bdware/doip/DoipClientTest.java deleted file mode 100644 index 0e9ed0f..0000000 --- a/src/test/java/org/bdware/doip/DoipClientTest.java +++ /dev/null @@ -1,80 +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 { - @Test - public void doipClientRetrieveTest(){ - 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(); - } - } - } - } - - @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(); - } - } - } - } -}