factor out shell command execution

This commit is contained in:
Martin Michelsen
2025-01-06 00:11:53 -08:00
parent d51f7a0fe7
commit 0be056adce
3 changed files with 18 additions and 11 deletions
+15 -10
View File
@@ -17,7 +17,7 @@ using namespace std;
struct CommandArgs { struct CommandArgs {
shared_ptr<ServerState> s; shared_ptr<ServerState> s;
shared_ptr<ServerShell> shell; ServerShell* shell;
string command; string command;
string args; string args;
string session_name; string session_name;
@@ -107,17 +107,10 @@ void ServerShell::thread_fn() {
} }
phosg::strip_trailing_whitespace(command); phosg::strip_trailing_whitespace(command);
phosg::strip_leading_whitespace(command);
try { try {
// Find the entry in the command table and run the command this->execute_command(command);
size_t command_end = phosg::skip_non_whitespace(command, 0);
size_t args_begin = phosg::skip_whitespace(command, command_end);
CommandArgs args;
args.s = this->state;
args.shell = this->shared_from_this();
args.command = command.substr(0, command_end);
args.args = command.substr(args_begin);
CommandDefinition::dispatch(args);
} catch (const exit_shell&) { } catch (const exit_shell&) {
event_base_loopexit(this->state->base.get(), nullptr); event_base_loopexit(this->state->base.get(), nullptr);
return; return;
@@ -127,6 +120,18 @@ void ServerShell::thread_fn() {
} }
} }
void ServerShell::execute_command(const string& command) {
// Find the entry in the command table and run the command
size_t command_end = phosg::skip_non_whitespace(command, 0);
size_t args_begin = phosg::skip_whitespace(command, command_end);
CommandArgs args;
args.s = this->state;
args.shell = this;
args.command = command.substr(0, command_end);
args.args = command.substr(args_begin);
CommandDefinition::dispatch(args);
}
shared_ptr<ProxyServer::LinkedSession> ServerShell::get_proxy_session(const string& name) { shared_ptr<ProxyServer::LinkedSession> ServerShell::get_proxy_session(const string& name) {
if (!this->state->proxy_server.get()) { if (!this->state->proxy_server.get()) {
throw runtime_error("the proxy server is disabled"); throw runtime_error("the proxy server is disabled");
+2
View File
@@ -25,6 +25,8 @@ public:
std::shared_ptr<ProxyServer::LinkedSession> get_proxy_session(const std::string& name); std::shared_ptr<ProxyServer::LinkedSession> get_proxy_session(const std::string& name);
void execute_command(const std::string& command);
protected: protected:
std::shared_ptr<ServerState> state; std::shared_ptr<ServerState> state;
std::thread th; std::thread th;
+1 -1
View File
@@ -383,7 +383,7 @@ struct ServerState : public std::enable_shared_from_this<ServerState> {
std::vector<PortConfiguration> parse_port_configuration(const phosg::JSON& json) const; std::vector<PortConfiguration> parse_port_configuration(const phosg::JSON& json) const;
template <typename T> template <typename T>
inline void call_on_event_thread(std::function<T()>&& fn) { inline T call_on_event_thread(std::function<T()>&& fn) {
return ::call_on_event_thread<T>(this->base, std::move(fn)); return ::call_on_event_thread<T>(this->base, std::move(fn));
} }
inline void forward_to_event_thread(std::function<void()>&& fn) { inline void forward_to_event_thread(std::function<void()>&& fn) {