hwc_state_manager.cc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "hwc_state_manager.h"
  2. // local
  3. #include "hwc_init_state.h"
  4. #include "hwc_register_state.h"
  5. #include "hwc_binlog_state.h"
  6. #include "hwc_fault_state.h"
  7. HwcStateManager::HwcStateManager()
  8. : i_pre_state_(E_HWC_STATE_INIT)
  9. , i_current_state_(E_HWC_STATE_INIT)
  10. , i_next_state_(E_HWC_STATE_INIT)
  11. , p_current_state_(NULL)
  12. , p_db_config_(NULL)
  13. , o_hwc_state_map_()
  14. {
  15. o_hwc_state_map_.insert(std::make_pair(E_HWC_STATE_INIT , new InitState(this)));
  16. o_hwc_state_map_.insert(std::make_pair(E_HWC_STATE_REGISTER , new RegisterState(this)));
  17. o_hwc_state_map_.insert(std::make_pair(E_HWC_STATE_BINLOG_SYNC , new BinlogState(this)));
  18. o_hwc_state_map_.insert(std::make_pair(E_HWC_STATE_FAULT , new FaultState(this)));
  19. p_current_state_ = o_hwc_state_map_[E_HWC_STATE_INIT];
  20. }
  21. HwcStateManager::~HwcStateManager()
  22. {
  23. HwcStateMapIter iter = o_hwc_state_map_.begin();
  24. for (; iter != o_hwc_state_map_.end(); ++iter)
  25. {
  26. DELETE(iter->second);
  27. }
  28. o_hwc_state_map_.clear();
  29. DELETE(p_db_config_);
  30. }
  31. void HwcStateManager::Start()
  32. {
  33. assert(p_current_state_ != NULL);
  34. p_current_state_->Enter();
  35. p_current_state_->HandleEvent();
  36. }
  37. void HwcStateManager::ChangeState(int iNewState)
  38. {
  39. assert(p_current_state_ != NULL);
  40. i_next_state_ = iNewState;
  41. p_current_state_->Exit();
  42. log4cplus_info(LOG_KEY_WORD "changeState from %d to %d" , i_current_state_ , iNewState);
  43. assert(o_hwc_state_map_.find(iNewState) != o_hwc_state_map_.end());
  44. p_current_state_ = o_hwc_state_map_[iNewState];
  45. i_pre_state_ = i_current_state_;
  46. i_current_state_ = iNewState;
  47. p_current_state_->Enter();
  48. p_current_state_->HandleEvent();
  49. }