ha_state_machine_control.cc 1.8 KB
Newer Older
1 2 3 4 5 6
// Copyright (C) 2018 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

7 8
#include <ha_log.h>
#include <ha_service_states.h>
9
#include <ha_state_machine_control.h>
10
#include <boost/algorithm/string.hpp>
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

namespace isc {
namespace ha {

HAStateMachineControl::HAStateMachineControl(const HAConfigPtr& config)
    : config_(config), paused_(false), visited_states_() {
}

void
HAStateMachineControl::notify(const int state) {
    // Always get the configuration to verify that the state identifier is
    // recognized.
    HAConfig::StateConfigPtr state_config = config_->getStateConfig(state);

    // Pause if we should always pause in this state or we should pause once
    // and this is the first time we visit this state.
    bool first_visit = (visited_states_.count(state) == 0);

    // If this is the first time we're in this state, record it.
    if (first_visit) {
        visited_states_.insert(state);
    }

    // Only pause the state machine if it is not paused already.
    if (!amPaused()) {
        if ((state_config->getPausing() == HAConfig::StateConfig::PAUSE_ALWAYS) ||
            ((state_config->getPausing() == HAConfig::StateConfig::PAUSE_ONCE) &&
             first_visit)) {
39 40 41 42
            std::string state_name = stateToString(state);
            boost::to_upper(state_name);
            LOG_INFO(ha_logger, HA_STATE_MACHINE_PAUSED)
                .arg(state_name);
43 44 45 46 47
            paused_ = true;
        }
    }
}

48 49 50 51 52 53 54 55
void
HAStateMachineControl::unpause() {
    if (amPaused()) {
        LOG_INFO(ha_logger, HA_STATE_MACHINE_CONTINUED);
        paused_ = false;
    }
}

56 57
} // end of namespace isc::ha
} // end of namespace isc