in eventmesh-sdks/eventmesh-sdk-c/src/rmb_msg.c [1038:1560]
int trans_json_2_rmb_msg (StRmbMsg * pStMsg, const char *bodyJson,
const char *command)
{
if (pStMsg == NULL || bodyJson == NULL)
{
LOGRMB (RMB_LOG_ERROR, "pStMsg or bodyJson is null");
return -1;
}
rmb_msg_clear (pStMsg);
pStMsg->cApiType = C_TYPE_WEMQ;
WEMQJSON *systemHeader = NULL;
WEMQJSON *dest = NULL;
WEMQJSON *jsonDecoder = NULL;
WEMQJSON *property = NULL;
WEMQJSON *jsonByteBody = NULL;
WEMQJSON *jsonBody = json_tokener_parse (bodyJson);
if (jsonBody == NULL)
{
LOGRMB (RMB_LOG_ERROR, "json_tokener_parse failed!,buf is:%s", bodyJson);
return -1;
}
if (!json_object_object_get_ex
(jsonBody, MSG_BODY_BYTE_BODY_JSON, &jsonByteBody))
{
LOGRMB (RMB_LOG_ERROR, "body json no byte body!");
return -2;
} //byte body json
if (!json_object_object_get_ex
(jsonBody, MSG_BODY_PROPERTY_JSON, &property))
{
LOGRMB (RMB_LOG_ERROR, "body json no properties!");
return -2;
} //property json
jsonByteBody = json_tokener_parse (json_object_get_string (jsonByteBody));
if (!json_object_object_get_ex
(jsonByteBody, MSG_BODY_BYTE_BODY_SYSTEM_HEADER_CONTENT_JSON,
&systemHeader))
{
LOGRMB (RMB_LOG_ERROR, "byte body json no system header content!");
return -2;
} //system header json
systemHeader = json_tokener_parse (json_object_get_string (systemHeader));
if (json_object_object_get_ex
(systemHeader, MSG_BODY_SYSTEM_BIZ_STR, &jsonDecoder))
{
const char *bizNo = json_object_get_string (jsonDecoder);
if (bizNo != NULL)
{
snprintf (pStMsg->sysHeader.cBizSeqNo,
sizeof (pStMsg->sysHeader.cBizSeqNo), "%s", bizNo);
}
else
{
LOGRMB (RMB_LOG_ERROR, "In systemHeader, %s is null!",
MSG_BODY_SYSTEM_BIZ_STR);
}
} //system header bizno
if (json_object_object_get_ex
(systemHeader, MSG_BODY_SYSTEM_SEQNO_STR, &jsonDecoder))
{
const char *seqNo = json_object_get_string (jsonDecoder);
if (seqNo != NULL)
{
snprintf (pStMsg->sysHeader.cConsumerSeqNo,
sizeof (pStMsg->sysHeader.cConsumerSeqNo), "%s", seqNo);
}
else
{
LOGRMB (RMB_LOG_ERROR, "In systemHeader, %s is null!",
MSG_BODY_SYSTEM_SEQNO_STR);
}
} //system header seqNo
if (json_object_object_get_ex
(systemHeader, MSG_BODY_SYSTEM_SVRID_STR, &jsonDecoder))
{
const char *svrId = json_object_get_string (jsonDecoder);
if (svrId != NULL)
{
snprintf (pStMsg->sysHeader.cConsumerSvrId,
sizeof (pStMsg->sysHeader.cConsumerSvrId), "%s", svrId);
}
else
{
LOGRMB (RMB_LOG_ERROR, "In systemHeader, %s is null!",
MSG_BODY_SYSTEM_SVRID_STR);
}
} //system header svrId
if (json_object_object_get_ex
(systemHeader, MSG_BODY_SYSTEM_ORGSYS_STR, &jsonDecoder))
{
const char *orgId = json_object_get_string (jsonDecoder);
if (orgId != NULL)
{
snprintf (pStMsg->sysHeader.cOrgSysId,
sizeof (pStMsg->sysHeader.cOrgSysId), "%s", orgId);
}
else
{
LOGRMB (RMB_LOG_ERROR, "In systemHeader, %s is null!",
MSG_BODY_SYSTEM_ORGSYS_STR);
}
} //system header orgId
if (json_object_object_get_ex
(systemHeader, MSG_BODY_SYSTEM_CSMID_STR, &jsonDecoder))
{
const char *csmId = json_object_get_string (jsonDecoder);
if (csmId != NULL)
{
snprintf (pStMsg->sysHeader.cConsumerSysId,
sizeof (pStMsg->sysHeader.cConsumerSysId), "%s", csmId);
}
else
{
LOGRMB (RMB_LOG_ERROR, "In systemHeader, %s is null!",
MSG_BODY_SYSTEM_CSMID_STR);
}
} //system header csmId
if (json_object_object_get_ex
(systemHeader, MSG_BODY_SYSTEM_TIME_LINT, &jsonDecoder))
{
pStMsg->sysHeader.ulTranTimeStamp = json_object_get_int64 (jsonDecoder);
} //system header transTime
if (json_object_object_get_ex
(systemHeader, MSG_BODY_SYSTEM_CSMDCN_STR, &jsonDecoder))
{
const char *csmDcn = json_object_get_string (jsonDecoder);
if (csmDcn != NULL)
{
snprintf (pStMsg->sysHeader.cConsumerDcn,
sizeof (pStMsg->sysHeader.cConsumerDcn), "%s", csmDcn);
}
else
{
LOGRMB (RMB_LOG_ERROR, "In systemHeader, %s is null!",
MSG_BODY_SYSTEM_CSMDCN_STR);
}
} //system header csmDcn
if (json_object_object_get_ex
(systemHeader, MSG_BODY_SYSTEM_ORGSVR_STR, &jsonDecoder))
{
const char *orgSysId = json_object_get_string (jsonDecoder);
if (orgSysId != NULL)
{
snprintf (pStMsg->sysHeader.cOrgSvrId,
sizeof (pStMsg->sysHeader.cOrgSvrId), "%s", orgSysId);
}
else
{
LOGRMB (RMB_LOG_ERROR, "In systemHeader, %s is null!",
MSG_BODY_SYSTEM_ORGSVR_STR);
}
} //system header orgSysId
if (json_object_object_get_ex
(systemHeader, MSG_BODY_SYSTEM_ORGID_STR, &jsonDecoder))
{
const char *cOrgId = json_object_get_string (jsonDecoder);
if (cOrgId != NULL)
{
snprintf (pStMsg->sysHeader.cOrgId, sizeof (pStMsg->sysHeader.cOrgId),
"%s", cOrgId);
}
else
{
LOGRMB (RMB_LOG_ERROR, "In systemHeader, %s is null!",
MSG_BODY_SYSTEM_ORGID_STR);
}
} //system header cOrgId
if (json_object_object_get_ex
(systemHeader, MSG_BODY_SYSTEM_UNIID_STR, &jsonDecoder))
{
const char *cUniqueId = json_object_get_string (jsonDecoder);
if (cUniqueId != NULL)
{
snprintf (pStMsg->sysHeader.cUniqueId,
sizeof (pStMsg->sysHeader.cUniqueId), "%s", cUniqueId);
}
else
{
LOGRMB (RMB_LOG_ERROR, "In systemHeader, %s is null!",
MSG_BODY_SYSTEM_UNIID_STR);
}
} //system header cUniqueId
if (json_object_object_get_ex
(systemHeader, MSG_BODY_SYSTEM_CONLEN_INT, &jsonDecoder))
{
pStMsg->sysHeader.iContentLength = json_object_get_int (jsonDecoder);
} //system header iContentLength
if (json_object_object_get_ex
(systemHeader, MSG_BODY_SYSTEM_SENDTIME_LINT, &jsonDecoder))
{
pStMsg->sysHeader.ulSendTime = json_object_get_int64 (jsonDecoder);
} //system header ulSendTime
if (json_object_object_get_ex
(systemHeader, MSG_BODY_SYSTEM_RECVTIME_LINT, &jsonDecoder))
{
pStMsg->sysHeader.ulReceiveTime = json_object_get_int64 (jsonDecoder);
}
if (pStMsg->sysHeader.ulReceiveTime == 0)
{
GetRmbNowLongTime ();
pStMsg->sysHeader.ulReceiveTime = pRmbStConfig->ulNowTtime;
}
//system header ulReceiveTime
if (json_object_object_get_ex
(systemHeader, MSG_BODY_SYSTEM_REPLYTIME_LINT, &jsonDecoder))
{
pStMsg->sysHeader.ulReplyTime = json_object_get_int64 (jsonDecoder);
} //system header ulReplyTime
if (json_object_object_get_ex
(jsonByteBody, MSG_BODY_BYTE_BODY_APPHEADER_NAME_STR, &jsonDecoder))
{
const char *pAppheaderClass = json_object_get_string (jsonDecoder);
if (pAppheaderClass != NULL)
{
snprintf (pStMsg->sysHeader.cAppHeaderClass,
sizeof (pStMsg->sysHeader.cAppHeaderClass), "%s",
pAppheaderClass);
}
else
{
LOGRMB (RMB_LOG_ERROR, "In systemHeader, %s is null!",
MSG_BODY_BYTE_BODY_APPHEADER_NAME_STR);
}
} // system header cAppHeaderClass
if (json_object_object_get_ex
(systemHeader, MSG_BODY_SYSTEM_VER_STR, &jsonDecoder))
{
const char *pConsumerSysVersion = json_object_get_string (jsonDecoder);
if (pConsumerSysVersion != NULL)
{
snprintf (pStMsg->sysHeader.cConsumerSysVersion,
sizeof (pStMsg->sysHeader.cConsumerSysVersion), "%s",
pConsumerSysVersion);
}
else
{
LOGRMB (RMB_LOG_ERROR, "In systemHeader, %s is null!",
MSG_BODY_SYSTEM_VER_STR);
}
}
//system consumerSysVersion
const char *cProperty = json_object_get_string (property);
if (cProperty != NULL)
{
int len = (int) strlen (property);
if (len >= RMB_SYSTEMHEADER_PROPERTY_MAX_LEN)
{
LOGRMB (RMB_LOG_ERROR, "property len=%d too large!max_limit=%d: %s",
len, RMB_SYSTEMHEADER_PROPERTY_MAX_LEN, cProperty);
}
else
{
snprintf (pStMsg->sysHeader.cProperty,
sizeof (pStMsg->sysHeader.cProperty), "%s", cProperty);
}
}
else
{
LOGRMB (RMB_LOG_ERROR, "In systemHeader, %s is null!",
MSG_BODY_PROPERTY_JSON);
}
if (json_object_object_get_ex
(systemHeader, MSG_BODY_SYSTEM_EXTFIELDS_STR, &jsonDecoder))
{
const char *extFields = json_object_get_string (jsonDecoder);
if (extFields != NULL)
{
int len = (int) strlen (extFields);
if (len >= RMB_SYSTEMHEADER_EXTFIELDS_MAX_LEN)
{
LOGRMB (RMB_LOG_ERROR,
"systemHeader len=%d too large!max_limit=%d: %s", len,
RMB_SYSTEMHEADER_EXTFIELDS_MAX_LEN, extFields);
}
else
{
snprintf (pStMsg->sysHeader.cExtFields,
sizeof (pStMsg->sysHeader.cExtFields), "%s", extFields);
}
if (strstr (extFields, "\"IS_DYED_MSG\": \"true\"") != NULL)
{
LOGRMB (RMB_LOG_DEBUG, "trans msg, extfield is %s", extFields);
strcpy (pStMsg->isDyedMsg, "true");
}
else
{
strcpy (pStMsg->isDyedMsg, "false");
}
}
else
{
LOGRMB (RMB_LOG_ERROR, "In systemHeader, %s is null!",
MSG_BODY_SYSTEM_EXTFIELDS_STR);
}
} //system header extFields
if (json_object_object_get_ex
(jsonByteBody, MSG_BODY_BYTE_BODY_APPHEADER_CONTENT_JSON, &jsonDecoder))
{
const char *appHeader = json_object_get_string (jsonDecoder);
if (appHeader != NULL)
{
//get appHeader len
unsigned int uiAppHeaderLen = json_object_get_string_len (jsonDecoder);
if (uiAppHeaderLen >= pStMsg->iMallocAppHeaderLength)
{
int iFitSize =
rmb_get_fit_size (uiAppHeaderLen, MAX_APPHEADER_LENGTH);
if (iFitSize < 0)
{
LOGRMB (RMB_LOG_ERROR, "appHeader len=%d too large!max_limit=%u",
uiAppHeaderLen, MAX_APPHEADER_LENGTH);
return -1;
}
free (pStMsg->cAppHeader);
pStMsg->cAppHeader = NULL;
pStMsg->cAppHeader = (char *) malloc (iFitSize);
pStMsg->iMallocAppHeaderLength = iFitSize;
}
pStMsg->iAppHeaderLen = uiAppHeaderLen;
memcpy (pStMsg->cAppHeader, json_object_get_string (jsonDecoder),
uiAppHeaderLen);
pStMsg->cAppHeader[pStMsg->iAppHeaderLen] = '\0';
}
else
{
LOGRMB (RMB_LOG_ERROR, "In body, %s is null!", MSG_BODY_APP_JSON);
}
} //appHeader
if (json_object_object_get_ex
(property, MSG_BODY_PROPERTY_TTL_INT, &jsonDecoder))
{
pStMsg->ulMsgLiveTime = json_object_get_int64 (jsonDecoder);
} //header ulMsgLiveTime
//if (json_object_object_get_ex(jsonBody, MSG_BODY_TOPIC_STR, &jsonDecoder))
//{
// const char *topic = json_object_get_string(jsonDecoder);
// dest = generate_destination_from_topic(pStMsg,topic);
if (json_object_object_get_ex (jsonByteBody, MSG_BODY_DEST_JSON, &dest))
{
dest = json_tokener_parse (json_object_get_string (dest));
}
else
{
if (json_object_object_get_ex
(jsonBody, MSG_BODY_TOPIC_STR, &jsonDecoder))
{
const char *topic = json_object_get_string (jsonDecoder);
dest = generate_destination_from_topic (pStMsg, topic);
}
}
if (NULL != dest)
{
if (json_object_object_get_ex
(dest, MSG_BODY_DEST_NAME_STR, &jsonDecoder))
{
//get name
const char *destTmp = json_object_get_string (jsonDecoder);
if (destTmp != NULL)
{
snprintf (pStMsg->dest.cDestName, sizeof (pStMsg->dest.cDestName),
"%s", destTmp);
}
else
{
LOGRMB (RMB_LOG_ERROR, "In destination, %s is null",
MSG_BODY_DEST_NAME_STR);
}
} //get name
if (json_object_object_get_ex
(dest, MSG_BODY_DEST_SORE_STR, &jsonDecoder))
{
const char *pServiceId = json_object_get_string (jsonDecoder);
if (pServiceId != NULL)
{
snprintf (pStMsg->strServiceId, sizeof (pStMsg->strServiceId), "%s",
pServiceId);
pStMsg->iEventOrService =
(*(pServiceId + 3) == '0') ? RMB_SERVICE_CALL : RMB_EVENT_CALL;
}
else
{
LOGRMB (RMB_LOG_ERROR, "In destination, %s is null",
MSG_BODY_DEST_SORE_STR);
}
} //get serviceOrEventId
if (json_object_object_get_ex
(dest, MSG_BODY_DEST_SCENARIO_STR, &jsonDecoder))
{
const char *pScenarioId = json_object_get_string (jsonDecoder);
if (pScenarioId != NULL)
{
snprintf (pStMsg->strScenarioId, sizeof (pStMsg->strScenarioId), "%s",
pScenarioId);
}
else
{
LOGRMB (RMB_LOG_ERROR, "In destination, %s is null!",
MSG_BODY_DEST_SCENARIO_STR);
}
} //get scenario
if (json_object_object_get_ex (dest, MSG_BODY_DEST_DCN_STR, &jsonDecoder))
{
const char *pDcn = json_object_get_string (jsonDecoder);
if (pDcn != NULL)
{
snprintf (pStMsg->strTargetDcn, sizeof (pStMsg->strTargetDcn), "%s",
pDcn);
}
else
{
LOGRMB (RMB_LOG_ERROR, "In destination, %s is null!",
MSG_BODY_DEST_DCN_STR);
}
} //get dcn
if (json_object_object_get_ex
(dest, MSG_BODY_DEST_ORGID_STR, &jsonDecoder))
{
const char *pOrganization = json_object_get_string (jsonDecoder);
if (pOrganization != NULL)
{
snprintf (pStMsg->strTargetOrgId, sizeof (pStMsg->strTargetOrgId),
"%s", pOrganization);
}
else
{
LOGRMB (RMB_LOG_ERROR, "In destination, %s is null",
MSG_BODY_DEST_ORGID_STR);
}
} //get organization
//header dest
}
//topic
if (json_object_object_get_ex
(property, MSG_BODY_PROPERTY_REPLYTO_STR, &jsonDecoder))
{
const char *replyTo = json_object_get_string (jsonDecoder);
if (replyTo != NULL)
{
snprintf (pStMsg->replyTo.cDestName, sizeof (pStMsg->replyTo.cDestName),
"%s", replyTo);
}
else
{
LOGRMB (RMB_LOG_ERROR, "In bodyjson, %s is null", MSG_BODY_REPLYTO_STR);
}
} //header replyTo
if (json_object_object_get_ex
(jsonByteBody, MSG_BODY_BYTE_BODY_CONTENT_STR, &jsonDecoder))
{
const char *cContent = json_object_get_string (jsonDecoder);
if (cContent != NULL)
{
// pStMsg->iContentLen = strlen(cContent);
pStMsg->iContentLen = json_object_get_string_len (jsonDecoder);
LOGRMB (RMB_LOG_DEBUG, "get content len:%d", pStMsg->iContentLen);
if (pStMsg->iContentLen >= pStMsg->iMallocContentLength)
{
int iFitSize =
rmb_get_fit_size (pStMsg->iContentLen, MAX_MSG_CONTENT_SIZE);
if (iFitSize < 0)
{
LOGRMB (RMB_LOG_ERROR, "content len=%d too large!max_limit=%u",
pStMsg->iContentLen, MAX_MSG_CONTENT_SIZE);
return -1;
}
free (pStMsg->cContent);
pStMsg->cContent = NULL;
pStMsg->cContent = (char *) malloc (iFitSize);
pStMsg->iMallocContentLength = iFitSize;
}
//strncpy(pStMsg->cContent, cContent, pStMsg->iContentLen);
memcpy (pStMsg->cContent, cContent, pStMsg->iContentLen);
pStMsg->cContent[pStMsg->iContentLen] = '\0';
LOGRMB (RMB_LOG_DEBUG, "get content:%d - %s", pStMsg->iContentLen,
pStMsg->cContent);
}
else
{
LOGRMB (RMB_LOG_ERROR, "In bodyjson, %s is null", MSG_BODY_CONTENT_STR);
}
} //header cContent
json_object_put (dest);
json_object_put (systemHeader);
json_object_put (jsonBody);
json_object_put (jsonByteBody);
return 0;
}