Skip to content

Commit

Permalink
opt(flow):1.7.18, 优化Action,使用Reason传递block的原因
Browse files Browse the repository at this point in the history
  • Loading branch information
hevake committed Mar 14, 2024
1 parent 8b31cab commit 82c2c56
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 19 deletions.
17 changes: 15 additions & 2 deletions modules/flow/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ bool Action::finish(bool is_succ) {
}
}

bool Action::block(int why) {
bool Action::block(const Reason &why) {
if (state_ != State::kFinished && state_ != State::kStoped) {
LogDbg("action %d:%s[%s] blocked", id_, type_.c_str(), label_.c_str());

Expand Down Expand Up @@ -282,7 +282,7 @@ void Action::onStop() { is_base_func_invoked_ = true; }

void Action::onReset() { is_base_func_invoked_ = true; }

void Action::onBlock(int why) {
void Action::onBlock(const Reason &why) {
if (block_cb_)
block_cb_run_id_ = loop_.runNext(std::bind(block_cb_, why), "Action::block");

Expand Down Expand Up @@ -310,6 +310,19 @@ void Action::cancelDispatchedCallback() {
}
}

Action::Reason::Reason(const Reason &other)
: code(other.code)
, message(other.message)
{ }

Action::Reason& Action::Reason::operator = (const Reason &other) {
if (this != &other) {
code = other.code;
message = other.message;
}
return *this;
}

std::string ToString(Action::State state) {
const char *tbl[] = { "idle", "running", "pause", "finished", "stoped" };
auto index = static_cast<size_t>(state);
Expand Down
21 changes: 17 additions & 4 deletions modules/flow/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ class Action {
kFail, //!< 失败
};

//! 失败或阻塞原因
struct Reason {
int code; //!< 错误码
std::string message; //!< 错误说明

Reason() : code(0) { }
Reason(int c) : code(c) { }
Reason(int c, std::string m) : code(c), message(m) { }

Reason(const Reason &other);
Reason& operator = (const Reason &other);
};

inline int id() const { return id_; }
inline const std::string& type() const { return type_; }

Expand All @@ -78,7 +91,7 @@ class Action {
inline void setFinishCallback(FinishCallback &&cb) { finish_cb_ = std::move(cb); }

//!< 设置阻塞回调
using BlockCallback = std::function<void(int)>;
using BlockCallback = std::function<void(const Reason &)>;
inline void setBlockCallback(BlockCallback &&cb) { block_cb_ = std::move(cb); }

void setTimeout(std::chrono::milliseconds ms);
Expand All @@ -95,12 +108,12 @@ class Action {
void reset(); //!< 重置,将所有的状态恢复到刚构建状态

protected:
bool block(int why = 0); //!< 主动暂停动作
bool finish(bool is_succ); //!< 主动结束动作
bool block(const Reason &why); //!< 主动暂停动作
bool finish(bool is_succ); //!< 主动结束动作

virtual void onStart();
virtual void onPause();
virtual void onBlock(int why);
virtual void onBlock(const Reason &why);
virtual void onResume();
virtual void onStop();
virtual void onReset();
Expand Down
2 changes: 1 addition & 1 deletion modules/flow/action_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ TEST(Action, StartBlock) {
bool is_block = false;

TestAction action(*loop);
action.setBlockCallback([&](int why) { is_block = true; });
action.setBlockCallback([&](const Action::Reason &) { is_block = true; });
action.start();

loop->exitLoop(std::chrono::milliseconds(10));
Expand Down
2 changes: 1 addition & 1 deletion modules/flow/actions/composite_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void CompositeAction::onChildFinished(bool is_succ) {
finish(is_succ);
}

void CompositeAction::onChildBlocked(int why) {
void CompositeAction::onChildBlocked(const Reason &why) {
if (state() == State::kRunning)
block(why);
}
Expand Down
2 changes: 1 addition & 1 deletion modules/flow/actions/composite_action.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class CompositeAction : public AssembleAction {
virtual void onFinished(bool is_succ) override;

void onChildFinished(bool is_succ);
void onChildBlocked(int why);
void onChildBlocked(const Reason &why);

private:
Action *child_ = nullptr;
Expand Down
4 changes: 2 additions & 2 deletions modules/flow/actions/composite_action_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ TEST(CompositeAction, ChildBlock) {

ParentAction action(*loop);

action.setBlockCallback([&] (int why) {
action.setBlockCallback([&] (const Action::Reason &why) {
is_blocked = true;
EXPECT_EQ(why, 1);
EXPECT_EQ(why.code, 1);
EXPECT_FALSE(is_finished);
EXPECT_EQ(action.state(), Action::State::kPause);
action.resume();
Expand Down
2 changes: 1 addition & 1 deletion modules/flow/actions/event_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void EventAction::onPause() {
Action::onPause();
}

void EventAction::onBlock(int why) {
void EventAction::onBlock(const Reason &why) {
pub_.unsubscribe(this);
Action::onBlock(why);
}
Expand Down
2 changes: 1 addition & 1 deletion modules/flow/actions/event_action.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class EventAction : public Action,
virtual void onStart() override;
virtual void onStop() override;
virtual void onPause() override;
virtual void onBlock(int why) override;
virtual void onBlock(const Reason &why) override;
virtual void onResume() override;
virtual void onReset() override;
virtual void onFinished(bool succ) override;
Expand Down
4 changes: 2 additions & 2 deletions modules/flow/actions/if_else_action_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,9 @@ TEST(IfElseAction, BlockOnIf) {
EXPECT_TRUE(if_else_action.setChildAs(succ_action, "succ"));
EXPECT_TRUE(if_else_action.isReady());

if_else_action.setBlockCallback([&] (int why) {
if_else_action.setBlockCallback([&] (const Action::Reason &why) {
is_blocked = true;
EXPECT_EQ(why, 1);
EXPECT_EQ(why.code, 1);
EXPECT_EQ(if_else_action.state(), Action::State::kPause);
if_else_action.resume();
});
Expand Down
2 changes: 1 addition & 1 deletion modules/flow/actions/parallel_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void ParallelAction::onChildFinished(int index, bool is_succ) {
}
}

void ParallelAction::onChildBlocked(int index, int why) {
void ParallelAction::onChildBlocked(int index, const Reason &why) {
if (state() == State::kRunning) {
blocked_children_[index] = why;
//!FIXME:目前遇到block的动作,仅停止它。将来需要更精细化的处理
Expand Down
4 changes: 2 additions & 2 deletions modules/flow/actions/parallel_action.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ParallelAction : public AssembleAction {
virtual bool isReady() const override;

using FinishedChildren = std::map<int, bool>;
using BlockedChildren = std::map<int, int>;
using BlockedChildren = std::map<int, Reason>;

FinishedChildren getFinishedChildren() const { return finished_children_; }
BlockedChildren getBlockedChildren() const { return blocked_children_; }
Expand All @@ -57,7 +57,7 @@ class ParallelAction : public AssembleAction {
private:
void tryFinish();
void onChildFinished(int index, bool is_succ);
void onChildBlocked(int index, int why);
void onChildBlocked(int index, const Reason &why);

private:
std::vector<Action*> children_;
Expand Down
2 changes: 1 addition & 1 deletion version.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# TBOX版本号
TBOX_VERSION_MAJOR := 1
TBOX_VERSION_MINOR := 7
TBOX_VERSION_REVISION := 17
TBOX_VERSION_REVISION := 18

0 comments on commit 82c2c56

Please sign in to comment.