source: trunk/FACT++/src/StateMachineDim.h@ 16533

Last change on this file since 16533 was 16483, checked in by tbretz, 12 years ago
Added also a one second sleep before shutting down dim. This allows the log message queue to get empty.
File size: 3.0 KB
Line 
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
14class 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
32class DimStart : public DimErrorRedirecter
33{
34protected:
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
42 // Give some time to come up before
43 // the first log messages are sent
44 sleep(1);
45 }
46 }
47 ~DimStart()
48 {
49 // Give some time for pending log messages to be
50 // transmitted before the network is stopped
51 sleep(1);
52 DimServer::stop();
53 }
54};
55
56// ***************************************************************************
57/**
58 @class StateMachineDim
59
60 @brief Class for a state machine implementation within a DIM network
61
62**/
63// ***************************************************************************
64#include "StateMachine.h" // StateMachien
65
66class StateMachineDim : public DimCommandHandler, public DimInfoHandler, public DimLog, public DimStart, public StateMachineImp
67{
68private:
69 static const int fVersion; /// Version number
70
71 DimDescribedService fDescriptionStates; /// DimService propagating the state descriptions
72 DimDescribedService fSrvState; /// DimService offering fCurrentState
73// DimService fSrvVersion; /// DimService offering fVersion
74
75 void exitHandler(int code); /// Overwritten DimCommand::exitHandler.
76 void commandHandler(); /// Overwritten DimCommand::commandHandler
77 void infoHandler(); /// Overwritten DimInfo::infoHandler
78
79 EventImp *CreateEvent(const std::string &name, const std::string &fmt);
80 EventImp *CreateService(const std::string &name);
81
82protected:
83 /// This is an internal function to do some action in case of
84 /// a state change, like updating the corresponding service.
85 std::string SetCurrentState(int state, const char *txt="", const std::string &cmd="");
86
87 void Lock();
88 void UnLock();
89
90public:
91 StateMachineDim(std::ostream &out=std::cout, const std::string &name="DEFAULT");
92
93 /// Redirect our own logging to fLog
94 int Write(const Time &time, const std::string &txt, int qos=kMessage);
95
96 bool AddStateName(const int state, const std::string &name, const std::string &doc="");
97};
98
99#endif
Note: See TracBrowser for help on using the repository browser.