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 | const bool fIsValidServer;
|
---|
35 |
|
---|
36 | protected:
|
---|
37 | DimStart(const std::string &name, MessageImp &imp) : DimErrorRedirecter(imp), fIsValidServer(!name.empty())
|
---|
38 | {
|
---|
39 | DimClient::setNoDataCopy();
|
---|
40 | if (fIsValidServer)
|
---|
41 | {
|
---|
42 | DimServer::start(name.c_str());
|
---|
43 |
|
---|
44 | // Give some time to come up before
|
---|
45 | // the first log messages are sent
|
---|
46 | sleep(1);
|
---|
47 | }
|
---|
48 | }
|
---|
49 | ~DimStart()
|
---|
50 | {
|
---|
51 | if (!fIsValidServer)
|
---|
52 | return;
|
---|
53 |
|
---|
54 | // Give some time for pending log messages to be
|
---|
55 | // transmitted before the network is stopped
|
---|
56 | sleep(1);
|
---|
57 | DimServer::stop();
|
---|
58 | }
|
---|
59 | };
|
---|
60 |
|
---|
61 | // ***************************************************************************
|
---|
62 | /**
|
---|
63 | @class StateMachineDim
|
---|
64 |
|
---|
65 | @brief Class for a state machine implementation within a DIM network
|
---|
66 |
|
---|
67 | **/
|
---|
68 | // ***************************************************************************
|
---|
69 | #include "StateMachine.h" // StateMachien
|
---|
70 |
|
---|
71 | class StateMachineDim : public DimCommandHandler, public DimInfoHandler, public DimLog, public DimStart, public StateMachineImp
|
---|
72 | {
|
---|
73 | private:
|
---|
74 | static const int fVersion; /// Version number
|
---|
75 |
|
---|
76 | DimDescribedService fDescriptionStates; /// DimService propagating the state descriptions
|
---|
77 | DimDescribedService fSrvState; /// DimService offering fCurrentState
|
---|
78 | // DimService fSrvVersion; /// DimService offering fVersion
|
---|
79 |
|
---|
80 | void exitHandler(int code); /// Overwritten DimCommand::exitHandler.
|
---|
81 | void commandHandler(); /// Overwritten DimCommand::commandHandler
|
---|
82 | void infoHandler(); /// Overwritten DimInfo::infoHandler
|
---|
83 |
|
---|
84 | EventImp *CreateEvent(const std::string &name, const std::string &fmt);
|
---|
85 | EventImp *CreateService(const std::string &name);
|
---|
86 |
|
---|
87 | protected:
|
---|
88 | /// This is an internal function to do some action in case of
|
---|
89 | /// a state change, like updating the corresponding service.
|
---|
90 | std::string SetCurrentState(int state, const char *txt="", const std::string &cmd="");
|
---|
91 |
|
---|
92 | void Lock();
|
---|
93 | void UnLock();
|
---|
94 |
|
---|
95 | public:
|
---|
96 | StateMachineDim(std::ostream &out=std::cout, const std::string &name="DEFAULT");
|
---|
97 |
|
---|
98 | /// Redirect our own logging to fLog
|
---|
99 | int Write(const Time &time, const std::string &txt, int qos=kMessage);
|
---|
100 |
|
---|
101 | bool AddStateName(const int state, const std::string &name, const std::string &doc="");
|
---|
102 |
|
---|
103 | bool MessageQueueEmpty() const { return DimLog::fLog.MessageQueueEmpty(); }
|
---|
104 | };
|
---|
105 |
|
---|
106 | #endif
|
---|