18 :
LabeledValue(value, label), handler_(handler), pausing_(state_pausing),
54 }
catch (
const std::exception& ex) {
68 StatePtr state = boost::dynamic_pointer_cast<
State>(
get(value));
90 curr_state_(NEW_ST), prev_state_(NEW_ST),
91 last_event_(NOP_EVT), next_event_(NOP_EVT),
92 on_entry_flag_(false), on_exit_flag_(false),
93 paused_(false), mutex_(new
std::mutex) {
114 if (!dictionaries_initted_) {
115 abortModel(
"runModel invoked before model has been initialized");
129 }
catch (
const std::exception& ex) {
145 std::lock_guard<std::mutex> lock(*mutex_);
146 if (dictionaries_initted_) {
153 }
catch (
const std::exception& ex) {
161 }
catch (
const std::exception& ex) {
166 dictionaries_initted_ =
true;
171 if (!isModelNewInternal()) {
174 << event_value <<
" - " << label);
179 events_.
add(event_value, label);
180 }
catch (
const std::exception& ex) {
189 "Event value is not defined:" << event_value);
192 return (events_.
get(event_value));
198 if (!isModelNewInternal()) {
201 << state_value <<
" - " << label);
206 states_.
add(state_value, label, handler, state_pausing);
207 }
catch (
const std::exception& ex) {
214 std::lock_guard<std::mutex> lock(*mutex_);
222 "State value is not defined:" << state_value);
225 return (states_.
getState(state_value));
265 std::lock_guard<std::mutex> lock(*mutex_);
266 setStateInternal(state);
267 postNextEventInternal(event);
277 std::lock_guard<std::mutex> lock(*mutex_);
285 std::ostringstream stream ;
292 std::lock_guard<std::mutex> lock(*mutex_);
293 setStateInternal(state);
297 StateModel::setStateInternal(
unsigned int state) {
300 "Attempt to set state to an undefined value: " << state );
303 prev_state_ = curr_state_;
307 on_entry_flag_ = ((state !=
END_ST) && (prev_state_ != curr_state_));
310 on_exit_flag_ = on_entry_flag_;
321 std::lock_guard<std::mutex> lock(*mutex_);
322 postNextEventInternal(event_value);
326 StateModel::postNextEventInternal(
unsigned int event_value) {
331 "Attempt to post an undefined event, value: " << event_value);
334 last_event_ = next_event_;
335 next_event_ = event_value;
340 std::lock_guard<std::mutex> lock(*mutex_);
341 bool ret = on_entry_flag_;
342 on_entry_flag_ =
false;
348 std::lock_guard<std::mutex> lock(*mutex_);
349 bool ret = on_exit_flag_;
350 on_exit_flag_ =
false;
356 std::lock_guard<std::mutex> lock(*mutex_);
357 return (curr_state_);
362 std::lock_guard<std::mutex> lock(*mutex_);
363 return (prev_state_);
368 std::lock_guard<std::mutex> lock(*mutex_);
369 return (last_event_);
374 std::lock_guard<std::mutex> lock(*mutex_);
375 return (next_event_);
380 std::lock_guard<std::mutex> lock(*mutex_);
381 return isModelNewInternal();
385 StateModel::isModelNewInternal()
const {
386 return (curr_state_ ==
NEW_ST);
391 std::lock_guard<std::mutex> lock(*mutex_);
392 return ((curr_state_ !=
NEW_ST) && (curr_state_ !=
END_ST));
397 std::lock_guard<std::mutex> lock(*mutex_);
398 return ((curr_state_ !=
NEW_ST) && (curr_state_ !=
END_ST) &&
404 std::lock_guard<std::mutex> lock(*mutex_);
405 return (curr_state_ ==
END_ST);
410 std::lock_guard<std::mutex> lock(*mutex_);
416 std::lock_guard<std::mutex> lock(*mutex_);
422 std::lock_guard<std::mutex> lock(*mutex_);
423 return getStateLabelInternal(state);
427 StateModel::getStateLabelInternal(
const int state)
const {
433 std::lock_guard<std::mutex> lock(*mutex_);
434 return getEventLabelInternal(event);
438 StateModel::getEventLabelInternal(
const int event)
const {
444 std::lock_guard<std::mutex> lock(*mutex_);
445 std::ostringstream stream;
446 stream <<
"current state: [ "
447 << curr_state_ <<
" " << getStateLabelInternal(curr_state_)
448 <<
" ] next event: [ "
449 << next_event_ <<
" " << getEventLabelInternal(next_event_) <<
" ]";
450 return (stream.str());
455 std::lock_guard<std::mutex> lock(*mutex_);
456 std::ostringstream stream;
457 stream <<
"previous state: [ "
458 << prev_state_ <<
" " << getStateLabelInternal(prev_state_)
459 <<
" ] last event: [ "
460 << next_event_ <<
" " << getEventLabelInternal(last_event_) <<
" ]";
461 return (stream.str());
void defineState(unsigned int value, const std::string &label, StateHandler handler, const StatePausing &state_pausing=STATE_PAUSE_NEVER)
Adds an state value and associated label to the set of states.
static const int NOP_EVT
Signifies that no event has occurred.
bool shouldPause()
Indicates if the state model should pause upon entering this state.
static const int FAIL_EVT
Event issued to abort the model execution.
void add(const int value, const std::string &label, StateHandler handler, const StatePausing &state_pausing)
Adds a state definition to the set of states.
StatePausing
State machine pausing modes.
void defineEvent(unsigned int value, const std::string &label)
Adds an event value and associated label to the set of events.
bool doOnExit()
Checks if on exit flag is true.
const StatePtr getState(unsigned int value)
Fetches the state referred to by value.
std::function< void()> StateHandler
Defines a pointer to an instance method for handling a state.
std::string getStateLabel(const int state) const
Fetches the label associated with an state value.
static const int NEW_ST
State that a state model is in immediately after construction.
void abortModel(const std::string &explanation)
Aborts model execution.
static const int START_EVT
Event issued to start the model execution.
std::string getPrevContextStr() const
Convenience method which returns a string rendition of the previous state and last event...
State(const int value, const std::string &label, StateHandler handler, const StatePausing &state_pausing=STATE_PAUSE_NEVER)
Constructor.
unsigned int getLastEvent() const
Fetches the model's last event.
void setState(unsigned int state)
Sets the current state to the given state value.
void run()
Invokes the State's handler.
virtual void onModelFailure(const std::string &explanation)
Handler for fatal model execution errors.
void endModel()
Conducts a normal transition to the end of the model.
unsigned int getCurrState() const
Fetches the model's current state.
LabeledValuePtr EventPtr
Define Event pointer.
virtual void verifyStates()
Validates the contents of the set of states.
virtual void runModel(unsigned int event)
Processes events through the state model.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
bool isModelRunning() const
Returns whether or not the model is running.
boost::shared_ptr< State > StatePtr
Defines a shared pointer to a State.
bool doOnEntry()
Checks if on entry flag is true.
boost::shared_ptr< LabeledValue > LabeledValuePtr
Defines a shared pointer to a LabeledValue instance.
std::string getEventLabel(const int event) const
Fetches the label associated with an event value.
virtual ~State()
Destructor.
Implements the concept of a constant value with a text label.
void nopStateHandler()
An empty state handler.
static const int SM_DERIVED_EVENT_MIN
Value at which custom events in a derived class should begin.
const StatePtr getState(int value)
Fetches a state for the given value.
static const int SM_DERIVED_STATE_MIN
Value at which custom states in a derived class should begin.
const LabeledValuePtr & get(int value)
Fetches a pointer to the entry associated with value.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
bool isModelNew() const
Returns whether or not the model is new.
std::string getContextStr() const
Convenience method which returns a string rendition of the current state and next event...
void startModel(const int start_state)
Begins execution of the model.
Defines a State within the State Model.
virtual ~StateSet()
Destructor.
void unpauseModel()
Unpauses state model.
Defines the logger used by the top-level component of kea-dhcp-ddns.
virtual void defineStates()
Populates the set of states.
unsigned int getPrevState() const
Fetches the model's previous state.
std::string getLabel(const int value) const
Fetches the label for the given value.
void transition(unsigned int state, unsigned int event)
Sets up the model to transition into given state with a given event.
bool isModelWaiting() const
Returns whether or not the model is waiting.
void initDictionaries()
Initializes the event and state dictionaries.
const StatePtr getStateInternal(unsigned int value)
Fetches the state referred to by value.
bool didModelFail() const
Returns whether or not the model failed.
bool isDefined(const int value) const
Tests if the set contains an entry for the given value.
static const int END_ST
Final state, all the state model has reached its conclusion.
bool isModelDone() const
Returns whether or not the model has finished execution.
This file defines the class StateModel.
const EventPtr & getEvent(unsigned int value)
Fetches the event referred to by value.
unsigned int getNextEvent() const
Fetches the model's next event.
bool isModelPaused() const
Returns whether or not the model is paused.
Thrown if the state machine encounters a general error.
virtual void verifyEvents()
Validates the contents of the set of events.
static const int END_EVT
Event issued to end the model execution.
virtual ~StateModel()
Destructor.
virtual void defineEvents()
Populates the set of events.
void postNextEvent(unsigned int event)
Sets the next event to the given event value.
void add(LabeledValuePtr entry)
Adds the given entry to the set.