Skip to content

Commit

Permalink
refactor: 重新整理ExecAgent结构
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Dec 6, 2023
1 parent 4ec1ced commit d2d79f2
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 27 deletions.
2 changes: 1 addition & 1 deletion source/MaaToolKit/ExecAgent/ActionExecAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ bool ActionExecAgent::run(MaaSyncContextHandle sync_context, std::string_view ta
std::vector<std::string> args = exec.exec_args;
args.insert(args.end(), std::make_move_iterator(extra_args.begin()), std::make_move_iterator(extra_args.end()));

auto output_opt = run_executor(exec.text_mode, exec.exec_path, args);
auto output_opt = run_executor(exec.exec_path, args, exec.text_mode, exec.image_mode);
if (!output_opt) {
LogError << "run_executor failed" << VAR(exec.exec_path) << VAR(exec.exec_args);
return false;
Expand Down
39 changes: 21 additions & 18 deletions source/MaaToolKit/ExecAgent/ExecAgentBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,35 +43,40 @@ bool ExecAgentBase::unregister_executor(MaaInstanceHandle handle, std::string_vi
std::string name_str(name);

bool ret = unregister_for_maa_inst(handle, name);
ret = executors_.erase(name_str) > 0 && ret;
ret &= executors_.erase(name_str) > 0;

return ret;
}

std::optional<std::string> ExecAgentBase::run_executor(TextTransferMode mode, const std::filesystem::path& exec_path,
const std::vector<std::string>& exec_args)
std::optional<std::string> ExecAgentBase::run_executor(const std::filesystem::path& exec_path,
const std::vector<std::string>& exec_args,
TextTransferMode text_mode, ImageTransferMode image_mode)
{
switch (mode) {
case TextTransferMode::StdIO: {
return run_executor_with_stdio(exec_path, exec_args);
auto searched_path = boost::process::search_path(exec_path);
if (!std::filesystem::exists(searched_path)) {
LogError << "path not exists" << VAR(searched_path);
return std::nullopt;
}

ChildPipeIOStream child(searched_path, exec_args);

switch (text_mode) {
case TextTransferMode::StdIO:
return handle_ipc(child, image_mode);

case TextTransferMode::FileIO:
LogError << "not implemented";
return std::nullopt;

default:
LogError << "not implemented";
return std::nullopt;
}
}

std::optional<std::string> ExecAgentBase::run_executor_with_stdio(const std::filesystem::path& exec_path,
const std::vector<std::string>& exec_args)
std::optional<std::string> ExecAgentBase::handle_ipc(IOStream& ios, ImageTransferMode image_mode)
{
auto searched_path = boost::process::search_path(exec_path);
if (!std::filesystem::exists(searched_path)) {
LogError << "path not exists" << VAR(searched_path);
return std::nullopt;
}

ChildPipeIOStream ios(searched_path, exec_args);
std::ignore = image_mode;

while (ios.is_open()) {
std::string line = ios.read_until("\n");
Expand All @@ -80,9 +85,7 @@ std::optional<std::string> ExecAgentBase::run_executor_with_stdio(const std::fil
// TODO
}

int exit_code = ios.release();
if (exit_code != 0) {
LogError << "process exit with code:" << exit_code;
if (!ios.release()) {
return std::nullopt;
}

Expand Down
11 changes: 7 additions & 4 deletions source/MaaToolKit/ExecAgent/ExecAgentBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "ExecAgent/ExecArgConverter.h"
#include "ExecAgentType.h"
#include "MaaFramework/MaaDef.h"
#include "Utils/IOStream/IOStream.h"

MAA_TOOLKIT_NS_BEGIN

Expand All @@ -25,15 +26,17 @@ class ExecAgentBase
virtual bool register_for_maa_inst(MaaInstanceHandle handle, std::string_view name) = 0;
virtual bool unregister_for_maa_inst(MaaInstanceHandle handle, std::string_view name) = 0;

std::optional<std::string> run_executor(TextTransferMode mode, const std::filesystem::path& exec_path,
const std::vector<std::string>& exec_args);
std::optional<std::string> run_executor_with_stdio(const std::filesystem::path& exec_path,
const std::vector<std::string>& exec_args);
std::optional<std::string> run_executor(const std::filesystem::path& exec_path,
const std::vector<std::string>& exec_args, TextTransferMode text_mode,
ImageTransferMode image_mode);

protected:
std::unordered_map</*name*/ std::string, Executor> executors_;

ExecArgConverter arg_cvt_;

private:
std::optional<std::string> handle_ipc(IOStream& ios, ImageTransferMode image_mode);
};

MAA_TOOLKIT_NS_END
2 changes: 1 addition & 1 deletion source/MaaToolKit/ExecAgent/ExecArgConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ std::string ExecArgConverter::sync_context_to_arg(MaaSyncContextHandle sync_cont
return uuid;
}

std::string ExecArgConverter::image_to_arg(ImageTransferMode mode, const cv::Mat& image)
std::string ExecArgConverter::image_to_arg(const cv::Mat& image, ImageTransferMode mode)
{
switch (mode) {
case ImageTransferMode::FileIO: {
Expand Down
2 changes: 1 addition & 1 deletion source/MaaToolKit/ExecAgent/ExecArgConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ExecArgConverter

public:
std::string sync_context_to_arg(MaaSyncContextHandle sync_context);
std::string image_to_arg(ImageTransferMode mode, const cv::Mat& image);
std::string image_to_arg(const cv::Mat& image, ImageTransferMode mode);

MaaSyncContextHandle arg_to_sync_context(const std::string& arg) const;

Expand Down
4 changes: 2 additions & 2 deletions source/MaaToolKit/ExecAgent/RecognizerExecAgent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ std::optional<RecognizerExecAgent::AnalyzeResult> RecognizerExecAgent::analyze(
auto& exec = exec_it->second;

std::string handle_arg = arg_cvt_.sync_context_to_arg(sync_context);
std::string image_arg = arg_cvt_.image_to_arg(exec.image_mode, image);
std::string image_arg = arg_cvt_.image_to_arg(image, exec.image_mode);

std::vector<std::string> extra_args = { handle_arg, image_arg, std::string(task_name),
std::string(custom_recognition_param) };
std::vector<std::string> args = exec.exec_args;
args.insert(args.end(), std::make_move_iterator(extra_args.begin()), std::make_move_iterator(extra_args.end()));

auto output_opt = run_executor(exec.text_mode, exec.exec_path, args);
auto output_opt = run_executor(exec.exec_path, args, exec.text_mode, exec.image_mode);
if (!output_opt) {
LogError << "run_executor failed" << VAR(exec.exec_path) << VAR(exec.exec_args);
return std::nullopt;
Expand Down

0 comments on commit d2d79f2

Please sign in to comment.