From 537ecbfc6cec2733ad8cf1ba3c7ef3b518c159a8 Mon Sep 17 00:00:00 2001 From: yanghuanyu <991390114@qq.com> Date: Mon, 20 Dec 2021 10:32:36 +0800 Subject: [PATCH] feat: add get&set ClusterName --- .../server/nodecenter/NCManagerAction.java | 72 ++++++++++++------- 1 file changed, 47 insertions(+), 25 deletions(-) diff --git a/src/main/java/org/bdware/server/nodecenter/NCManagerAction.java b/src/main/java/org/bdware/server/nodecenter/NCManagerAction.java index 1d3c12f..ea4ebea 100644 --- a/src/main/java/org/bdware/server/nodecenter/NCManagerAction.java +++ b/src/main/java/org/bdware/server/nodecenter/NCManagerAction.java @@ -25,6 +25,7 @@ import java.util.*; public class NCManagerAction { // key为centerManager的数据表示平台管理员(中心管理员) public static final String centerManger = "__CenterManager__"; + public static final String clusterName = "__ClusterName__"; static final String Licence = "__LICENCE___"; static final ECPublicKeyParameters licencePub = BCECUtil.createECPublicKeyFromStrParameters( @@ -49,12 +50,14 @@ public class NCManagerAction { && !StringUtil.isNullOrEmpty(pubkey) && pubkey.equals(ret); } - private void simpleReply(ResultCallback resultCallback, String action, String data){ + + private void simpleReply(ResultCallback resultCallback, String action, String data) { JsonObject jsonResult = new JsonObject(); jsonResult.addProperty("action", action); jsonResult.addProperty("data", data); resultCallback.onResult(jsonResult); } + @Action(userPermission = 0) public void getNodeSessionID(JsonObject json, ResultCallback resultCallback) { getSessionID(json, resultCallback); @@ -80,12 +83,29 @@ public class NCManagerAction { @Action(userPermission = 0) public void getManagerPubkey(JsonObject json, ResultCallback resultCallback) { String ret = KeyValueDBUtil.instance.getValue(NCTables.ConfigDB.toString(), centerManger); - simpleReply(resultCallback,"onGetManagerPubkey",ret); + simpleReply(resultCallback, "onGetManagerPubkey", ret); + } + + @Action(userPermission = 0) + public void getClusterName(JsonObject json, ResultCallback resultCallback) { + String ret = KeyValueDBUtil.instance.getValue(NCTables.ConfigDB.toString(), clusterName); + simpleReply(resultCallback, "onGetClusterName", ret); + } + + @Action(userPermission = 0) + public void setClusterName(JsonObject json, ResultCallback resultCallback) { + if (json.has("name")) { + KeyValueDBUtil.instance.setValue( + NCTables.ConfigDB.toString(), clusterName, json.get("name").getAsString()); + simpleReply(resultCallback, "onSetClusterName", "success"); + } else { + simpleReply(resultCallback, "onSetClusterName", "failed"); + } } public void getRole(JsonObject json, ResultCallback resultCallback) { if (pubKey == null) { - simpleReply(resultCallback,"onLogin",Role.Anonymous.name()); + simpleReply(resultCallback, "onLogin", Role.Anonymous.name()); return; } try { @@ -107,16 +127,18 @@ public class NCManagerAction { role = Role.Anonymous.name(); } handler.setPermission(Role.compoundValue(role.split(","))); - simpleReply(resultCallback,"onLogin",role); + simpleReply(resultCallback, "onLogin", role); } else { KeyValueDBUtil.instance.setValue( NCTables.ConfigDB.toString(), centerManger, pubKey); + KeyValueDBUtil.instance.setValue( + NCTables.ConfigDB.toString(), clusterName, "clusterName_" + pubKey.substring(0, 5)); handler.setPermission(0x30000ffL); - simpleReply(resultCallback,"onLogin","CenterManager"); + simpleReply(resultCallback, "onLogin", "CenterManager"); } } catch (Exception e) { e.printStackTrace(); - simpleReply(resultCallback,"onLogin",Role.Anonymous.name()); + simpleReply(resultCallback, "onLogin", Role.Anonymous.name()); } } @@ -125,7 +147,7 @@ public class NCManagerAction { long start = System.currentTimeMillis(); JsonObject jsonResult = new JsonObject(); if (sessionID == null) { - simpleReply(resultCallback,"onLogin",Role.Anonymous.name()); + simpleReply(resultCallback, "onLogin", Role.Anonymous.name()); return; } String signature = json.get("signature").getAsString(); @@ -138,7 +160,7 @@ public class NCManagerAction { LOGGER.debug("设置公钥" + pubKey); getRole(json, resultCallback); } else { - simpleReply(resultCallback,"onLogin",Role.Anonymous.name()); + simpleReply(resultCallback, "onLogin", Role.Anonymous.name()); } long end = System.currentTimeMillis(); LOGGER.debug("time:" + (end - start)); @@ -148,7 +170,7 @@ public class NCManagerAction { public void applyRole(JsonObject json, ResultCallback resultCallback) { long start = System.currentTimeMillis(); if (pubKey == null) { - simpleReply(resultCallback,"onApplyRole","missing pubKey"); + simpleReply(resultCallback, "onApplyRole", "missing pubKey"); return; } if (json.has("role")) { @@ -158,7 +180,7 @@ public class NCManagerAction { return; } } - simpleReply(resultCallback,"onApplyRole","failed"); + simpleReply(resultCallback, "onApplyRole", "failed"); } @Action(userPermission = 1 << 6) @@ -172,10 +194,10 @@ public class NCManagerAction { NCTables.NodeTime.toString(), json.get("nodePubKey").getAsString(), Long.toString(new Date().getTime())); - simpleReply(resultCallback,"onAddNodes","success"); + simpleReply(resultCallback, "onAddNodes", "success"); } catch (Exception e) { e.printStackTrace(); - simpleReply(resultCallback,"onAddNodes","failed"); + simpleReply(resultCallback, "onAddNodes", "failed"); } } @@ -183,7 +205,7 @@ public class NCManagerAction { String str = KeyValueDBUtil.instance.getValue(NCTables.ApplyRole.toString(), pubKey); String already = KeyValueDBUtil.instance.getValue(NCTables.NodeUser.toString(), pubKey); if (already != null && already.contains(role.toString())) { - simpleReply(resultCallback,"onApplyRole","already has!"); + simpleReply(resultCallback, "onApplyRole", "already has!"); return; } if (str == null || str.length() == 0) { @@ -198,7 +220,7 @@ public class NCManagerAction { NCTables.ApplyTime.toString(), pubKey, Long.toString(new Date().getTime())); } } - simpleReply(resultCallback,"onApplyRole","success!"); + simpleReply(resultCallback, "onApplyRole", "success!"); } @Action(userPermission = 1 << 3) @@ -221,13 +243,13 @@ public class NCManagerAction { boolean isAccept = json.get("isAccept").getAsBoolean(); LOGGER.debug("[NCManagerAction] " + json.toString()); if (pubKey == null || pubKey.length() == 0) { - simpleReply(resultCallback,"onAuthNodeManager","missing pubKey"); + simpleReply(resultCallback, "onAuthNodeManager", "missing pubKey"); return; } if (!isAccept) { KeyValueDBUtil.instance.delete(NCTables.ApplyRole.toString(), pubKey); KeyValueDBUtil.instance.delete(NCTables.ApplyTime.toString(), pubKey); - simpleReply(resultCallback,"onAuthNodeManager","success"); + simpleReply(resultCallback, "onAuthNodeManager", "success"); return; } String already = KeyValueDBUtil.instance.getValue(NCTables.NodeUser.toString(), pubKey); @@ -235,7 +257,7 @@ public class NCManagerAction { if (roles == null || roles.length() == 0) { KeyValueDBUtil.instance.delete(NCTables.ApplyRole.toString(), pubKey); KeyValueDBUtil.instance.delete(NCTables.ApplyTime.toString(), pubKey); - simpleReply(resultCallback,"onAuthNodeManager","empty apply!"); + simpleReply(resultCallback, "onAuthNodeManager", "empty apply!"); return; } if (already != null && already.length() > 0) { @@ -248,7 +270,7 @@ public class NCManagerAction { NCTables.NodeTime.toString(), pubKey, Long.toString(new Date().getTime())); KeyValueDBUtil.instance.delete(NCTables.ApplyRole.toString(), pubKey); KeyValueDBUtil.instance.delete(NCTables.ApplyTime.toString(), pubKey); - simpleReply(resultCallback,"onAuthNodeManager","success"); + simpleReply(resultCallback, "onAuthNodeManager", "success"); long end = System.currentTimeMillis(); LOGGER.debug("[authNodeManager:time]" + (end - start)); } @@ -274,7 +296,7 @@ public class NCManagerAction { String pubKey = json.get("pubKey").getAsString(); KeyValueDBUtil.instance.delete(NCTables.NodeUser.toString(), pubKey); KeyValueDBUtil.instance.delete(NCTables.NodeTime.toString(), pubKey); - simpleReply(resultCallback,"onDelete","success"); + simpleReply(resultCallback, "onDelete", "success"); } @Action(userPermission = 1 << 4) @@ -350,7 +372,7 @@ public class NCManagerAction { String sign = json.get("sign").getAsString(); String content = json.get("content").getAsString(); if (StringUtil.isNullOrEmpty(sign) || StringUtil.isNullOrEmpty(content)) { - simpleReply(resultCallback,"onUpdateLicence","missing signature or content"); + simpleReply(resultCallback, "onUpdateLicence", "missing signature or content"); return; } long expiredDate; @@ -363,16 +385,16 @@ public class NCManagerAction { uuid = jo.get("uuid").getAsString(); } catch (Exception e) { - simpleReply(resultCallback,"onUpdateLicence","content error"); + simpleReply(resultCallback, "onUpdateLicence", "content error"); return; } if (expiredDate == 0 || nodeCount == 0) { - simpleReply(resultCallback,"onUpdateLicence","parse expiredDate/nodeCount error"); + simpleReply(resultCallback, "onUpdateLicence", "parse expiredDate/nodeCount error"); return; } if (!uuid.equals( ByteUtil.encodeBASE64(HardwareInfo.getCPUID().getBytes()).replaceAll("\n", ""))) { - simpleReply(resultCallback,"onUpdateLicence","invalid licence"); + simpleReply(resultCallback, "onUpdateLicence", "invalid licence"); return; } boolean verify = @@ -381,11 +403,11 @@ public class NCManagerAction { if (verify) { KeyValueDBUtil.instance.setValue( NCTables.ConfigDB.toString(), Licence, json.toString()); - simpleReply(resultCallback,"onUpdateLicence","success"); + simpleReply(resultCallback, "onUpdateLicence", "success"); long end = System.currentTimeMillis(); LOGGER.debug("[listLicence:time]" + (end - start)); return; - } else simpleReply(resultCallback,"onUpdateLicence","failed"); + } else simpleReply(resultCallback, "onUpdateLicence", "failed"); long end = System.currentTimeMillis(); LOGGER.debug("[updateLicence:time]" + (end - start)); }