diff --git a/cmconfig.readme.md b/cmconfig.readme.md new file mode 100644 index 0000000..a36be48 --- /dev/null +++ b/cmconfig.readme.md @@ -0,0 +1,5 @@ +# 配置项 +## wsPluginActions: WS端,包括contractManagerFrameHandler和CMHttpHandler +## clientToAgentPlugins: client -> cluster 包括MasterClientFrameHandler +## clientToClusterPlugins:[], client -> NodeCenterClientHandler +## tcpPlugins:tcp 包括TcpserverFrameHandler \ No newline at end of file diff --git a/src/main/java/org/bdware/server/CMHttpServer.java b/src/main/java/org/bdware/server/CMHttpServer.java index 6b186b6..c707ed9 100644 --- a/src/main/java/org/bdware/server/CMHttpServer.java +++ b/src/main/java/org/bdware/server/CMHttpServer.java @@ -35,6 +35,9 @@ import org.bdware.units.NetworkManager; import java.io.*; import java.lang.reflect.Field; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; import java.util.Date; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; @@ -42,6 +45,7 @@ import java.util.concurrent.atomic.AtomicInteger; public class CMHttpServer { private static final Logger LOGGER = LogManager.getLogger(CMHttpServer.class); private static final String CONFIG_PATH = "cmconfig.json"; + public static CMDConf cmdConf; public static EventLoopGroup workerGroup = new NioEventLoopGroup(); public static MultiIndexTimeRocksDBUtil nodeLogDB = new MultiIndexTimeRocksDBUtil( @@ -49,11 +53,6 @@ public class CMHttpServer { private static SslContext sslContext = null; final String PATH = "/SCIDE/SCExecutor"; private final int port; - - private CMHttpServer(int port) { - this.port = port; - } - private static void parseConf(CMDConf cmdConf) throws IOException { if (cmdConf.disableDoRepo) { DoConfig.callContractUsingDOI = false; @@ -64,6 +63,7 @@ public class CMHttpServer { GlobalConfigurations.certPath = conf[0]; GlobalConfigurations.certPassword = conf[1]; } + if (!cmdConf.doipUserHandle.isEmpty()) { GlobalConfigurations.User_Handle = cmdConf.doipUserHandle; } @@ -135,6 +135,12 @@ public class CMHttpServer { } } + public static URLClassLoader pluginLoader; + + private CMHttpServer(int port) { + this.port = port; + } + private static void addDirToPath(String s) { try { LOGGER.info("add to path: " + s); @@ -163,7 +169,8 @@ public class CMHttpServer { if (!confFile.exists() && confTemplate.exists()) { FileUtils.copyFile(confTemplate, confFile); } - CMDConf cmdConf = CMDConf.parseConf(CONFIG_PATH); + cmdConf = CMDConf.parseConf(CONFIG_PATH); + // addDirToPath(new File("./dynamicLibrary").getAbsolutePath()); // for compatibility @@ -208,10 +215,10 @@ public class CMHttpServer { } } - start(cmdConf.ip, cmdConf.servicePort, cmdConf.doipPort); + start(cmdConf.ip, cmdConf.servicePort, cmdConf.doipPort,cmdConf); } - public static void start(String ip, int port, int doipPort) throws IOException { + public static void start(String ip, int port, int doipPort,CMDConf cmdConf) throws IOException { if (DoConfig.callContractUsingDOI) { ContractManager.threadPool.execute( () -> ContractRepositoryMain.start("tcp://" + ip + ":" + doipPort)); @@ -231,6 +238,22 @@ public class CMHttpServer { // EpollServerSocketChannel // ContractManager.reconnectPort = (port - 18000) * 30 + 1630; // if (ContractManager.reconnectPort < 0) ContractManager.reconnectPort = 1630; + File[] pluginJar = new File("./pluginLib/").listFiles(new FileFilter() { + @Override + public boolean accept(File pathname) { + return pathname.getName().endsWith(".jar"); + } + }); + URL[] urls = new URL[pluginJar.length]; + for (int i=0;i= 18000 && port < 18100) { ContractManager.cPort = new ContractPort(1615 + (port - 18000) * 30); } else { diff --git a/src/main/java/org/bdware/server/http/CMHttpHandler.java b/src/main/java/org/bdware/server/http/CMHttpHandler.java index e39af50..f62df0d 100644 --- a/src/main/java/org/bdware/server/http/CMHttpHandler.java +++ b/src/main/java/org/bdware/server/http/CMHttpHandler.java @@ -18,6 +18,8 @@ import org.bdware.sc.db.KeyValueDBUtil; import org.bdware.sc.db.TimeDBUtil; import org.bdware.sc.util.ExceptionUtil; import org.bdware.sc.util.JsonUtil; +import org.bdware.server.CMDConf; +import org.bdware.server.CMHttpServer; import org.bdware.server.GRPCPool; import org.bdware.server.action.*; import org.bdware.server.permission.Role; @@ -34,6 +36,7 @@ import java.util.List; import java.util.Map; import static io.netty.handler.codec.http.HttpResponseStatus.OK; +import static org.bdware.server.CMHttpServer.pluginLoader; @Sharable public class CMHttpHandler extends SimpleChannelInboundHandler { @@ -113,6 +116,14 @@ public class CMHttpHandler extends SimpleChannelInboundHandler { return flag && flag2; } }; + + if (CMHttpServer.cmdConf.wsPluginActions != null){ + for (String str:CMHttpServer.cmdConf.wsPluginActions){ + Object obj = createInstanceByClzName(str); + actionExecutor.appendHandler(obj); + } + } + uriHandlers = new URIHandler(); DOIPOverHttpHandler doipOverHttpHandler = new DOIPOverHttpHandler(); uriHandlers.register(this); @@ -122,7 +133,14 @@ public class CMHttpHandler extends SimpleChannelInboundHandler { uriHandlers.register(FileActions.class); uriHandlers.printURIHandlers(); } - + private Object createInstanceByClzName(String clzName){ + try { + Class clz = Class.forName(clzName,true,pluginLoader); + return clz.newInstance(); + }catch(Exception e) { + return null; + } + } @Override public void channelReadComplete(ChannelHandlerContext ctx) { ctx.flush(); diff --git a/src/main/java/org/bdware/server/nodecenter/client/NodeCenterClientHandler.java b/src/main/java/org/bdware/server/nodecenter/client/NodeCenterClientHandler.java index d54a528..c0db4b5 100644 --- a/src/main/java/org/bdware/server/nodecenter/client/NodeCenterClientHandler.java +++ b/src/main/java/org/bdware/server/nodecenter/client/NodeCenterClientHandler.java @@ -12,6 +12,8 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.bdware.sc.conn.ResultCallback; import org.bdware.sc.util.JsonUtil; +import org.bdware.server.CMDConf; +import org.bdware.server.CMHttpServer; import org.bdware.server.GlobalConf; import org.bdware.server.action.ActionExecutor; import org.bdware.server.ws.ContractManagerFrameHandler; @@ -22,6 +24,8 @@ import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import static org.bdware.server.CMHttpServer.pluginLoader; + @Sharable public class NodeCenterClientHandler extends SimpleChannelInboundHandler { private static final Logger LOGGER = LogManager.getLogger(NodeCenterClientHandler.class); @@ -41,6 +45,21 @@ public class NodeCenterClientHandler extends SimpleChannelInboundHandler ae = new ActionExecutor<>(ContractManagerFrameHandler.executorService, controller); isConnected = false; hasPermission = false; + if (CMHttpServer.cmdConf.clientToClusterPlugins != null){ + for (String str:CMHttpServer.cmdConf.clientToAgentPlugins){ + Object obj = createInstanceByClzName(str); + ae.appendHandler(obj); + } + } + } + + private Object createInstanceByClzName(String clzName){ + try { + Class clz = Class.forName(clzName,true,pluginLoader); + return clz.newInstance(); + }catch(Exception e) { + return null; + } } @Override diff --git a/src/main/java/org/bdware/server/tcp/MasterClientFrameHandler.java b/src/main/java/org/bdware/server/tcp/MasterClientFrameHandler.java index cbbaa03..f3ff92a 100644 --- a/src/main/java/org/bdware/server/tcp/MasterClientFrameHandler.java +++ b/src/main/java/org/bdware/server/tcp/MasterClientFrameHandler.java @@ -13,6 +13,8 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.bdware.sc.conn.ResultCallback; import org.bdware.sc.util.JsonUtil; +import org.bdware.server.CMDConf; +import org.bdware.server.CMHttpServer; import org.bdware.server.CongestionControl; import org.bdware.server.action.ActionExecutor; import org.bdware.server.action.p2p.MasterClientRecoverMechAction; @@ -26,6 +28,8 @@ import java.io.PrintStream; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import static org.bdware.server.CMHttpServer.pluginLoader; + @ChannelHandler.Sharable public class MasterClientFrameHandler extends SimpleChannelInboundHandler { private static final Logger LOGGER = LogManager.getLogger(MasterClientFrameHandler.class); @@ -48,8 +52,24 @@ public class MasterClientFrameHandler extends SimpleChannelInboundHandler( executorService, actions, recoverActions, transferActions); + if (CMHttpServer.cmdConf.clientToAgentPlugins != null){ + for (String str:CMHttpServer.cmdConf.clientToAgentPlugins){ + Object obj = createInstanceByClzName(str); + ae.appendHandler(obj); + } + } } + private Object createInstanceByClzName(String clzName){ + try { + Class clz = Class.forName(clzName,true,pluginLoader); + return clz.newInstance(); + }catch(Exception e) { + return null; + } + } + + public void updateContractID2Client(String contract) { // 如果该合约正在和旧的master保持连接 if (MasterClientTCPAction.contractID2MasterInfo.containsKey(contract)) { diff --git a/src/main/java/org/bdware/server/ws/ContractManagerFrameHandler.java b/src/main/java/org/bdware/server/ws/ContractManagerFrameHandler.java index 3edd3b2..4a907d9 100644 --- a/src/main/java/org/bdware/server/ws/ContractManagerFrameHandler.java +++ b/src/main/java/org/bdware/server/ws/ContractManagerFrameHandler.java @@ -10,6 +10,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.bdware.sc.conn.ResultCallback; import org.bdware.sc.util.JsonUtil; +import org.bdware.server.CMDConf; import org.bdware.server.CMHttpServer; import org.bdware.server.CongestionControl; import org.bdware.server.GRPCPool; @@ -21,6 +22,8 @@ import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import static org.bdware.server.CMHttpServer.pluginLoader; + public class ContractManagerFrameHandler extends SimpleChannelInboundHandler { private static final Logger LOGGER = LogManager.getLogger(ContractManagerFrameHandler.class); public static ExecutorService executorService = Executors.newFixedThreadPool(10); @@ -97,6 +100,21 @@ public class ContractManagerFrameHandler extends SimpleChannelInboundHandler { private static final Logger LOGGER = LogManager.getLogger(TCPClientFrameHandler.class); @@ -27,6 +30,21 @@ public class TCPClientFrameHandler extends SimpleChannelInboundHandler public TCPClientFrameHandler(String peerId) { peer = peerId; // heartBeat = new HeartBeatProtocol(peer); + if (CMHttpServer.cmdConf.tcpPlugins != null){ + for (String str:CMHttpServer.cmdConf.tcpPlugins){ + Object obj = createInstanceByClzName(str); + ae.appendHandler(obj); + } + } + } + + private Object createInstanceByClzName(String clzName){ + try { + Class clz = Class.forName(clzName,true,pluginLoader); + return clz.newInstance(); + }catch(Exception e) { + return null; + } } @Override