feat: responseID automatically append in Websocket interfaces

This commit is contained in:
CaiHQ 2021-11-01 16:48:18 +08:00
parent 8ed9df7414
commit 1c70c9ad21
6 changed files with 143 additions and 148 deletions

View File

@ -26,17 +26,21 @@ public class LogActions {
this.managerAction = managerAction; this.managerAction = managerAction;
} }
private void simpleReply(ResultCallback resultCallback, String action, Object data) {
Map<String, Object> jsonResult = new HashMap<>();
jsonResult.put("action", action);
jsonResult.put("data", data);
resultCallback.onResult(jsonResult);
}
private void queryInternal( private void queryInternal(
String actResp, String actResp,
MultiIndexTimeDBUtilIntf db, MultiIndexTimeDBUtilIntf db,
JsonObject json, JsonObject json,
ResultCallback resultCallback) { ResultCallback resultCallback) {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
Map<String, Object> ret = new HashMap<>();
if (!json.has("start")) { if (!json.has("start")) {
ret.put("action", actResp); simpleReply(resultCallback, actResp, new ArrayList<>());
ret.put("data", new ArrayList<>());
resultCallback.onResult(JsonUtil.toJson(ret));
return; return;
} }
long startTime = json.get("start").getAsLong(); long startTime = json.get("start").getAsLong();
@ -54,12 +58,7 @@ public class LogActions {
if (str == null) data.put("primary", array); if (str == null) data.put("primary", array);
else data.put(str, array); else data.put(str, array);
} }
ret.put("action", actResp); simpleReply(resultCallback, actResp, data);
ret.put("data", data);
if (json.has("requestID")) {
ret.put("responseID", json.get("requestID"));
}
resultCallback.onResult(JsonUtil.toJson(ret));
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
LOGGER.debug("[queryInternal:time]" + (end - start)); LOGGER.debug("[queryInternal:time]" + (end - start));
} }
@ -69,11 +68,8 @@ public class LogActions {
MultiIndexTimeDBUtilIntf db, MultiIndexTimeDBUtilIntf db,
JsonObject json, JsonObject json,
ResultCallback resultCallback) { ResultCallback resultCallback) {
Map<String, Object> ret = new HashMap<>();
if (!json.has("start")) { if (!json.has("start")) {
ret.put("action", actResp); simpleReply(resultCallback, actResp, new ArrayList<>());
ret.put("data", new ArrayList<>());
resultCallback.onResult(JsonUtil.toJson(ret));
return; return;
} }
long startTime = json.get("start").getAsLong(); long startTime = json.get("start").getAsLong();
@ -92,12 +88,7 @@ public class LogActions {
if (str == null) data.add("primary", array); if (str == null) data.add("primary", array);
else data.add(str, array); else data.add(str, array);
} }
ret.put("action", actResp); simpleReply(resultCallback, actResp, data);
ret.put("data", data);
if (json.has("requestID")) {
ret.put("responseID", json.get("requestID"));
}
resultCallback.onResult(JsonUtil.toJson(ret));
} }
@Action(userPermission = 1 << 8, async = true) @Action(userPermission = 1 << 8, async = true)
@ -127,12 +118,11 @@ public class LogActions {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
long userList = KeyValueDBUtil.instance.getCount(NCTables.NodeUser.toString()); long userList = KeyValueDBUtil.instance.getCount(NCTables.NodeUser.toString());
long applylist = KeyValueDBUtil.instance.getCount(NCTables.ApplyRole.toString()); long applylist = KeyValueDBUtil.instance.getCount(NCTables.ApplyRole.toString());
resultCallback.onResult( JsonObject jsonResult = new JsonObject();
"{\"action\":\"onQueryUserStat\",\"userListCount\":" jsonResult.addProperty("action", "onQueryUserStat");
+ userList jsonResult.addProperty("userListCount", userList);
+ ",\"applyListCount\":" jsonResult.addProperty("applyListCount", applylist);
+ applylist resultCallback.onResult(jsonResult);
+ "}");
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
LOGGER.debug("[queryUserStat:time]" + (end - start)); LOGGER.debug("[queryUserStat:time]" + (end - start));
} }
@ -164,7 +154,7 @@ public class LogActions {
ret.put("action", "onListNodes"); ret.put("action", "onListNodes");
ret.put("online", onlineNodes); ret.put("online", onlineNodes);
ret.put("offline", dbnodes); ret.put("offline", dbnodes);
resultCallback.onResult(JsonUtil.toJson(ret)); resultCallback.onResult(ret);
return; return;
} }
for (Map.Entry<String, CMNode> entry : nodeinfos.entrySet()) { for (Map.Entry<String, CMNode> entry : nodeinfos.entrySet()) {
@ -178,7 +168,7 @@ public class LogActions {
ret.put("online", cinodeinfos); ret.put("online", cinodeinfos);
ret.put("offline", new ArrayList<String>()); ret.put("offline", new ArrayList<String>());
// 合约管理员看不到offline nodes // 合约管理员看不到offline nodes
resultCallback.onResult(JsonUtil.toJson(ret)); resultCallback.onResult(ret);
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
LOGGER.debug("[listNodes:time]" + (end - start)); LOGGER.debug("[listNodes:time]" + (end - start));
} }

View File

@ -97,7 +97,7 @@ public class MetaIndexAction { // public static IndexWriter indexWriter;
if (null != thisDesp.contractName && (docs.scoreDocs == null || docs.scoreDocs.length == 0)) { if (null != thisDesp.contractName && (docs.scoreDocs == null || docs.scoreDocs.length == 0)) {
req.addProperty("action", "requestReadMe"); req.addProperty("action", "requestReadMe");
req.addProperty("contractID", thisDesp.contractID); req.addProperty("contractID", thisDesp.contractID);
rc.onResult(req.toString()); rc.onResult(req);
LOGGER.info("contract " + thisDesp.contractName + " --> actually to index"); LOGGER.info("contract " + thisDesp.contractName + " --> actually to index");
continue; continue;
} }
@ -107,7 +107,7 @@ public class MetaIndexAction { // public static IndexWriter indexWriter;
} }
req.addProperty("action", "requestReadMe"); req.addProperty("action", "requestReadMe");
req.addProperty("contractID", thisDesp.contractID); req.addProperty("contractID", thisDesp.contractID);
rc.onResult(req.toString()); rc.onResult(req);
LOGGER.info("contract " + thisDesp.contractName + " --> actually to index"); LOGGER.info("contract " + thisDesp.contractName + " --> actually to index");
} }
if (null != indexReader) { if (null != indexReader) {
@ -473,7 +473,7 @@ public class MetaIndexAction { // public static IndexWriter indexWriter;
System.out.println("zzzResult" + result); System.out.println("zzzResult" + result);
object.add("result", JsonParser.parseString(result)); object.add("result", JsonParser.parseString(result));
object.addProperty("action", "getMetabyReadme"); object.addProperty("action", "getMetabyReadme");
rc.onResult(object.toString()); rc.onResult(object);
} }
@Action(async = true, userPermission = 1L) @Action(async = true, userPermission = 1L)
@ -501,7 +501,7 @@ public class MetaIndexAction { // public static IndexWriter indexWriter;
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
rc.onResult(object.toString()); rc.onResult(object);
try { try {
stream.close(); stream.close();
} catch (IOException e) { } catch (IOException e) {
@ -518,7 +518,7 @@ public class MetaIndexAction { // public static IndexWriter indexWriter;
if (json.has("requestID")) object.add("responseID", json.get("requestID")); if (json.has("requestID")) object.add("responseID", json.get("requestID"));
object.add("result", JsonParser.parseString(result)); object.add("result", JsonParser.parseString(result));
object.addProperty("action", "getMetabyCID"); object.addProperty("action", "getMetabyCID");
rc.onResult(object.toString()); rc.onResult(object);
} }
@Action(async = true, userPermission = 1L) @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")); if (json.has("requestID")) object.add("responseID", json.get("requestID"));
object.add("result", JsonParser.parseString(result)); object.add("result", JsonParser.parseString(result));
object.addProperty("action", "getMetabyOwner"); object.addProperty("action", "getMetabyOwner");
rc.onResult(object.toString()); rc.onResult(object);
} }
@Action(async = true, userPermission = 1L) @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")); if (json.has("requestID")) object.add("responseID", json.get("requestID"));
object.add("result", JsonParser.parseString(result)); object.add("result", JsonParser.parseString(result));
object.addProperty("action", "getMetabyPubkey"); object.addProperty("action", "getMetabyPubkey");
rc.onResult(object.toString()); rc.onResult(object);
} }
@Action(async = true, userPermission = 1L) @Action(async = true, userPermission = 1L)

