add first version DOOPRequestHandler

This commit is contained in:
haoeliu@foxmail.com 2022-11-15 11:47:26 +08:00
parent a35ca147e3
commit 65aa9fd78d
4 changed files with 222 additions and 4 deletions

View File

@ -1,18 +1,19 @@
package org.bdware.sc.compiler.ap; 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.compiler.AnnotationProcessor;
import org.bdware.sc.node.AnnotationNode; import org.bdware.sc.node.AnnotationNode;
import org.bdware.sc.node.ContractNode; import org.bdware.sc.node.ContractNode;
import org.bdware.sc.node.FunctionNode; 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 // DOOP is designed for DoipModule which contains specific functions for RepositoryHandler
public class DOOP extends AnnotationProcessor { public class DOOP extends AnnotationProcessor {
@Override @Override
public void processFunction(AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) { public void processFunction(AnnotationNode anno, ContractNode contractNode, FunctionNode functionNode) {
// 通过DOOP注解解析对应的值并放进对应的FunctionNode中
functionNode.setDoipOperationInfo(DoipOperationInfo.create(anno, contractNode));
// 维护DoipOperation维
contractNode.addDoipOperation(functionNode);
} }
} }

View File

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

View File

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

View File

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