| #include "bmc/remote_state_monitor.h" |
| |
| #include <memory> |
| #include <string> |
| #include <utility> |
| |
| #include "polling_state_monitor.h" |
| #include "system_state.pb.h" |
| #include "state_updater.h" |
| #include "absl/log/log.h" |
| #include "absl/time/time.h" |
| |
| namespace safepower_agent { |
| |
| RemoteStateMonitor::RemoteStateMonitor( |
| std::string entity_tag, absl::Duration interval, |
| std::shared_ptr<StateUpdater<safepower_agent_proto::SystemState>> |
| system_state_updater, |
| std::string monitor_name) |
| : PollingStateMonitor(std::move(entity_tag), interval, |
| std::move(system_state_updater), |
| std::move(monitor_name)) {} |
| |
| RemoteStateMonitor::~RemoteStateMonitor() = default; |
| |
| void RemoteStateMonitor::OnPause() { |
| // When the state updater is paused, we don't know the current state of the |
| // node. e.g. it is possible that the node goes down or comes back up while |
| // the updater is paused. When a new listener is added, setting an unknown |
| // state ensures that we never report a stale state. Since state comparisons |
| // are usually equality checks, this should not break any assumptions. |
| UpdateReachableState(ReachableState::kUnknown); |
| } |
| |
| void RemoteStateMonitor::UpdateReachableState(ReachableState reachable_state) { |
| if (reachable_state == reachable_state_) { |
| return; |
| } |
| safepower_agent_proto::SystemState new_state; |
| UpdateNodeState((*new_state.mutable_node_state())[entity_tag_], |
| reachable_state); |
| system_state_updater_->UpdateState(new_state); |
| reachable_state_ = reachable_state; |
| } |
| |
| void RemoteStateMonitor::Check() { |
| if (checking_->test_and_set()) { |
| LOG(WARNING) << "Check already in progress for " << entity_tag_; |
| return; |
| } |
| DoCheck(CheckGuard(checking_)); |
| } |
| |
| } // namespace safepower_agent |