View File

@ -49,7 +49,12 @@ public class NCManagerAction {
&& !StringUtil.isNullOrEmpty(pubkey) && !StringUtil.isNullOrEmpty(pubkey)
&& pubkey.equals(ret); && 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) @Action(userPermission = 0)
public void getNodeSessionID(JsonObject json, ResultCallback resultCallback) { public void getNodeSessionID(JsonObject json, ResultCallback resultCallback) {
getSessionID(json, resultCallback); getSessionID(json, resultCallback);
@ -59,10 +64,10 @@ public class NCManagerAction {
public void getSessionID(JsonObject json, ResultCallback resultCallback) { public void getSessionID(JsonObject json, ResultCallback resultCallback) {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
sessionID = random.nextLong() + "_session"; sessionID = random.nextLong() + "_session";
Map<String, String> result = new HashMap<>(); JsonObject result = new JsonObject();
result.put("action", "onSessionID"); result.addProperty("action", "onSessionID");
result.put("session", sessionID); result.addProperty("session", sessionID);
resultCallback.onResult(JsonUtil.toJson(result)); resultCallback.onResult(result);
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
LOGGER.debug("time:" + (end - start) + "data:" + JsonUtil.toJson(result)); LOGGER.debug("time:" + (end - start) + "data:" + JsonUtil.toJson(result));
} }
@ -75,13 +80,12 @@ public class NCManagerAction {
@Action(userPermission = 0) @Action(userPermission = 0)
public void getManagerPubkey(JsonObject json, ResultCallback resultCallback) { public void getManagerPubkey(JsonObject json, ResultCallback resultCallback) {
String ret = KeyValueDBUtil.instance.getValue(NCTables.ConfigDB.toString(), centerManger); 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) { public void getRole(JsonObject json, ResultCallback resultCallback) {
if (pubKey == null) { if (pubKey == null) {
resultCallback.onResult( simpleReply(resultCallback,"onLogin",Role.Anonymous.name());
"{\"action\":\"onLogin\",\"data\":\"" + Role.Anonymous.name() + "\"}");
return; return;
} }
try { try {
@ -103,26 +107,25 @@ public class NCManagerAction {
role = Role.Anonymous.name(); role = Role.Anonymous.name();
} }
handler.setPermission(Role.compoundValue(role.split(","))); handler.setPermission(Role.compoundValue(role.split(",")));
resultCallback.onResult("{\"action\":\"onLogin\",\"data\":\"" + role + "\"}"); simpleReply(resultCallback,"onLogin",role);
} else { } else {
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(
NCTables.ConfigDB.toString(), centerManger, pubKey); NCTables.ConfigDB.toString(), centerManger, pubKey);
handler.setPermission(0x30000ffL); handler.setPermission(0x30000ffL);
resultCallback.onResult("{\"action\":\"onLogin\",\"data\":\"CenterManager\"}"); simpleReply(resultCallback,"onLogin","CenterManager");
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
resultCallback.onResult( simpleReply(resultCallback,"onLogin",Role.Anonymous.name());
"{\"action\":\"onLogin\",\"data\":\"" + Role.Anonymous.name() + "\"}");
} }
} }
@Action(userPermission = 0) @Action(userPermission = 0)
public void login(JsonObject json, ResultCallback resultCallback) { public void login(JsonObject json, ResultCallback resultCallback) {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
JsonObject jsonResult = new JsonObject();
if (sessionID == null) { if (sessionID == null) {
resultCallback.onResult( simpleReply(resultCallback,"onLogin",Role.Anonymous.name());
"{\"action\":\"onLogin\",\"data\":\"" + Role.Anonymous.name() + "\"}");
return; return;
} }
String signature = json.get("signature").getAsString(); String signature = json.get("signature").getAsString();
@ -135,8 +138,7 @@ public class NCManagerAction {
LOGGER.debug("设置公钥" + pubKey); LOGGER.debug("设置公钥" + pubKey);
getRole(json, resultCallback); getRole(json, resultCallback);
} else { } else {
resultCallback.onResult( simpleReply(resultCallback,"onLogin",Role.Anonymous.name());
"{\"action\":\"onLogin\",\"data\":\"" + Role.Anonymous.name() + "\"}");
} }
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
LOGGER.debug("time:" + (end - start)); LOGGER.debug("time:" + (end - start));
@ -146,7 +148,7 @@ public class NCManagerAction {
public void applyRole(JsonObject json, ResultCallback resultCallback) { public void applyRole(JsonObject json, ResultCallback resultCallback) {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
if (pubKey == null) { if (pubKey == null) {
resultCallback.onResult("{\"action\":\"onApplyRole\",\"data\":\"missing pubKey\"}"); simpleReply(resultCallback,"onApplyRole","missing pubKey");
return; return;
} }
if (json.has("role")) { if (json.has("role")) {
@ -156,7 +158,7 @@ public class NCManagerAction {
return; return;
} }
} }
resultCallback.onResult("{\"action\":\"onApplyRole\",\"data\":\"failed\"}"); simpleReply(resultCallback,"onApplyRole","failed");
} }
@Action(userPermission = 1 << 6) @Action(userPermission = 1 << 6)
@ -170,10 +172,10 @@ public class NCManagerAction {
NCTables.NodeTime.toString(), NCTables.NodeTime.toString(),
json.get("nodePubKey").getAsString(), json.get("nodePubKey").getAsString(),
Long.toString(new Date().getTime())); Long.toString(new Date().getTime()));
resultCallback.onResult("{\"action\":\"onAddNodes\",\"data\":\"success\"}"); simpleReply(resultCallback,"onAddNodes","success");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); 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 str = KeyValueDBUtil.instance.getValue(NCTables.ApplyRole.toString(), pubKey);
String already = KeyValueDBUtil.instance.getValue(NCTables.NodeUser.toString(), pubKey); String already = KeyValueDBUtil.instance.getValue(NCTables.NodeUser.toString(), pubKey);
if (already != null && already.contains(role.toString())) { if (already != null && already.contains(role.toString())) {
resultCallback.onResult("{\"action\":\"onApplyRole\",\"data\":\"already has!\"}"); simpleReply(resultCallback,"onApplyRole","already has!");
return; return;
} }
if (str == null || str.length() == 0) { if (str == null || str.length() == 0) {
@ -196,7 +198,7 @@ public class NCManagerAction {
NCTables.ApplyTime.toString(), pubKey, Long.toString(new Date().getTime())); NCTables.ApplyTime.toString(), pubKey, Long.toString(new Date().getTime()));
} }
} }
resultCallback.onResult("{\"action\":\"onApplyRole\",\"data\":\"success\"}"); simpleReply(resultCallback,"onApplyRole","success!");
} }
@Action(userPermission = 1 << 3) @Action(userPermission = 1 << 3)
@ -204,12 +206,12 @@ public class NCManagerAction {
List<KV> kv = KeyValueDBUtil.instance.getKeyValues(NCTables.NodeUser.toString()); List<KV> kv = KeyValueDBUtil.instance.getKeyValues(NCTables.NodeUser.toString());
List<KV> time = KeyValueDBUtil.instance.getKeyValues(NCTables.NodeTime.toString()); List<KV> time = KeyValueDBUtil.instance.getKeyValues(NCTables.NodeTime.toString());
Map<String, Object> ret = new HashMap<>(); Map<String, Object> ret = new HashMap<>();
ResultBack result = new ResultBack();
ret.put("kv", kv); ret.put("kv", kv);
ret.put("time", time); ret.put("time", time);
result.action = "onListAllUsers"; Map<String, Object> result = new HashMap<>();
result.data = ret; result.put("action", "onListAllUsers");
resultCallback.onResult(JsonUtil.toJson(result)); result.put("data", ret);
resultCallback.onResult(result);
} }
@Action(userPermission = 1 << 2) @Action(userPermission = 1 << 2)
@ -219,20 +221,21 @@ public class NCManagerAction {
boolean isAccept = json.get("isAccept").getAsBoolean(); boolean isAccept = json.get("isAccept").getAsBoolean();
LOGGER.debug("[NCManagerAction] " + json.toString()); LOGGER.debug("[NCManagerAction] " + json.toString());
if (pubKey == null || pubKey.length() == 0) { if (pubKey == null || pubKey.length() == 0) {
resultCallback.onResult( simpleReply(resultCallback,"onAuthNodeManager","missing pubKey");
"{\"action\":\"onAuthNodeManager\",\"data\":\"missing pubKey\"}");
return; return;
} }
if (!isAccept) { if (!isAccept) {
KeyValueDBUtil.instance.delete(NCTables.ApplyRole.toString(), pubKey); KeyValueDBUtil.instance.delete(NCTables.ApplyRole.toString(), pubKey);
KeyValueDBUtil.instance.delete(NCTables.ApplyTime.toString(), pubKey); KeyValueDBUtil.instance.delete(NCTables.ApplyTime.toString(), pubKey);
resultCallback.onResult("{\"action\":\"onAuthNodeManager\",\"data\":\"success\"}"); simpleReply(resultCallback,"onAuthNodeManager","success");
return; return;
} }
String already = KeyValueDBUtil.instance.getValue(NCTables.NodeUser.toString(), pubKey); String already = KeyValueDBUtil.instance.getValue(NCTables.NodeUser.toString(), pubKey);
String roles = KeyValueDBUtil.instance.getValue(NCTables.ApplyRole.toString(), pubKey); String roles = KeyValueDBUtil.instance.getValue(NCTables.ApplyRole.toString(), pubKey);
if (roles == null || roles.length() == 0) { 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; return;
} }
if (already != null && already.length() > 0) { if (already != null && already.length() > 0) {
@ -240,13 +243,12 @@ public class NCManagerAction {
} else { } else {
already = roles; already = roles;
} }
KeyValueDBUtil.instance.setValue(NCTables.NodeUser.toString(), pubKey, already); KeyValueDBUtil.instance.setValue(NCTables.NodeUser.toString(), pubKey, already);
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(
NCTables.NodeTime.toString(), pubKey, Long.toString(new Date().getTime())); NCTables.NodeTime.toString(), pubKey, Long.toString(new Date().getTime()));
KeyValueDBUtil.instance.delete(NCTables.ApplyRole.toString(), pubKey); KeyValueDBUtil.instance.delete(NCTables.ApplyRole.toString(), pubKey);
KeyValueDBUtil.instance.delete(NCTables.ApplyTime.toString(), pubKey); KeyValueDBUtil.instance.delete(NCTables.ApplyTime.toString(), pubKey);
resultCallback.onResult("{\"action\":\"onAuthNodeManager\",\"data\":\"success\"}"); simpleReply(resultCallback,"onAuthNodeManager","success");
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
LOGGER.debug("[authNodeManager:time]" + (end - start)); LOGGER.debug("[authNodeManager:time]" + (end - start));
} }
@ -259,10 +261,10 @@ public class NCManagerAction {
Map<String, Object> ret = new HashMap<>(); Map<String, Object> ret = new HashMap<>();
ret.put("kv", kv); ret.put("kv", kv);
ret.put("time", time); ret.put("time", time);
ResultBack result = new ResultBack(); Map<String, Object> result = new HashMap<>();
result.action = "onListApplyList"; result.put("action", "onListApplyList");
result.data = ret; result.put("data", ret);
resultCallback.onResult(JsonUtil.toJson(result)); resultCallback.onResult(result);
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
LOGGER.debug("[listApplyList:time]" + (end - start)); LOGGER.debug("[listApplyList:time]" + (end - start));
} }
@ -272,7 +274,7 @@ public class NCManagerAction {
String pubKey = json.get("pubKey").getAsString(); String pubKey = json.get("pubKey").getAsString();
KeyValueDBUtil.instance.delete(NCTables.NodeUser.toString(), pubKey); KeyValueDBUtil.instance.delete(NCTables.NodeUser.toString(), pubKey);
KeyValueDBUtil.instance.delete(NCTables.NodeTime.toString(), pubKey); KeyValueDBUtil.instance.delete(NCTables.NodeTime.toString(), pubKey);
resultCallback.onResult("{\"action\":\"onDelete\",\"data\":\"success\"}"); simpleReply(resultCallback,"onDelete","success");
} }
@Action(userPermission = 1 << 4) @Action(userPermission = 1 << 4)
@ -280,16 +282,22 @@ public class NCManagerAction {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
String licence = KeyValueDBUtil.instance.getValue(NCTables.ConfigDB.toString(), Licence); String licence = KeyValueDBUtil.instance.getValue(NCTables.ConfigDB.toString(), Licence);
if (licence == null || licence.length() == 0) { if (licence == null || licence.length() == 0) {
resultCallback.onResult( JsonObject jsonResult = new JsonObject();
"{\"action\":\"onListLicence\",\"nodeCount\":" + 0 + ",\"dueDate\":\"已过期\"}"); jsonResult.addProperty("action", "onListLicence");
jsonResult.addProperty("nodeCount", 0);
jsonResult.addProperty("dueDate", "已过期");
resultCallback.onResult(jsonResult);
return; return;
} }
json = JsonParser.parseString(licence).getAsJsonObject(); json = JsonParser.parseString(licence).getAsJsonObject();
String sign = json.get("sign").getAsString(); String sign = json.get("sign").getAsString();
String content = json.get("content").getAsString(); String content = json.get("content").getAsString();
if (StringUtil.isNullOrEmpty(sign) || StringUtil.isNullOrEmpty(content)) { if (StringUtil.isNullOrEmpty(sign) || StringUtil.isNullOrEmpty(content)) {
resultCallback.onResult( JsonObject jsonResult = new JsonObject();
"{\"action\":\"onListLicence\",\"nodeCount\":0,\"dueDate\":\"已过期\"}"); jsonResult.addProperty("action", "onListLicence");
jsonResult.addProperty("nodeCount", 0);
jsonResult.addProperty("dueDate", "已过期");
resultCallback.onResult(jsonResult);
return; return;
} }
long expiredDate; long expiredDate;
@ -299,30 +307,38 @@ public class NCManagerAction {
nodeCount = jo.get("nodeCount").getAsLong(); nodeCount = jo.get("nodeCount").getAsLong();
expiredDate = jo.get("expiredDate").getAsLong(); expiredDate = jo.get("expiredDate").getAsLong();
} catch (Exception e) { } catch (Exception e) {
resultCallback.onResult( JsonObject jsonResult = new JsonObject();
"{\"action\":\"onListLicence\",\"nodeCount\":0,\"dueDate\":\"已过期\"}"); jsonResult.addProperty("action", "onListLicence");
jsonResult.addProperty("nodeCount", 0);
jsonResult.addProperty("dueDate", "已过期");
resultCallback.onResult(jsonResult);
return; return;
} }
if (expiredDate == 0 || nodeCount == 0) { if (expiredDate == 0 || nodeCount == 0) {
resultCallback.onResult( JsonObject jsonResult = new JsonObject();
"{\"action\":\"onListLicence\",\"nodeCount\":0,\"dueDate\":\"已过期\"}"); jsonResult.addProperty("action", "onListLicence");
jsonResult.addProperty("nodeCount", 0);
jsonResult.addProperty("dueDate", "已过期");
resultCallback.onResult(jsonResult);
return; return;
} }
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
boolean verify = boolean verify =
SM2Util.verify(licencePub, content.getBytes(), ByteUtils.fromHexString(sign)); SM2Util.verify(licencePub, content.getBytes(), ByteUtils.fromHexString(sign));
if (verify) { if (verify) {
resultCallback.onResult( JsonObject jsonResult = new JsonObject();
"{\"action\":\"onListLicence\",\"nodeCount\":" jsonResult.addProperty("action", "onListLicence");
+ nodeCount jsonResult.addProperty("nodeCount", nodeCount);
+ ",\"dueDate\":\"" jsonResult.addProperty("dueDate", format.format(new Date(expiredDate)));
+ format.format(new Date(expiredDate)) resultCallback.onResult(jsonResult);
+ "\"}");
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
LOGGER.debug("[listLicence:time]" + (end - start)); LOGGER.debug("[listLicence:time]" + (end - start));
} else { } else {
resultCallback.onResult( JsonObject jsonResult = new JsonObject();
"{\"action\":\"onListLicence\",\"nodeCount\":0,\"dueDate\":\"已过期\"}"); jsonResult.addProperty("action", "onListLicence");
jsonResult.addProperty("nodeCount", 0);
jsonResult.addProperty("dueDate", "已过期");
resultCallback.onResult(jsonResult);
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
LOGGER.debug("[listLicence:time]" + (end - start)); LOGGER.debug("[listLicence:time]" + (end - start));
} }
@ -334,8 +350,7 @@ public class NCManagerAction {
String sign = json.get("sign").getAsString(); String sign = json.get("sign").getAsString();
String content = json.get("content").getAsString(); String content = json.get("content").getAsString();
if (StringUtil.isNullOrEmpty(sign) || StringUtil.isNullOrEmpty(content)) { if (StringUtil.isNullOrEmpty(sign) || StringUtil.isNullOrEmpty(content)) {
resultCallback.onResult( simpleReply(resultCallback,"onUpdateLicence","missing signature or content");
"{\"action\":\"onUpdateLicence\",\"data\":\"missing signature or content\"}");
return; return;
} }
long expiredDate; long expiredDate;
@ -348,18 +363,16 @@ public class NCManagerAction {
uuid = jo.get("uuid").getAsString(); uuid = jo.get("uuid").getAsString();
} catch (Exception e) { } catch (Exception e) {
resultCallback.onResult("{\"action\":\"onUpdateLicence\",\"data\":\"content error\"}"); simpleReply(resultCallback,"onUpdateLicence","content error");
return; return;
} }
if (expiredDate == 0 || nodeCount == 0) { if (expiredDate == 0 || nodeCount == 0) {
resultCallback.onResult( simpleReply(resultCallback,"onUpdateLicence","parse expiredDate/nodeCount error");
"{\"action\":\"onUpdateLicence\",\"data\":\"parse expiredDate/nodeCount error\"}");
return; return;
} }
if (!uuid.equals( if (!uuid.equals(
ByteUtil.encodeBASE64(HardwareInfo.getCPUID().getBytes()).replaceAll("\n", ""))) { ByteUtil.encodeBASE64(HardwareInfo.getCPUID().getBytes()).replaceAll("\n", ""))) {
resultCallback.onResult( simpleReply(resultCallback,"onUpdateLicence","invalid licence");
"{\"action\":\"onUpdateLicence\",\"data\":\"invalid licence\"}");
return; return;
} }
boolean verify = boolean verify =
@ -368,11 +381,11 @@ public class NCManagerAction {
if (verify) { if (verify) {
KeyValueDBUtil.instance.setValue( KeyValueDBUtil.instance.setValue(
NCTables.ConfigDB.toString(), Licence, json.toString()); NCTables.ConfigDB.toString(), Licence, json.toString());
resultCallback.onResult("{\"action\":\"onUpdateLicence\",\"data\":\"success\"}"); simpleReply(resultCallback,"onUpdateLicence","success");
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
LOGGER.debug("[listLicence:time]" + (end - start)); LOGGER.debug("[listLicence:time]" + (end - start));
return; return;
} else resultCallback.onResult("{\"action\":\"onUpdateLicence\",\"data\":\"failed\"}"); } else simpleReply(resultCallback,"onUpdateLicence","failed");
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
LOGGER.debug("[updateLicence:time]" + (end - start)); LOGGER.debug("[updateLicence:time]" + (end - start));
} }
@ -383,7 +396,7 @@ public class NCManagerAction {
result.put("action", "onGetOtherNC"); result.put("action", "onGetOtherNC");
String s = OtherNCProxy.instance.getOtherNCs().replace(";", " "); String s = OtherNCProxy.instance.getOtherNCs().replace(";", " ");
result.put("address", s); result.put("address", s);
resultCallback.onResult(JsonUtil.toJson(result)); resultCallback.onResult(result);
} }
// 改为NodeManager权限 // 改为NodeManager权限
@ -391,11 +404,10 @@ public class NCManagerAction {
public void changeOtherNC(JsonObject json, ResultCallback resultCallback) { public void changeOtherNC(JsonObject json, ResultCallback resultCallback) {
String add = json.get("data").getAsString().replace(" ", ";"); String add = json.get("data").getAsString().replace(" ", ";");
String s = OtherNCProxy.instance.setOtherNCs(add).replace(";", " "); String s = OtherNCProxy.instance.setOtherNCs(add).replace(";", " ");
JsonObject result = new JsonObject();
Map<String, String> result = new HashMap<>(); result.addProperty("action", "onGetOtherNC");
result.put("action", "onGetOtherNC"); result.addProperty("address", s);
result.put("address", s); resultCallback.onResult(result);
resultCallback.onResult(JsonUtil.toJson(result));
} }
// NodeManager权限 // NodeManager权限
@ -403,21 +415,19 @@ public class NCManagerAction {
public void changeNCFile(JsonObject json, ResultCallback resultCallback) { public void changeNCFile(JsonObject json, ResultCallback resultCallback) {
String add = json.get("data").getAsString(); String add = json.get("data").getAsString();
String s = OtherNCProxy.instance.setNCFile(add); String s = OtherNCProxy.instance.setNCFile(add);
JsonObject result = new JsonObject();
Map<String, String> result = new HashMap<>(); result.addProperty("action", "onGetNCFile");
result.put("action", "onGetNCFile"); result.addProperty("fileName", s);
result.put("fileName", s); resultCallback.onResult(result);
resultCallback.onResult(JsonUtil.toJson(result));
} }
@Action(userPermission = 1 << 9) @Action(userPermission = 1 << 9)
public void getNCFile(JsonObject json, ResultCallback resultCallback) { public void getNCFile(JsonObject json, ResultCallback resultCallback) {
String s = OtherNCProxy.instance.getNCFile(); String s = OtherNCProxy.instance.getNCFile();
Map<String, String> result = new HashMap<>(); Map<String, String> result = new HashMap<>();
result.put("action", "onGetNCFile"); result.put("action", "onGetNCFile");
result.put("fileName", s); result.put("fileName", s);
resultCallback.onResult(JsonUtil.toJson(result)); resultCallback.onResult(result);
} }
// TODO 迁移合约实例从节点A将合约M的实例迁移到节点B的权限? // TODO 迁移合约实例从节点A将合约M的实例迁移到节点B的权限?
@ -427,7 +437,6 @@ public class NCManagerAction {
@Action(userPermission = 0) @Action(userPermission = 0)
public void transferContractInstance(JsonObject json, ResultCallback resultCallback) { public void transferContractInstance(JsonObject json, ResultCallback resultCallback) {
LOGGER.info("transferContractInstance"); LOGGER.info("transferContractInstance");
String node1ID = json.get("node1ID").getAsString(); String node1ID = json.get("node1ID").getAsString();
String node2ID = json.get("node2ID").getAsString(); String node2ID = json.get("node2ID").getAsString();
String contractID = json.get("contractID").getAsString(); String contractID = json.get("contractID").getAsString();
@ -468,6 +477,6 @@ public class NCManagerAction {
+ node1ID + node1ID
+ " to node:" + " to node:"
+ node2ID); + node2ID);
resultCallback.onResult(JsonUtil.toJson(result)); resultCallback.onResult(result);
} }
} }

