From f463cd6705cc1a27b6b4f92ed3a280e0474bf7ee Mon Sep 17 00:00:00 2001 From: chalith Date: Wed, 1 Nov 2023 13:53:19 +0530 Subject: [PATCH] Changes to support follow commands --- examples/js_client/text-client.js | 6 ++- src/hpsh/hpsh.cpp | 66 ++++++++++++++---------------- src/hpsh/hpsh.hpp | 2 - src/usr/usr.cpp | 2 +- test/bin/hpsh | Bin 18752 -> 18752 bytes 5 files changed, 36 insertions(+), 40 deletions(-) diff --git a/examples/js_client/text-client.js b/examples/js_client/text-client.js index d402ac47..36e4e16d 100644 --- a/examples/js_client/text-client.js +++ b/examples/js_client/text-client.js @@ -124,7 +124,11 @@ async function main() { hpc.getStatus().then(stat => console.log(stat)); } else if (inp.startsWith("hpsh ")) { - hpc.submitHpshRequest(inp.substr(5)).then(reply => console.log(reply)); + hpc.submitHpshRequest(inp.substr(5)).then(id => { + hpc.on(id, (reply) => { + console.log(reply); + }) + }); } else { diff --git a/src/hpsh/hpsh.cpp b/src/hpsh/hpsh.cpp index 6700190b..7e9479be 100644 --- a/src/hpsh/hpsh.cpp +++ b/src/hpsh/hpsh.cpp @@ -43,7 +43,7 @@ namespace hpsh std::string fd_str; fd_str.resize(10); - snprintf(fd_str.data(), sizeof(fd_str), "%d", ctx.control_fds[0]); + snprintf(fd_str.data(), 10, "%d", ctx.control_fds[0]); char *argv[] = {(char *)conf::ctx.hpsh_exe_path.data(), fd_str.data(), NULL}; @@ -149,8 +149,20 @@ namespace hpsh void remove_user_commands(std::string_view user_pubkey) { std::scoped_lock lock(ctx.command_mutex); - ctx.commands.remove_if([&](const command_context &command) - { return command.user_pubkey == user_pubkey; }); + auto itr = ctx.commands.begin(); + while (itr != ctx.commands.end()) + { + if (itr->user_pubkey == user_pubkey) + { + // Close the file descriptor and remove the command from context. + close(itr->child_fds[1]); + itr = ctx.commands.erase(itr); + } + else + { + itr++; + } + } } int execute(std::string_view id, std::string_view user_pubkey, std::string_view message) @@ -202,7 +214,7 @@ namespace hpsh return -1; } - if (write(child_fds[1], message.data(), sizeof(message)) < 0) + if (write(child_fds[1], message.data(), message.size()) < 0) { LOG_ERROR << errno << ": Error writing to child fd."; close(child_fds[0]); @@ -214,7 +226,7 @@ namespace hpsh { std::scoped_lock lock(ctx.command_mutex); - ctx.commands.push_back(command_context{std::string(id), std::string(user_pubkey), {child_fds[0], child_fds[1]}, std::string(), false}); + ctx.commands.push_back(command_context{std::string(id), std::string(user_pubkey), {child_fds[0], child_fds[1]}}); } return 0; @@ -247,29 +259,12 @@ namespace hpsh } else if (pfd.revents & POLLIN) { - itr->response.resize(READ_BUFFER_SIZE); - const int res = read(pfd.fd, itr->response.data(), READ_BUFFER_SIZE); - + std::string response; + response.resize(READ_BUFFER_SIZE); + const int res = read(pfd.fd, response.data(), READ_BUFFER_SIZE); if (res > 0) - itr->response.resize(res); // Resize back to the actual bytes read. - else if (res == -1) - { - // Assuming that EPIPE or ECONNRESET resulted from contract termination, consider this as a neutral read. - if (errno == EPIPE || errno == ECONNRESET) - itr->read_completed = true; - else - LOG_ERROR << errno << ": Error reading from fd"; - } - } - else - { - itr->read_completed = true; - } - - // Send command back to user; - if (itr->read_completed) - { { + response.resize(res); std::scoped_lock lock(usr::ctx.users_mutex); // Find the user session by user pubkey. @@ -279,22 +274,21 @@ namespace hpsh const usr::connected_user &user = user_itr->second; msg::usrmsg::usrmsg_parser parser(user.protocol); std::vector msg; - parser.create_hpsh_response_container(msg, itr->id, itr->response); + parser.create_hpsh_response_container(msg, itr->id, response); user.session.send(msg); + response.clear(); } } + else if (res == -1) + { + LOG_ERROR << errno << ": Error reading from fd"; + } + } - // Close the file descriptor and remove the command from context. - close(itr->child_fds[1]); - itr = ctx.commands.erase(itr); - } - else - { - itr++; - } + itr++; } } - util::sleep(1000); + util::sleep(100); } } } \ No newline at end of file diff --git a/src/hpsh/hpsh.hpp b/src/hpsh/hpsh.hpp index 24dd2c9d..c368e991 100644 --- a/src/hpsh/hpsh.hpp +++ b/src/hpsh/hpsh.hpp @@ -12,8 +12,6 @@ namespace hpsh std::string id; std::string user_pubkey; int child_fds[2]; - std::string response; - bool read_completed = false; }; struct hpsh_context diff --git a/src/usr/usr.cpp b/src/usr/usr.cpp index 14a6760b..bb0839be 100644 --- a/src/usr/usr.cpp +++ b/src/usr/usr.cpp @@ -278,7 +278,7 @@ namespace usr std::string id, content; if (hpsh::ctx.is_initialized && parser.extract_hpsh_request(id, content) != -1) { - if (hpsh::execute(id, user.pubkey, content.c_str()) == -1) + if (hpsh::execute(id, user.pubkey, content) == -1) return -1; return 0; diff --git a/test/bin/hpsh b/test/bin/hpsh index 464264046384853e748562115621245fdc02a0e2..4b9f3f2603a6d38f3fb3db3a55ec2841e98a773d 100755 GIT binary patch delta 2207 zcmZWrdrT8|9RFPbDG$BMAfr4h9!^A<2o3GUD4lbM99*^`dCtb6q?GJGt{=K}=8Mv_hb#A7 znpYBXN3Az5tWMVr+h5X8qkfPDX*$vIAGs+#6rXq?)*}U?1#cN?j0&~eW z;)U1BI`RKVnYoWuKu z!FIs5-ZL28$8_y?O~neG4jxtKwECsf|Iiox;0H-%IuUUkl3z0lOE zw5McvT43WHgULaTkWssk#kmHG2D=C`u=<>Gy-+9h+l5A!XrEnZB6KOLkOD5Du9F$b zmuV&-{5|}h37GJ{65vR$-!2+t?MZ^IGhi3$&a$#Gr4u-va^JmAMgCx)*z&uFRPtS; zmHtuQw-JtFOW9kz-w#`{Whd9z{2mSiY2MMmkMu}EpKuJCqCvJIlaBtX1>n%(yjc8I znb^z1W>LWsmQtak-Y3Y_>lP%QfBy&s5^|}KkgRVnIPcCx59Y+iY!5V6yLfQ3&pCg>I{8p?f?jtva&gBPD&p^XexX66wRSU?-tyl1em zLF>^t9CE)y?xWqn>4XCldsaj4lpoU1S*DT2x`5!hPQOU2Q-fF`{ERU{_u>b(HQF5g~P zE3Ox7#SP-dbN?;{t$6(G2G|iL3ux%--d<$;bogeN(ff}yjnBk7B!9y6SXK1s$qTF$ zk=j|(7KB4L<|64xlRjcGg4WVV8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