From 65aa9fd78dcd60155a3b303f77777b0a22b85499 Mon Sep 17 00:00:00 2001 From: "haoeliu@foxmail.com" Date: Tue, 15 Nov 2022 11:47:26 +0800 Subject: [PATCH] add first version DOOPRequestHandler --- .../java/org/bdware/sc/compiler/ap/DOOP.java | 9 ++- .../sc/handler/DOOPRepositoryHandler.java | 71 ++++++++++++++++ .../bdware/sc/handler/DOOPRequestHandler.java | 81 +++++++++++++++++++ .../org/bdware/sc/server/DOOPDoipServer.java | 65 +++++++++++++++ 4 files changed, 222 insertions(+), 4 deletions(-) create mode 100644 src/main/java/org/bdware/sc/handler/DOOPRepositoryHandler.java create mode 100644 src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java create mode 100644 src/main/java/org/bdware/sc/server/DOOPDoipServer.java 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 d0d546d..806a2f7 100644 --- a/src/main/java/org/bdware/sc/compiler/ap/DOOP.java +++ b/src/main/java/org/bdware/sc/compiler/ap/DOOP.java @@ -1,18 +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.node.AnnotationNode; import org.bdware.sc.node.ContractNode; import org.bdware.sc.node.FunctionNode; -import java.util.HashMap; -import java.util.Map; // 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) { - + // 通过DOOP注解,解析对应的值,并放进对应的FunctionNode中 + functionNode.setDoipOperationInfo(DoipOperationInfo.create(anno, contractNode)); + // 维护DoipOperation维 + contractNode.addDoipOperation(functionNode); } } diff --git a/src/main/java/org/bdware/sc/handler/DOOPRepositoryHandler.java b/src/main/java/org/bdware/sc/handler/DOOPRepositoryHandler.java new file mode 100644 index 0000000..f67df99 --- /dev/null +++ b/src/main/java/org/bdware/sc/handler/DOOPRepositoryHandler.java @@ -0,0 +1,71 @@ +package org.bdware.sc.handler; + +import com.google.gson.JsonPrimitive; +import org.bdware.doip.codec.doipMessage.DoipMessage; +import org.bdware.doip.codec.operations.BasicOperations; +import org.bdware.doip.endpoint.server.RepositoryHandler; +import org.bdware.sc.ContractProcess; +import org.bdware.sc.ContractResult; +import org.bdware.sc.node.ContractNode; +import org.bdware.sc.node.FunctionNode; +import org.bdware.sc.node.YjsType; + +public class DOOPRepositoryHandler implements RepositoryHandler { + private ContractNode cn; + private boolean isDoipModule; + + public DOOPRepositoryHandler(ContractNode cn) { + if(cn.getYjsType() == YjsType.DoipModule) { + isDoipModule = true; + this.cn = cn; + } + } + + @Override + public DoipMessage handleHello(DoipMessage request) { + FunctionNode fn = cn.getDoipOperation(BasicOperations.Hello); + ContractProcess.instance.executeContract(""); + return null; + } + + @Override + public DoipMessage handleListOps(DoipMessage request) { + FunctionNode fn = cn.getDoipOperation(BasicOperations.ListOps); + + return null; + } + + @Override + public DoipMessage handleCreate(DoipMessage request) { + FunctionNode fn = cn.getDoipOperation(BasicOperations.Create); + return null; + } + + @Override + public DoipMessage handleUpdate(DoipMessage request) { + FunctionNode fn = cn.getDoipOperation(BasicOperations.Update); + return null; + } + + @Override + public DoipMessage handleDelete(DoipMessage request) { + FunctionNode fn = cn.getDoipOperation(BasicOperations.Delete); + return null; + } + + @Override + public DoipMessage handleRetrieve(DoipMessage request) { + FunctionNode fn = cn.getDoipOperation(BasicOperations.Retrieve); + return null; + } + + private ContractResult invokeFunction(FunctionNode fun, BasicOperations basicOperation) { + if (fun == null) { + return new ContractResult( + ContractResult.Status.Exception, + new JsonPrimitive("DoipOperation " + basicOperation + " is not exists")); + } + + return null; + } +} diff --git a/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java b/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java new file mode 100644 index 0000000..3130380 --- /dev/null +++ b/src/main/java/org/bdware/sc/handler/DOOPRequestHandler.java @@ -0,0 +1,81 @@ +package org.bdware.sc.handler; + +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.operations.BasicOperations; +import org.bdware.doip.endpoint.server.DoipRequestHandler; +import org.bdware.doip.endpoint.server.NettyServerHandler; +import org.bdware.doip.endpoint.server.Op; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +public class DOOPRequestHandler implements DoipRequestHandler { + Map handlers; + static Logger logger = LogManager.getLogger(NettyServerHandler.class); + protected DOOPRepositoryHandler doopRepoHandler; + + public DOOPRequestHandler(DOOPRepositoryHandler doopRepoHandler) { + handlers = new HashMap<>(); + this.doopRepoHandler = doopRepoHandler; + Class handlerClass = doopRepoHandler.getClass(); + + // 建立doopRepoHandler这个类中,方法名和对应的Method的对应关系 + while (handlerClass != Object.class) { + putDoipHandlerMethod(handlerClass); + Class[] interfaces = handlerClass.getInterfaces(); + for (Class clz : interfaces) { + putDoipHandlerMethod(clz); + } + handlerClass = handlerClass.getSuperclass(); + } + } + + private void putDoipHandlerMethod(Class handlerClass) { + Method[] methods = handlerClass.getDeclaredMethods(); + for (Method m : methods) { + Op a = m.getAnnotation(Op.class); + if (a != null) { +// logger.debug("method annotation: " + a.op().getName()); +// logger.debug("method annotation: " + a.name()); + if (a.op() != BasicOperations.Extension) { + putHandler(a.op().getName(), m); + } else { + putHandler(a.name(), m); + } + } + } + } + + private void putHandler(String name, Method m) { + if (handlers.containsKey(name)) { +// logger.error("Duplicated operation handler:" + name + " methodName:" + m.getName()); + return; + } + logger.debug("[Register operation] name: " + name); + m.setAccessible(true); + handlers.put(name, m); + } + + @Override + public DoipMessage onRequest(ChannelHandlerContext ctx, DoipMessage msg) { + String str = msg.header.parameters.operation; + logger.debug("[Call operation] name: " + str); + if (str != null) { + Method m; + m = handlers.get(str); + if (m == null) m = handlers.get(BasicOperations.Unknown.getName()); + if (m != null) { + try { + return (DoipMessage) m.invoke(doopRepoHandler, msg); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + return null; + } +} diff --git a/src/main/java/org/bdware/sc/server/DOOPDoipServer.java b/src/main/java/org/bdware/sc/server/DOOPDoipServer.java new file mode 100644 index 0000000..fe0db45 --- /dev/null +++ b/src/main/java/org/bdware/sc/server/DOOPDoipServer.java @@ -0,0 +1,65 @@ +package org.bdware.sc.server; + +import org.bdware.doip.audit.EndpointConfig; +import org.bdware.doip.audit.EndpointInfo; +import org.bdware.doip.audit.client.AuditIrpClient; +import org.bdware.doip.audit.server.AuditRequestHandler; +import org.bdware.doip.audit.writer.ConfigurableAuditConfig; +import org.bdware.doip.endpoint.server.DoipListenerConfig; +import org.bdware.doip.endpoint.server.DoipServerImpl; +import org.bdware.doip.endpoint.server.DoipServiceInfo; +import org.bdware.doip.endpoint.server.RepositoryHandler; +import org.bdware.sc.ContractProcess; +import org.bdware.sc.handler.DOOPRepositoryHandler; +import org.bdware.sc.handler.DOOPRequestHandler; +import org.bdware.sc.node.ContractNode; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; + +import static org.bdware.doip.audit.EndpointConfig.defaultDOIPServerPort; +import static org.bdware.doip.audit.EndpointConfig.defaultRepoType; + +public class DOOPDoipServer extends DoipServerImpl { + private final ConfigurableAuditConfig auditConfig; + EndpointConfig config; + static AuditIrpClient irpClient; + + + public DOOPDoipServer(EndpointConfig config) { + super(resolveInfo(config)); + this.config = config; + this.auditConfig = irpClient.getAuditConfig(); + } + + private static DoipServiceInfo resolveInfo(EndpointConfig config) { + irpClient = new AuditIrpClient(config); + EndpointInfo info = irpClient.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; + } + + @Override + public void setRepositoryHandler(RepositoryHandler handler) { + Class clz = DoipServerImpl.class; + Field f = null; + try { + f = clz.getDeclaredField("requestCallback"); + f.setAccessible(true); +// f.set(this, new DOOPRequestHandler(); + } catch (Exception e) { + e.printStackTrace(); + } + } +}