diff --git a/src/main/java/org/bdware/server/nodecenter/LogActions.java b/src/main/java/org/bdware/server/nodecenter/LogActions.java index e7f28dc..15ce797 100644 --- a/src/main/java/org/bdware/server/nodecenter/LogActions.java +++ b/src/main/java/org/bdware/server/nodecenter/LogActions.java @@ -26,17 +26,21 @@ public class LogActions { this.managerAction = managerAction; } + private void simpleReply(ResultCallback resultCallback, String action, Object data) { + Map jsonResult = new HashMap<>(); + jsonResult.put("action", action); + jsonResult.put("data", data); + resultCallback.onResult(jsonResult); + } + private void queryInternal( String actResp, MultiIndexTimeDBUtilIntf db, JsonObject json, ResultCallback resultCallback) { long start = System.currentTimeMillis(); - Map ret = new HashMap<>(); if (!json.has("start")) { - ret.put("action", actResp); - ret.put("data", new ArrayList<>()); - resultCallback.onResult(JsonUtil.toJson(ret)); + simpleReply(resultCallback, actResp, new ArrayList<>()); return; } long startTime = json.get("start").getAsLong(); @@ -54,12 +58,7 @@ public class LogActions { if (str == null) data.put("primary", array); else data.put(str, array); } - ret.put("action", actResp); - ret.put("data", data); - if (json.has("requestID")) { - ret.put("responseID", json.get("requestID")); - } - resultCallback.onResult(JsonUtil.toJson(ret)); + simpleReply(resultCallback, actResp, data); long end = System.currentTimeMillis(); LOGGER.debug("[queryInternal:time]" + (end - start)); } @@ -69,11 +68,8 @@ public class LogActions { MultiIndexTimeDBUtilIntf db, JsonObject json, ResultCallback resultCallback) { - Map ret = new HashMap<>(); if (!json.has("start")) { - ret.put("action", actResp); - ret.put("data", new ArrayList<>()); - resultCallback.onResult(JsonUtil.toJson(ret)); + simpleReply(resultCallback, actResp, new ArrayList<>()); return; } long startTime = json.get("start").getAsLong(); @@ -92,12 +88,7 @@ public class LogActions { if (str == null) data.add("primary", array); else data.add(str, array); } - ret.put("action", actResp); - ret.put("data", data); - if (json.has("requestID")) { - ret.put("responseID", json.get("requestID")); - } - resultCallback.onResult(JsonUtil.toJson(ret)); + simpleReply(resultCallback, actResp, data); } @Action(userPermission = 1 << 8, async = true) @@ -127,12 +118,11 @@ public class LogActions { long start = System.currentTimeMillis(); long userList = KeyValueDBUtil.instance.getCount(NCTables.NodeUser.toString()); long applylist = KeyValueDBUtil.instance.getCount(NCTables.ApplyRole.toString()); - resultCallback.onResult( - "{\"action\":\"onQueryUserStat\",\"userListCount\":" - + userList - + ",\"applyListCount\":" - + applylist - + "}"); + JsonObject jsonResult = new JsonObject(); + jsonResult.addProperty("action", "onQueryUserStat"); + jsonResult.addProperty("userListCount", userList); + jsonResult.addProperty("applyListCount", applylist); + resultCallback.onResult(jsonResult); long end = System.currentTimeMillis(); LOGGER.debug("[queryUserStat:time]" + (end - start)); } @@ -164,7 +154,7 @@ public class LogActions { ret.put("action", "onListNodes"); ret.put("online", onlineNodes); ret.put("offline", dbnodes); - resultCallback.onResult(JsonUtil.toJson(ret)); + resultCallback.onResult(ret); return; } for (Map.Entry entry : nodeinfos.entrySet()) { @@ -178,7 +168,7 @@ public class LogActions { ret.put("online", cinodeinfos); ret.put("offline", new ArrayList()); // 合约管理员看不到offline nodes - resultCallback.onResult(JsonUtil.toJson(ret)); + resultCallback.onResult(ret); long end = System.currentTimeMillis(); LOGGER.debug("[listNodes:time]" + (end - start)); } diff --git a/src/main/java/org/bdware/server/nodecenter/MetaIndexAction.java b/src/main/java/org/bdware/server/nodecenter/MetaIndexAction.java index 5b8663b..68f7995 100644 --- a/src/main/java/org/bdware/server/nodecenter/MetaIndexAction.java +++ b/src/main/java/org/bdware/server/nodecenter/MetaIndexAction.java @@ -97,7 +97,7 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; if (null != thisDesp.contractName && (docs.scoreDocs == null || docs.scoreDocs.length == 0)) { req.addProperty("action", "requestReadMe"); req.addProperty("contractID", thisDesp.contractID); - rc.onResult(req.toString()); + rc.onResult(req); LOGGER.info("contract " + thisDesp.contractName + " --> actually to index"); continue; } @@ -107,7 +107,7 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; } req.addProperty("action", "requestReadMe"); req.addProperty("contractID", thisDesp.contractID); - rc.onResult(req.toString()); + rc.onResult(req); LOGGER.info("contract " + thisDesp.contractName + " --> actually to index"); } if (null != indexReader) { @@ -473,7 +473,7 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; System.out.println("zzzResult" + result); object.add("result", JsonParser.parseString(result)); object.addProperty("action", "getMetabyReadme"); - rc.onResult(object.toString()); + rc.onResult(object); } @Action(async = true, userPermission = 1L) @@ -501,7 +501,7 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; // TODO Auto-generated catch block e.printStackTrace(); } finally { - rc.onResult(object.toString()); + rc.onResult(object); try { stream.close(); } catch (IOException e) { @@ -518,7 +518,7 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; if (json.has("requestID")) object.add("responseID", json.get("requestID")); object.add("result", JsonParser.parseString(result)); object.addProperty("action", "getMetabyCID"); - rc.onResult(object.toString()); + rc.onResult(object); } @Action(async = true, userPermission = 1L) @@ -528,7 +528,7 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; if (json.has("requestID")) object.add("responseID", json.get("requestID")); object.add("result", JsonParser.parseString(result)); object.addProperty("action", "getMetabyOwner"); - rc.onResult(object.toString()); + rc.onResult(object); } @Action(async = true, userPermission = 1L) @@ -538,7 +538,7 @@ public class MetaIndexAction { // public static IndexWriter indexWriter; if (json.has("requestID")) object.add("responseID", json.get("requestID")); object.add("result", JsonParser.parseString(result)); object.addProperty("action", "getMetabyPubkey"); - rc.onResult(object.toString()); + rc.onResult(object); } @Action(async = true, userPermission = 1L) diff --git a/src/main/java/org/bdware/server/nodecenter/NCManagerAction.java b/src/main/java/org/bdware/server/nodecenter/NCManagerAction.java index b6d5696..1d3c12f 100644 --- a/src/main/java/org/bdware/server/nodecenter/NCManagerAction.java +++ b/src/main/java/org/bdware/server/nodecenter/NCManagerAction.java @@ -49,7 +49,12 @@ public class NCManagerAction { && !StringUtil.isNullOrEmpty(pubkey) && pubkey.equals(ret); } - + 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); @@ -59,10 +64,10 @@ public class NCManagerAction { public void getSessionID(JsonObject json, ResultCallback resultCallback) { long start = System.currentTimeMillis(); sessionID = random.nextLong() + "_session"; - Map result = new HashMap<>(); - result.put("action", "onSessionID"); - result.put("session", sessionID); - resultCallback.onResult(JsonUtil.toJson(result)); + JsonObject result = new JsonObject(); + result.addProperty("action", "onSessionID"); + result.addProperty("session", sessionID); + resultCallback.onResult(result); long end = System.currentTimeMillis(); LOGGER.debug("time:" + (end - start) + "data:" + JsonUtil.toJson(result)); } @@ -75,13 +80,12 @@ public class NCManagerAction { @Action(userPermission = 0) public void getManagerPubkey(JsonObject json, ResultCallback resultCallback) { String ret = KeyValueDBUtil.instance.getValue(NCTables.ConfigDB.toString(), centerManger); - resultCallback.onResult("{\"action\":\"onGetManagerPubkey\",\"data\":\"" + ret + "\"}"); + simpleReply(resultCallback,"onGetManagerPubkey",ret); } public void getRole(JsonObject json, ResultCallback resultCallback) { if (pubKey == null) { - resultCallback.onResult( - "{\"action\":\"onLogin\",\"data\":\"" + Role.Anonymous.name() + "\"}"); + simpleReply(resultCallback,"onLogin",Role.Anonymous.name()); return; } try { @@ -103,26 +107,25 @@ public class NCManagerAction { role = Role.Anonymous.name(); } handler.setPermission(Role.compoundValue(role.split(","))); - resultCallback.onResult("{\"action\":\"onLogin\",\"data\":\"" + role + "\"}"); + simpleReply(resultCallback,"onLogin",role); } else { KeyValueDBUtil.instance.setValue( NCTables.ConfigDB.toString(), centerManger, pubKey); handler.setPermission(0x30000ffL); - resultCallback.onResult("{\"action\":\"onLogin\",\"data\":\"CenterManager\"}"); + simpleReply(resultCallback,"onLogin","CenterManager"); } } catch (Exception e) { e.printStackTrace(); - resultCallback.onResult( - "{\"action\":\"onLogin\",\"data\":\"" + Role.Anonymous.name() + "\"}"); + simpleReply(resultCallback,"onLogin",Role.Anonymous.name()); } } @Action(userPermission = 0) public void login(JsonObject json, ResultCallback resultCallback) { long start = System.currentTimeMillis(); + JsonObject jsonResult = new JsonObject(); if (sessionID == null) { - resultCallback.onResult( - "{\"action\":\"onLogin\",\"data\":\"" + Role.Anonymous.name() + "\"}"); + simpleReply(resultCallback,"onLogin",Role.Anonymous.name()); return; } String signature = json.get("signature").getAsString(); @@ -135,8 +138,7 @@ public class NCManagerAction { LOGGER.debug("设置公钥" + pubKey); getRole(json, resultCallback); } else { - resultCallback.onResult( - "{\"action\":\"onLogin\",\"data\":\"" + Role.Anonymous.name() + "\"}"); + simpleReply(resultCallback,"onLogin",Role.Anonymous.name()); } long end = System.currentTimeMillis(); LOGGER.debug("time:" + (end - start)); @@ -146,7 +148,7 @@ public class NCManagerAction { public void applyRole(JsonObject json, ResultCallback resultCallback) { long start = System.currentTimeMillis(); if (pubKey == null) { - resultCallback.onResult("{\"action\":\"onApplyRole\",\"data\":\"missing pubKey\"}"); + simpleReply(resultCallback,"onApplyRole","missing pubKey"); return; } if (json.has("role")) { @@ -156,7 +158,7 @@ public class NCManagerAction { return; } } - resultCallback.onResult("{\"action\":\"onApplyRole\",\"data\":\"failed\"}"); + simpleReply(resultCallback,"onApplyRole","failed"); } @Action(userPermission = 1 << 6) @@ -170,10 +172,10 @@ public class NCManagerAction { NCTables.NodeTime.toString(), json.get("nodePubKey").getAsString(), Long.toString(new Date().getTime())); - resultCallback.onResult("{\"action\":\"onAddNodes\",\"data\":\"success\"}"); + simpleReply(resultCallback,"onAddNodes","success"); } catch (Exception e) { e.printStackTrace(); - resultCallback.onResult("{\"action\":\"onAddNodes\",\"data\":\"failed\"}"); + simpleReply(resultCallback,"onAddNodes","failed"); } } @@ -181,7 +183,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())) { - resultCallback.onResult("{\"action\":\"onApplyRole\",\"data\":\"already has!\"}"); + simpleReply(resultCallback,"onApplyRole","already has!"); return; } if (str == null || str.length() == 0) { @@ -196,7 +198,7 @@ public class NCManagerAction { NCTables.ApplyTime.toString(), pubKey, Long.toString(new Date().getTime())); } } - resultCallback.onResult("{\"action\":\"onApplyRole\",\"data\":\"success\"}"); + simpleReply(resultCallback,"onApplyRole","success!"); } @Action(userPermission = 1 << 3) @@ -204,12 +206,12 @@ public class NCManagerAction { List kv = KeyValueDBUtil.instance.getKeyValues(NCTables.NodeUser.toString()); List time = KeyValueDBUtil.instance.getKeyValues(NCTables.NodeTime.toString()); Map ret = new HashMap<>(); - ResultBack result = new ResultBack(); ret.put("kv", kv); ret.put("time", time); - result.action = "onListAllUsers"; - result.data = ret; - resultCallback.onResult(JsonUtil.toJson(result)); + Map result = new HashMap<>(); + result.put("action", "onListAllUsers"); + result.put("data", ret); + resultCallback.onResult(result); } @Action(userPermission = 1 << 2) @@ -219,20 +221,21 @@ public class NCManagerAction { boolean isAccept = json.get("isAccept").getAsBoolean(); LOGGER.debug("[NCManagerAction] " + json.toString()); if (pubKey == null || pubKey.length() == 0) { - resultCallback.onResult( - "{\"action\":\"onAuthNodeManager\",\"data\":\"missing pubKey\"}"); + simpleReply(resultCallback,"onAuthNodeManager","missing pubKey"); return; } if (!isAccept) { KeyValueDBUtil.instance.delete(NCTables.ApplyRole.toString(), pubKey); KeyValueDBUtil.instance.delete(NCTables.ApplyTime.toString(), pubKey); - resultCallback.onResult("{\"action\":\"onAuthNodeManager\",\"data\":\"success\"}"); + simpleReply(resultCallback,"onAuthNodeManager","success"); return; } String already = KeyValueDBUtil.instance.getValue(NCTables.NodeUser.toString(), pubKey); String roles = KeyValueDBUtil.instance.getValue(NCTables.ApplyRole.toString(), pubKey); if (roles == null || roles.length() == 0) { - resultCallback.onResult("{\"action\":\"onAuthNodeManager\",\"data\":\"empty apply\"}"); + KeyValueDBUtil.instance.delete(NCTables.ApplyRole.toString(), pubKey); + KeyValueDBUtil.instance.delete(NCTables.ApplyTime.toString(), pubKey); + simpleReply(resultCallback,"onAuthNodeManager","empty apply!"); return; } if (already != null && already.length() > 0) { @@ -240,13 +243,12 @@ public class NCManagerAction { } else { already = roles; } - KeyValueDBUtil.instance.setValue(NCTables.NodeUser.toString(), pubKey, already); KeyValueDBUtil.instance.setValue( NCTables.NodeTime.toString(), pubKey, Long.toString(new Date().getTime())); KeyValueDBUtil.instance.delete(NCTables.ApplyRole.toString(), pubKey); KeyValueDBUtil.instance.delete(NCTables.ApplyTime.toString(), pubKey); - resultCallback.onResult("{\"action\":\"onAuthNodeManager\",\"data\":\"success\"}"); + simpleReply(resultCallback,"onAuthNodeManager","success"); long end = System.currentTimeMillis(); LOGGER.debug("[authNodeManager:time]" + (end - start)); } @@ -259,10 +261,10 @@ public class NCManagerAction { Map ret = new HashMap<>(); ret.put("kv", kv); ret.put("time", time); - ResultBack result = new ResultBack(); - result.action = "onListApplyList"; - result.data = ret; - resultCallback.onResult(JsonUtil.toJson(result)); + Map result = new HashMap<>(); + result.put("action", "onListApplyList"); + result.put("data", ret); + resultCallback.onResult(result); long end = System.currentTimeMillis(); LOGGER.debug("[listApplyList:time]" + (end - start)); } @@ -272,7 +274,7 @@ public class NCManagerAction { String pubKey = json.get("pubKey").getAsString(); KeyValueDBUtil.instance.delete(NCTables.NodeUser.toString(), pubKey); KeyValueDBUtil.instance.delete(NCTables.NodeTime.toString(), pubKey); - resultCallback.onResult("{\"action\":\"onDelete\",\"data\":\"success\"}"); + simpleReply(resultCallback,"onDelete","success"); } @Action(userPermission = 1 << 4) @@ -280,16 +282,22 @@ public class NCManagerAction { long start = System.currentTimeMillis(); String licence = KeyValueDBUtil.instance.getValue(NCTables.ConfigDB.toString(), Licence); if (licence == null || licence.length() == 0) { - resultCallback.onResult( - "{\"action\":\"onListLicence\",\"nodeCount\":" + 0 + ",\"dueDate\":\"已过期\"}"); + JsonObject jsonResult = new JsonObject(); + jsonResult.addProperty("action", "onListLicence"); + jsonResult.addProperty("nodeCount", 0); + jsonResult.addProperty("dueDate", "已过期"); + resultCallback.onResult(jsonResult); return; } json = JsonParser.parseString(licence).getAsJsonObject(); String sign = json.get("sign").getAsString(); String content = json.get("content").getAsString(); if (StringUtil.isNullOrEmpty(sign) || StringUtil.isNullOrEmpty(content)) { - resultCallback.onResult( - "{\"action\":\"onListLicence\",\"nodeCount\":0,\"dueDate\":\"已过期\"}"); + JsonObject jsonResult = new JsonObject(); + jsonResult.addProperty("action", "onListLicence"); + jsonResult.addProperty("nodeCount", 0); + jsonResult.addProperty("dueDate", "已过期"); + resultCallback.onResult(jsonResult); return; } long expiredDate; @@ -299,30 +307,38 @@ public class NCManagerAction { nodeCount = jo.get("nodeCount").getAsLong(); expiredDate = jo.get("expiredDate").getAsLong(); } catch (Exception e) { - resultCallback.onResult( - "{\"action\":\"onListLicence\",\"nodeCount\":0,\"dueDate\":\"已过期\"}"); + JsonObject jsonResult = new JsonObject(); + jsonResult.addProperty("action", "onListLicence"); + jsonResult.addProperty("nodeCount", 0); + jsonResult.addProperty("dueDate", "已过期"); + resultCallback.onResult(jsonResult); return; } if (expiredDate == 0 || nodeCount == 0) { - resultCallback.onResult( - "{\"action\":\"onListLicence\",\"nodeCount\":0,\"dueDate\":\"已过期\"}"); + JsonObject jsonResult = new JsonObject(); + jsonResult.addProperty("action", "onListLicence"); + jsonResult.addProperty("nodeCount", 0); + jsonResult.addProperty("dueDate", "已过期"); + resultCallback.onResult(jsonResult); return; } SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); boolean verify = SM2Util.verify(licencePub, content.getBytes(), ByteUtils.fromHexString(sign)); if (verify) { - resultCallback.onResult( - "{\"action\":\"onListLicence\",\"nodeCount\":" - + nodeCount - + ",\"dueDate\":\"" - + format.format(new Date(expiredDate)) - + "\"}"); + JsonObject jsonResult = new JsonObject(); + jsonResult.addProperty("action", "onListLicence"); + jsonResult.addProperty("nodeCount", nodeCount); + jsonResult.addProperty("dueDate", format.format(new Date(expiredDate))); + resultCallback.onResult(jsonResult); long end = System.currentTimeMillis(); LOGGER.debug("[listLicence:time]" + (end - start)); } else { - resultCallback.onResult( - "{\"action\":\"onListLicence\",\"nodeCount\":0,\"dueDate\":\"已过期\"}"); + JsonObject jsonResult = new JsonObject(); + jsonResult.addProperty("action", "onListLicence"); + jsonResult.addProperty("nodeCount", 0); + jsonResult.addProperty("dueDate", "已过期"); + resultCallback.onResult(jsonResult); long end = System.currentTimeMillis(); LOGGER.debug("[listLicence:time]" + (end - start)); } @@ -334,8 +350,7 @@ public class NCManagerAction { String sign = json.get("sign").getAsString(); String content = json.get("content").getAsString(); if (StringUtil.isNullOrEmpty(sign) || StringUtil.isNullOrEmpty(content)) { - resultCallback.onResult( - "{\"action\":\"onUpdateLicence\",\"data\":\"missing signature or content\"}"); + simpleReply(resultCallback,"onUpdateLicence","missing signature or content"); return; } long expiredDate; @@ -348,18 +363,16 @@ public class NCManagerAction { uuid = jo.get("uuid").getAsString(); } catch (Exception e) { - resultCallback.onResult("{\"action\":\"onUpdateLicence\",\"data\":\"content error\"}"); + simpleReply(resultCallback,"onUpdateLicence","content error"); return; } if (expiredDate == 0 || nodeCount == 0) { - resultCallback.onResult( - "{\"action\":\"onUpdateLicence\",\"data\":\"parse expiredDate/nodeCount error\"}"); + simpleReply(resultCallback,"onUpdateLicence","parse expiredDate/nodeCount error"); return; } if (!uuid.equals( ByteUtil.encodeBASE64(HardwareInfo.getCPUID().getBytes()).replaceAll("\n", ""))) { - resultCallback.onResult( - "{\"action\":\"onUpdateLicence\",\"data\":\"invalid licence\"}"); + simpleReply(resultCallback,"onUpdateLicence","invalid licence"); return; } boolean verify = @@ -368,11 +381,11 @@ public class NCManagerAction { if (verify) { KeyValueDBUtil.instance.setValue( NCTables.ConfigDB.toString(), Licence, json.toString()); - resultCallback.onResult("{\"action\":\"onUpdateLicence\",\"data\":\"success\"}"); + simpleReply(resultCallback,"onUpdateLicence","success"); long end = System.currentTimeMillis(); LOGGER.debug("[listLicence:time]" + (end - start)); return; - } else resultCallback.onResult("{\"action\":\"onUpdateLicence\",\"data\":\"failed\"}"); + } else simpleReply(resultCallback,"onUpdateLicence","failed"); long end = System.currentTimeMillis(); LOGGER.debug("[updateLicence:time]" + (end - start)); } @@ -383,7 +396,7 @@ public class NCManagerAction { result.put("action", "onGetOtherNC"); String s = OtherNCProxy.instance.getOtherNCs().replace(";", " "); result.put("address", s); - resultCallback.onResult(JsonUtil.toJson(result)); + resultCallback.onResult(result); } // 改为NodeManager权限 @@ -391,11 +404,10 @@ public class NCManagerAction { public void changeOtherNC(JsonObject json, ResultCallback resultCallback) { String add = json.get("data").getAsString().replace(" ", ";"); String s = OtherNCProxy.instance.setOtherNCs(add).replace(";", " "); - - Map result = new HashMap<>(); - result.put("action", "onGetOtherNC"); - result.put("address", s); - resultCallback.onResult(JsonUtil.toJson(result)); + JsonObject result = new JsonObject(); + result.addProperty("action", "onGetOtherNC"); + result.addProperty("address", s); + resultCallback.onResult(result); } // NodeManager权限 @@ -403,21 +415,19 @@ public class NCManagerAction { public void changeNCFile(JsonObject json, ResultCallback resultCallback) { String add = json.get("data").getAsString(); String s = OtherNCProxy.instance.setNCFile(add); - - Map result = new HashMap<>(); - result.put("action", "onGetNCFile"); - result.put("fileName", s); - resultCallback.onResult(JsonUtil.toJson(result)); + JsonObject result = new JsonObject(); + result.addProperty("action", "onGetNCFile"); + result.addProperty("fileName", s); + resultCallback.onResult(result); } @Action(userPermission = 1 << 9) public void getNCFile(JsonObject json, ResultCallback resultCallback) { String s = OtherNCProxy.instance.getNCFile(); - Map result = new HashMap<>(); result.put("action", "onGetNCFile"); result.put("fileName", s); - resultCallback.onResult(JsonUtil.toJson(result)); + resultCallback.onResult(result); } // TODO 迁移合约实例(从节点A将合约M的实例迁移到节点B)的权限? @@ -427,7 +437,6 @@ public class NCManagerAction { @Action(userPermission = 0) public void transferContractInstance(JsonObject json, ResultCallback resultCallback) { LOGGER.info("transferContractInstance"); - String node1ID = json.get("node1ID").getAsString(); String node2ID = json.get("node2ID").getAsString(); String contractID = json.get("contractID").getAsString(); @@ -468,6 +477,6 @@ public class NCManagerAction { + node1ID + " to node:" + node2ID); - resultCallback.onResult(JsonUtil.toJson(result)); + resultCallback.onResult(result); } } diff --git a/src/main/java/org/bdware/server/nodecenter/NodeCenterWSFrameHandler.java b/src/main/java/org/bdware/server/nodecenter/NodeCenterWSFrameHandler.java index 827c656..c79d560 100644 --- a/src/main/java/org/bdware/server/nodecenter/NodeCenterWSFrameHandler.java +++ b/src/main/java/org/bdware/server/nodecenter/NodeCenterWSFrameHandler.java @@ -16,6 +16,7 @@ import org.bdware.server.action.ActionExecutor; import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -33,8 +34,6 @@ public class NodeCenterWSFrameHandler extends SimpleChannelInboundHandler( executorService, managerAction, logActions, unitActions, new MetaIndexAction(), new TracingAction()) { @@ -111,10 +110,27 @@ public class NodeCenterWSFrameHandler extends SimpleChannelInboundHandler jsonResult = new HashMap<>(); + jsonResult.put("action", action); + jsonResult.put("data", data); + resultCallback.onResult(jsonResult); + } private static String convertContractName(String contractID) { ContractDesp desp = NodeCenterActions.getContractByName(contractID); if (desp != null) return desp.contractID; @@ -36,18 +41,13 @@ public class UnitActions { public void listTrustUnits(JsonObject json, ResultCallback resultCallback) { long start = System.currentTimeMillis(); final String pubKey = managerAction.pubKey; - Map ret = new HashMap<>(); List allunits = KeyValueDBUtil.instance.getKeyValues(NCTables.TrustUnitsDB.toString()); - if (pubKey != null && KeyValueDBUtil.instance .getValue(NCTables.ConfigDB.toString(), "__CenterManager__") .contains(pubKey)) { + simpleReply(resultCallback,"onListTrustUnits",allunits); LOGGER.debug("is center manager"); - ret.put("action", "onListTrustUnits"); - ret.put("data", allunits); - LOGGER.debug("[listTrustUnits] " + JsonUtil.toJson(ret)); - resultCallback.onResult(JsonUtil.toJson(ret)); long end = System.currentTimeMillis(); LOGGER.debug("[listTrustUnits:time]" + (end - start)); return; @@ -61,10 +61,7 @@ public class UnitActions { ciunits.add(kv); } } - ret.put("action", "onListTrustUnits"); - ret.put("data", ciunits); - LOGGER.debug("[listTrustUnits] " + JsonUtil.toJson(ret)); - resultCallback.onResult(JsonUtil.toJson(ret)); + simpleReply(resultCallback,"onListTrustUnits",ciunits); long end = System.currentTimeMillis(); LOGGER.debug("[listTrustUnits:time]" + (end - start)); } @@ -80,7 +77,7 @@ public class UnitActions { Map ret = new HashMap<>(); ret.put("action", "onCreateTrustUnit"); ret.put("status", "Success"); - resultCallback.onResult(JsonUtil.toJson(ret)); + resultCallback.onResult(ret); } @Action(userPermission = 1 << 6, async = true) @@ -99,7 +96,7 @@ public class UnitActions { Map ret = new HashMap<>(); ret.put("action", "onDeleteTrustUnit"); ret.put("status", "Success"); - resultCallback.onResult(JsonUtil.toJson(ret)); + resultCallback.onResult(ret); } // @Action(async = true) @@ -119,10 +116,7 @@ public class UnitActions { nodeunits.add(kv); } } - Map ret = new HashMap<>(); - ret.put("action", "onGetNodeTrustUnits"); - ret.put("data", nodeunits); - resultCallback.onResult(JsonUtil.toJson(ret)); + simpleReply(resultCallback,"onGetNodeTrustUnits",nodeunits); } // @Action(async = true, userPermission = 0) @@ -198,8 +192,6 @@ public class UnitActions { @Action(async = true, userPermission = 1L << 6) public void listContractProcess(JsonObject args, final ResultCallback rc) { - Map ret = new HashMap<>(); - ret.put("action", "onListContractProcess"); List info = new ArrayList<>(); LOGGER.debug( "[contracts] " @@ -211,14 +203,11 @@ public class UnitActions { ContractDesp desp = NodeCenterActions.getContractByID(key); if (desp != null) info.add(desp); } - ret.put("data", JsonUtil.toJson(info)); - rc.onResult(JsonUtil.toJson(ret)); + simpleReply(rc,"onListContractProcess",JsonUtil.toJson(info)); } @Action(userPermission = 1 << 6, async = true) public void listMultiPointContractProcess(JsonObject json, ResultCallback resultCallback) { - Map ret = new HashMap<>(); - ret.put("action", "onListMultiPointContractProcess"); List info = new ArrayList<>(); LOGGER.debug( "[contracts] " @@ -230,8 +219,7 @@ public class UnitActions { ContractDesp desp = NodeCenterActions.getContractByID(key); if (desp != null) info.add(desp); } - ret.put("data", JsonUtil.toJson(info)); - resultCallback.onResult(JsonUtil.toJson(ret)); + simpleReply(resultCallback,"onListMultiPointContractProcess",JsonUtil.toJson(info)); } @Action(userPermission = 1 << 6, async = true) @@ -398,10 +386,4 @@ public class UnitActions { // + contract.getID() // + "\",\"action\":\"onStartTrustfulContract\"}"); // } - - private void example() { - for (CMNode node : NodeCenterActions.nodeInfos.values()) { - // node.connection.executeContractTrustfully(args, rc); - } - } }