From 83008583db1a07a7e3760ed1c8033ca64c00d587 Mon Sep 17 00:00:00 2001 From: "Frank.R.Wu" Date: Fri, 5 Nov 2021 14:52:27 +0800 Subject: [PATCH] feat: update args in CMDConf make plugin related args in CMDConf to String; move plugin related fields to related handlers from CMDConf, and make them don't depend on CMDConf --- .../java/org/bdware/server/CMHttpServer.java | 45 +++++++++++-------- .../org/bdware/server/action/FileActions.java | 40 ++++++++--------- .../org/bdware/server/http/CMHttpHandler.java | 19 ++++---- .../client/NodeCenterClientHandler.java | 19 ++++---- .../server/tcp/MasterClientFrameHandler.java | 36 ++++++--------- .../ws/ContractManagerFrameHandler.java | 17 +++---- .../units/tcp/TCPClientFrameHandler.java | 18 ++++---- 7 files changed, 91 insertions(+), 103 deletions(-) diff --git a/src/main/java/org/bdware/server/CMHttpServer.java b/src/main/java/org/bdware/server/CMHttpServer.java index c707ed9..9cb2416 100644 --- a/src/main/java/org/bdware/server/CMHttpServer.java +++ b/src/main/java/org/bdware/server/CMHttpServer.java @@ -30,8 +30,11 @@ import org.bdware.sc.util.ExceptionUtil; import org.bdware.server.action.FileActions; import org.bdware.server.doip.ContractRepositoryMain; import org.bdware.server.http.CMHttpHandler; +import org.bdware.server.nodecenter.client.NodeCenterClientHandler; +import org.bdware.server.tcp.MasterClientFrameHandler; import org.bdware.server.ws.ContractManagerFrameHandler; import org.bdware.units.NetworkManager; +import org.bdware.units.tcp.TCPClientFrameHandler; import java.io.*; import java.lang.reflect.Field; @@ -45,14 +48,19 @@ 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( "./ContractManagerDB", CMTables.LocalNodeLogDB.toString()); + public static URLClassLoader pluginLoader; 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; @@ -113,12 +121,21 @@ public class CMHttpServer { LOGGER.debug(ExceptionUtil.exceptionToString(e)); } } + + // cmi file ContractClient.cmi = cmdConf.cmi; FileWriter fw = new FileWriter("CMI"); fw.write(ContractClient.cmi); fw.close(); LOGGER.info("write CMI=" + ContractClient.cmi + " to ./CMI"); FileActions.setTextFileSuffixes(cmdConf.textFileSuffixes); + + // plugins + CMHttpHandler.wsPluginActions = cmdConf.wsPluginActions.split(","); + MasterClientFrameHandler.clientToAgentPlugins = cmdConf.clientToAgentPlugins.split(","); + NodeCenterClientHandler.clientToClusterPlugins = cmdConf.clientToClusterPlugins.split(","); + TCPClientFrameHandler.tcpPlugins = cmdConf.tcpPlugins.split(","); + if (!cmdConf.debug.isEmpty()) { try { String[] classes = cmdConf.debug.split(","); @@ -135,12 +152,6 @@ 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); @@ -169,7 +180,7 @@ public class CMHttpServer { if (!confFile.exists() && confTemplate.exists()) { FileUtils.copyFile(confTemplate, confFile); } - cmdConf = CMDConf.parseConf(CONFIG_PATH); + CMDConf cmdConf = CMDConf.parseConf(CONFIG_PATH); // addDirToPath(new File("./dynamicLibrary").getAbsolutePath()); @@ -215,10 +226,10 @@ public class CMHttpServer { } } - start(cmdConf.ip, cmdConf.servicePort, cmdConf.doipPort,cmdConf); + start(cmdConf.ip, cmdConf.servicePort, cmdConf.doipPort, cmdConf); } - public static void start(String ip, int port, int doipPort,CMDConf cmdConf) 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)); @@ -238,22 +249,18 @@ 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"); - } - }); + File[] pluginJar = new File("./pluginLib/") + .listFiles(pathname -> 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/action/FileActions.java b/src/main/java/org/bdware/server/action/FileActions.java index b10905f..c0e863f 100644 --- a/src/main/java/org/bdware/server/action/FileActions.java +++ b/src/main/java/org/bdware/server/action/FileActions.java @@ -1191,24 +1191,24 @@ public class FileActions { if (json.has("projectDOI")) manifestFout.write( ("{\n \"main\":\"" - + fileName - + ".yjs\",\n" - + " \"doipFlag\":true,\n" - + " \"doi\":\"" - + json.get("projectDOI").getAsString() - + "\",\n" - + " \"authInfoPersistDOI\":\"" - + authInfoPersistDOI - + "\"\n}") + + fileName + + ".yjs\",\n" + + " \"doipFlag\":true,\n" + + " \"doi\":\"" + + json.get("projectDOI").getAsString() + + "\",\n" + + " \"authInfoPersistDOI\":\"" + + authInfoPersistDOI + + "\"\n}") .getBytes()); else manifestFout.write( ("{\n \"main\":\"" - + fileName - + ".yjs\",\n" - + " \"authInfoPersistDOI\":\"" - + authInfoPersistDOI - + "\"\n}") + + fileName + + ".yjs\",\n" + + " \"authInfoPersistDOI\":\"" + + authInfoPersistDOI + + "\"\n}") .getBytes()); YJSPacker.unpack( @@ -1219,18 +1219,18 @@ public class FileActions { ("import \"naiveDAC/naiveDAC.yjs\"\n\n").getBytes()); mainyjsFout.write( ("contract " - + fileName - + "{\n function onCreate(){\n initDAC(requester);\n }\n}") + + fileName + + "{\n function onCreate(){\n initDAC(requester);\n }\n}") .getBytes()); break; default: if (json.has("projectDOI")) manifestFout.write( ("{\n \"main\":\"" - + fileName - + ".yjs\",\n \"doipFlag\":true,\n \"doi\":\"" - + json.get("projectDOI").getAsString() - + "\"\n}") + + fileName + + ".yjs\",\n \"doipFlag\":true,\n \"doi\":\"" + + json.get("projectDOI").getAsString() + + "\"\n}") .getBytes()); else manifestFout.write( diff --git a/src/main/java/org/bdware/server/http/CMHttpHandler.java b/src/main/java/org/bdware/server/http/CMHttpHandler.java index f62df0d..4e1f3b6 100644 --- a/src/main/java/org/bdware/server/http/CMHttpHandler.java +++ b/src/main/java/org/bdware/server/http/CMHttpHandler.java @@ -18,8 +18,6 @@ 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; @@ -42,6 +40,7 @@ import static org.bdware.server.CMHttpServer.pluginLoader; public class CMHttpHandler extends SimpleChannelInboundHandler { private static final String UNSUPPORTED_ACTION = "{\"msg\":\"unsupported action\"}"; private static final Logger LOGGER = LogManager.getLogger(CMHttpHandler.class); + public static String[] wsPluginActions; public final ActionExecutor actionExecutor; URIHandler uriHandlers; @@ -117,11 +116,9 @@ public class CMHttpHandler extends SimpleChannelInboundHandler { } }; - if (CMHttpServer.cmdConf.wsPluginActions != null){ - for (String str:CMHttpServer.cmdConf.wsPluginActions){ - Object obj = createInstanceByClzName(str); - actionExecutor.appendHandler(obj); - } + for (String str : wsPluginActions) { + Object obj = createInstanceByClzName(str); + actionExecutor.appendHandler(obj); } uriHandlers = new URIHandler(); @@ -133,14 +130,16 @@ public class CMHttpHandler extends SimpleChannelInboundHandler { uriHandlers.register(FileActions.class); uriHandlers.printURIHandlers(); } - private Object createInstanceByClzName(String clzName){ + + private Object createInstanceByClzName(String clzName) { try { - Class clz = Class.forName(clzName,true,pluginLoader); + Class clz = Class.forName(clzName, true, pluginLoader); return clz.newInstance(); - }catch(Exception e) { + } 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 c0db4b5..837de19 100644 --- a/src/main/java/org/bdware/server/nodecenter/client/NodeCenterClientHandler.java +++ b/src/main/java/org/bdware/server/nodecenter/client/NodeCenterClientHandler.java @@ -12,8 +12,6 @@ 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; @@ -29,6 +27,7 @@ import static org.bdware.server.CMHttpServer.pluginLoader; @Sharable public class NodeCenterClientHandler extends SimpleChannelInboundHandler { private static final Logger LOGGER = LogManager.getLogger(NodeCenterClientHandler.class); + public static String[] clientToClusterPlugins; public boolean hasPermission; public NodeCenterClientController controller; public Map distributeReqMap = new ConcurrentHashMap<>(); @@ -45,19 +44,17 @@ 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); - } + for (String str : clientToClusterPlugins) { + Object obj = createInstanceByClzName(str); + ae.appendHandler(obj); } } - private Object createInstanceByClzName(String clzName){ + private Object createInstanceByClzName(String clzName) { try { - Class clz = Class.forName(clzName,true,pluginLoader); + Class clz = Class.forName(clzName, true, pluginLoader); return clz.newInstance(); - }catch(Exception e) { + } catch (Exception e) { return null; } } @@ -94,7 +91,7 @@ public class NodeCenterClientHandler extends SimpleChannelInboundHandler } @Override - protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { + protected void channelRead0(ChannelHandlerContext ctx, Object msg) { ByteBuf bb = (ByteBuf) msg; byte[] bytes = null; try { diff --git a/src/main/java/org/bdware/server/tcp/MasterClientFrameHandler.java b/src/main/java/org/bdware/server/tcp/MasterClientFrameHandler.java index f3ff92a..7859a92 100644 --- a/src/main/java/org/bdware/server/tcp/MasterClientFrameHandler.java +++ b/src/main/java/org/bdware/server/tcp/MasterClientFrameHandler.java @@ -1,7 +1,6 @@ package org.bdware.server.tcp; import com.google.gson.JsonObject; -import com.google.gson.JsonParser; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufInputStream; import io.netty.buffer.Unpooled; @@ -13,8 +12,6 @@ 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; @@ -33,7 +30,8 @@ import static org.bdware.server.CMHttpServer.pluginLoader; @ChannelHandler.Sharable public class MasterClientFrameHandler extends SimpleChannelInboundHandler { private static final Logger LOGGER = LogManager.getLogger(MasterClientFrameHandler.class); - static ExecutorService executorService = Executors.newFixedThreadPool(10); + private static final ExecutorService executorService = Executors.newFixedThreadPool(10); + public static String[] clientToAgentPlugins; public String pubKey; public ActionExecutor ae; MasterClientTCPAction actions; @@ -49,22 +47,19 @@ 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); - } + ae = new ActionExecutor<>( + executorService, actions, recoverActions, transferActions); + for (String str : clientToAgentPlugins) { + Object obj = createInstanceByClzName(str); + ae.appendHandler(obj); } } - private Object createInstanceByClzName(String clzName){ + private Object createInstanceByClzName(String clzName) { try { - Class clz = Class.forName(clzName,true,pluginLoader); + Class clz = Class.forName(clzName, true, pluginLoader); return clz.newInstance(); - }catch(Exception e) { + } catch (Exception e) { return null; } } @@ -113,15 +108,11 @@ public class MasterClientFrameHandler extends SimpleChannelInboundHandler clz = Class.forName(clzName, true, pluginLoader); return clz.newInstance(); - }catch(Exception e) { + } catch (Exception e) { return null; } } @@ -187,7 +185,6 @@ public class ContractManagerFrameHandler extends SimpleChannelInboundHandler { private static final Logger LOGGER = LogManager.getLogger(TCPClientFrameHandler.class); + public static String[] tcpPlugins; ActionExecutor ae; ChannelHandlerContext ctx; private Channel channel; @@ -30,19 +30,17 @@ 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); - } + for (String str : tcpPlugins) { + Object obj = createInstanceByClzName(str); + ae.appendHandler(obj); } } - private Object createInstanceByClzName(String clzName){ + private Object createInstanceByClzName(String clzName) { try { - Class clz = Class.forName(clzName,true,pluginLoader); + Class clz = Class.forName(clzName, true, pluginLoader); return clz.newInstance(); - }catch(Exception e) { + } catch (Exception e) { return null; } } @@ -92,7 +90,7 @@ public class TCPClientFrameHandler extends SimpleChannelInboundHandler bb.readBytes(bytes); unitMessage = UnitMessage.parseFrom(bytes); } catch (InvalidProtocolBufferException e) { - LOGGER.error("[TCPClientFrameHandler] receive msg" + bb.toString()); + LOGGER.error("receive msg " + bb); e.printStackTrace(); return; }