| #ifndef PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_BMC_REMOTE_STATE_PING_H_ |
| #define PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_BMC_REMOTE_STATE_PING_H_ |
| |
| #include <cstdint> |
| #include <memory> |
| #include <string> |
| #include <vector> |
| |
| #include "callback_manager.h" |
| #include "safepower_agent.pb.h" |
| #include "safepower_agent_config.pb.h" |
| #include "state_updater.h" |
| #include "absl/base/thread_annotations.h" |
| #include "absl/status/status.h" |
| #include "absl/strings/string_view.h" |
| #include "absl/synchronization/mutex.h" |
| #include "absl/time/time.h" |
| #include "boost/process.hpp" // NOLINT(readability/boost) Runs on BMC. |
| |
| namespace safepower_agent { |
| class PingStateMonitor { |
| public: |
| explicit PingStateMonitor( |
| std::string entity_tag, std::string hostname, |
| safepower_agent_config::PingConfig ping_config, |
| std::shared_ptr<StateUpdater<safepower_agent_proto::SystemState>> |
| system_state_updater); |
| // Movable before Start() is called. This is useful for adding monitors to |
| // containers. |
| PingStateMonitor(PingStateMonitor&&); |
| PingStateMonitor& operator=(PingStateMonitor&&); |
| |
| ~PingStateMonitor(); |
| |
| absl::Status Start(); |
| |
| absl::string_view node_entity_tag() const { return entity_tag_; } |
| |
| private: |
| bool Started() const ABSL_LOCKS_EXCLUDED(task_mutex_); |
| |
| enum class ReachableState : uint8_t { |
| kUnknown, |
| kReachable, |
| kUnreachable, |
| }; |
| |
| void StartTimer(absl::Duration interval) |
| ABSL_EXCLUSIVE_LOCKS_REQUIRED(task_mutex_); |
| void StopTimer() ABSL_EXCLUSIVE_LOCKS_REQUIRED(task_mutex_); |
| void Pause() ABSL_LOCKS_EXCLUDED(task_mutex_); |
| void Resume() ABSL_LOCKS_EXCLUDED(task_mutex_); |
| void Ping(); |
| void ProcessPingResult(boost::system::error_code ec, int exit_code); |
| void UpdateReachableState(ReachableState new_reachable_state); |
| ReachableState GetReachableState(int exit_code) const; |
| |
| std::string entity_tag_; |
| std::string hostname_; |
| std::string command_; |
| absl::Duration interval_; |
| std::shared_ptr<StateUpdater<safepower_agent_proto::SystemState>> |
| system_state_updater_; |
| std::vector<std::string> args_; |
| |
| mutable absl::Mutex task_mutex_; |
| // The name of the timer task, or empty if the timer is not running. |
| std::string task_name_ ABSL_GUARDED_BY(task_mutex_); |
| ReachableState reachable_state_ = ReachableState::kUnknown; |
| |
| CallbackManager::Handle activate_callback_handle_; |
| CallbackManager::Handle idle_callback_handle_; |
| }; |
| |
| } // namespace safepower_agent |
| |
| #endif // PRODUCTION_BORG_MGMT_NODE_PROXY_SAFEPOWER_SAFEPOWER_AGENT_BMC_REMOTE_STATE_PING_H_ |