View File

@ -16,6 +16,7 @@ import org.bdware.server.action.ActionExecutor;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.Map;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
@ -33,8 +34,6 @@ public class NodeCenterWSFrameHandler extends SimpleChannelInboundHandler<WebSoc
managerAction = new NCManagerAction(this); managerAction = new NCManagerAction(this);
logActions = new LogActions(managerAction); logActions = new LogActions(managerAction);
unitActions = new UnitActions(managerAction); unitActions = new UnitActions(managerAction);
ae = ae =
new ActionExecutor<ResultCallback, JsonObject>( new ActionExecutor<ResultCallback, JsonObject>(
executorService, managerAction, logActions, unitActions, new MetaIndexAction(), new TracingAction()) { executorService, managerAction, logActions, unitActions, new MetaIndexAction(), new TracingAction()) {
@ -111,10 +110,27 @@ public class NodeCenterWSFrameHandler extends SimpleChannelInboundHandler<WebSoc
} }
} }
String action = map.get("action").getAsString(); String action = map.get("action").getAsString();
final JsonObject jmap = map;
ae.handle( ae.handle(
action, action,
map, map,
new ResultCallback() { new ResultCallback() {
@Override
public void onResult(Map jo) {
if (jmap.has("requestID")) {
jo.put("responseID", jmap.get("requestID"));
}
onResult(JsonUtil.toJson(jo));
}
@Override
public void onResult(JsonObject jo) {
if (jmap.has("requestID")) {
jo.add("responseID", jmap.get("requestID"));
}
onResult(jo.toString());
}
@Override @Override
public void onResult(String ret) { public void onResult(String ret) {
if (ret != null) if (ret != null)

View File

@ -52,10 +52,8 @@ public class TracingAction {
public void getDependentContract(JsonObject json, ResultCallback rc) { public void getDependentContract(JsonObject json, ResultCallback rc) {
JsonObject result = getDependentContract(json); JsonObject result = getDependentContract(json);
JsonObject jo = new JsonObject(); JsonObject jo = new JsonObject();
if (json.has("requestID"))
jo.add("responseID", json.get("requestID"));
jo.add("result", result); jo.add("result", result);
jo.addProperty("action", "onGetDependentContract"); jo.addProperty("action", "onGetDependentContract");
rc.onResult(jo.toString()); rc.onResult(jo);
} }
} }

View File

@ -19,7 +19,12 @@ public class UnitActions {
public UnitActions(NCManagerAction managerAction) { public UnitActions(NCManagerAction managerAction) {
this.managerAction = managerAction; this.managerAction = managerAction;
} }
private void simpleReply(ResultCallback resultCallback, String action, Object data){
Map<String,Object> jsonResult = new HashMap<>();
jsonResult.put("action", action);
jsonResult.put("data", data);
resultCallback.onResult(jsonResult);
}
private static String convertContractName(String contractID) { private static String convertContractName(String contractID) {
ContractDesp desp = NodeCenterActions.getContractByName(contractID); ContractDesp desp = NodeCenterActions.getContractByName(contractID);
if (desp != null) return desp.contractID; if (desp != null) return desp.contractID;
@ -36,18 +41,13 @@ public class UnitActions {
public void listTrustUnits(JsonObject json, ResultCallback resultCallback) { public void listTrustUnits(JsonObject json, ResultCallback resultCallback) {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
final String pubKey = managerAction.pubKey; final String pubKey = managerAction.pubKey;
Map<String, Object> ret = new HashMap<>();
List<KV> allunits = KeyValueDBUtil.instance.getKeyValues(NCTables.TrustUnitsDB.toString()); List<KV> allunits = KeyValueDBUtil.instance.getKeyValues(NCTables.TrustUnitsDB.toString());
if (pubKey != null if (pubKey != null
&& KeyValueDBUtil.instance && KeyValueDBUtil.instance
.getValue(NCTables.ConfigDB.toString(), "__CenterManager__") .getValue(NCTables.ConfigDB.toString(), "__CenterManager__")
.contains(pubKey)) { .contains(pubKey)) {
simpleReply(resultCallback,"onListTrustUnits",allunits);
LOGGER.debug("is center manager"); 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(); long end = System.currentTimeMillis();
LOGGER.debug("[listTrustUnits:time]" + (end - start)); LOGGER.debug("[listTrustUnits:time]" + (end - start));
return; return;
@ -61,10 +61,7 @@ public class UnitActions {
ciunits.add(kv); ciunits.add(kv);
} }
} }
ret.put("action", "onListTrustUnits"); simpleReply(resultCallback,"onListTrustUnits",ciunits);
ret.put("data", ciunits);
LOGGER.debug("[listTrustUnits] " + JsonUtil.toJson(ret));
resultCallback.onResult(JsonUtil.toJson(ret));
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
LOGGER.debug("[listTrustUnits:time]" + (end - start)); LOGGER.debug("[listTrustUnits:time]" + (end - start));
} }
@ -80,7 +77,7 @@ public class UnitActions {
Map<String, Object> ret = new HashMap<>(); Map<String, Object> ret = new HashMap<>();
ret.put("action", "onCreateTrustUnit"); ret.put("action", "onCreateTrustUnit");
ret.put("status", "Success"); ret.put("status", "Success");
resultCallback.onResult(JsonUtil.toJson(ret)); resultCallback.onResult(ret);
} }
@Action(userPermission = 1 << 6, async = true) @Action(userPermission = 1 << 6, async = true)
@ -99,7 +96,7 @@ public class UnitActions {
Map<String, Object> ret = new HashMap<>(); Map<String, Object> ret = new HashMap<>();
ret.put("action", "onDeleteTrustUnit"); ret.put("action", "onDeleteTrustUnit");
ret.put("status", "Success"); ret.put("status", "Success");
resultCallback.onResult(JsonUtil.toJson(ret)); resultCallback.onResult(ret);
} }
// @Action(async = true) // @Action(async = true)
@ -119,10 +116,7 @@ public class UnitActions {
nodeunits.add(kv); nodeunits.add(kv);
} }
} }
Map<String, Object> ret = new HashMap<>(); simpleReply(resultCallback,"onGetNodeTrustUnits",nodeunits);
ret.put("action", "onGetNodeTrustUnits");
ret.put("data", nodeunits);
resultCallback.onResult(JsonUtil.toJson(ret));
} }
// @Action(async = true, userPermission = 0) // @Action(async = true, userPermission = 0)
@ -198,8 +192,6 @@ public class UnitActions {
@Action(async = true, userPermission = 1L << 6) @Action(async = true, userPermission = 1L << 6)
public void listContractProcess(JsonObject args, final ResultCallback rc) { public void listContractProcess(JsonObject args, final ResultCallback rc) {
Map<String, Object> ret = new HashMap<>();
ret.put("action", "onListContractProcess");
List<ContractDesp> info = new ArrayList<>(); List<ContractDesp> info = new ArrayList<>();
LOGGER.debug( LOGGER.debug(
"[contracts] " "[contracts] "
@ -211,14 +203,11 @@ public class UnitActions {
ContractDesp desp = NodeCenterActions.getContractByID(key); ContractDesp desp = NodeCenterActions.getContractByID(key);
if (desp != null) info.add(desp); if (desp != null) info.add(desp);
} }
ret.put("data", JsonUtil.toJson(info)); simpleReply(rc,"onListContractProcess",JsonUtil.toJson(info));
rc.onResult(JsonUtil.toJson(ret));
} }
@Action(userPermission = 1 << 6, async = true) @Action(userPermission = 1 << 6, async = true)
public void listMultiPointContractProcess(JsonObject json, ResultCallback resultCallback) { public void listMultiPointContractProcess(JsonObject json, ResultCallback resultCallback) {
Map<String, Object> ret = new HashMap<>();
ret.put("action", "onListMultiPointContractProcess");
List<ContractDesp> info = new ArrayList<>(); List<ContractDesp> info = new ArrayList<>();
LOGGER.debug( LOGGER.debug(
"[contracts] " "[contracts] "
@ -230,8 +219,7 @@ public class UnitActions {
ContractDesp desp = NodeCenterActions.getContractByID(key); ContractDesp desp = NodeCenterActions.getContractByID(key);
if (desp != null) info.add(desp); if (desp != null) info.add(desp);
} }
ret.put("data", JsonUtil.toJson(info)); simpleReply(resultCallback,"onListMultiPointContractProcess",JsonUtil.toJson(info));
resultCallback.onResult(JsonUtil.toJson(ret));
} }
@Action(userPermission = 1 << 6, async = true) @Action(userPermission = 1 << 6, async = true)
@ -398,10 +386,4 @@ public class UnitActions {
// + contract.getID() // + contract.getID()
// + "\",\"action\":\"onStartTrustfulContract\"}"); // + "\",\"action\":\"onStartTrustfulContract\"}");
// } // }
private void example() {
for (CMNode node : NodeCenterActions.nodeInfos.values()) {
// node.connection.executeContractTrustfully(args, rc);
}
}
} }