-
Notifications
You must be signed in to change notification settings - Fork 5
refactor: streamline application startup and IPC service management #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
00dace0
feat: enhance ServiceImpl to accept CatterRuntime and update runtime …
Seele-Official db97209
feat: implement app configuration and runtime planning structures
Seele-Official 841b947
feat: enhance session management with ProcessLaunchPlan and update ru…
Seele-Official 318c594
refactor: enhance ServiceImpl factory methods with const correctness
Seele-Official 5adf0ab
feat: refactor inject and execute_service functions to streamline ses…
Seele-Official File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| #include "app_config.h" | ||
|
|
||
| #include <format> | ||
| #include <fstream> | ||
| #include <iterator> | ||
| #include <string_view> | ||
| #include <unordered_map> | ||
|
|
||
| namespace catter::app { | ||
|
|
||
| StartupConfig to_startup_config(const core::Option::CatterOption& opt) { | ||
| return StartupConfig{ | ||
| .log = true, | ||
| .mode = *opt.mode, | ||
| .script_path = *opt.script_path, | ||
| .script_args = opt.script_args.has_value() ? *opt.script_args : std::vector<std::string>{}, | ||
| .build_system_command = *opt.args, | ||
| .working_dir = opt.working_dir.has_value() ? std::filesystem::absolute(*opt.working_dir) | ||
| : std::filesystem::current_path(), | ||
| }; | ||
| } | ||
|
|
||
| RuntimePlan build_runtime_plan(const StartupConfig& config) { | ||
| struct ModeMeta { | ||
| ipc::ServiceMode mode; | ||
| js::CatterRuntime runtime; | ||
| }; | ||
|
|
||
| const static std::unordered_map<std::string_view, ModeMeta> mode_map = { | ||
| {"inject", | ||
| {.mode = ipc::ServiceMode::INJECT, | ||
| .runtime = { | ||
| .supportActions = {js::ActionType::drop, | ||
| js::ActionType::skip, | ||
| js::ActionType::modify}, | ||
| .supportEvents = {js::EventType::finish}, | ||
| .type = js::CatterRuntime::Type::inject, | ||
| .supportParentId = true, | ||
| }}} | ||
| }; | ||
|
|
||
| auto it = mode_map.find(config.mode); | ||
| if(it == mode_map.end()) { | ||
| throw std::runtime_error(std::format("Unsupported mode: {}", config.mode)); | ||
| } | ||
|
|
||
| return RuntimePlan{ | ||
| .log = config.log, | ||
| .mode = it->second.mode, | ||
| .script_path = config.script_path, | ||
| .script_args = config.script_args, | ||
| .build_system_command = config.build_system_command, | ||
| .working_dir = config.working_dir, | ||
| .runtime = &it->second.runtime, | ||
| }; | ||
| } | ||
|
|
||
| std::string load_script_content(const std::string& script_path) { | ||
| const static std::unordered_map<std::string_view, std::string_view> internal_scripts = { | ||
| {"script::cdb", | ||
| R"( | ||
| import { scripts, service } from "catter"; | ||
| service.register(new scripts.CDB()); | ||
| )"} | ||
| }; | ||
|
|
||
| if(auto it = internal_scripts.find(script_path); it != internal_scripts.end()) { | ||
| return std::string(it->second); | ||
| } | ||
|
|
||
| std::ifstream ifs{script_path}; | ||
| if(!ifs.good()) { | ||
| throw std::runtime_error(std::format("Failed to open script file: {}", script_path)); | ||
| } | ||
|
|
||
| return std::string((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>()); | ||
| } | ||
|
|
||
| } // namespace catter::app | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| #pragma once | ||
|
|
||
| #include <filesystem> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| #include "capi/type.h" | ||
| #include "ipc.h" | ||
| #include "opt/main/option.h" | ||
|
|
||
| namespace catter::app { | ||
|
|
||
| struct StartupConfig { | ||
| bool log; | ||
| std::string mode; | ||
| std::string script_path; | ||
| std::vector<std::string> script_args; | ||
| std::vector<std::string> build_system_command; | ||
| std::filesystem::path working_dir; | ||
| }; | ||
|
|
||
| struct RuntimePlan { | ||
| bool log; | ||
| ipc::ServiceMode mode; | ||
| std::string script_path; | ||
| std::vector<std::string> script_args; | ||
| std::vector<std::string> build_system_command; | ||
| std::filesystem::path working_dir; | ||
| const js::CatterRuntime* runtime; | ||
| }; | ||
|
|
||
| StartupConfig to_startup_config(const core::Option::CatterOption& opt); | ||
|
|
||
| RuntimePlan build_runtime_plan(const StartupConfig& config); | ||
|
|
||
| std::string load_script_content(const std::string& script_path); | ||
|
|
||
| } // namespace catter::app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| #include "app_runner.h" | ||
|
|
||
| #include <cstdint> | ||
| #include <format> | ||
| #include <memory> | ||
| #include <stdexcept> | ||
| #include <string> | ||
| #include <utility> | ||
|
|
||
| #include "app_config.h" | ||
| #include "capi/type.h" | ||
| #include "ipc.h" | ||
| #include "js.h" | ||
| #include "qjs.h" | ||
| #include "session.h" | ||
| #include "config/catter-proxy.h" | ||
| #include "util/crossplat.h" | ||
| #include "util/data.h" | ||
|
|
||
| namespace catter::app { | ||
| namespace { | ||
|
|
||
| class ServiceImpl : public ipc::InjectService { | ||
| public: | ||
| ServiceImpl(data::ipcid_t id, const js::CatterRuntime* runtime) : id(id), runtime(runtime) {} | ||
|
|
||
| ~ServiceImpl() override = default; | ||
|
|
||
| data::ipcid_t create(data::ipcid_t parent_id) override { | ||
| this->parent_id = parent_id; | ||
| return this->id; | ||
| } | ||
|
|
||
| data::action make_decision(data::command cmd) override { | ||
| auto act = js::on_command(this->id, | ||
| js::CommandData{ | ||
| .cwd = cmd.cwd, | ||
| .exe = cmd.executable, | ||
| .argv = cmd.args, | ||
| .env = cmd.env, | ||
| .runtime = *runtime, | ||
| .parent = this->parent_id, | ||
| }); | ||
|
|
||
| switch(act.type()) { | ||
| case js::ActionType::drop: { | ||
| return data::action{.type = data::action::DROP, .cmd = {}}; | ||
| } | ||
| case js::ActionType::skip: { | ||
| return data::action{.type = data::action::INJECT, .cmd = cmd}; | ||
| } | ||
| case js::ActionType::modify: { | ||
| auto& tag = act.get<js::ActionType::modify>(); | ||
| return data::action{ | ||
| .type = data::action::INJECT, | ||
| .cmd = { | ||
| .cwd = std::move(tag.data.cwd), | ||
| .executable = std::move(tag.data.exe), | ||
| .args = std::move(tag.data.argv), | ||
| .env = std::move(tag.data.env), | ||
| } | ||
| }; | ||
| } | ||
| default: throw std::runtime_error("Unhandled action type"); | ||
| } | ||
| } | ||
|
|
||
| void finish(int64_t code) override { | ||
| js::on_execution(this->id, js::Tag<js::EventType::finish>{.code = code}); | ||
| } | ||
|
|
||
| void report_error(data::ipcid_t parent_id, std::string error_msg) override { | ||
| js::on_command(id, std::unexpected(js::CatterErr{.msg = std::move(error_msg)})); | ||
| } | ||
|
|
||
| struct Factory { | ||
| const js::CatterRuntime* runtime; | ||
|
|
||
| std::unique_ptr<ServiceImpl> operator() (data::ipcid_t id) const { | ||
| return std::make_unique<ServiceImpl>(id, runtime); | ||
| } | ||
| }; | ||
|
|
||
| private: | ||
| data::ipcid_t id = 0; | ||
| data::ipcid_t parent_id = 0; | ||
| const js::CatterRuntime* runtime = nullptr; | ||
| }; | ||
|
|
||
| int64_t inject(const js::CatterConfig& config) { | ||
|
|
||
| auto proxy_path = util::get_catter_root_path() / config::proxy::EXE_NAME; | ||
| Session::ProcessLaunchPlan launch_plan{ | ||
| .executable = proxy_path.string(), | ||
| .args = | ||
| { | ||
| proxy_path.string(), | ||
| "-p", "0", | ||
| "--exec", config.buildSystemCommand.front(), | ||
| "--", }, | ||
| }; | ||
| append_range_to_vector(launch_plan.args, config.buildSystemCommand); | ||
|
|
||
| Session session; | ||
| auto session_plan = Session::make_run_plan(std::move(launch_plan), | ||
| ServiceImpl::Factory{.runtime = &config.runtime}); | ||
|
|
||
| return session.run(std::move(session_plan)); | ||
Seele-Official marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| int64_t execute_service(ipc::ServiceMode mode, const js::CatterConfig& config) { | ||
| switch(mode) { | ||
| case ipc::ServiceMode::INJECT: { | ||
| return inject(config); | ||
| } | ||
| default: { | ||
| throw std::runtime_error( | ||
| std::format("UnExpected mode: {:0x}", static_cast<uint8_t>(mode))); | ||
| } | ||
| } | ||
| throw std::runtime_error("Not implemented"); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| void run(const core::Option::CatterOption& opt) { | ||
| auto startup = to_startup_config(opt); | ||
| auto plan = build_runtime_plan(startup); | ||
| auto script_content = load_script_content(plan.script_path); | ||
|
|
||
| js::init_qjs({.pwd = plan.working_dir}); | ||
| js::run_js_file(script_content, plan.script_path); | ||
|
|
||
| auto config = js::on_start({ | ||
| .scriptPath = plan.script_path, | ||
| .scriptArgs = plan.script_args, | ||
| .buildSystemCommand = plan.build_system_command, | ||
| .runtime = *plan.runtime, | ||
| .options = {.log = plan.log}, | ||
| .isScriptSupported = true, | ||
| }); | ||
|
|
||
| js::on_finish(js::Tag<js::EventType::finish>{ | ||
| .code = execute_service(plan.mode, config), | ||
| }); | ||
Seele-Official marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| } // namespace catter::app | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #pragma once | ||
|
|
||
| #include "opt/main/option.h" | ||
|
|
||
| namespace catter::app { | ||
|
|
||
| void run(const core::Option::CatterOption& opt); | ||
|
|
||
| } // namespace catter::app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.