add doip cluster server logic

This commit is contained in:
haoeliu@foxmail.com 2023-01-25 21:01:19 +08:00
parent 1a4bf502bb
commit 6e25abbef0
10 changed files with 331 additions and 150 deletions

View File

@ -32,7 +32,7 @@ import org.bdware.sc.engine.hook.*;
import org.bdware.sc.handler.ContractHandler; import org.bdware.sc.handler.ContractHandler;
import org.bdware.sc.index.TimeSerialIndex; import org.bdware.sc.index.TimeSerialIndex;
import org.bdware.sc.node.*; 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.trace.ProgramPointCounter;
import org.bdware.sc.util.FileUtil; import org.bdware.sc.util.FileUtil;
import org.bdware.sc.util.HashUtil; import org.bdware.sc.util.HashUtil;
@ -519,9 +519,14 @@ public class ContractProcess {
jo.addProperty("status", result.status.merge(onCreate.status).toString()); jo.addProperty("status", result.status.merge(onCreate.status).toString());
LOGGER.debug("result: " + jo.toString()); LOGGER.debug("result: " + jo.toString());
// doipModule的话拉起DoipServer服务端口
if(cn.getYjsType() == YjsType.DoipModule) { if(cn.getYjsType() == YjsType.DoipModule) {
LOGGER.info("the doipServer has started"); // 只有一台机器去更新Router中的repoInfo就可以了
DoipServerTest.main(new String[]{"8080"}); if(JavaScriptEntry.shardingID == 0) {
DoipClusterServer server = DoipClusterServer.getDOOPServerInstance();
server.updateRepoInfo(contract, cn);
}
invokeOnStartingDoipServer(cn, contract.getCreateParam());
} }
return jo.toString(); return jo.toString();
@ -660,8 +665,12 @@ public class ContractProcess {
// doipModule的话拉起DoipServer服务端口 // doipModule的话拉起DoipServer服务端口
if(cn.getYjsType() == YjsType.DoipModule) { if(cn.getYjsType() == YjsType.DoipModule) {
LOGGER.info("the doipServer has started"); // 只有一台机器去更新Router中的repoInfo就可以了
DoipServerTest.main(new String[]{"8080"}); if(JavaScriptEntry.shardingID == 0) {
DoipClusterServer server = DoipClusterServer.getDOOPServerInstance();
server.updateRepoInfo(contract, cn);
}
invokeOnStartingDoipServer(cn, contract.getCreateParam());
} }
return jo.toString(); 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<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 invokeOnStartingDoipServer function");
e.printStackTrace();
}
}
private void handleLog() { private void handleLog() {
for (FunctionNode fun : cn.getFunctions()) { for (FunctionNode fun : cn.getFunctions()) {
StringBuilder detail = new StringBuilder(); StringBuilder detail = new StringBuilder();

View File

@ -1,23 +1,9 @@
package org.bdware.sc.boundry.utils; package org.bdware.sc.boundry.utils;
import com.google.gson.JsonArray;
import org.bdware.sc.boundry.JavaScriptEntry; import org.bdware.sc.boundry.JavaScriptEntry;
import org.bdware.sc.compiler.PermissionStub; import org.bdware.sc.compiler.PermissionStub;
import org.bdware.sc.engine.JSONTool;
import org.bdware.sc.node.Permission; import org.bdware.sc.node.Permission;
import org.bdware.sc.util.JsonUtil;
import wrp.jdk.nashorn.internal.objects.NativeArray; 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) @PermissionStub(permission = Permission.Cluster)
public class ClusterUtil { public class ClusterUtil {

View File

@ -5,11 +5,13 @@ import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DiagnosticErrorListener; import org.antlr.v4.runtime.DiagnosticErrorListener;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.bdware.doip.audit.EndpointConfig;
import org.bdware.sc.engine.YJSFilter; import org.bdware.sc.engine.YJSFilter;
import org.bdware.sc.node.*; import org.bdware.sc.node.*;
import org.bdware.sc.parser.JavaScriptLexer; import org.bdware.sc.parser.JavaScriptLexer;
import org.bdware.sc.parser.YJSParser; import org.bdware.sc.parser.YJSParser;
import org.bdware.sc.parser.YJSParser.ProgramContext; import org.bdware.sc.parser.YJSParser.ProgramContext;
import org.bdware.sc.server.DoipClusterServer;
import org.bdware.sc.util.JsonUtil; import org.bdware.sc.util.JsonUtil;
import org.bdware.sc.visitor.ContractReader; import org.bdware.sc.visitor.ContractReader;
import wrp.jdk.nashorn.internal.objects.Global; import wrp.jdk.nashorn.internal.objects.Global;
@ -118,6 +120,17 @@ public class YJSCompiler {
JsonUtil.GSON.fromJson( JsonUtil.GSON.fromJson(
new InputStreamReader(manifestInput), ContractManifest.class); 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进行插装 // 如果没有就不限制根据gas进行插装
if (0L != cm.getInsnLimit()) { if (0L != cm.getInsnLimit()) {
LOGGER.info("++++++++++++++++++++++true"); LOGGER.info("++++++++++++++++++++++true");

View File

@ -526,7 +526,7 @@ public class DesktopEngine extends JSEngine {
} }
} }
//System.out.println("[DesktopEngine MaskConfig]"+ContractProcess.instance.getProjectConfig().getMaskConfig().config.toString()); //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) { if (ppc != null) {
contractRes.extraGas = ppc.extraGas; contractRes.extraGas = ppc.extraGas;
contractRes.executionGas = ppc.cost; contractRes.executionGas = ppc.cost;

View File

@ -84,14 +84,13 @@ public class DOOPHandler implements AnnotationHook {
case Delete: case Delete:
case ListOps: case ListOps:
return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\"}}"); return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\"}}");
case Retrieve:
return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\", \"attributes\":{\"element\":\"string\", \"includeElementData\":\"boolean\"}}}");
case Create: case Create:
return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\", \"attributes\":{\"element\":\"string\", \"includeElementData\":\"boolean\"}}, \"!body\":\"string\"}");
case Update: case Update:
return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\"}, \"!body\":\"string\"}"); return JsonParser.parseString("{\"!header\":{\"!identifier\":\"string\"}, \"!body\":\"string\"}");
case Search: 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 Extension:
case Unknown: case Unknown:
default: default:
@ -128,30 +127,18 @@ public class DOOPHandler implements AnnotationHook {
case Hello: case Hello:
case Delete: case Delete:
case ListOps: case ListOps:
doipMessage = new DoipMessageFactory.DoipMessageBuilder().createRequest(header.get("identifier").getAsString(), httpReq.getAction()).create(); doipMessage = new DoipMessageFactory.DoipMessageBuilder().createRequest(header.get("identifier").getAsString(), httpOperation.getName()).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());
break; break;
case Create: 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: case Update:
doipMessage = new DoipMessageFactory.DoipMessageBuilder() 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)) .setBody(body.getAsString().getBytes(StandardCharsets.UTF_8))
.create(); .create();
break; break;
case Search: case Search:
DoipMessageFactory.DoipMessageBuilder searchBuilder = new DoipMessageFactory.DoipMessageBuilder() 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"); JsonElement query = header.get("query");
if(query != null) searchBuilder.addAttributes("query", query.getAsString()); if(query != null) searchBuilder.addAttributes("query", query.getAsString());
JsonElement pageNum = header.get("pageNum"); JsonElement pageNum = header.get("pageNum");
@ -163,12 +150,19 @@ public class DOOPHandler implements AnnotationHook {
doipMessage = searchBuilder.create(); doipMessage = searchBuilder.create();
break; 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: case Extension:
DoipMessageFactory.DoipMessageBuilder extensionBuilder = new DoipMessageFactory.DoipMessageBuilder(); DoipMessageFactory.DoipMessageBuilder extensionBuilder = new DoipMessageFactory.DoipMessageBuilder();
if(header != null) { if(header != null) {
if(header.get("identifier") != null) { if(header.get("identifier") != null) {
extensionBuilder = extensionBuilder extensionBuilder = extensionBuilder
.createRequest(header.get("identifier").getAsString(), BasicOperations.Extension.getName()); .createRequest(header.get("identifier").getAsString(), httpOperation.getName());
} }
Set<Map.Entry<String, JsonElement>> entries = header.entrySet(); Set<Map.Entry<String, JsonElement>> entries = header.entrySet();
@ -201,24 +195,22 @@ public class DOOPHandler implements AnnotationHook {
validateJsonElementRulesByArgSchemaVisitor(jsonResponseRules, visitor); validateJsonElementRulesByArgSchemaVisitor(jsonResponseRules, visitor);
JsonObject header = jsonParams.get("header") != null ? jsonParams.get("header").getAsJsonObject() : null; 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) { if(header != null) {
JsonObject respCodeJson = header.get("response") != null ? header.get("response").getAsJsonObject() : null; String headerRespCode = header.get("response") != null ? header.get("response").getAsString() : null;
DoipResponseCode respCode = null; if(headerRespCode != null) {
if(respCodeJson != null) {
for (DoipResponseCode responseCode : DoipResponseCode.values()) { for (DoipResponseCode responseCode : DoipResponseCode.values()) {
if(responseCode.toString().equals(respCodeJson.toString())) { if(responseCode.toString().equals(headerRespCode)) {
respCode = responseCode; msg.header.parameters.response = responseCode;
break;
} }
} }
if(respCode != null) msg.header.parameters.response = respCode;
} }
} }
if(body != null) { if(body != null) {
msg.body.encodedData = body.getAsString().getBytes(StandardCharsets.UTF_8); msg.body.encodedData = body.getBytes(StandardCharsets.UTF_8);
} }
return msg; return msg;

View File

@ -20,14 +20,14 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler { public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler {
public Map<String, FunctionNode> doipOperationsMap; public Map<String, FunctionNode> doipFunctionNodeMap;
static Logger logger = LogManager.getLogger(NettyServerHandler.class); static Logger logger = LogManager.getLogger(NettyServerHandler.class);
static Gson gson; static Gson gson;
public static DOOPRequestHandler instance; public static DOOPRequestHandler instance;
public DOOPRequestHandler() { public DOOPRequestHandler() {
doipOperationsMap = new HashMap<>(); doipFunctionNodeMap = new HashMap<>();
gson = new Gson(); gson = new Gson();
} }
@ -40,7 +40,7 @@ public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler
} }
public void addDoipOperation(FunctionNode function) { public void addDoipOperation(FunctionNode function) {
doipOperationsMap.put(function.getDoipOperationInfo().operationName, function); doipFunctionNodeMap.put(function.getDoipOperationInfo().operationName, function);
} }
@Override @Override
@ -49,8 +49,8 @@ public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler
logger.debug("[Call operation] name: " + str); logger.debug("[Call operation] name: " + str);
if (str != null) { if (str != null) {
FunctionNode fn; FunctionNode fn;
fn = doipOperationsMap.get(str); fn = doipFunctionNodeMap.get(str);
if (fn == null) fn = doipOperationsMap.get(BasicOperations.Unknown.getName()); if (fn == null) fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName());
if (fn != null) { if (fn != null) {
return buildRequestAndInvokeEngine(fn, msg); return buildRequestAndInvokeEngine(fn, msg);
} }
@ -60,61 +60,76 @@ public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler
@Override @Override
public DoipMessage handleHello(DoipMessage request) { public DoipMessage handleHello(DoipMessage request) {
FunctionNode fn = doipOperationsMap.get(BasicOperations.Hello.getName()); FunctionNode fn = doipFunctionNodeMap.get(BasicOperations.Hello.getName());
if (fn == null) fn = doipOperationsMap.get(BasicOperations.Unknown.getName()); if (fn == null) fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName());
if (fn != null) { if (fn != null) {
return buildRequestAndInvokeEngine(fn, request); return buildRequestAndInvokeEngine(fn, request);
} else {
logger.error("DoipOperation Hello is not provided");
} }
return null; return null;
} }
@Override @Override
public DoipMessage handleListOps(DoipMessage request) { public DoipMessage handleListOps(DoipMessage request) {
FunctionNode fn = doipOperationsMap.get(BasicOperations.ListOps.getName()); FunctionNode fn = doipFunctionNodeMap.get(BasicOperations.ListOps.getName());
if (fn == null) fn = doipOperationsMap.get(BasicOperations.Unknown.getName()); if (fn == null) fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName());
if (fn != null) { if (fn != null) {
return buildRequestAndInvokeEngine(fn, request); return buildRequestAndInvokeEngine(fn, request);
} else {
logger.error("DoipOperation ListOps is not provided");
} }
return null; return null;
} }
@Override @Override
public DoipMessage handleCreate(DoipMessage request) { public DoipMessage handleCreate(DoipMessage request) {
FunctionNode fn = doipOperationsMap.get(BasicOperations.Create.getName()); FunctionNode fn = doipFunctionNodeMap.get(BasicOperations.Create.getName());
if (fn == null) fn = doipOperationsMap.get(BasicOperations.Unknown.getName()); if (fn == null) fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName());
if (fn != null) { if (fn != null) {
return buildRequestAndInvokeEngine(fn, request); return buildRequestAndInvokeEngine(fn, request);
} else {
logger.error("DoipOperation Create is not provided");
} }
return null; return null;
} }
@Override @Override
public DoipMessage handleUpdate(DoipMessage request) { public DoipMessage handleUpdate(DoipMessage request) {
FunctionNode fn = doipOperationsMap.get(BasicOperations.Update.getName()); FunctionNode fn = doipFunctionNodeMap.get(BasicOperations.Update.getName());
if (fn == null) fn = doipOperationsMap.get(BasicOperations.Unknown.getName()); if (fn == null) fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName());
if (fn != null) { if (fn != null) {
return buildRequestAndInvokeEngine(fn, request); return buildRequestAndInvokeEngine(fn, request);
} else {
logger.error("DoipOperation Update is not provided");
} }
return null; return null;
} }
@Override @Override
public DoipMessage handleDelete(DoipMessage request) { public DoipMessage handleDelete(DoipMessage request) {
FunctionNode fn = doipOperationsMap.get(BasicOperations.Delete.getName()); FunctionNode fn = doipFunctionNodeMap.get(BasicOperations.Delete.getName());
if (fn == null) fn = doipOperationsMap.get(BasicOperations.Unknown.getName()); if (fn == null) fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName());
if (fn != null) { if (fn != null) {
return buildRequestAndInvokeEngine(fn, request); return buildRequestAndInvokeEngine(fn, request);
} else {
logger.error("DoipOperation Delete is not provided");
} }
return null; return null;
} }
@Override @Override
public DoipMessage handleRetrieve(DoipMessage request) { public DoipMessage handleRetrieve(DoipMessage request) {
FunctionNode fn = doipOperationsMap.get(BasicOperations.Retrieve.getName()); FunctionNode fn = doipFunctionNodeMap.get(BasicOperations.Retrieve.getName());
if (fn == null) fn = doipOperationsMap.get(BasicOperations.Unknown.getName()); if (fn == null) fn = doipFunctionNodeMap.get(BasicOperations.Unknown.getName());
if (fn != null) { if (fn != null) {
return buildRequestAndInvokeEngine(fn, request); return buildRequestAndInvokeEngine(fn, request);
} else {
logger.error("DoipOperation Retrieve is not provided");
} }
return null; return null;
} }
@ -127,6 +142,7 @@ public class DOOPRequestHandler implements DoipRequestHandler, RepositoryHandler
return DOOPHandler.convertJsonResponseToDoipMessage(fn, (JsonElement) ret, msg); return DOOPHandler.convertJsonResponseToDoipMessage(fn, (JsonElement) ret, msg);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
logger.error("buildRequestAndInvokeEngine has something wrong, executeWithoutLock err or validateJsonElementRulesByArgSchemaVisitor err");
} }
return null; return null;

View File

@ -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<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();
// 所有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<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();
// 对于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());
}
}
}
}
}
}
}
}
}
}

View File

@ -11,8 +11,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
public class DoipServerTest { public class DoipLocalSingleton {
static Logger LOGGER = LogManager.getLogger(DoipServerTest.class); static Logger LOGGER = LogManager.getLogger(DoipLocalSingleton.class);
public static void main(String[] arg) throws InterruptedException { public static void main(String[] arg) throws InterruptedException {
final int port = (arg.length == 0 ? 21042 : Integer.parseInt(arg[0])); final int port = (arg.length == 0 ? 21042 : Integer.parseInt(arg[0]));
@ -21,7 +21,7 @@ public class DoipServerTest {
@Override @Override
public void run() { public void run() {
try { try {
DoipServerTest.run(port); DoipLocalSingleton.run(port);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -49,4 +49,23 @@ public class DoipServerTest {
Thread.sleep(10000); 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,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();
}
}
}
}
}