bool round_node_getregistry(RoundNode* node, const char* key, char** value, RoundError* err) { if (!node || !key || !value) return false; // Create request RoundJSONObject* reqObj = round_json_rpc_request_new(); if (!reqObj) { round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR); return false; } round_json_rpc_setmethod(reqObj, ROUND_SYSTEM_METHOD_GET_REGISTRY); // Set params RoundJSONObject* paramsObj = round_json_map_new(); if (!paramsObj) { round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR); round_json_object_delete(reqObj); return false; } round_json_map_setstring(paramsObj, ROUND_SYSTEM_METHOD_PARAM_KEY, key); round_json_rpc_setparamsobject(reqObj, paramsObj); // Request RoundJSONObject* resObj = NULL; bool isSuccess = round_node_postmessage(node, reqObj, &resObj, err); // Get result if (isSuccess) { RoundJSONObject* resultObj; if (round_json_rpc_getparamsobject(reqObj, &resultObj)) { const char* keyValue; if (round_json_map_getstring(paramsObj, ROUND_SYSTEM_METHOD_PARAM_KEY, &keyValue)) { *value = round_strdup(keyValue); } } } // Delete objects if (resObj) { round_json_object_delete(resObj); } round_json_object_delete(reqObj); return isSuccess; }
bool round_node_setmethod(RoundNode* node, const char* lang, const char* name, const char* code, RoundEncodeType encType, RoundError* err) { if (!node || !lang || !name || !code) return false; // Create request RoundJSONObject* reqObj = round_json_rpc_request_new(); if (!reqObj) { round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR); return false; } round_json_rpc_setmethod(reqObj, ROUND_SYSTEM_METHOD_SET_METHOD); // Set params RoundJSONObject* paramsObj = round_json_map_new(); if (!paramsObj) { round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR); round_json_object_delete(reqObj); return false; } round_json_map_setstring(paramsObj, ROUND_SYSTEM_METHOD_PARAM_LANGUAGE, lang); round_json_map_setstring(paramsObj, ROUND_SYSTEM_METHOD_PARAM_NAME, name); round_json_map_setstring(paramsObj, ROUND_SYSTEM_METHOD_PARAM_CODE, code); switch (encType) { case RoundEncodeBase64: round_json_map_setstring(paramsObj, ROUND_SYSTEM_METHOD_PARAM_ENCODE, ROUND_SYSTEM_METHOD_PARAM_BASE64); break; default: break; } round_json_rpc_setparamsobject(reqObj, paramsObj); // Request RoundJSONObject* resObj = NULL; bool isSuccess = round_node_postmessage(node, reqObj, &resObj, err); // Delete objects if (resObj) { round_json_object_delete(resObj); } round_json_object_delete(reqObj); return isSuccess; }
bool round_json_object_delete(RoundJSONObject* obj) { if (!obj) return false; round_json_object_clearcaches(obj); #if defined(ROUND_USE_JSON_PARSER_JANSSON) if (obj->jsonObj) { json_decref(obj->jsonObj); obj->jsonObj = NULL; } #endif // Clear Child Nodes if (obj->childObj) { if (obj->childObj->childObj) { round_json_object_delete(obj->childObj->childObj); obj->childObj->childObj = NULL; } free(obj->childObj); obj->childObj = NULL; } free(obj); return true; }
bool round_json_delete(RoundJSON* json) { if (!json) return false; round_json_clear(json); round_json_object_delete(json->pathObj); free(json); return true; }
bool round_node_removemethod(RoundNode* node, const char* name, RoundError* err) { if (!node || !name) return false; // Create request RoundJSONObject* reqObj = round_json_rpc_request_new(); if (!reqObj) { round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR); return false; } round_json_rpc_setmethod(reqObj, ROUND_SYSTEM_METHOD_REMOVE_METHOD); // Set params RoundJSONObject* paramsObj = round_json_map_new(); if (!paramsObj) { round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR); round_json_object_delete(reqObj); return false; } round_json_map_setstring(paramsObj, ROUND_SYSTEM_METHOD_PARAM_NAME, name); round_json_rpc_setparamsobject(reqObj, paramsObj); // Request RoundJSONObject* resObj = NULL; bool isSuccess = round_node_postmessage(node, reqObj, &resObj, err); // Delete objects if (resObj) { round_json_object_delete(resObj); } round_json_object_delete(reqObj); return isSuccess; }
bool round_json_clear(RoundJSON* json) { if (!json) return false; if (json->rootObj) { round_json_object_delete(json->rootObj); json->rootObj = NULL; } round_json_setoption(json, 0); round_json_object_setjanssonobject(json->pathObj, NULL); return true; }
RoundJSONObject* round_json_object_new(void) { RoundJSONObject* obj; obj = (RoundJSONObject*)malloc(sizeof(RoundJSONObject)); if (!obj) return NULL; if (!round_json_object_init(obj)) { round_json_object_delete(obj); return NULL; } return obj; }
bool round_local_node_postmessage(RoundLocalNode* node, RoundJSONObject* reqObj, RoundJSONObject** resObj, RoundError* err) { if (!node || !reqObj || !resObj || !err) { round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR); return false; } *resObj = NULL; // Updated local clock round_local_node_updateclockbyjsonobject(node, reqObj); // Check request if (!round_json_object_ismap(reqObj) && !round_json_object_isarray(reqObj)) { round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INVALID_REQUEST); return false; } // Single request if (round_json_object_ismap(reqObj)) return round_local_node_execrequest(node, reqObj, resObj, err); // Batch request if (round_json_object_isarray(reqObj)) { *resObj = round_json_array_new(); size_t msgArrayCnt = round_json_array_size(reqObj); size_t n; for (n = 0; n < msgArrayCnt; n++) { RoundJSONObject* reqArrayObj = round_json_array_get(reqObj, n); RoundJSONObject* resArrayObj = NULL; round_local_node_execrequest(node, reqArrayObj, &resArrayObj, err); if (reqArrayObj) { round_json_array_append(*resObj, resArrayObj); round_json_object_delete(resArrayObj); } } return true; } return round_node_rpcerrorcode2error(node, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR, err); }
bool round_json_rpc_seterror(RoundJSONObject* mapObj, RoundError* err) { if (!round_json_object_ismap(mapObj)) return false; RoundJSONObject* errMap = round_json_map_new(); if (!errMap) return false; round_json_map_setinteger(errMap, ROUND_JSON_RPC_CODE, round_error_getdetailcode(err)); round_json_map_setstring(errMap, ROUND_JSON_RPC_MESSAGE, round_error_getdetailmessage(err)); round_json_map_setobject(mapObj, ROUND_JSON_RPC_ERROR, errMap); round_json_object_delete(errMap); return false; }
bool round_local_node_execjsonrequest(RoundLocalNode* node, const char *jsonReq, const char **jsonRes, RoundError* err) { if (!node || !jsonReq || !jsonRes || !err) return round_node_rpcerrorcode2error(node, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR, err); RoundJSON* json = round_json_new(); if (!json) return round_node_rpcerrorcode2error(node, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR, err); if (!round_json_parse(json, jsonReq, err)) { round_json_delete(json); return round_node_rpcerrorcode2error(node, ROUND_RPC_ERROR_CODE_INVALID_REQUEST, err); } // Post request RoundJSONObject* reqObj = round_json_getrootobject(json); if (!reqObj) { round_json_delete(json); return round_node_rpcerrorcode2error(node, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR, err); } RoundJSONObject *resObj = NULL; if (round_local_node_postmessage(node, reqObj, &resObj, err)) { round_json_delete(json); return false; } if (resObj) { round_json_object_tocompactstring(resObj, jsonRes); round_json_object_delete(resObj); } round_json_delete(json); return true; }
JSBool round_js_sm_getregistry(JSContext* cx, unsigned argc, jsval* vp) { if (argc < 1) return JS_FALSE; RoundLocalNode* node = round_js_sm_getlocalnode(); if (!node) return JS_FALSE; JS_BeginRequest(cx); jsval* argv = JS_ARGV(cx, vp); jsval param = argv[0]; #if defined(ROUND_USE_JS_JSON_PARAMS) char key[ROUND_SCRIPT_JS_SM_REGISTRY_KEY_MAX]; if (!JSOBJECT_GET_GETPROPERTYSTRING(cx, param, ROUND_SYSTEM_METHOD_PARAM_KEY, key, sizeof(key))) return false; #else char paramStr[ROUND_SCRIPT_JS_SM_REGISTRY_VALUE_MAX]; if (!JSOBJECT_TO_CSTRING(param, paramStr, sizeof(paramStr))) return false; const char* key; RoundJSON* json = round_json_new(); RoundError* err = round_error_new(); if (json && err && round_json_parse(json, paramStr, err)) { RoundJSONObject* paramObj = round_json_getrootobject(json); if (round_json_object_ismap(paramObj)) { round_json_map_getstring(paramObj, ROUND_SYSTEM_METHOD_PARAM_KEY, &key); } } #endif RoundRegistry* reg = round_local_node_getregistry(node, key); #if !defined(ROUND_USE_JS_JSON_PARAMS) if (json) { round_json_delete(json); } if (err) { round_error_delete(err); } #endif if (reg) { #if defined(ROUND_GETREGISTRY_RETURN_OTHER_PARAMS) RoundJSONObject* map = round_json_map_new(); if (map) { round_script_registry2json(reg, map); const char* result; if (round_json_object_tostring(map, &result)) { JS_SET_STDSTRING_RVAL(cx, vp, result); } round_json_object_delete(map); } else { JS_SET_RVAL(cx, vp, BOOLEAN_TO_JSVAL(JS_FALSE)); } #else const char *val; if (round_registry_getstring(reg, &val)) { JS_SET_STDSTRING_RVAL(cx, vp, val); } else { JS_SET_RVAL(cx, vp, BOOLEAN_TO_JSVAL(JS_FALSE)); } #endif } else { JS_SET_RVAL(cx, vp, BOOLEAN_TO_JSVAL(JS_FALSE)); } JS_EndRequest(cx); return JS_TRUE; }
bool round_local_node_execrequest(RoundLocalNode* node, RoundJSONObject* reqObj, RoundJSONObject** resObj, RoundError* err) { // Set Response *resObj = round_json_rpc_response_new(); if (!resObj) { round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR); return false; } // Set id and timestamp round_json_rpc_setrequestid(*resObj, reqObj); round_json_rpc_settimestamp(*resObj, round_local_node_getclock(node)); // Check node if (!node) { round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR); round_json_rpc_seterror(*resObj, err); return false; } // Check request if (!round_json_object_ismap(reqObj)) { round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INVALID_REQUEST); round_json_rpc_seterror(*resObj, err); return false; } /* // Check dest if (!nodeReq->isDestValid()) { setError(RPC::JSON::ErrorCodeInvalidParams, error); return false; } bool isDestHash = nodeReq->isDestHash(); if (isDestHash) { std::string nodeHash; if (nodeReq->getDest(&nodeHash)) { NodeGraph *nodeGraph = getNodeGraph(); if (!nodeGraph->isHandleNode(this, nodeHash)) { setError(RPC::JSON::ErrorCodeMovedPermanently, error); return false; } } } // Exec Method (One node) bool isDestOne = nodeReq->isDestOne(); if (isDestOne) { return execMethod(nodeReq, nodeRes, error); } // Exec Method (Multi node) JSONArray *batchArray = new JSONArray(); nodeRes->setResult(batchArray); Error thisNodeError; NodeResponse *thisNodeRes = new NodeResponse(); execMethod(nodeReq, thisNodeRes, &thisNodeError); thisNodeRes->setDest(this); batchArray->add(thisNodeRes); NodeList otherNodes; if (nodeReq->isDestAll()) { getAllOtherNodes(&otherNodes); } else if (nodeReq->hasQuorum()) { size_t quorum; if (nodeReq->getQuorum(&quorum)) { getQuorumNodes(&otherNodes, quorum); } } for (NodeList::iterator node = otherNodes.begin(); node != otherNodes.end(); node++) { Error otherNodeError; NodeResponse *otherNodeRes = new NodeResponse(); (*node)->postMessage(nodeReq, otherNodeRes, &otherNodeError); otherNodeRes->setDest((*node)); batchArray->add(otherNodeRes); } */ // Exec Message const char* method = NULL; if (!round_json_rpc_getmethod(reqObj, &method) && (0 < round_strlen(method))) { round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INVALID_REQUEST); round_json_rpc_seterror(*resObj, err); return false; } const char* params = NULL; round_json_rpc_getparamsstring(reqObj, ¶ms); RoundJSONObject* resultObj = NULL; bool isMethodExecuted = round_method_manager_execmethod(node->methodMgr, method, params, &resultObj, err); if (isMethodExecuted) { if (resultObj) { round_json_rpc_setresult(*resObj, resultObj); round_json_object_delete(resultObj); } } else { round_json_rpc_seterror(*resObj, err); } /* bool isMethodExecuted = false; bool isMethodSuccess = false; if (isAliasMethod(name)) { isMethodExecuted = true; isMethodSuccess = execAliasMethod(nodeReq, nodeRes, error); } if (!isMethodExecuted) { setError(ROUND_RPC_ERROR_CODE_METHOD_NOT_FOUND, error); return false; } if (!isMethodSuccess) return false; if (!hasRoute(name)) { return true; } NodeResponse routeNodeRes; if (!execRoute(name, nodeRes, &routeNodeRes, error)) { return false; } nodeRes->set(&routeNodeRes); */ return isMethodExecuted; }