Read requests synchronous replies. (#361)

This commit is contained in:
Ravin Perera
2022-02-26 07:48:02 +05:30
committed by GitHub
parent d5f0c1e664
commit 5c349dfa95
14 changed files with 136 additions and 66 deletions

View File

@@ -331,11 +331,12 @@ namespace msg::usrmsg::json
* Message format:
* {
* "type": "contract_read_response",
* "reply_for": "<corresponding request id>",
* "content": "<response string>"
* }
* @param content The contract binary output content to be put in the message.
*/
void create_contract_read_response_container(std::vector<uint8_t> &msg, std::string_view content)
void create_contract_read_response_container(std::vector<uint8_t> &msg, std::string_view reply_for, std::string_view content)
{
msg.reserve(content.size() + 256);
msg += "{\"";
@@ -343,6 +344,10 @@ namespace msg::usrmsg::json
msg += SEP_COLON;
msg += msg::usrmsg::MSGTYPE_CONTRACT_READ_RESPONSE;
msg += SEP_COMMA;
msg += msg::usrmsg::FLD_REPLY_FOR;
msg += SEP_COLON;
msg += reply_for;
msg += SEP_COMMA;
msg += msg::usrmsg::FLD_CONTENT;
msg += SEP_COLON_NOQUOTE;
@@ -843,24 +848,26 @@ namespace msg::usrmsg::json
* Accepted signed input container format:
* {
* "type": "contract_read_request",
* "id": "<any string>",
* "content": "<any string>"
* }
* @return 0 on successful extraction. -1 for failure.
*/
int extract_read_request(std::string &extracted_content, const jsoncons::json &d)
int extract_read_request(std::string &extracted_id, std::string &extracted_content, const jsoncons::json &d)
{
if (!d.contains(msg::usrmsg::FLD_CONTENT))
if (!d.contains(msg::usrmsg::FLD_ID) || !d[msg::usrmsg::FLD_ID].is<std::string>())
{
LOG_DEBUG << "Read request required fields missing.";
LOG_DEBUG << "Read request 'id' field missing or invalid.";
return -1;
}
if (!d[msg::usrmsg::FLD_CONTENT].is<std::string>())
if (!d.contains(msg::usrmsg::FLD_CONTENT) || !d[msg::usrmsg::FLD_CONTENT].is<std::string>())
{
LOG_DEBUG << "Read request invalid field values.";
LOG_DEBUG << "Read request 'content' field missing or invalid.";
return -1;
}
extracted_id = d[msg::usrmsg::FLD_ID].as<std::string>();
extracted_content = d[msg::usrmsg::FLD_CONTENT].as<std::string>();
return 0;
}