| 1 | #ifndef FACT_StateMachineDim
|
|---|
| 2 | #define FACT_StateMachineDim
|
|---|
| 3 |
|
|---|
| 4 | // ***************************************************************************
|
|---|
| 5 | /**
|
|---|
| 6 | @class DimLog
|
|---|
| 7 |
|
|---|
| 8 | @brief Ensures that the MessageDimTX is initialized before errors could be redirected to it
|
|---|
| 9 |
|
|---|
| 10 | **/
|
|---|
| 11 | // ***************************************************************************
|
|---|
| 12 | #include "MessageDim.h" // MessageDimTX
|
|---|
| 13 |
|
|---|
| 14 | class DimLog
|
|---|
| 15 | {
|
|---|
| 16 | friend class StateMachineDim;
|
|---|
| 17 |
|
|---|
| 18 | MessageDimTX fLog;
|
|---|
| 19 | DimLog(std::ostream &out, const std::string &name) : fLog(name, out) { }
|
|---|
| 20 | };
|
|---|
| 21 |
|
|---|
| 22 | // ***************************************************************************
|
|---|
| 23 | /**
|
|---|
| 24 | @class DimStart
|
|---|
| 25 |
|
|---|
| 26 | @brief Ensures calling DimServer::start() in its constructor and DimServer::stop() in its destructor
|
|---|
| 27 |
|
|---|
| 28 | **/
|
|---|
| 29 | // ***************************************************************************
|
|---|
| 30 | #include "DimErrorRedirecter.h"
|
|---|
| 31 |
|
|---|
| 32 | class DimStart : public DimErrorRedirecter
|
|---|
| 33 | {
|
|---|
| 34 | protected:
|
|---|
| 35 | DimStart(const std::string &name, MessageImp &imp) : DimErrorRedirecter(imp)
|
|---|
| 36 | {
|
|---|
| 37 | DimClient::setNoDataCopy();
|
|---|
| 38 | if (!name.empty())
|
|---|
| 39 | {
|
|---|
| 40 | DimServer::start(name.c_str());
|
|---|
| 41 | sleep(1);
|
|---|
| 42 | }
|
|---|
| 43 | }
|
|---|
| 44 | ~DimStart()
|
|---|
| 45 | {
|
|---|
| 46 | DimServer::stop();
|
|---|
| 47 | }
|
|---|
| 48 | };
|
|---|
| 49 |
|
|---|
| 50 | // ***************************************************************************
|
|---|
| 51 | /**
|
|---|
| 52 | @class StateMachineDim
|
|---|
| 53 |
|
|---|
| 54 | @brief Class for a state machine implementation within a DIM network
|
|---|
| 55 |
|
|---|
| 56 | **/
|
|---|
| 57 | // ***************************************************************************
|
|---|
| 58 | #include "StateMachine.h" // StateMachien
|
|---|
| 59 |
|
|---|
| 60 | class StateMachineDim : public DimCommandHandler, public DimInfoHandler, public DimLog, public DimStart, public StateMachineImp
|
|---|
| 61 | {
|
|---|
| 62 | private:
|
|---|
| 63 | static const int fVersion; /// Version number
|
|---|
| 64 |
|
|---|
| 65 | DimDescribedService fDescriptionStates; /// DimService propagating the state descriptions
|
|---|
| 66 | DimDescribedService fSrvState; /// DimService offering fCurrentState
|
|---|
| 67 | // DimService fSrvVersion; /// DimService offering fVersion
|
|---|
| 68 |
|
|---|
| 69 | void exitHandler(int code); /// Overwritten DimCommand::exitHandler.
|
|---|
| 70 | void commandHandler(); /// Overwritten DimCommand::commandHandler
|
|---|
| 71 | void infoHandler(); /// Overwritten DimInfo::infoHandler
|
|---|
| 72 |
|
|---|
| 73 | EventImp *CreateEvent(const std::string &name, const std::string &fmt);
|
|---|
| 74 | EventImp *CreateService(const std::string &name);
|
|---|
| 75 |
|
|---|
| 76 | protected:
|
|---|
| 77 | /// This is an internal function to do some action in case of
|
|---|
| 78 | /// a state change, like updating the corresponding service.
|
|---|
| 79 | std::string SetCurrentState(int state, const char *txt="", const std::string &cmd="");
|
|---|
| 80 |
|
|---|
| 81 | void Lock();
|
|---|
| 82 | void UnLock();
|
|---|
| 83 |
|
|---|
| 84 | public:
|
|---|
| 85 | StateMachineDim(std::ostream &out=std::cout, const std::string &name="DEFAULT");
|
|---|
| 86 |
|
|---|
| 87 | /// Redirect our own logging to fLog
|
|---|
| 88 | int Write(const Time &time, const std::string &txt, int qos=kMessage);
|
|---|
| 89 |
|
|---|
| 90 | bool AddStateName(const int state, const std::string &name, const std::string &doc="");
|
|---|
| 91 | };
|
|---|
| 92 |
|
|---|
| 93 | #endif
|
|---|