diff --git a/backend/src/main/java/org/bdware/sc/controlproxy/TopologyCollector.java b/backend/src/main/java/org/bdware/sc/controlproxy/TopologyCollector.java index 40eaf8e..037bca6 100644 --- a/backend/src/main/java/org/bdware/sc/controlproxy/TopologyCollector.java +++ b/backend/src/main/java/org/bdware/sc/controlproxy/TopologyCollector.java @@ -15,6 +15,8 @@ import java.io.PrintStream; import java.net.URI; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; public class TopologyCollector { @@ -102,12 +104,12 @@ public class TopologyCollector { } graph.pruneEdges(); return graph.ret; - }catch ( Exception e){ + } catch (Exception e) { List ret = new ArrayList<>(); JsonObject errorInfo = new JsonObject(); ByteArrayOutputStream bo = new ByteArrayOutputStream(); e.printStackTrace(new PrintStream(bo)); - errorInfo.addProperty("stacktrace",bo.toString()); + errorInfo.addProperty("stacktrace", bo.toString()); return ret; } } @@ -135,7 +137,8 @@ public class TopologyCollector { static TopologyGraph cachedGraph; public static boolean isCacheValid() { - if (System.currentTimeMillis() - lastUpdate < 10000) { + if (cachedGraph != null) return true; + if (System.currentTimeMillis() - lastUpdate < 30000) { return true; } return false; @@ -152,18 +155,38 @@ public class TopologyCollector { return "done"; } - public synchronized static List getLevelTopology(int arg) { + public void reflushCache() { + + } + + public static List getLevelTopology(int arg) { if (isCacheValid()) { LOGGER.info("getLevelTopology use Cache!"); + reflushCacheAsync(arg); return cachedGraph.ret; } + reflushCacheSync(arg); + return cachedGraph.ret; + } + + static ExecutorService pool = Executors.newFixedThreadPool(2); + + private static void reflushCacheAsync(int arg) { + pool.execute(new Runnable() { + @Override + public void run() { + reflushCacheSync(2); + } + }); + } + + private synchronized static void reflushCacheSync(int arg) { LOGGER.info("getLevelTopology refresh"); List points = getCurrentTopology(); TopologyGraph graph = new TopologyGraph(); graph.addGraph(points); if (arg > 0) { ContractResult nextRouters = executeContract(RepoProxy.router, "listLRS", new JsonPrimitive("a")); - JsonArray routersArray = nextRouters.result.getAsJsonObject().get("data").getAsJsonArray(); for (JsonElement je : routersArray) { if (!je.getAsJsonObject().get("doId").getAsString().startsWith(points.get(0).get("doId").getAsString())) { @@ -186,6 +209,7 @@ public class TopologyCollector { } lastUpdate = System.currentTimeMillis(); cachedGraph = graph; - return graph.ret; } + + } diff --git a/backend/yjs/ControlProxy.yjs b/backend/yjs/ControlProxy.yjs index d845708..7d6dcc8 100644 --- a/backend/yjs/ControlProxy.yjs +++ b/backend/yjs/ControlProxy.yjs @@ -136,11 +136,11 @@ contract ShanxiControlProxy { return org.bdware.sc.controlproxy.TopologyCollector.getLevelTopology(arg/1.0); } - //export function getAllTopology(arg) { - // if (arg==null|| arg.length==0) { - // return org.bdware.sc.controlproxy.TopologyCollector.getLevelTopology(2); - // } else return org.bdware.sc.controlproxy.TopologyCollector.getLevelTopology(arg/1.0); - //} + export function getAllTopology(arg) { + if (arg==null|| arg.length==0) { + return org.bdware.sc.controlproxy.TopologyCollector.getLevelTopology(2); + } else return org.bdware.sc.controlproxy.TopologyCollector.getLevelTopology(arg/1.0); + } //总体拓扑图 //@范博 export function setTopologyLevel(arg) { diff --git a/backend/yjs/ShanxiGlobalRouter.yjs b/backend/yjs/ShanxiGlobalRouter.yjs index 4a94eb4..62c7b10 100644 --- a/backend/yjs/ShanxiGlobalRouter.yjs +++ b/backend/yjs/ShanxiGlobalRouter.yjs @@ -1,137 +1,260 @@ -module ShanxiGlobalRouter{ - export function getAllTopology() { - return executeContract("GlobalRouter","getAllTopology",arg).result; - } +module ShanxiGlobalRouter { export function getRouterInfo(arg) { - if (!checkPermission(requester)) { + if (! checkPermission(requester)) { YancloudUtil.exceptionReturn({ - "msg":"no permission", "code":1 + "msg" : "no permission", "code" : 1 }); } - var ret = executeContract("GlobalRouter", "getRouterInfo", arg); + var ret = executeContract(Global.router, "getRouterInfo", arg); var obj = ret.result; - if (obj.result=="success" || obj.result == undefined) { + if (obj.result == "success") { obj.result = undefined; obj.code = 0; } else { YancloudUtil.exceptionReturn({ - "msg":obj.data, "code":1 + "msg" : obj.data, "code" : 1 }); } obj.data.status = "online"; - obj.data.date-=1000*60*60*24*30*8; obj.data.name = "山西广域数联网"; return obj; } export function listLRS(arg) { - if (!checkPermission(requester)) { + if (! checkPermission(requester)) { YancloudUtil.exceptionReturn({ - "msg":"no permission", "code":1 + "msg" : "no permission", "code" : 1 }); } - var ret = executeContract("GlobalRouter", "listLRS", ""); + var ret = executeContract(Global.router, "listLRS", ""); var obj = ret.result; - if (obj.result=="success" || obj.result == undefined) { + if (obj.result == "success" || obj.result == undefined) { obj.result = undefined; obj.code = 0; } else { YancloudUtil.exceptionReturn({ - "msg":obj.data, "code":1 + "msg" : obj.data, "code" : 1 }); } - for (var i = 0; - i