Пример #1
0
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;
}
Пример #2
0
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;
}
Пример #3
0
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);
}
Пример #4
0
bool round_system_method_removeregistry(RoundLocalNode* node, RoundJSONObject* params, RoundJSONObject** result, RoundError* err)
{
  const char* key;

  if (!round_json_object_ismap(params)) {
    round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INVALID_PARAMS);
    return false;
  }

  if (!round_json_map_getstring(params, ROUND_SYSTEM_METHOD_PARAM_KEY, &key)) {
    round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INVALID_PARAMS);
    return false;
  }

  if (!round_local_node_removeregistry(node, key)) {
    round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR);
    return false;
  }

  return true;
}
Пример #5
0
bool round_system_method_getregistry(RoundLocalNode* node, RoundJSONObject* params, RoundJSONObject** resultMap, RoundError* err)
{
  const char* key, *val;
  RoundRegistry* reg;
  
  if (!round_json_object_ismap(params)) {
    round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INVALID_PARAMS);
    return false;
  }

  if (!round_json_map_getstring(params, ROUND_SYSTEM_METHOD_PARAM_KEY, &key)) {
    round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INVALID_PARAMS);
    return false;
  }

  reg = round_local_node_getregistry(node, key);
  if (!reg) {
    round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INVALID_PARAMS);
    return false;
  }

  *resultMap = round_json_map_new();
  if (!(*resultMap)) {
    round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR);
    return false;
  }

  if (!round_registry_getstring(reg, &val)) {
    round_error_setjsonrpcerrorcode(err, ROUND_RPC_ERROR_CODE_INTERNAL_ERROR);
    return false;
  }
  
  round_json_map_setstring((*resultMap), ROUND_SYSTEM_METHOD_PARAM_KEY, key);
  round_json_map_setstring((*resultMap), ROUND_SYSTEM_METHOD_PARAM_VALUE, val);
  round_json_map_setinteger((*resultMap), ROUND_SYSTEM_METHOD_PARAM_TS, round_registry_getts(reg));
  round_json_map_setinteger((*resultMap), ROUND_SYSTEM_METHOD_PARAM_LTS, round_registry_getlts(reg));

  return true;
}
Пример #6
0
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;
}
Пример #7
0
bool round_error_setjsonrpcerror(RoundError* err, RoundJSONObject* errObj)
{
  if (!err || !errObj)
    return false;

  if (!round_json_object_ismap(errObj))
    return false;

  long errCode;
  if (round_json_rpc_geterrorcode(errObj, &errCode)) {
    round_error_setjsonrpcerrorcode(err, (int)errCode);
    round_error_setdetailcode(err, (int)errCode);
  }

  const char* errMsg;
  if (round_json_rpc_geterrormessage(errObj, &errMsg)) {
    round_error_setdetailmessage(err, errMsg);
  }

  return true;
}
Пример #8
0
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, &params);

  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;
}