 |
Kea
1.5.0
|
Go to the documentation of this file.
9 #include <boost/bind.hpp>
19 :
LabeledValue(value, label), handler_(handler), pausing_(state_pausing),
55 }
catch (
const std::exception& ex) {
69 StatePtr state = boost::dynamic_pointer_cast<State>(
get(value));
91 curr_state_(NEW_ST), prev_state_(NEW_ST),
92 last_event_(NOP_EVT), next_event_(NOP_EVT),
93 on_entry_flag_(false), on_exit_flag_(false),
115 if (!dictionaries_initted_) {
116 abortModel(
"runModel invoked before model has been initialized");
131 catch (
const std::exception& ex) {
151 }
catch (
const std::exception& ex) {
159 }
catch (
const std::exception& ex) {
164 dictionaries_initted_ =
true;
172 << event_value <<
" - " << label);
177 events_.
add(event_value, label);
178 }
catch (
const std::exception& ex) {
187 "Event value is not defined:" << event_value);
190 return (events_.
get(event_value));
199 << state_value <<
" - " << label);
204 states_.
add(state_value, label, handler, state_pausing);
205 }
catch (
const std::exception& ex) {
214 "State value is not defined:" << state_value);
217 return (states_.
getState(state_value));
275 std::ostringstream stream ;
284 "Attempt to set state to an undefined value: " << state );
287 prev_state_ = curr_state_;
291 on_entry_flag_ = ((state !=
END_ST) && (prev_state_ != curr_state_));
294 on_exit_flag_ = on_entry_flag_;
298 if (on_entry_flag_ && !paused_ && (
getState(state)->shouldPause())) {
309 "Attempt to post an undefined event, value: " << event_value);
312 last_event_ = next_event_;
313 next_event_ = event_value;
318 bool ret = on_entry_flag_;
319 on_entry_flag_ =
false;
325 bool ret = on_exit_flag_;
326 on_exit_flag_ =
false;
332 return (curr_state_);
337 return (prev_state_);
342 return (last_event_);
347 return (next_event_);
351 return (curr_state_ ==
NEW_ST);
356 return ((curr_state_ !=
NEW_ST) && (curr_state_ !=
END_ST));
366 return (curr_state_ ==
END_ST);
391 std::ostringstream stream;
392 stream <<
"current state: [ "
394 <<
" ] next event: [ "
396 return(stream.str());
401 std::ostringstream stream;
402 stream <<
"previous state: [ "
404 <<
" ] last event: [ "
406 return(stream.str());
virtual ~State()
Destructor.
StatePausing
State machine pausing modes.
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.
void unpauseModel()
Unpauses state model.
static const int NOP_EVT
Signifies that no event has occurred.
static const int SM_DERIVED_STATE_MIN
Value at which custom states in a derived class should begin.
boost::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.
void transition(unsigned int state, unsigned int event)
Sets up the model to transition into given state with a given event.
void run()
Invokes the State's handler.
bool doOnEntry()
Checks if on entry flag is true.
static const int END_EVT
Event issued to end the model execution.
bool isDefined(const int value) const
Tests if the set contains an entry for the given value.
Defines a State within the State Model.
void defineEvent(unsigned int value, const std::string &label)
Adds an event value and associated label to the set of events.
std::string getLabel(const int value) const
Fetches the label for the given value.
virtual void defineEvents()
Populates the set of events.
virtual void defineStates()
Populates the set of states.
boost::shared_ptr< State > StatePtr
Defines a shared pointer to a State.
std::string getEventLabel(const int event) const
Fetches the label associated with an event value.
virtual ~StateModel()
Destructor.
const EventPtr & getEvent(unsigned int value)
Fetches the event referred to by value.
bool isModelDone() const
Returns whether or not the model has finished execution.
bool isModelWaiting() const
Returns whether or not the model is waiting.
const LabeledValuePtr & get(int value)
Fetches a pointer to the entry associated with value.
Defines the logger used by the top-level component of kea-dhcp-ddns.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
unsigned int getPrevState() const
Fetches the model's previous state.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
State(const int value, const std::string &label, StateHandler handler, const StatePausing &state_pausing=STATE_PAUSE_NEVER)
Constructor.
void nopStateHandler()
An empty state handler.
bool isModelPaused() const
Returns whether or not the model is paused.
boost::shared_ptr< LabeledValue > LabeledValuePtr
Defines a shared pointer to a LabeledValue instance.
virtual void onModelFailure(const std::string &explanation)
Handler for fatal model execution errors.
std::string getContextStr() const
Convenience method which returns a string rendition of the current state and next event.
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.
virtual void runModel(unsigned int event)
Processes events through the state model.
static const int NEW_ST
State that a state model is in immediately after construction.
Implements the concept of a constant value with a text label.
virtual ~StateSet()
Destructor.
virtual void verifyEvents()
Validates the contents of the set of events.
void initDictionaries()
Initializes the event and state dictionaries.
const StatePtr getState(int value)
Fetches a state for the given value.
static const int SM_DERIVED_EVENT_MIN
Value at which custom events in a derived class should begin.
bool didModelFail() const
Returns whether or not the model failed.
unsigned int getCurrState() const
Fetches the model's current state.
std::string getPrevContextStr() const
Convenience method which returns a string rendition of the previous state and last event.
virtual void verifyStates()
Validates the contents of the set of states.
void endModel()
Conducts a normal transition to the end of the model.
unsigned int getNextEvent() const
Fetches the model's next event.
static const int END_ST
Final state, all the state model has reached its conclusion.
Thrown if the state machine encounters a general error.
void postNextEvent(unsigned int event)
Sets the next event to the given event value.
bool isModelNew() const
Returns whether or not the model is new.
static const int START_EVT
Event issued to start the model execution.
void abortModel(const std::string &explanation)
Aborts model execution.
void add(LabeledValuePtr entry)
Adds the given entry to the set.
void startModel(const int start_state)
Begins execution of the model.
bool isModelRunning() const
Returns whether or not the model is running.
void setState(unsigned int state)
Sets the current state to the given state value.
void add(const int value, const std::string &label, StateHandler handler, const StatePausing &state_pausing)
Adds a state definition to the set of states.
LabeledValuePtr EventPtr
Define Event pointer.
bool doOnExit()
Checks if on exit flag is true.
unsigned int getLastEvent() const
Fetches the model's last event.
const StatePtr getState(unsigned int value)
Fetches the state referred to by value.