Kea  1.5.0
state_model.h
Go to the documentation of this file.
1 // Copyright (C) 2013-2018 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 
7 #ifndef STATE_MODEL_H
8 #define STATE_MODEL_H
9 
11 
12 #include <exceptions/exceptions.h>
13 #include <util/labeled_value.h>
14 #include <boost/function.hpp>
15 #include <boost/shared_ptr.hpp>
16 #include <map>
17 #include <string>
18 
19 namespace isc {
20 namespace util {
21 
24 public:
25  StateModelError(const char* file, size_t line, const char* what) :
26  isc::Exception(file, line, what) { };
27 };
28 
31 
34 
36 typedef boost::function<void()> StateHandler;
37 
48 };
49 
60 class State : public LabeledValue {
61 public:
79  State(const int value, const std::string& label, StateHandler handler,
80  const StatePausing& state_pausing = STATE_PAUSE_NEVER);
81 
83  virtual ~State();
84 
86  void run();
87 
95  bool shouldPause();
96 
97 private:
99  StateHandler handler_;
100 
102  StatePausing pausing_;
103 
106  bool was_paused_;
107 };
108 
110 typedef boost::shared_ptr<State> StatePtr;
111 
117 class StateSet : public LabeledValueSet {
118 public:
120  StateSet();
121 
123  virtual ~StateSet();
124 
134  void add(const int value, const std::string& label, StateHandler handler,
135  const StatePausing& state_pausing);
136 
145  const StatePtr getState(int value);
146 };
147 
225 
271 class StateModel {
272 public:
273 
275  static const int NEW_ST = 0;
277 
279  static const int END_ST = 1;
280 
282  static const int SM_DERIVED_STATE_MIN = 11;
284 
286  static const int NOP_EVT = 0;
290 
292  static const int START_EVT = 1;
293 
295  static const int END_EVT = 2;
296 
298  static const int FAIL_EVT = 3;
299 
301  static const int SM_DERIVED_EVENT_MIN = 11;
303 
305  StateModel();
306 
308  virtual ~StateModel();
309 
320  void startModel(const int start_state);
321 
344  virtual void runModel(unsigned int event);
345 
353  void endModel();
354 
356  void unpauseModel();
357 
363  void nopStateHandler();
364 
365 protected:
373  void initDictionaries();
374 
396  virtual void defineEvents();
397 
406  void defineEvent(unsigned int value, const std::string& label);
407 
415  const EventPtr& getEvent(unsigned int value);
416 
440  virtual void verifyEvents();
441 
463  virtual void defineStates();
464 
477  void defineState(unsigned int value, const std::string& label,
478  StateHandler handler,
479  const StatePausing& state_pausing = STATE_PAUSE_NEVER);
480 
488  const StatePtr getState(unsigned int value);
489 
512  virtual void verifyStates();
513 
524  virtual void onModelFailure(const std::string& explanation);
525 
537  void transition(unsigned int state, unsigned int event);
538 
548  void abortModel(const std::string& explanation);
549 
561  void setState(unsigned int state);
562 
572  void postNextEvent(unsigned int event);
573 
583  bool doOnEntry();
584 
594  bool doOnExit();
595 
596 public:
604  unsigned int getCurrState() const;
605 
609  unsigned int getPrevState() const;
610 
614  unsigned int getLastEvent() const;
615 
623  unsigned int getNextEvent() const;
624 
628  bool isModelNew() const;
629 
634  bool isModelRunning() const;
635 
640  bool isModelWaiting() const;
641 
645  bool isModelDone() const;
646 
650  bool isModelPaused() const;
651 
656  bool didModelFail() const;
657 
664  std::string getEventLabel(const int event) const;
665 
672  std::string getStateLabel(const int state) const;
673 
682  std::string getContextStr() const;
683 
692  std::string getPrevContextStr() const;
693 
694 private:
696  LabeledValueSet events_;
697 
699  StateSet states_;
700 
702  bool dictionaries_initted_;
703 
705  unsigned int curr_state_;
706 
708  unsigned int prev_state_;
709 
711  unsigned int last_event_;
712 
714  unsigned int next_event_;
715 
717  bool on_entry_flag_;
718 
720  bool on_exit_flag_;
721 
723  bool paused_;
724 };
725 
727 typedef boost::shared_ptr<StateModel> StateModelPtr;
728 
729 } // namespace isc::util
730 } // namespace isc
731 #endif
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.
Definition: state_model.cc:194
static const int NOP_EVT
Signifies that no event has occurred.
Definition: state_model.h:289
bool shouldPause()
Indicates if the state model should pause upon entering this state.
Definition: state_model.cc:32
bool isModelDone() const
Returns whether or not the model has finished execution.
Definition: state_model.cc:365
static const int FAIL_EVT
Event issued to abort the model execution.
Definition: state_model.h:298
This file defines classes: LabeledValue and LabeledValueSet.
void add(const int value, const std::string &label, StateHandler handler, const StatePausing &state_pausing)
Adds a state definition to the set of states.
Definition: state_model.cc:50
StatePausing
State machine pausing modes.
Definition: state_model.h:44
Implements a finite state machine.
Definition: state_model.h:271
std::string getContextStr() const
Convenience method which returns a string rendition of the current state and next event.
Definition: state_model.cc:390
void defineEvent(unsigned int value, const std::string &label)
Adds an event value and associated label to the set of events.
Definition: state_model.cc:168
bool doOnExit()
Checks if on exit flag is true.
Definition: state_model.cc:324
std::string getEventLabel(const int event) const
Fetches the label associated with an event value.
Definition: state_model.cc:385
const StatePtr getState(unsigned int value)
Fetches the state referred to by value.
Definition: state_model.cc:211
bool isModelNew() const
Returns whether or not the model is new.
Definition: state_model.cc:350
static const int NEW_ST
State that a state model is in immediately after construction.
Definition: state_model.h:276
void abortModel(const std::string &explanation)
Aborts model execution.
Definition: state_model.cc:272
StateModelError(const char *file, size_t line, const char *what)
Definition: state_model.h:25
static const int START_EVT
Event issued to start the model execution.
Definition: state_model.h:292
bool isModelWaiting() const
Returns whether or not the model is waiting.
Definition: state_model.cc:360
State(const int value, const std::string &label, StateHandler handler, const StatePausing &state_pausing=STATE_PAUSE_NEVER)
Constructor.
Definition: state_model.cc:17
StateSet()
Constructor.
Definition: state_model.cc:43
std::string getPrevContextStr() const
Convenience method which returns a string rendition of the previous state and last event.
Definition: state_model.cc:400
void setState(unsigned int state)
Sets the current state to the given state value.
Definition: state_model.cc:281
void run()
Invokes the State's handler.
Definition: state_model.cc:27
boost::function< void()> StateHandler
Defines a pointer to an instance method for handling a state.
Definition: state_model.h:36
virtual void onModelFailure(const std::string &explanation)
Handler for fatal model execution errors.
Definition: state_model.cc:251
void endModel()
Conducts a normal transition to the end of the model.
Definition: state_model.cc:262
LabeledValuePtr EventPtr
Define Event pointer.
Definition: state_model.h:33
bool didModelFail() const
Returns whether or not the model failed.
Definition: state_model.cc:370
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
virtual void verifyStates()
Validates the contents of the set of states.
Definition: state_model.cc:245
boost::shared_ptr< StateModel > StateModelPtr
Defines a pointer to a StateModel.
Definition: state_model.h:727
unsigned int getLastEvent() const
Fetches the model's last event.
Definition: state_model.cc:341
virtual void runModel(unsigned int event)
Processes events through the state model.
Definition: state_model.cc:113
boost::shared_ptr< State > StatePtr
Defines a shared pointer to a State.
Definition: state_model.h:110
bool doOnEntry()
Checks if on entry flag is true.
Definition: state_model.cc:317
boost::shared_ptr< LabeledValue > LabeledValuePtr
Defines a shared pointer to a LabeledValue instance.
Definition: labeled_value.h:92
virtual ~State()
Destructor.
Definition: state_model.cc:23
Implements the concept of a constant value with a text label.
Definition: labeled_value.h:39
void nopStateHandler()
An empty state handler.
Definition: state_model.cc:142
unsigned int getNextEvent() const
Fetches the model's next event.
Definition: state_model.cc:346
static const int SM_DERIVED_EVENT_MIN
Value at which custom events in a derived class should begin.
Definition: state_model.h:301
LabeledValue Event
Define an Event.
Definition: state_model.h:30
const StatePtr getState(int value)
Fetches a state for the given value.
Definition: state_model.cc:62
bool isModelRunning() const
Returns whether or not the model is running.
Definition: state_model.cc:355
static const int SM_DERIVED_STATE_MIN
Value at which custom states in a derived class should begin.
Definition: state_model.h:282
unsigned int getCurrState() const
Fetches the model's current state.
Definition: state_model.cc:331
void startModel(const int start_state)
Begins execution of the model.
Definition: state_model.cc:101
Defines a State within the State Model.
Definition: state_model.h:60
virtual ~StateSet()
Destructor.
Definition: state_model.cc:46
void unpauseModel()
Unpauses state model.
Definition: state_model.cc:267
This is a base class for exceptions thrown from the DNS library module.
Defines the logger used by the top-level component of kea-dhcp-ddns.
virtual void defineStates()
Populates the set of states.
Definition: state_model.cc:237
Implements a set of unique LabeledValues.
void transition(unsigned int state, unsigned int event)
Sets up the model to transition into given state with a given event.
Definition: state_model.cc:256
std::string getStateLabel(const int state) const
Fetches the label associated with an state value.
Definition: state_model.cc:380
unsigned int getPrevState() const
Fetches the model's previous state.
Definition: state_model.cc:336
void initDictionaries()
Initializes the event and state dictionaries.
Definition: state_model.cc:146
static const int END_ST
Final state, all the state model has reached its conclusion.
Definition: state_model.h:279
const EventPtr & getEvent(unsigned int value)
Fetches the event referred to by value.
Definition: state_model.cc:184
Implements a unique set or dictionary of states.
Definition: state_model.h:117
bool isModelPaused() const
Returns whether or not the model is paused.
Definition: state_model.cc:375
Thrown if the state machine encounters a general error.
Definition: state_model.h:23
StateModel()
Constructor.
Definition: state_model.cc:90
virtual void verifyEvents()
Validates the contents of the set of events.
Definition: state_model.cc:229
static const int END_EVT
Event issued to end the model execution.
Definition: state_model.h:295
virtual ~StateModel()
Destructor.
Definition: state_model.cc:97
virtual void defineEvents()
Populates the set of events.
Definition: state_model.cc:221
void postNextEvent(unsigned int event)
Sets the next event to the given event value.
Definition: state_model.cc:304