1 | // **************************************************************************
|
---|
2 | /** @class StateMachineDim
|
---|
3 |
|
---|
4 | @brief Class for a state machine implementation within a DIM network
|
---|
5 |
|
---|
6 | This class implements a StateMachine within a Dim network. It redirects
|
---|
7 | all output posted via MessageImp to a service called "NAME/MESSAGE"
|
---|
8 | while name is the name of the machine given in the constructor. In
|
---|
9 | addition two services are offered: NAME/STATE and NAME/VERSION.
|
---|
10 | NAME/STATE propagates any state change to the netork.
|
---|
11 |
|
---|
12 | When constructing the Dim network is started and while dstruction it is
|
---|
13 | stopped.
|
---|
14 |
|
---|
15 | @todo
|
---|
16 | Proper support for versioning
|
---|
17 |
|
---|
18 | */
|
---|
19 | // **************************************************************************
|
---|
20 | #include "StateMachineDim.h"
|
---|
21 |
|
---|
22 | #include <boost/lexical_cast.hpp>
|
---|
23 |
|
---|
24 | #include "tools.h"
|
---|
25 |
|
---|
26 | #include "Time.h"
|
---|
27 | #include "EventDim.h"
|
---|
28 |
|
---|
29 | using namespace std;
|
---|
30 | using boost::lexical_cast;
|
---|
31 |
|
---|
32 | const int StateMachineDim::fVersion = 42;
|
---|
33 |
|
---|
34 | // --------------------------------------------------------------------------
|
---|
35 | //
|
---|
36 | //! The constrcutor first initialized DimStart with the given machine name.
|
---|
37 | //! DimStart is just a wrapper which constructor calls DimServer::start()
|
---|
38 | //! to ensure that initializing the Dim sub-system, is the first what is
|
---|
39 | //! done.
|
---|
40 | //!
|
---|
41 | //! The second objet instantiated is the MessageDim class which offers
|
---|
42 | //! the MESSAGE service used to broadcast logging messages to other
|
---|
43 | //! Dim clients.
|
---|
44 | //!
|
---|
45 | //! After this the services STATE and VERSION are setup. STATE will
|
---|
46 | //! be used to broadcast the state of the machine. Version broadcasts
|
---|
47 | //! the global version number of the StateMachineDim implementation
|
---|
48 | //!
|
---|
49 | //! After redirecting the handler which handels dim's EXIT command
|
---|
50 | //! to ourself (it will then call StateMachineDim::exitHandler) and
|
---|
51 | //! adding human readable state names for the default states
|
---|
52 | //! implemented by StateMachingImp the state is set to kSM_Initializing.
|
---|
53 | //! Warning: The EXIT handler is global!
|
---|
54 | //!
|
---|
55 | //! @param name
|
---|
56 | //! The name with which the dim-services should be prefixed, e.g.
|
---|
57 | //! "DRIVE" will lead to "DRIVE/SERVICE". It is also propagated
|
---|
58 | //! to DimServer::start()
|
---|
59 | //!
|
---|
60 | //! @param out
|
---|
61 | //! A refrence to an ostream which allows to redirect the log-output
|
---|
62 | //! to something else than cout. The default is cout. The reference
|
---|
63 | //! is propagated to fLog
|
---|
64 | //!
|
---|
65 | //! @todo
|
---|
66 | //! - Shell the VERSION be set from the derived class?
|
---|
67 | //
|
---|
68 | StateMachineDim::StateMachineDim(ostream &out, const std::string &name)
|
---|
69 | : StateMachine(out, name), DimStart(name, *this), fLog(name, out),
|
---|
70 | fDescriptionStates((name+"/STATE_LIST").c_str(), const_cast<char*>(""),
|
---|
71 | "Provides a list with descriptions for each service."
|
---|
72 | "|StateList[string]:A \\n separated list of the form id:name=description"),
|
---|
73 | fSrvState((name+"/STATE").c_str(), const_cast<char*>(""),
|
---|
74 | "Provides the state of the state machine as quality of service."
|
---|
75 | "|Text[string]:A human readable string sent by the last state change.")
|
---|
76 | // fSrvVersion((name+"/VERSION").c_str(), const_cast<int&>(fVersion)),
|
---|
77 | {
|
---|
78 | SetDefaultStateNames();
|
---|
79 | }
|
---|
80 |
|
---|
81 | // --------------------------------------------------------------------------
|
---|
82 | //
|
---|
83 | //! Overwrite StateMachineImp::AddTransition to create a EventDim
|
---|
84 | //! instead of an Event object. The event name propagated to the EventDim
|
---|
85 | //! is fName+"/"+name.
|
---|
86 | //!
|
---|
87 | //! For parameter description see StateMachineImp.
|
---|
88 | //!
|
---|
89 | EventImp *StateMachineDim::CreateEvent(int targetstate, const char *name, const char *fmt)
|
---|
90 | {
|
---|
91 | return new EventDim(targetstate, GetName()+"/"+name, fmt, this);
|
---|
92 | }
|
---|
93 |
|
---|
94 | // --------------------------------------------------------------------------
|
---|
95 | //
|
---|
96 | //! Overwrite StateMachineImp::AddStateName. In addition to storing the
|
---|
97 | //! state locally it is also propagated through Dim in the STATE_LIST
|
---|
98 | //! service.
|
---|
99 | //!
|
---|
100 | //! @param state
|
---|
101 | //! Number of the state to which a name should be assigned
|
---|
102 | //!
|
---|
103 | //! @param name
|
---|
104 | //! A name which should be assigned to the state, e.g. "Tracking"
|
---|
105 | //!
|
---|
106 | //! @param doc
|
---|
107 | //! A explanatory text describing the state
|
---|
108 | //!
|
---|
109 | void StateMachineDim::AddStateName(const int state, const std::string &name, const std::string &doc)
|
---|
110 | {
|
---|
111 | StateMachineImp::AddStateName(state, name, doc);
|
---|
112 |
|
---|
113 | const string str0 = reinterpret_cast<char*>(fDescriptionStates.itsData);
|
---|
114 | const string str1 = lexical_cast<string>(state)+':'+name+'=';
|
---|
115 |
|
---|
116 | if (str0.find(str1)!=string::npos)
|
---|
117 | return;
|
---|
118 |
|
---|
119 | fDescriptionStates.setData(const_cast<char*>((str0+str1+doc+'\n').c_str()));
|
---|
120 | fDescriptionStates.updateService();
|
---|
121 | }
|
---|
122 |
|
---|
123 | // --------------------------------------------------------------------------
|
---|
124 | //
|
---|
125 | //! Overwrite StateMachineImp::SetCurrentState. In addition to
|
---|
126 | //! calling StateMachineImo::SetCurrentState the new state is also
|
---|
127 | //! distributed via the DimService STATE.
|
---|
128 | //!
|
---|
129 | //! For parameter description see StateMachineImp.
|
---|
130 | //!
|
---|
131 | string StateMachineDim::SetCurrentState(int state, const char *txt, const std::string &cmd)
|
---|
132 | {
|
---|
133 | const string msg = StateMachineImp::SetCurrentState(state, txt, cmd);
|
---|
134 | if (msg.empty())
|
---|
135 | return "";
|
---|
136 |
|
---|
137 | fSrvState.setQuality(state);
|
---|
138 | fSrvState.setData(const_cast<char*>(msg.c_str()));
|
---|
139 | fSrvState.updateService();
|
---|
140 |
|
---|
141 | return msg;
|
---|
142 | }
|
---|
143 |
|
---|
144 | // --------------------------------------------------------------------------
|
---|
145 | //
|
---|
146 | //! Overwritten DimCommand::commandHandler()
|
---|
147 | //!
|
---|
148 | //! If fCurrentState is smaller than 0 or we are in kSM_FatalError state,
|
---|
149 | //! all incoming commands are ignored.
|
---|
150 | //!
|
---|
151 | //! The commandHandler will go through the list of available commands
|
---|
152 | //! (fListOfEventss). If the received command was recognized, it is added
|
---|
153 | //! via PushCommand into the fifo.
|
---|
154 | //!
|
---|
155 | //! @todo
|
---|
156 | //! - Fix the exit when cmd is not of type EventImp
|
---|
157 | //! - Fix docu
|
---|
158 | //! - Do we need a possibility to suppress a call to "HandleEvent"
|
---|
159 | //! or is a state<0 enough?
|
---|
160 | //
|
---|
161 | void StateMachineDim::commandHandler()
|
---|
162 | {
|
---|
163 | DimCommand *cmd = getCommand();
|
---|
164 | if (!cmd)
|
---|
165 | return;
|
---|
166 |
|
---|
167 | const EventImp *evt = dynamic_cast<EventImp*>(cmd);
|
---|
168 |
|
---|
169 | // FIXME: In the case of HandleEvent there is no need to copy the data
|
---|
170 | // FIMXE: Currentyl the time is not copied
|
---|
171 | // FIXME: Handle Event is not thread safe (Dim + Console)
|
---|
172 | if (HasEvent(evt))
|
---|
173 | PostEvent(*evt);
|
---|
174 | }
|
---|
175 |
|
---|
176 | // --------------------------------------------------------------------------
|
---|
177 | //
|
---|
178 | //! Overwrites MessageImp::Update. This redirects output issued via
|
---|
179 | //! MessageImp to MessageDim object.
|
---|
180 | //
|
---|
181 | int StateMachineDim::Write(const Time &time, const string &txt, int qos)
|
---|
182 | {
|
---|
183 | return fLog.Write(time, txt, qos);
|
---|
184 | }
|
---|
185 |
|
---|
186 | // --------------------------------------------------------------------------
|
---|
187 | //
|
---|
188 | //! exitHandler of the DimServer. The EXIT command is implemented by each
|
---|
189 | //! DimServer automatically. exitHandler calls Stop(code) and exit(-1)
|
---|
190 | //! in case the received exit-value is a special number (42). abort()
|
---|
191 | //! is called if 0x42 is received.
|
---|
192 | //!
|
---|
193 | //! @param code
|
---|
194 | //! value which is passed to Stop(code)
|
---|
195 | //
|
---|
196 | void StateMachineDim::exitHandler(int code)
|
---|
197 | {
|
---|
198 | Out() << " -- " << Time().GetAsStr() << ": EXIT(" << code << ") command received." << endl;
|
---|
199 | if (code<0) // negative values reserved for internal use
|
---|
200 | {
|
---|
201 | Out() << " -- " << Time().GetAsStr() << ": ignored." << endl;
|
---|
202 | return;
|
---|
203 | }
|
---|
204 |
|
---|
205 | Stop(code);
|
---|
206 | if (code==42)
|
---|
207 | exit(-1);
|
---|
208 | if (code==0x42)
|
---|
209 | abort();
|
---|
210 | }
|
---|