Kea 3.3.3
state_model.h
Go to the documentation of this file.
1// Copyright (C) 2013-2026 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
13#include <util/labeled_value.h>
14
15#include <cstdint>
16#include <functional>
17#include <map>
18#include <mutex>
19#include <string>
20
21#include <boost/shared_ptr.hpp>
22
23namespace isc {
24namespace util {
25
28public:
29 StateModelError(const char* file, size_t line, const char* what) :
30 isc::Exception(file, line, what) { }
31};
32
35
38
40typedef std::function<void()> StateHandler;
41
53
64class State : public LabeledValue {
65public:
83 State(const int value, const std::string& label, StateHandler handler,
84 const StatePausing& state_pausing = STATE_PAUSE_NEVER);
85
87 virtual ~State();
88
90 void run();
91
99 bool shouldPause();
100
101private:
103 StateHandler handler_;
104
106 StatePausing pausing_;
107
110 bool was_paused_;
111};
112
114typedef boost::shared_ptr<State> StatePtr;
115
121class StateSet : public LabeledValueSet {
122public:
124 StateSet();
125
127 virtual ~StateSet();
128
138 void add(const int value, const std::string& label, StateHandler handler,
139 const StatePausing& state_pausing);
140
149 const StatePtr getState(int value);
150};
151
229
278public:
279
281
282 static const int NEW_ST = 0;
283
285 static const int END_ST = 1;
286
288 static const int SM_DERIVED_STATE_MIN = 11;
290
292
295 static const int NOP_EVT = 0;
296
298 static const int START_EVT = 1;
299
301 static const int END_EVT = 2;
302
304 static const int FAIL_EVT = 3;
305
307 static const int SM_DERIVED_EVENT_MIN = 11;
309
311 StateModel();
312
314 virtual ~StateModel();
315
327 void startModel(const int start_state);
328
351 virtual void runModel(int event);
352
360 void endModel();
361
363 void unpauseModel();
364
370 void nopStateHandler();
371
372protected:
373
382 void initDictionaries();
383
408 virtual void defineEvents();
409
420 void defineEvent(int value, const std::string& label);
421
431 const EventPtr& getEvent(int value);
432
459 virtual void verifyEvents();
460
485 virtual void defineStates();
486
501 void defineState(int value, const std::string& label,
502 StateHandler handler,
503 const StatePausing& state_pausing = STATE_PAUSE_NEVER);
504
512 const StatePtr getState(int value);
513
539 virtual void verifyStates();
540
551 virtual void onModelFailure(const std::string& explanation);
552
564 void transition(int state, int event);
565
575 void abortModel(const std::string& explanation);
576
588 void setState(int state);
589
599 void postNextEvent(int event);
600
610 bool doOnEntry();
611
621 bool doOnExit();
622
623public:
624
632 int getCurrState() const;
633
637 int getPrevState() const;
638
642 int getLastEvent() const;
643
651 int getNextEvent() const;
652
656 bool isModelNew() const;
657
662 bool isModelRunning() const;
663
668 bool isModelWaiting() const;
669
673 bool isModelDone() const;
674
678 bool isModelPaused() const;
679
684 bool didModelFail() const;
685
692 std::string getEventLabel(const int event) const;
693
700 std::string getStateLabel(const int state) const;
701
710 std::string getContextStr() const;
711
720 std::string getPrevContextStr() const;
721
722protected:
723
733 const StatePtr getStateInternal(int value);
734
735private:
736
749 void setStateInternal(int state);
750
761 void postNextEventInternal(int event);
762
768 bool isModelNewInternal() const;
769
778 std::string getEventLabelInternal(const int event) const;
779
788 std::string getStateLabelInternal(const int state) const;
789
800 std::string getContextStrInternal() const;
801
812 std::string getPrevContextStrInternal() const;
813
815 LabeledValueSet events_;
816
818 StateSet states_;
819
821 bool dictionaries_initted_;
822
824 int curr_state_;
825
827 int prev_state_;
828
830 int last_event_;
831
833 int next_event_;
834
836 bool on_entry_flag_;
837
839 bool on_exit_flag_;
840
842 bool paused_;
843
845 boost::shared_ptr<std::mutex> mutex_;
846};
847
849typedef boost::shared_ptr<StateModel> StateModelPtr;
850
851} // namespace isc::util
852} // namespace isc
853#endif
This is a base class for exceptions thrown from the DNS library module.
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Implements a set of unique LabeledValues.
Implements the concept of a constant value with a text label.
LabeledValue(const int value, const std::string &label)
Constructor.
StateModelError(const char *file, size_t line, const char *what)
Definition state_model.h:29
bool isModelWaiting() const
Returns whether or not the model is waiting.
void initDictionaries()
Initializes the event and state dictionaries.
void endModel()
Conducts a normal transition to the end of the model.
static const int SM_DERIVED_STATE_MIN
Value at which custom states in a derived class should begin.
std::string getStateLabel(const int state) const
Fetches the label associated with an state value.
const StatePtr getStateInternal(int value)
Fetches the state referred to by value.
void unpauseModel()
Unpauses state model.
static const int FAIL_EVT
Event issued to abort the model execution.
bool isModelDone() const
Returns whether or not the model has finished execution.
int getLastEvent() const
Fetches the model's last event.
bool isModelPaused() const
Returns whether or not the model is paused.
virtual void defineEvents()
Populates the set of events.
virtual void verifyStates()
Validates the contents of the set of states.
bool isModelRunning() const
Returns whether or not the model is running.
bool doOnExit()
Checks if on exit flag is true.
StateModel()
Constructor.
static const int NEW_ST
State that a state model is in immediately after construction.
void nopStateHandler()
An empty state handler.
virtual void onModelFailure(const std::string &explanation)
Handler for fatal model execution errors.
void defineEvent(int value, const std::string &label)
Adds an event value and associated label to the set of events.
virtual void verifyEvents()
Validates the contents of the set of events.
static const int END_EVT
Event issued to end the model execution.
bool doOnEntry()
Checks if on entry flag is true.
static const int NOP_EVT
Signifies that no event has occurred.
static const int START_EVT
Event issued to start the model execution.
void abortModel(const std::string &explanation)
Aborts model execution.
int getCurrState() const
Fetches the model's current state.
std::string getEventLabel(const int event) const
Fetches the label associated with an event value.
bool isModelNew() const
Returns whether or not the model is new.
void setState(int state)
Sets the current state to the given state value.
void defineState(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 startModel(const int start_state)
Begins execution of the model.
const EventPtr & getEvent(int value)
Fetches the event referred to by value.
virtual void defineStates()
Populates the set of states.
std::string getPrevContextStr() const
Convenience method which returns a string rendition of the previous state and last event.
virtual ~StateModel()
Destructor.
virtual void runModel(int event)
Processes events through the state model.
void transition(int state, int event)
Sets up the model to transition into given state with a given event.
int getNextEvent() const
Fetches the model's next event.
int getPrevState() const
Fetches the model's previous state.
std::string getContextStr() const
Convenience method which returns a string rendition of the current state and next event.
static const int SM_DERIVED_EVENT_MIN
Value at which custom events in a derived class should begin.
const StatePtr getState(int value)
Fetches the state referred to by value.
static const int END_ST
Final state, all the state model has reached its conclusion.
void postNextEvent(int event)
Sets the next event to the given event value.
bool didModelFail() const
Returns whether or not the model failed.
Implements a unique set or dictionary of states.
const StatePtr getState(int value)
Fetches a state for the given value.
StateSet()
Constructor.
void add(const int value, const std::string &label, StateHandler handler, const StatePausing &state_pausing)
Adds a state definition to the set of states.
virtual ~StateSet()
Destructor.
virtual ~State()
Destructor.
void run()
Invokes the State's handler.
bool shouldPause()
Indicates if the state model should pause upon entering this state.
State(const int value, const std::string &label, StateHandler handler, const StatePausing &state_pausing=STATE_PAUSE_NEVER)
Constructor.
This file defines classes: LabeledValue and LabeledValueSet.
LabeledValue Event
Define an Event.
Definition state_model.h:34
StatePausing
State machine pausing modes.
Definition state_model.h:48
@ STATE_PAUSE_ALWAYS
Definition state_model.h:49
@ STATE_PAUSE_ONCE
Definition state_model.h:51
@ STATE_PAUSE_NEVER
Definition state_model.h:50
std::function< void()> StateHandler
Defines a pointer to an instance method for handling a state.
Definition state_model.h:40
boost::shared_ptr< StateModel > StateModelPtr
Defines a pointer to a StateModel.
boost::shared_ptr< State > StatePtr
Defines a shared pointer to a State.
LabeledValuePtr EventPtr
Define Event pointer.
Definition state_model.h:37
boost::shared_ptr< LabeledValue > LabeledValuePtr
Defines a shared pointer to a LabeledValue instance.
Defines the logger used by the top-level component of kea-lfc.