commit a2fa767d8974c93cce0e8af32934d6f73c7e7f05 Author: CaiHQ Date: Thu Feb 17 17:06:10 2022 +0800 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e9e941c --- /dev/null +++ b/.gitignore @@ -0,0 +1,26 @@ +/build/ +/testoutput/ +*/build/* +# Compiled class file +*.class +.DS_Store +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..82c00c3 --- /dev/null +++ b/build.gradle @@ -0,0 +1,38 @@ +plugins { + id 'java-library' +} + +group 'com.bdware.sc' +version '1.0-SNAPSHOT' + +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + +sourceSets { + main { + java { + srcDir 'src/main/java' + } + resources { + srcDir 'src/main/resources' + } + } + test { + java { + srcDir 'src/test/java' + } + resources { + srcDir 'src/test/resources' + } + } +} + +dependencies { + api project(":cm") + api project(":front-base") + + testImplementation 'junit:junit:4.13.2' +} \ No newline at end of file diff --git a/src/main/java/org/bdware/sdk/consistency/ConsistencyPluginManager.java b/src/main/java/org/bdware/sdk/consistency/ConsistencyPluginManager.java new file mode 100644 index 0000000..44d10d6 --- /dev/null +++ b/src/main/java/org/bdware/sdk/consistency/ConsistencyPluginManager.java @@ -0,0 +1,176 @@ +package org.bdware.sdk.consistency; + +import org.apache.commons.io.FileUtils; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.bdware.sc.bean.ContractExecType; +import org.bdware.sdk.consistency.api.ContractExecutorFactory; +import org.bdware.sdk.consistency.api.context.ISDKContext; +import org.bdware.server.CMDConf; +import org.bdware.server.trustedmodel.ContractExecutor; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Modifier; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; + +public class ConsistencyPluginManager { + private static final Logger LOGGER = LogManager.getLogger(ConsistencyPluginManager.class); + + private static final String CONFIG_PATH = "cmconfig.json"; + + private ISDKContext SDKContext; + + public static void setContext(ISDKContext SDKContext) { + getInstance().SDKContext = SDKContext; + } + + public static ConsistencyPluginManager getInstance() { + return Inner.instance; + } + + public static ISDKContext getContext() { + return getInstance().SDKContext; + } + + /** + * 静态内部类单例 懒加载 + */ + private static class Inner { + private static final ConsistencyPluginManager instance = new ConsistencyPluginManager(); + } + + private final Map factoriesMap = new HashMap<>(); + + private URLClassLoader urlClassLoader; + + private ConsistencyPluginManager() { + loadPlugins(); + } + + private void loadPlugins() { + // 读取配置 + CMDConf cmdConf = getCmdConf(); + if (cmdConf.consistencyPlugins.isEmpty()) { + LOGGER.info("Consistency SDK: no plugin detected"); + return; + } + + // 装配urlClassLoader + String[] consistencyPluginURLs = cmdConf.consistencyPlugins.split(","); + URL[] urls = new URL[consistencyPluginURLs.length]; + for (int i = 0; i < consistencyPluginURLs.length; i++) { + try { + if(consistencyPluginURLs[i].endsWith(".jar")) { + urls[i] = new URL("file:" + consistencyPluginURLs[i]); + } + } catch (MalformedURLException e) { + e.printStackTrace(); + } + } + urlClassLoader = new URLClassLoader(urls, Thread.currentThread().getContextClassLoader()); + + // 扫描jar包 转载插件 + for (String path : consistencyPluginURLs) { + parseJar(path); + } + System.out.println(); + } + + private CMDConf getCmdConf() { + File confFile = new File(CONFIG_PATH); + File confTemplate = new File(CONFIG_PATH + ".template"); + if (!confTemplate.exists()) { + CMDConf conf = new CMDConf(); + conf.write(confTemplate.getAbsolutePath()); + } + if (!confFile.exists() && confTemplate.exists()) { + try { + FileUtils.copyFile(confTemplate, confFile); + } catch (IOException e) { + e.printStackTrace(); + } + } + return CMDConf.parseFile(CONFIG_PATH); + } + + /** + * 从jar包中解析ContractExecutorFactory子类 + * + * @param path + */ + private void parseJar(String path) { + try { + File file = new File(path); + if (file.exists()) { + JarFile jarFile = new JarFile(file.getCanonicalPath()); + Enumeration enumeration = jarFile.entries(); + while (enumeration.hasMoreElements()) { + JarEntry entry = enumeration.nextElement(); + String entryName = entry.getName(); + if (entryName.endsWith(".class") && entryName.lastIndexOf(".") >= entryName.lastIndexOf("/")) { + try { + String className = entryName.replaceAll("/", ".").substring(0, entryName.length() - 6); + Class clazz = urlClassLoader.loadClass(className); + // ContractExecutorFactory子类 && 非抽象类 + if (ContractExecutorFactory.class.isAssignableFrom(clazz) && !Modifier.isAbstract(clazz.getModifiers())) { + ContractExecutorFactory factory = (ContractExecutorFactory)(clazz.newInstance()); + factoriesMap.put(factory.getExecutorName(), factory); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + public ContractExecutor createContractExecutor(String name, Map args) { + return factoriesMap.containsKey(name) ? factoriesMap.get(name).getInstance(args) : null; + } + + @Deprecated + public ContractExecutor createContractExecutor(ContractExecType type, Map args) { + String name = ""; + switch (type) { + case Sole: + name = "Sole"; + break; + case PBFT: + name = "PBFT"; + break; + case Sharding: + name = "Sharding"; + break; + case RequestOnce: + name = "RequestOnce"; + break; + case ResponseOnce: + name = "ResponseOnce"; + break; + case SelfAdaptiveSharding: + name = "SASharding"; + break; + case RequestAllResponseAll: + name = "RARA"; + break; + case RequestAllResponseHalf: + name = "RARH"; + break; + case RequestAllResponseFirst: + name = "RARF"; + break; + } + return createContractExecutor(name, args); + } +} diff --git a/src/main/java/org/bdware/sdk/consistency/api/ContractExecutorFactory.java b/src/main/java/org/bdware/sdk/consistency/api/ContractExecutorFactory.java new file mode 100644 index 0000000..50d8e95 --- /dev/null +++ b/src/main/java/org/bdware/sdk/consistency/api/ContractExecutorFactory.java @@ -0,0 +1,11 @@ +package org.bdware.sdk.consistency.api; + +import org.bdware.server.trustedmodel.ContractExecutor; + +import java.util.Map; + +public interface ContractExecutorFactory { + String getExecutorName(); + + ContractExecutor getInstance(Map args); +} diff --git a/src/main/java/org/bdware/sdk/consistency/api/NotifiableResultMerger.java b/src/main/java/org/bdware/sdk/consistency/api/NotifiableResultMerger.java new file mode 100644 index 0000000..4621fb2 --- /dev/null +++ b/src/main/java/org/bdware/sdk/consistency/api/NotifiableResultMerger.java @@ -0,0 +1,7 @@ +package org.bdware.sdk.consistency.api; + +public interface NotifiableResultMerger { + String getContractID(); + + String getInfo(); +} diff --git a/src/main/java/org/bdware/sdk/consistency/api/context/ICMActions.java b/src/main/java/org/bdware/sdk/consistency/api/context/ICMActions.java new file mode 100644 index 0000000..cac77f3 --- /dev/null +++ b/src/main/java/org/bdware/sdk/consistency/api/context/ICMActions.java @@ -0,0 +1,7 @@ +package org.bdware.sdk.consistency.api.context; + +import org.bdware.sc.ContractManager; + +public interface ICMActions { + ContractManager getManager(); +} diff --git a/src/main/java/org/bdware/sdk/consistency/api/context/IGlobalConf.java b/src/main/java/org/bdware/sdk/consistency/api/context/IGlobalConf.java new file mode 100644 index 0000000..2682326 --- /dev/null +++ b/src/main/java/org/bdware/sdk/consistency/api/context/IGlobalConf.java @@ -0,0 +1,9 @@ +package org.bdware.sdk.consistency.api.context; + +import org.zz.gmhelper.SM2KeyPair; + +public interface IGlobalConf { + String getNodeID(); + + SM2KeyPair getKeyPair(); +} diff --git a/src/main/java/org/bdware/sdk/consistency/api/context/IMasterServerRecoverMechAction.java b/src/main/java/org/bdware/sdk/consistency/api/context/IMasterServerRecoverMechAction.java new file mode 100644 index 0000000..3e6e57f --- /dev/null +++ b/src/main/java/org/bdware/sdk/consistency/api/context/IMasterServerRecoverMechAction.java @@ -0,0 +1,11 @@ +package org.bdware.sdk.consistency.api.context; + +import org.bdware.sc.units.RecoverFlag; + +import java.util.Map; + +public interface IMasterServerRecoverMechAction { + Map> getRecoverStatusMap(); + + void restartContractFromCommonMode(String nodeID, String contractID); +} diff --git a/src/main/java/org/bdware/sdk/consistency/api/context/IMasterServerTCPAction.java b/src/main/java/org/bdware/sdk/consistency/api/context/IMasterServerTCPAction.java new file mode 100644 index 0000000..aef3b76 --- /dev/null +++ b/src/main/java/org/bdware/sdk/consistency/api/context/IMasterServerTCPAction.java @@ -0,0 +1,12 @@ +package org.bdware.sdk.consistency.api.context; + +import org.bdware.sc.units.RequestCache; +import org.bdware.server.action.SyncResult; + +import java.util.Map; + +public interface IMasterServerTCPAction { + SyncResult getSync(); + + Map getReqCache(); +} diff --git a/src/main/java/org/bdware/sdk/consistency/api/context/INetworkManager.java b/src/main/java/org/bdware/sdk/consistency/api/context/INetworkManager.java new file mode 100644 index 0000000..b29cde9 --- /dev/null +++ b/src/main/java/org/bdware/sdk/consistency/api/context/INetworkManager.java @@ -0,0 +1,11 @@ +package org.bdware.sdk.consistency.api.context; + +import org.bdware.sc.conn.ResultCallback; + +public interface INetworkManager { + void sendToAgent(String pubkey, String content); + + boolean hasAgentConnection(String pubKey); + + ResultCallback createResultCallback(String requestID, ResultCallback rc, int count); +} diff --git a/src/main/java/org/bdware/sdk/consistency/api/context/ISDKContext.java b/src/main/java/org/bdware/sdk/consistency/api/context/ISDKContext.java new file mode 100644 index 0000000..1b9008f --- /dev/null +++ b/src/main/java/org/bdware/sdk/consistency/api/context/ISDKContext.java @@ -0,0 +1,13 @@ +package org.bdware.sdk.consistency.api.context; + +public interface ISDKContext { + IMasterServerTCPAction getMasterServerTCPAction(); + + INetworkManager getNetworkManager(); + + ICMActions getCMActions(); + + IMasterServerRecoverMechAction getMasterServerRecoverMechAction(); + + IGlobalConf getGlobalConf(); +}