From 835b702e7e7cc955ba573d96864c76a5ade81524 Mon Sep 17 00:00:00 2001 From: chalith Date: Tue, 31 Oct 2023 18:31:06 +0530 Subject: [PATCH] Remove command when user disconnected --- src/hpsh/hpsh.cpp | 30 +++++++++++++++++++----------- src/hpsh/hpsh.hpp | 6 ++++-- src/usr/usr.cpp | 10 ++++++++-- test/bin/hpsh | Bin 18752 -> 18752 bytes 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/hpsh/hpsh.cpp b/src/hpsh/hpsh.cpp index 815abedc..6700190b 100644 --- a/src/hpsh/hpsh.cpp +++ b/src/hpsh/hpsh.cpp @@ -146,7 +146,14 @@ namespace hpsh return (write(ctx.control_fds[1], HPSH_CTR_TERMINATE, 10) < 0) ? -1 : 0; } - int execute(std::string_view id, std::string_view pubkey, std::string_view message) + 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; }); + } + + int execute(std::string_view id, std::string_view user_pubkey, std::string_view message) { if (ctx.is_shutting_down) return -1; @@ -203,9 +210,11 @@ namespace hpsh return -1; } + close(child_fds[0]); + { std::scoped_lock lock(ctx.command_mutex); - ctx.commands.push_back(command_context{std::string(id), std::string(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]}, std::string(), false}); } return 0; @@ -219,6 +228,8 @@ namespace hpsh { if (ctx.commands.size() > 0) { + std::scoped_lock lock(ctx.command_mutex); + auto itr = ctx.commands.begin(); while (itr != ctx.commands.end()) { @@ -262,7 +273,7 @@ namespace hpsh std::scoped_lock lock(usr::ctx.users_mutex); // Find the user session by user pubkey. - const auto user_itr = usr::ctx.users.find(itr->pubkey); + const auto user_itr = usr::ctx.users.find(itr->user_pubkey); if (user_itr != usr::ctx.users.end()) // match found { const usr::connected_user &user = user_itr->second; @@ -272,10 +283,10 @@ namespace hpsh user.session.send(msg); } } - { - std::scoped_lock lock(ctx.command_mutex); - itr = ctx.commands.erase(itr); - } + + // Close the file descriptor and remove the command from context. + close(itr->child_fds[1]); + itr = ctx.commands.erase(itr); } else { @@ -283,10 +294,7 @@ namespace hpsh } } } - else - { - util::sleep(1000); - } + util::sleep(1000); } } } \ No newline at end of file diff --git a/src/hpsh/hpsh.hpp b/src/hpsh/hpsh.hpp index 11a5d7a8..24dd2c9d 100644 --- a/src/hpsh/hpsh.hpp +++ b/src/hpsh/hpsh.hpp @@ -10,7 +10,7 @@ namespace hpsh struct command_context { std::string id; - std::string pubkey; + std::string user_pubkey; int child_fds[2]; std::string response; bool read_completed = false; @@ -37,7 +37,9 @@ namespace hpsh int send_terminate_message(); - int execute(std::string_view id, std::string_view pubkey, std::string_view message); + void remove_user_commands(std::string_view user_pubkey); + + int execute(std::string_view id, std::string_view user_pubkey, std::string_view message); void response_watcher(); } diff --git a/src/usr/usr.cpp b/src/usr/usr.cpp index 096646dd..14a6760b 100644 --- a/src/usr/usr.cpp +++ b/src/usr/usr.cpp @@ -420,8 +420,14 @@ namespace usr */ int remove_user(const std::string &pubkey) { - std::scoped_lock lock(ctx.users_mutex); - ctx.users.erase(pubkey); + { + std::scoped_lock lock(ctx.users_mutex); + ctx.users.erase(pubkey); + } + // Remove any hpsh commands sent by the user. + if (hpsh::ctx.is_initialized) + hpsh::remove_user_commands(pubkey); + return 0; } diff --git a/test/bin/hpsh b/test/bin/hpsh index 4a225764dc18d461bf713c00e44f0230069a6910..464264046384853e748562115621245fdc02a0e2 100755 GIT binary patch delta 1066 zcmX|ATWpI_96zV)tnD^mH)h)1yUtdZxa`7EQAWSTc)*NAVwmyZGBK10m3H`2O=d@e z%!A8=d9vht*+eKdTOy(pBVJq{BwvGUbDbf_@AP9Q>G}Qsx8MJNPQMdrqmee+I+a?_ z*@nz5$t1p7)HJ%2rdbPga~zpscr&?Yp}j+Ue!a@%E6Kif_|&ge-yIS4Si)wLZP>2o zcP`e^qyvevk>pI;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