Added basic shell command I/O functionality

This commit is contained in:
Dulana Peiris
2023-10-10 12:28:07 +05:30
parent f8877fd61a
commit 41d2f5a447
8 changed files with 97 additions and 0 deletions

View File

@@ -497,6 +497,39 @@ namespace msg::usrmsg::bson
return 0;
}
/**
* Extracts a contract shell input message sent by user.
*
* @param extracted_content The content to be passed to the contract, extracted from the message.
* @param d The bson document holding the shell input message.
* Accepted signed input container format:
* {
* "type": "contract_shell_input",
* "id": "<any string>",
* "content": <binary buffer>
* }
* @return 0 on successful extraction. -1 for failure.
*/
int extract_shell_input(std::string &extracted_id, std::string &extracted_content, const jsoncons::ojson &d)
{
if (!d.contains(msg::usrmsg::FLD_ID) || !d[msg::usrmsg::FLD_ID].is<std::string>())
{
LOG_DEBUG << "Shell input 'id' field missing or invalid.";
return -1;
}
if (!d.contains(msg::usrmsg::FLD_CONTENT) || !d[msg::usrmsg::FLD_CONTENT].is_byte_string_view())
{
LOG_DEBUG << "Shell input 'content' field missing or invalid.";
return -1;
}
extracted_id = d[msg::usrmsg::FLD_ID].as<std::string>();
const jsoncons::byte_string_view &bsv = d[msg::usrmsg::FLD_CONTENT].as_byte_string_view();
extracted_content = std::string_view(reinterpret_cast<const char *>(bsv.data()), bsv.size());
return 0;
}
/**
* Extracts a signed input container message sent by user.
*

View File

@@ -43,6 +43,8 @@ namespace msg::usrmsg::bson
int extract_read_request(std::string &extracted_id, std::string &extracted_content, const jsoncons::ojson &d);
int extract_shell_input(std::string &extracted_id, std::string &extracted_content, const jsoncons::ojson &d);
int extract_signed_input_container(std::string &extracted_input_container, std::string &extracted_sig,
const jsoncons::ojson &d);

View File

@@ -872,6 +872,38 @@ namespace msg::usrmsg::json
return 0;
}
/**
* Extracts a contract shell input message sent by user.
*
* @param extracted_content The content to be passed to the contract, extracted from the message.
* @param d The json document holding the shell input message.
* Accepted signed input container format:
* {
* "type": "contract_shell_input",
* "id": "<any string>",
* "content": "<any string>"
* }
* @return 0 on successful extraction. -1 for failure.
*/
int extract_shell_input(std::string &extracted_id, std::string &extracted_content, const jsoncons::json &d)
{
if (!d.contains(msg::usrmsg::FLD_ID) || !d[msg::usrmsg::FLD_ID].is<std::string>())
{
LOG_DEBUG << "Shell input 'id' field missing or invalid.";
return -1;
}
if (!d.contains(msg::usrmsg::FLD_CONTENT) || !d[msg::usrmsg::FLD_CONTENT].is<std::string>())
{
LOG_DEBUG << "Shell input '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;
}
/**
* Extracts a signed input container message sent by user.
*

View File

@@ -47,6 +47,8 @@ namespace msg::usrmsg::json
int extract_read_request(std::string &extracted_id, std::string &extracted_content, const jsoncons::json &d);
int extract_shell_input(std::string &extracted_id, std::string &extracted_content, const jsoncons::json &d);
int extract_signed_input_container(std::string &extracted_input_container, std::string &extracted_sig,
const jsoncons::json &d);

View File

@@ -80,6 +80,8 @@ namespace msg::usrmsg
constexpr const char *MSGTYPE_SERVER_CHALLENGE_RESPONSE = "server_challenge_response";
constexpr const char *MSGTYPE_CONTRACT_READ_REQUEST = "contract_read_request";
constexpr const char *MSGTYPE_CONTRACT_READ_RESPONSE = "contract_read_response";
constexpr const char *MSGTYPE_CONTRACT_SHELL_INPUT = "contract_shell_input";
constexpr const char *MSGTYPE_CONTRACT_SHELL_OUTPUT = "contract_shell_output";
constexpr const char *MSGTYPE_CONTRACT_INPUT = "contract_input";
constexpr const char *MSGTYPE_CONTRACT_INPUT_STATUS = "contract_input_status";
constexpr const char *MSGTYPE_CONTRACT_OUTPUT = "contract_output";

View File

@@ -121,6 +121,14 @@ namespace msg::usrmsg
return busrmsg::extract_read_request(extracted_id, extracted_content, bdoc);
}
int usrmsg_parser::extract_shell_input(std::string &extracted_id, std::string &extracted_content) const
{
if (protocol == util::PROTOCOL::JSON)
return jusrmsg::extract_shell_input(extracted_id, extracted_content, jdoc);
else
return busrmsg::extract_shell_input(extracted_id, extracted_content, bdoc);
}
int usrmsg_parser::extract_signed_input_container(std::string &extracted_input_container, std::string &extracted_sig) const
{
if (protocol == util::PROTOCOL::JSON)

View File

@@ -49,6 +49,8 @@ namespace msg::usrmsg
int extract_read_request(std::string &extracted_id, std::string &extracted_content) const;
int extract_shell_input(std::string &extracted_id, std::string &extracted_content) const;
int extract_signed_input_container(std::string &extracted_input_container, std::string &extracted_sig) const;
int extract_input_container(std::string &input, uint64_t &nonce,

View File

@@ -272,6 +272,22 @@ namespace usr
user.session.send(resp);
return 0;
}
else if (msg_type == msg::usrmsg::MSGTYPE_CONTRACT_SHELL_INPUT)
{
std::string id, content;
if (parser.extract_shell_input(id, content) != -1){
LOG_INFO << "Received Shell Input.";
LOG_INFO << "User PubKey:" << user.pubkey;
LOG_INFO << "ID:" << id;
LOG_INFO << "Content:" << content;
return 0;
}
else
{
send_input_status(parser, user.session, msg::usrmsg::STATUS_REJECTED, msg::usrmsg::REASON_BAD_MSG_FORMAT, "");
return -1;
}
}
else
{
LOG_DEBUG << "Invalid user message type: " << msg_type;