|
| 1 | +/* |
| 2 | + * .============. |
| 3 | + * // M A K E / \ |
| 4 | + * // C++ DEV / \ |
| 5 | + * // E A S Y / \/ \ |
| 6 | + * ++ ----------. \/\ . |
| 7 | + * \\ \ \ /\ / |
| 8 | + * \\ \ \ / |
| 9 | + * \\ \ \ / |
| 10 | + * -============' |
| 11 | + * |
| 12 | + * Copyright (c) 2025 Hevake and contributors, all rights reserved. |
| 13 | + * |
| 14 | + * This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox) |
| 15 | + * Use of this source code is governed by MIT license that can be found |
| 16 | + * in the LICENSE file in the root of the source tree. All contributing |
| 17 | + * project authors may be found in the CONTRIBUTORS.md file in the root |
| 18 | + * of the source tree. |
| 19 | + */ |
| 20 | +#include "switch_action.h" |
| 21 | + |
| 22 | +#include <tbox/base/assert.h> |
| 23 | +#include <tbox/base/defines.h> |
| 24 | +#include <tbox/base/json.hpp> |
| 25 | +#include <tbox/util/string.h> |
| 26 | + |
| 27 | +namespace tbox { |
| 28 | +namespace flow { |
| 29 | + |
| 30 | +using namespace std::placeholders; |
| 31 | + |
| 32 | +SwitchAction::SwitchAction(event::Loop &loop) |
| 33 | + : AssembleAction(loop, "Switch") |
| 34 | +{ } |
| 35 | + |
| 36 | +SwitchAction::~SwitchAction() { |
| 37 | + CHECK_DELETE_RESET_OBJ(switch_action_); |
| 38 | + CHECK_DELETE_RESET_OBJ(default_action_); |
| 39 | + for (auto &item : case_actions_) |
| 40 | + CHECK_DELETE_OBJ(item.second); |
| 41 | + case_actions_.clear(); |
| 42 | +} |
| 43 | + |
| 44 | +void SwitchAction::toJson(Json &js) const { |
| 45 | + AssembleAction::toJson(js); |
| 46 | + |
| 47 | + auto &js_children = js["children"]; |
| 48 | + switch_action_->toJson(js_children["00.switch"]); |
| 49 | + |
| 50 | + if (default_action_ != nullptr) |
| 51 | + default_action_->toJson(js_children["99.default"]); |
| 52 | + |
| 53 | + int index = 1; |
| 54 | + for (auto &item : case_actions_) { |
| 55 | + std::ostringstream oss; |
| 56 | + oss << std::setw(2) << std::setfill('0') << index << '.' << item.first; |
| 57 | + item.second->toJson(js_children[oss.str()]); |
| 58 | + ++index; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +bool SwitchAction::setChildAs(Action *child, const std::string &role) { |
| 63 | + if (child == nullptr) { |
| 64 | + LogWarn("%d:%s[%s], child is nullptr, role:%s", id(), type().c_str(), label().c_str(), role.c_str()); |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + if (role == "switch") { |
| 69 | + child->setFinishCallback(std::bind(&SwitchAction::onSwitchActionFinished, this, _1, _2, _3)); |
| 70 | + child->setBlockCallback(std::bind(&SwitchAction::block, this, _1, _2)); |
| 71 | + child->setParent(this); |
| 72 | + CHECK_DELETE_RESET_OBJ(switch_action_); |
| 73 | + switch_action_ = child; |
| 74 | + return true; |
| 75 | + |
| 76 | + } else if (role == "default") { |
| 77 | + child->setFinishCallback(std::bind(&SwitchAction::finish, this, _1, _2, _3)); |
| 78 | + child->setBlockCallback(std::bind(&SwitchAction::block, this, _1, _2)); |
| 79 | + child->setParent(this); |
| 80 | + CHECK_DELETE_RESET_OBJ(default_action_); |
| 81 | + default_action_ = child; |
| 82 | + return true; |
| 83 | + |
| 84 | + } else if (util::string::IsStartWith(role, "case:")) { |
| 85 | + auto result = case_actions_.emplace(role, child); |
| 86 | + if (result.second) { |
| 87 | + child->setFinishCallback(std::bind(&SwitchAction::finish, this, _1, _2, _3)); |
| 88 | + child->setBlockCallback(std::bind(&SwitchAction::block, this, _1, _2)); |
| 89 | + child->setParent(this); |
| 90 | + return true; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + LogWarn("%d:%s[%s], unsupport role:%s", id(), type().c_str(), label().c_str(), role.c_str()); |
| 95 | + return false; |
| 96 | +} |
| 97 | + |
| 98 | +bool SwitchAction::isReady() const { |
| 99 | + //! switch_action没有安装,自然就没有就绪 |
| 100 | + if (switch_action_ == nullptr) { |
| 101 | + LogWarn("%d:%s[%s] no switch action", id(), type().c_str(), label().c_str()); |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + //! 如果default与case都没有,则表示Action没有安装好 |
| 106 | + if (!default_action_ && case_actions_.empty()) { |
| 107 | + LogWarn("%d:%s[%s] both default and case action is null", id(), type().c_str(), label().c_str()); |
| 108 | + return false; |
| 109 | + } |
| 110 | + |
| 111 | + if (!switch_action_->isReady()) |
| 112 | + return false; |
| 113 | + |
| 114 | + if (default_action_ != nullptr && !default_action_->isReady()) |
| 115 | + return false; |
| 116 | + |
| 117 | + //! 检查所有的case_action,看看有没有哪个没有就绪 |
| 118 | + for (auto &item : case_actions_) { |
| 119 | + if (!item.second->isReady()) |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | + return true; |
| 124 | +} |
| 125 | + |
| 126 | +void SwitchAction::onStart() { |
| 127 | + AssembleAction::onStart(); |
| 128 | + |
| 129 | + TBOX_ASSERT(switch_action_ != nullptr); |
| 130 | + running_action_ = switch_action_; |
| 131 | + running_action_->start(); |
| 132 | +} |
| 133 | + |
| 134 | +void SwitchAction::onStop() { |
| 135 | + if (running_action_ != nullptr) |
| 136 | + running_action_->stop(); |
| 137 | + |
| 138 | + AssembleAction::onStop(); |
| 139 | +} |
| 140 | + |
| 141 | +void SwitchAction::onPause() { |
| 142 | + if (running_action_ != nullptr) |
| 143 | + running_action_->pause(); |
| 144 | + |
| 145 | + AssembleAction::onPause(); |
| 146 | +} |
| 147 | + |
| 148 | +void SwitchAction::onResume() { |
| 149 | + AssembleAction::onResume(); |
| 150 | + |
| 151 | + if (running_action_ != nullptr) |
| 152 | + running_action_->resume(); |
| 153 | +} |
| 154 | + |
| 155 | +void SwitchAction::onReset() { |
| 156 | + TBOX_ASSERT(switch_action_ != nullptr); |
| 157 | + switch_action_->reset(); |
| 158 | + |
| 159 | + if (default_action_ != nullptr) |
| 160 | + default_action_->reset(); |
| 161 | + |
| 162 | + for (auto &item : case_actions_) |
| 163 | + item.second->reset(); |
| 164 | + |
| 165 | + running_action_ = nullptr; |
| 166 | + |
| 167 | + AssembleAction::onReset(); |
| 168 | +} |
| 169 | + |
| 170 | +void SwitchAction::onSwitchActionFinished(bool is_succ, const Reason &why, const Trace &trace) { |
| 171 | + if (state() == State::kRunning) { |
| 172 | + if (is_succ) { |
| 173 | + running_action_ = default_action_; |
| 174 | + |
| 175 | + auto iter = case_actions_.find(why.message); |
| 176 | + if (iter != case_actions_.end()) |
| 177 | + running_action_ = iter->second; |
| 178 | + |
| 179 | + if (running_action_ != nullptr) { |
| 180 | + running_action_->start(); |
| 181 | + } else { |
| 182 | + finish(false, Reason(ACTION_REASON_SWITCH_SKIP, "SwitchSkip")); |
| 183 | + } |
| 184 | + |
| 185 | + } else { |
| 186 | + finish(false, Reason(ACTION_REASON_SWITCH_FAIL, "SwitchFail")); |
| 187 | + } |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +} |
| 192 | +} |
0 commit comments