1 | // **************************************************************************
|
---|
2 | /** @class StateMachineImp
|
---|
3 |
|
---|
4 | @brief Base class for a state machine implementation
|
---|
5 |
|
---|
6 | \dot
|
---|
7 | digraph example {
|
---|
8 | node [shape=record, fontname=Helvetica, fontsize=10];
|
---|
9 | s [ label="Constructor" style="rounded" color="red" URL="\ref StateMachineImp::StateMachineImp"];
|
---|
10 | a [ label="State -3 (kSM_NotReady)" color="red" URL="\ref StateMachineImp::StateMachineImp"];
|
---|
11 | b [ label="State -2 (kSM_Initializing)" color="red" URL="\ref StateMachineImp::StateMachineImp"];
|
---|
12 | c [ label="State -1 (kSM_Configuring)" color="red" URL="\ref StateMachineImp::StateMachineImp"];
|
---|
13 | y [ label="State 0 (kSM_Ready)" URL="\ref StateMachineImp::Run"];
|
---|
14 | r [ label="User states (Running)" ];
|
---|
15 | e [ label="State 256 (kSM_Error)" ];
|
---|
16 | f [ label="State 65535 (kSM_FatalError)" color="red" URL="\ref StateMachineImp::Run"];
|
---|
17 |
|
---|
18 | // ---- manual means: command or program introduced ----
|
---|
19 |
|
---|
20 | // Startup from Run() to Ready
|
---|
21 | s -> a [ arrowhead="open" color="red" style="solid" ]; // automatic (mandatory)
|
---|
22 | a -> b [ arrowhead="open" color="red" style="solid" ]; // automatic (mandatory)
|
---|
23 | b -> c [ arrowhead="open" color="red" style="solid" ]; // automatic (mandatory)
|
---|
24 |
|
---|
25 | c -> y [ arrowhead="open" color="red" style="solid" URL="\ref StateMachineImp::Run" ]; // prg: Run()
|
---|
26 |
|
---|
27 | y -> c [ arrowhead="open" style="dashed" URL="\ref StateMachineDim::exitHandler" ]; // CMD: EXIT
|
---|
28 | r -> c [ arrowhead="open" style="dashed" URL="\ref StateMachineDim::exitHandler" ]; // CMD: EXIT
|
---|
29 | e -> c [ arrowhead="open" style="dashed" URL="\ref StateMachineDim::exitHandler" ]; // CMD: EXIT
|
---|
30 |
|
---|
31 | e -> y [ arrowhead="open" color="red" style="dashed" ]; // CMD: RESET (e.g.)
|
---|
32 |
|
---|
33 | y -> e [ arrowhead="open" color="blue" style="solid" ]; // prg
|
---|
34 | r -> e [ arrowhead="open" color="blue" style="solid" ]; // prg
|
---|
35 |
|
---|
36 | y -> r [ arrowhead="open" color="blue" style="dashed" ]; // CMD/PRG
|
---|
37 | r -> y [ arrowhead="open" color="blue" style="dashed" ]; // CMD/PRG
|
---|
38 |
|
---|
39 | y -> f [ arrowhead="open" color="blue" style="solid" ]; // prg
|
---|
40 | r -> f [ arrowhead="open" color="blue" style="solid" ]; // prg
|
---|
41 | e -> f [ arrowhead="open" color="blue" style="solid" ]; // prg
|
---|
42 | }
|
---|
43 | \enddot
|
---|
44 |
|
---|
45 | - <B>Red box</B>: Internal states. Events which are received are
|
---|
46 | discarded.
|
---|
47 | - <B>Black box</B>: State machine running. Events are accepted and
|
---|
48 | processed according to the implemented functions Transition(),
|
---|
49 | Configuration() and Execute(). Events are accepted accoding to the
|
---|
50 | lookup table of allowed transitions.
|
---|
51 | - <B>Red solid arrow</B>: A transition initiated by the program itself.
|
---|
52 | - <b>Dashed arrows in general</b>: Transitions which can be initiated
|
---|
53 | by a dim-command or get inistiated by the program.
|
---|
54 | - <b>Solid arrows in general</b>: These transitions are always initiated by
|
---|
55 | the program.
|
---|
56 | - <B>Red dashed</B>: Suggested RESET event (should be implemented by
|
---|
57 | the derived class)
|
---|
58 | - <B>Black dashed arrow</B>: Exit from the main loop. This can either
|
---|
59 | happen by the Dim-provided EXIT-command or a call to StateMachineDim::Stop.
|
---|
60 | - <B>Black arrows</B>: Other events or transitions which can be
|
---|
61 | implemented by the derived class.
|
---|
62 | - <B>Dotted black arrow</B>: Exit from the main-loop which is initiated
|
---|
63 | by the program itself through StateMachineDim::Stop() and not by the
|
---|
64 | state machine itself (Execute(), Configure() and Transition())
|
---|
65 | - <b>Blue dashed arrows</b>: Transitions which happen either by receiving
|
---|
66 | a event or are initiated from the state machine itself
|
---|
67 | (by return values of (Execute(), Configure() and Transition())
|
---|
68 | - <b>Blue solid</b>: Transitions which cannot be initiated by dim
|
---|
69 | event but only by the state machine itself.
|
---|
70 | - From the program point of view the fatal error is identical with
|
---|
71 | the kSM_Configuring state, i.e. it is returned from the main-loop.
|
---|
72 | Usually this will result in program termination. However, depending
|
---|
73 | on the state the program might decide to use different cleaning
|
---|
74 | routines.
|
---|
75 |
|
---|
76 | @todo
|
---|
77 | - A proper and correct cleanup after an EXIT or Stop() is missing.
|
---|
78 | maybe we have to force a state 0 first?
|
---|
79 | */
|
---|
80 | // **************************************************************************
|
---|
81 | #include "StateMachineImp.h"
|
---|
82 |
|
---|
83 | #include "Time.h"
|
---|
84 | #include "Event.h"
|
---|
85 |
|
---|
86 | #include "WindowLog.h"
|
---|
87 | #include "Converter.h"
|
---|
88 |
|
---|
89 | #include "tools.h"
|
---|
90 |
|
---|
91 | using namespace std;
|
---|
92 |
|
---|
93 | // --------------------------------------------------------------------------
|
---|
94 | //
|
---|
95 | //! The state of the state machine (fCurrentState) is initialized with
|
---|
96 | //! kSM_NotReady
|
---|
97 | //!
|
---|
98 | //! Default state names for kSM_NotReady, kSM_Ready, kSM_Error and
|
---|
99 | //! kSM_FatalError are set via AddStateName.
|
---|
100 | //!
|
---|
101 | //! fExitRequested is set to 0, fRunning to false.
|
---|
102 | //!
|
---|
103 | //! Furthermore, the ostream is propagated to MessageImp, as well as
|
---|
104 | //! stored in fOut.
|
---|
105 | //!
|
---|
106 | //! MessageImp is used for messages which are distributed (e.g. via DIM),
|
---|
107 | //! fOut is used for messages which are only displayed on the local console.
|
---|
108 | //!
|
---|
109 | //! Subsequent, i.e. derived classes should setup all allowed state
|
---|
110 | //! transitions as well as all allowed configuration event by
|
---|
111 | //! AddEvent and AddStateName.
|
---|
112 | //!
|
---|
113 | //! @param out
|
---|
114 | //! A refrence to an ostream which allows to redirect the log-output
|
---|
115 | //! to something else than cout. The default is cout. The reference
|
---|
116 | //! is propagated to fLog
|
---|
117 | //!
|
---|
118 | //! @param name
|
---|
119 | //! The server name stored in fName
|
---|
120 | //!
|
---|
121 | //
|
---|
122 | StateMachineImp::StateMachineImp(ostream &out, const std::string &name)
|
---|
123 | : MessageImp(out), fName(name), fCurrentState(kSM_NotReady),
|
---|
124 | fRunning(false), fExitRequested(0)
|
---|
125 | {
|
---|
126 | SetDefaultStateNames();
|
---|
127 | }
|
---|
128 |
|
---|
129 | // --------------------------------------------------------------------------
|
---|
130 | //
|
---|
131 | //! delete all object stored in fListOfEvent and in fEventQueue
|
---|
132 | //
|
---|
133 | StateMachineImp::~StateMachineImp()
|
---|
134 | {
|
---|
135 | // For this to work EventImp must be the first class from which
|
---|
136 | // the object inherits
|
---|
137 | for (vector<EventImp*>::iterator cmd=fListOfEvents.begin(); cmd!=fListOfEvents.end(); cmd++)
|
---|
138 | delete *cmd;
|
---|
139 |
|
---|
140 | // Unfortunately, front() doesn't necessarily return 0 if
|
---|
141 | // queue is empty
|
---|
142 | if (fEventQueue.size())
|
---|
143 | {
|
---|
144 | while (1)
|
---|
145 | {
|
---|
146 | Event *q=fEventQueue.front();
|
---|
147 | if (!q)
|
---|
148 | break;
|
---|
149 |
|
---|
150 | fEventQueue.pop();
|
---|
151 | delete q;
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | // --------------------------------------------------------------------------
|
---|
157 | //
|
---|
158 | //! Sets the default state names. This function should be called in
|
---|
159 | //! derived classes again if they overwrite SetStateName().
|
---|
160 | //
|
---|
161 | void StateMachineImp::SetDefaultStateNames()
|
---|
162 | {
|
---|
163 | AddStateName(kSM_NotReady, "NotReady", "State machine not ready, events are ignored.");
|
---|
164 | AddStateName(kSM_Ready, "Ready", "State machine ready to receive events.");
|
---|
165 | AddStateName(kSM_Error, "ERROR", "Common error state.");
|
---|
166 | AddStateName(kSM_FatalError, "FATAL", "A fatal error occured, the eventloop is stopped.");
|
---|
167 | }
|
---|
168 |
|
---|
169 | // --------------------------------------------------------------------------
|
---|
170 | //
|
---|
171 | //! Puts the given event into the fifo. The fifo will take over ownership.
|
---|
172 | //! Access to fEventQueue is encapsulated by fMutex.
|
---|
173 | //!
|
---|
174 | //! @param cmd
|
---|
175 | //! Pointer to an object of type Event to be stored in the fifo
|
---|
176 | //!
|
---|
177 | //! @todo
|
---|
178 | //! Can we also allow EventImp?
|
---|
179 | //
|
---|
180 | void StateMachineImp::PushEvent(Event *cmd)
|
---|
181 | {
|
---|
182 | fMutex.lock();
|
---|
183 | fEventQueue.push(cmd);
|
---|
184 | fMutex.unlock();
|
---|
185 | }
|
---|
186 |
|
---|
187 | // --------------------------------------------------------------------------
|
---|
188 | //
|
---|
189 | //! Get an event from the fifo. We will take over the owenership of the
|
---|
190 | //! object. The pointer is deleted from the fifo. Access of fEventQueue
|
---|
191 | //! is encapsulated by fMutex.
|
---|
192 | //!
|
---|
193 | //! @returns
|
---|
194 | //! A pointer to an Event object
|
---|
195 | //
|
---|
196 | Event *StateMachineImp::PopEvent()
|
---|
197 | {
|
---|
198 | fMutex.lock();
|
---|
199 |
|
---|
200 | // Get the next event from the stack
|
---|
201 | // and remove event from the stack
|
---|
202 | Event *cmd = fEventQueue.front();
|
---|
203 | fEventQueue.pop();
|
---|
204 |
|
---|
205 | fMutex.unlock();
|
---|
206 |
|
---|
207 | return cmd;
|
---|
208 | }
|
---|
209 |
|
---|
210 | // --------------------------------------------------------------------------
|
---|
211 | //
|
---|
212 | //! With this function commands are posted to the event queue. The data
|
---|
213 | //! is not given as binary data but as a string instead. It is converted
|
---|
214 | //! according to the format of the corresponding event and an event
|
---|
215 | //! is posted to the queue if successfull.
|
---|
216 | //!
|
---|
217 | //! @param lout
|
---|
218 | //! Stream to which output should be redirected
|
---|
219 | //! event should be for.
|
---|
220 | //!
|
---|
221 | //! @param str
|
---|
222 | //! Command with data, e.g. "COMMAND 1 2 3 4 5 test"
|
---|
223 | //!
|
---|
224 | //! @returns
|
---|
225 | //! false if no event was posted to the queue. If
|
---|
226 | //! PostEvent(EventImp&,const char*, size_t) was called return its
|
---|
227 | //! return value
|
---|
228 | //
|
---|
229 | bool StateMachineImp::PostEvent(ostream &lout, const string &str)
|
---|
230 | {
|
---|
231 | // Find the delimiter between the command name and the data
|
---|
232 | size_t p0 = str.find_first_of(' ');
|
---|
233 | if (p0==string::npos)
|
---|
234 | p0 = str.length();
|
---|
235 |
|
---|
236 | // Compile the command which will be sent to the state-machine
|
---|
237 | const string name = fName + "/" + str.substr(0, p0);
|
---|
238 |
|
---|
239 | // Check if this command is existing at all
|
---|
240 | EventImp *evt = FindEvent(name);
|
---|
241 | if (!evt)
|
---|
242 | {
|
---|
243 | lout << kRed << "Unknown command '" << name << "'" << endl;
|
---|
244 | return false;
|
---|
245 | }
|
---|
246 |
|
---|
247 | // Get the format of the event data
|
---|
248 | const string fmt = evt->GetFormat();
|
---|
249 |
|
---|
250 | // Convert the user entered data according to the format string
|
---|
251 | // into a data block which will be attached to the event
|
---|
252 | #ifndef DEBUG
|
---|
253 | ostringstream sout;
|
---|
254 | const Converter conv(sout, fmt, false);
|
---|
255 | #else
|
---|
256 | const Converter conv(lout, fmt, false);
|
---|
257 | #endif
|
---|
258 | if (!conv)
|
---|
259 | {
|
---|
260 | lout << kRed << "Couldn't properly parse the format... ignored." << endl;
|
---|
261 | return false;
|
---|
262 | }
|
---|
263 |
|
---|
264 | try
|
---|
265 | {
|
---|
266 | #ifdef DEBUG
|
---|
267 | lout << kBlue << name;
|
---|
268 | #endif
|
---|
269 | const vector<char> v = conv.GetVector(str.substr(p0));
|
---|
270 | #ifdef DEBUG
|
---|
271 | lout << endl;
|
---|
272 | #endif
|
---|
273 |
|
---|
274 | return PostEvent(*evt, v.data(), v.size());
|
---|
275 | }
|
---|
276 | catch (const std::runtime_error &e)
|
---|
277 | {
|
---|
278 | lout << endl << kRed << e.what() << endl;
|
---|
279 | }
|
---|
280 |
|
---|
281 | return false;
|
---|
282 | }
|
---|
283 |
|
---|
284 | // --------------------------------------------------------------------------
|
---|
285 | //
|
---|
286 | //! With this function commands are posted to the event queue. If the
|
---|
287 | //! event loop has not yet been started with Run() the command is directly
|
---|
288 | //! handled by HandleEvent.
|
---|
289 | //!
|
---|
290 | //! Events posted when the state machine is in a negative state or
|
---|
291 | //! kSM_FatalError are ignored.
|
---|
292 | //!
|
---|
293 | //! A new event is created and its data contents initialized with the
|
---|
294 | //! specified memory.
|
---|
295 | //!
|
---|
296 | //! @param evt
|
---|
297 | //! The event to be posted. The precise contents depend on what the
|
---|
298 | //! event should be for.
|
---|
299 | //!
|
---|
300 | //! @param ptr
|
---|
301 | //! pointer to the memory which should be attached to the event
|
---|
302 | //!
|
---|
303 | //! @param siz
|
---|
304 | //! size of the memory which should be attached to the event
|
---|
305 | //!
|
---|
306 | //! @returns
|
---|
307 | //! false if the event is ignored, true otherwise.
|
---|
308 | //!
|
---|
309 | //! @todo
|
---|
310 | //! - Shell we check for the validity of a command at the current state, too?
|
---|
311 | //! - should we also get the output stream as an argument here?
|
---|
312 | //
|
---|
313 | bool StateMachineImp::PostEvent(const EventImp &evt, const char *ptr, size_t siz)
|
---|
314 | {
|
---|
315 | if (/*GetCurrentState()<0 ||*/ GetCurrentState()==kSM_FatalError)
|
---|
316 | {
|
---|
317 | Out() << kYellow << "State<0 or FatalError: Event ignored." << endl;
|
---|
318 | return false;
|
---|
319 | }
|
---|
320 |
|
---|
321 | if (IsRunning())
|
---|
322 | {
|
---|
323 | Event *event = new Event(evt, ptr, siz);
|
---|
324 | //Debug("Posted: "+event->GetName());
|
---|
325 | PushEvent(event);
|
---|
326 | }
|
---|
327 | else
|
---|
328 | {
|
---|
329 | // FIXME: Is this thread safe? (Yes, because the data is copied)
|
---|
330 | // But two handlers could be called at the same time. Do we
|
---|
331 | // need to lock the handlers? (Dim + console)
|
---|
332 | // FIXME: Is copying of the data necessary?
|
---|
333 | const Event event(evt, ptr, siz);
|
---|
334 | Lock();
|
---|
335 | HandleEvent(event);
|
---|
336 | UnLock();
|
---|
337 | }
|
---|
338 | return true;
|
---|
339 | }
|
---|
340 |
|
---|
341 | // --------------------------------------------------------------------------
|
---|
342 | //
|
---|
343 | //! With this function commands are posted to the event queue. If the
|
---|
344 | //! event loop has not yet been started with Run() the command is directly
|
---|
345 | //! handled by HandleEvent.
|
---|
346 | //!
|
---|
347 | //! Events posted when the state machine is in a negative state or
|
---|
348 | //! kSM_FatalError are ignored.
|
---|
349 | //!
|
---|
350 | //! @param evt
|
---|
351 | //! The event to be posted. The precise contents depend on what the
|
---|
352 | //! event should be for.
|
---|
353 | //!
|
---|
354 | //! @returns
|
---|
355 | //! false if the event is ignored, true otherwise.
|
---|
356 | //!
|
---|
357 | //! @todo
|
---|
358 | //! - Shell we check for the validity of a command at the current state, too?
|
---|
359 | //! - should we also get the output stream as an argument here?
|
---|
360 | //
|
---|
361 | bool StateMachineImp::PostEvent(const EventImp &evt)
|
---|
362 | {
|
---|
363 | if (/*GetCurrentState()<0 ||*/ GetCurrentState()==kSM_FatalError)
|
---|
364 | {
|
---|
365 | Out() << kYellow << "State<0 or FatalError: Event ignored." << endl;
|
---|
366 | return false;
|
---|
367 | }
|
---|
368 |
|
---|
369 | if (IsRunning())
|
---|
370 | PushEvent(new Event(evt));
|
---|
371 | else
|
---|
372 | {
|
---|
373 | // FIXME: Is this thread safe? (Yes, because it is only used
|
---|
374 | // by Dim and this is thread safe) But two handlers could
|
---|
375 | // be called at the same time. Do we need to lock the handlers?
|
---|
376 | HandleEvent(evt);
|
---|
377 | }
|
---|
378 | return true;
|
---|
379 | }
|
---|
380 |
|
---|
381 | // --------------------------------------------------------------------------
|
---|
382 | //
|
---|
383 | //! Return all event names of the StateMachine
|
---|
384 | //!
|
---|
385 | //! @returns
|
---|
386 | //! A vector of strings with all event names of the state machine.
|
---|
387 | //! The event names all have the SERVER/ pre-fix removed.
|
---|
388 | //
|
---|
389 | const vector<string> StateMachineImp::GetEventNames() const
|
---|
390 | {
|
---|
391 | vector<string> v;
|
---|
392 |
|
---|
393 | const string &name = fName + "/";
|
---|
394 | const int len = name.length();
|
---|
395 |
|
---|
396 | for (vector<EventImp*>::const_iterator i=fListOfEvents.begin();
|
---|
397 | i!=fListOfEvents.end(); i++)
|
---|
398 | {
|
---|
399 | const string evt = (*i)->GetName();
|
---|
400 |
|
---|
401 | v.push_back(evt.substr(0, len)==name ? evt.substr(len) : evt);
|
---|
402 | }
|
---|
403 |
|
---|
404 | return v;
|
---|
405 | }
|
---|
406 |
|
---|
407 | // --------------------------------------------------------------------------
|
---|
408 | //
|
---|
409 | //! Call for each event in fListEvents its Print function with the given
|
---|
410 | //! stream.
|
---|
411 | //!
|
---|
412 | //! @param out
|
---|
413 | //! ostream to which the output should be redirected
|
---|
414 | //!
|
---|
415 | //! @param evt
|
---|
416 | //! if given only the given event is selected
|
---|
417 | //
|
---|
418 | void StateMachineImp::PrintListOfEvents(ostream &out, const string &evt) const
|
---|
419 | {
|
---|
420 | for (vector<EventImp*>::const_iterator c=fListOfEvents.begin(); c!=fListOfEvents.end(); c++)
|
---|
421 | if (evt.empty() || GetName()+'/'+evt==(*c)->GetName())
|
---|
422 | (*c)->Print(out, true);
|
---|
423 | }
|
---|
424 |
|
---|
425 | // --------------------------------------------------------------------------
|
---|
426 | //
|
---|
427 | //! Call for each event in fListEvents its Print function with the given
|
---|
428 | //! stream if it is an allowed event in the current state.
|
---|
429 | //!
|
---|
430 | //! @param out
|
---|
431 | //! ostream to which the output should be redirected
|
---|
432 | //!
|
---|
433 | //
|
---|
434 | void StateMachineImp::PrintListOfAllowedEvents(ostream &out) const
|
---|
435 | {
|
---|
436 | for (vector<EventImp*>::const_iterator c=fListOfEvents.begin(); c!=fListOfEvents.end(); c++)
|
---|
437 | if ((*c)->IsStateAllowed(fCurrentState))
|
---|
438 | (*c)->Print(out, true);
|
---|
439 | }
|
---|
440 |
|
---|
441 | // --------------------------------------------------------------------------
|
---|
442 | //
|
---|
443 | //! Call PrintListOfEvents with fOut as the output stream
|
---|
444 | //!
|
---|
445 | //! @param str
|
---|
446 | //! if given only the given event is selected
|
---|
447 | //
|
---|
448 | //
|
---|
449 | void StateMachineImp::PrintListOfEvents(const string &str) const
|
---|
450 | {
|
---|
451 | PrintListOfEvents(Out(), str);
|
---|
452 | }
|
---|
453 |
|
---|
454 | // --------------------------------------------------------------------------
|
---|
455 | //
|
---|
456 | //! Print a list of all states with descriptions.
|
---|
457 | //!
|
---|
458 | //! @param out
|
---|
459 | //! ostream to which the output should be redirected
|
---|
460 | //
|
---|
461 | void StateMachineImp::PrintListOfStates(std::ostream &out) const
|
---|
462 | {
|
---|
463 | out << endl;
|
---|
464 | out << kBold << "List of available states:" << endl;
|
---|
465 | for (StateNames::const_iterator i=fStateNames.begin(); i!=fStateNames.end(); i++)
|
---|
466 | {
|
---|
467 | ostringstream state;
|
---|
468 | state << i->first;
|
---|
469 | out << " -[" << kBold << state.str() << kReset << "]:" << setfill(' ') << setw(6-state.str().length()) << ' ' << kYellow << i->second.first << kBlue << " (" << i->second.second << ")" << endl;
|
---|
470 | }
|
---|
471 | out << endl;
|
---|
472 | }
|
---|
473 |
|
---|
474 | // --------------------------------------------------------------------------
|
---|
475 | //
|
---|
476 | //! Print a list of all states with descriptions.
|
---|
477 | //
|
---|
478 | void StateMachineImp::PrintListOfStates() const
|
---|
479 | {
|
---|
480 | PrintListOfStates(Out());
|
---|
481 | }
|
---|
482 |
|
---|
483 | // --------------------------------------------------------------------------
|
---|
484 | //
|
---|
485 | //! Check whether an event (same pointer!) is in fListOfEvents
|
---|
486 | //!
|
---|
487 | //! @returns
|
---|
488 | //! true if the event was found, false otherwise
|
---|
489 | //
|
---|
490 | bool StateMachineImp::HasEvent(const EventImp *cmd) const
|
---|
491 | {
|
---|
492 | // Find the event from the list of commands and queue it
|
---|
493 | return find(fListOfEvents.begin(), fListOfEvents.end(), cmd)!=fListOfEvents.end();
|
---|
494 | }
|
---|
495 |
|
---|
496 | // --------------------------------------------------------------------------
|
---|
497 | //
|
---|
498 | //! Check whether an event with the given name is found in fListOfEvents.
|
---|
499 | //! Note that currently there is no mechanism which ensures that not two
|
---|
500 | //! events have the same name.
|
---|
501 | //!
|
---|
502 | //! @returns
|
---|
503 | //! true if the event was found, false otherwise
|
---|
504 | //
|
---|
505 | EventImp *StateMachineImp::FindEvent(const std::string &evt) const
|
---|
506 | {
|
---|
507 | // Find the command from the list of commands and queue it
|
---|
508 | for (vector<EventImp*>::const_iterator c=fListOfEvents.begin(); c!=fListOfEvents.end(); c++)
|
---|
509 | if (evt == (*c)->GetName())
|
---|
510 | return *c;
|
---|
511 |
|
---|
512 | return 0;
|
---|
513 | }
|
---|
514 |
|
---|
515 | // --------------------------------------------------------------------------
|
---|
516 | //
|
---|
517 | //! Calling this function, a new (named) event is added to the state
|
---|
518 | //! machine. Via a call to CreateEvent a new event is created with the
|
---|
519 | //! given targetstate, name and format.
|
---|
520 | //!
|
---|
521 | //! The allowed states are passed to the new event and a message
|
---|
522 | //! is written to the output-stream.
|
---|
523 | //!
|
---|
524 | //! @param name
|
---|
525 | //! The command name which should initiate the transition. The DimCommand
|
---|
526 | //! will be constructed with the name given to the constructor and this
|
---|
527 | //! name, e.g. "DRIVE/CHANGE_STATE_TO_NEW_STATE"
|
---|
528 | //!
|
---|
529 | //! @param states
|
---|
530 | //! A comma sepeareted list of ints, e.g. "1, 4, 5, 9" with states
|
---|
531 | //! in which this new state transition is allowed and will be accepted.
|
---|
532 | //!
|
---|
533 | //! @param fmt
|
---|
534 | //! A format as defined by the dim system can be given for the command.
|
---|
535 | //! However, it has no real meaning except that it is stored within the
|
---|
536 | //! DimCommand object. However, the user must make sure that the data of
|
---|
537 | //! received commands is properly extracted. No check is done.
|
---|
538 | //
|
---|
539 | EventImp &StateMachineImp::AddEvent(const char *name, const char *states, const char *fmt)
|
---|
540 | {
|
---|
541 | EventImp *evt = CreateEvent(name, fmt);
|
---|
542 |
|
---|
543 | evt->AddAllowedStates(states);
|
---|
544 |
|
---|
545 | #ifdef DEBUG
|
---|
546 | Out() << ": " << Time().GetAsStr("%H:%M:%S.%f");
|
---|
547 | Out() << " - Adding command " << evt->GetName();
|
---|
548 | Out() << endl;
|
---|
549 | #endif
|
---|
550 |
|
---|
551 | fListOfEvents.push_back(evt);
|
---|
552 |
|
---|
553 | return *evt;
|
---|
554 | }
|
---|
555 |
|
---|
556 | // --------------------------------------------------------------------------
|
---|
557 | //
|
---|
558 | //! Calling this function, a new (named) event is added to the state
|
---|
559 | //! machine. Therefore an instance of type DimEvent is created and added
|
---|
560 | //! to the list of available commands fListOfEvents.
|
---|
561 | //!
|
---|
562 | //! @param name
|
---|
563 | //! The command name which should initiate the transition. The DimCommand
|
---|
564 | //! will be constructed with the name given to the constructor and this
|
---|
565 | //! name, e.g. "DRIVE/CHANGE_STATE_TO_NEW_STATE"
|
---|
566 | //!
|
---|
567 | //! @param s1, s2, s3, s4, s5
|
---|
568 | //! A list of states from which a transition to targetstate is allowed
|
---|
569 | //! by this command.
|
---|
570 | //
|
---|
571 | EventImp &StateMachineImp::AddEvent(const char *name, int s1, int s2, int s3, int s4, int s5)
|
---|
572 | {
|
---|
573 | ostringstream str;
|
---|
574 | str << s1 << ' ' << s2 << ' ' << s3 << ' ' << s4 << ' ' << s5;
|
---|
575 | return AddEvent(name, str.str().c_str(), "");
|
---|
576 | }
|
---|
577 |
|
---|
578 | // --------------------------------------------------------------------------
|
---|
579 | //
|
---|
580 | //! Calling this function, a new (named) event is added to the state
|
---|
581 | //! machine. Therefore an instance of type DimEvent is created and added
|
---|
582 | //! to the list of available commands fListOfEvents.
|
---|
583 | //!
|
---|
584 | //! @param name
|
---|
585 | //! The command name which should initiate the transition. The DimCommand
|
---|
586 | //! will be constructed with the name given to the constructor and this
|
---|
587 | //! name, e.g. "DRIVE/CHANGE_STATE_TO_NEW_STATE"
|
---|
588 | //!
|
---|
589 | //! @param fmt
|
---|
590 | //! A format as defined by the dim system can be given for the command.
|
---|
591 | //! However, it has no real meaning except that it is stored within the
|
---|
592 | //! DimCommand object. However, the user must make sure that the data of
|
---|
593 | //! received commands is properly extracted. No check is done.
|
---|
594 | //!
|
---|
595 | //! @param s1, s2, s3, s4, s5
|
---|
596 | //! A list of states from which a transition to targetstate is allowed
|
---|
597 | //! by this command.
|
---|
598 | //
|
---|
599 | EventImp &StateMachineImp::AddEvent(const char *name, const char *fmt, int s1, int s2, int s3, int s4, int s5)
|
---|
600 | {
|
---|
601 | ostringstream str;
|
---|
602 | str << s1 << ' ' << s2 << ' ' << s3 << ' ' << s4 << ' ' << s5;
|
---|
603 | return AddEvent(name, str.str().c_str(), fmt);
|
---|
604 | }
|
---|
605 |
|
---|
606 | EventImp *StateMachineImp::CreateService(const char *)
|
---|
607 | {
|
---|
608 | return new EventImp();
|
---|
609 | }
|
---|
610 |
|
---|
611 | // --------------------------------------------------------------------------
|
---|
612 | //
|
---|
613 | EventImp &StateMachineImp::Subscribe(const char *name)
|
---|
614 | {
|
---|
615 | EventImp *evt = CreateService(name);
|
---|
616 | fListOfEvents.push_back(evt);
|
---|
617 | return *evt;
|
---|
618 | }
|
---|
619 |
|
---|
620 | // --------------------------------------------------------------------------
|
---|
621 | //
|
---|
622 | //! To be able to name states, i.e. present the current state in human
|
---|
623 | //! readable for to the user, a string can be assigned to each state.
|
---|
624 | //! For each state this function can be called only once, i.e. state name
|
---|
625 | //! cannot be overwritten.
|
---|
626 | //!
|
---|
627 | //! Be aware that two states should not have the same name!
|
---|
628 | //!
|
---|
629 | //! @param state
|
---|
630 | //! Number of the state to which a name should be assigned
|
---|
631 | //!
|
---|
632 | //! @param name
|
---|
633 | //! A name which should be assigned to the state, e.g. "Tracking"
|
---|
634 | //!
|
---|
635 | //! @param doc
|
---|
636 | //! A explanatory text describing the state
|
---|
637 | //!
|
---|
638 | void StateMachineImp::AddStateName(const int state, const std::string &name, const std::string &doc)
|
---|
639 | {
|
---|
640 | if (fStateNames[state].first.empty())
|
---|
641 | fStateNames[state] = make_pair(name, doc);
|
---|
642 | }
|
---|
643 |
|
---|
644 | // --------------------------------------------------------------------------
|
---|
645 | //
|
---|
646 | //! @param state
|
---|
647 | //! The state for which the name should be returned.
|
---|
648 | //!
|
---|
649 | //! @returns
|
---|
650 | //! The state name as stored in fStateNames is returned, corresponding
|
---|
651 | //! to the state given. If no name exists the number is returned
|
---|
652 | //! as string.
|
---|
653 | //!
|
---|
654 | const string StateMachineImp::GetStateName(int state) const
|
---|
655 | {
|
---|
656 | const StateNames::const_iterator i = fStateNames.find(state);
|
---|
657 |
|
---|
658 | ostringstream s;
|
---|
659 | s << state;
|
---|
660 | return i==fStateNames.end() || i->second.first.empty() ? s.str() : i->second.first;
|
---|
661 | }
|
---|
662 |
|
---|
663 | // --------------------------------------------------------------------------
|
---|
664 | //
|
---|
665 | //! @param state
|
---|
666 | //! The state for which the name should be returned.
|
---|
667 | //!
|
---|
668 | //! @returns
|
---|
669 | //! The description of a state name as stored in fStateNames is returned,
|
---|
670 | //! corresponding to the state given. If no name exists an empty string is
|
---|
671 | //! returned.
|
---|
672 | //!
|
---|
673 | const string StateMachineImp::GetStateDesc(int state) const
|
---|
674 | {
|
---|
675 | const StateNames::const_iterator i = fStateNames.find(state);
|
---|
676 | return i==fStateNames.end() ? "" : i->second.second;
|
---|
677 | }
|
---|
678 |
|
---|
679 | // --------------------------------------------------------------------------
|
---|
680 | //
|
---|
681 | //! This functions works in analogy to GetStateName, but the state number
|
---|
682 | //! is added in []-parenthesis after the state name if it is available.
|
---|
683 | //!
|
---|
684 | //! @param state
|
---|
685 | //! The state for which the name should be returned.
|
---|
686 | //!
|
---|
687 | //! @returns
|
---|
688 | //! The state name as stored in fStateName is returned corresponding
|
---|
689 | //! to the state given plus the state number added in []-parenthesis.
|
---|
690 | //! If no name exists the number is returned as string.
|
---|
691 | //!
|
---|
692 | //
|
---|
693 | const string StateMachineImp::GetStateDescription(int state) const
|
---|
694 | {
|
---|
695 | const string &str = GetStateName(state);
|
---|
696 |
|
---|
697 | ostringstream s;
|
---|
698 | s << state;
|
---|
699 | if (str==s.str())
|
---|
700 | return str;
|
---|
701 |
|
---|
702 | return str.empty() ? s.str() : (str+'['+s.str()+']');
|
---|
703 | }
|
---|
704 |
|
---|
705 | // --------------------------------------------------------------------------
|
---|
706 | //
|
---|
707 | //! This function is a helpter function to do all the corresponding action
|
---|
708 | //! if the state machine decides to change its state.
|
---|
709 | //!
|
---|
710 | //! If state is equal to the current state (fCurrentState) nothing is done.
|
---|
711 | //! Then the service STATE (fSrcState) is updated with the new state
|
---|
712 | //! and the text message and updateService() is called to distribute
|
---|
713 | //! the update to all clients.
|
---|
714 | //!
|
---|
715 | //! In addition a log message is created and set via UpdateMsg.
|
---|
716 | //!
|
---|
717 | //! @param state
|
---|
718 | //! The new state which should be applied
|
---|
719 | //!
|
---|
720 | //! @param txt
|
---|
721 | //! A text corresponding to the state change which is distributed
|
---|
722 | //! together with the state itself for convinience.
|
---|
723 | //!
|
---|
724 | //! @param cmd
|
---|
725 | //! This argument can be used to give an additional name of the function
|
---|
726 | //! which is reponsible for the state change. It will be included in the
|
---|
727 | //! message
|
---|
728 | //!
|
---|
729 | //! @return
|
---|
730 | //! return the new state which was set or -1 in case of no change
|
---|
731 | //
|
---|
732 | string StateMachineImp::SetCurrentState(int state, const char *txt, const std::string &cmd)
|
---|
733 | {
|
---|
734 | if (state==fCurrentState)
|
---|
735 | {
|
---|
736 | Out() << " -- " << Time().GetAsStr() << ": State " << GetStateDescription(state) << " already set... ";
|
---|
737 | if (!cmd.empty())
|
---|
738 | Out() << "'" << cmd << "' ignored.";
|
---|
739 | Out() << endl;
|
---|
740 | return "";
|
---|
741 | }
|
---|
742 |
|
---|
743 | const int old = fCurrentState;
|
---|
744 |
|
---|
745 | const string nold = GetStateDescription(old);
|
---|
746 | const string nnew = GetStateDescription(state);
|
---|
747 |
|
---|
748 | string msg = nnew + " " + txt;
|
---|
749 | if (!cmd.empty())
|
---|
750 | msg += " (" + cmd + ")";
|
---|
751 |
|
---|
752 | fCurrentState = state;
|
---|
753 |
|
---|
754 | // State might have changed already again...
|
---|
755 | // Not very likely, but possible. That's why state is used
|
---|
756 | // instead of fCurrentState.
|
---|
757 |
|
---|
758 | ostringstream str;
|
---|
759 | str << "State Transition from " << nold << " to " << nnew << " (" << txt;
|
---|
760 | if (!cmd.empty())
|
---|
761 | str << ": " << cmd;
|
---|
762 | str << ")";
|
---|
763 | Message(str);
|
---|
764 |
|
---|
765 | return msg;
|
---|
766 | }
|
---|
767 |
|
---|
768 | // --------------------------------------------------------------------------
|
---|
769 | //
|
---|
770 | //! This function handles a new state issued by one of the event handlers.
|
---|
771 | //!
|
---|
772 | //! @param newstate
|
---|
773 | //! A possible new state
|
---|
774 | //!
|
---|
775 | //! @param evt
|
---|
776 | //! A pointer to the event which was responsible for the state change,
|
---|
777 | //! NULL if no event was responsible.
|
---|
778 | //!
|
---|
779 | //! @param txt
|
---|
780 | //! Text which is issued if the current state has changed and the new
|
---|
781 | //! state is identical to the target state as stored in the event
|
---|
782 | //! reference, or when no alternative text was given, or the pointer to
|
---|
783 | //! evt is NULL.
|
---|
784 | //!
|
---|
785 | //! @param alt
|
---|
786 | //! An alternative text which is issues when the newstate of a state change
|
---|
787 | //! doesn't match the expected target state.
|
---|
788 | //!
|
---|
789 | //! @returns
|
---|
790 | //! false if newstate is kSM_FatalError, true otherwise
|
---|
791 | //
|
---|
792 | bool StateMachineImp::HandleNewState(int newstate, const EventImp *evt,
|
---|
793 | const char *txt)
|
---|
794 | {
|
---|
795 | if (newstate==kSM_FatalError)
|
---|
796 | return false;
|
---|
797 |
|
---|
798 | if (newstate==fCurrentState)
|
---|
799 | return true;
|
---|
800 |
|
---|
801 | SetCurrentState(newstate, txt, evt ? evt->GetName() : "");
|
---|
802 |
|
---|
803 | return true;
|
---|
804 | }
|
---|
805 |
|
---|
806 | // --------------------------------------------------------------------------
|
---|
807 | //
|
---|
808 | //! This is the event handler. Depending on the type of event it calles
|
---|
809 | //! the function associated with the event, the Transition() or
|
---|
810 | //! Configure() function.
|
---|
811 | //!
|
---|
812 | //! It first checks if the given even is valid in the current state. If
|
---|
813 | //! it is not valid the function returns with true.
|
---|
814 | //!
|
---|
815 | //! If it is valid, it is checked whether a function is associated with
|
---|
816 | //! the event. If this is the case, evt.Exec() is called and HandleNewState
|
---|
817 | //! called with its return value.
|
---|
818 | //!
|
---|
819 | //! If the event's target state is negative (unnamed Event) the Configure()
|
---|
820 | //! function is called with the event as argument and HandleNewState with
|
---|
821 | //! its returned new state.
|
---|
822 | //!
|
---|
823 | //! If the event's target state is 0 or positive (named Event) the
|
---|
824 | //! Transition() function is called with the event as argument and
|
---|
825 | //! HandleNewState with its returned new state.
|
---|
826 | //!
|
---|
827 | //! In all three cases the return value of HandleNewState is returned.
|
---|
828 | //!
|
---|
829 | //! Any of the three commands should usually return the current state
|
---|
830 | //! or (in case of the Transition() command) return the new state. However,
|
---|
831 | //! all three command can issue a state change by returning a new state.
|
---|
832 | //! However, this will just change the internal state. Any action which
|
---|
833 | //! is connected with the state change must have been executed already.
|
---|
834 | //!
|
---|
835 | //! @param evt
|
---|
836 | //! a reference to the event which should be handled
|
---|
837 | //!
|
---|
838 | //! @returns
|
---|
839 | //! false in case one of the commands changed the state to kSM_FataError,
|
---|
840 | //! true otherwise
|
---|
841 | //
|
---|
842 | bool StateMachineImp::HandleEvent(const EventImp &evt)
|
---|
843 | {
|
---|
844 | if (!evt.HasFunc())
|
---|
845 | {
|
---|
846 | Warn(evt.GetName()+": No function assigned... ignored.");
|
---|
847 | return true;
|
---|
848 |
|
---|
849 | }
|
---|
850 |
|
---|
851 | #ifdef DEBUG
|
---|
852 | ostringstream out;
|
---|
853 | out << "Handle: " << evt.GetName() << "[" << evt.GetSize() << "]";
|
---|
854 | Debug(out);
|
---|
855 | #endif
|
---|
856 |
|
---|
857 | // Check if the received command is allow in the current state
|
---|
858 | if (!evt.IsStateAllowed(fCurrentState))
|
---|
859 | {
|
---|
860 | Warn(evt.GetName()+": Not allowed in state "+GetStateDescription()+"... ignored.");
|
---|
861 | return true;
|
---|
862 | }
|
---|
863 |
|
---|
864 | return HandleNewState(evt.ExecFunc(), &evt,
|
---|
865 | "by ExecFunc function-call");
|
---|
866 | }
|
---|
867 |
|
---|
868 | // --------------------------------------------------------------------------
|
---|
869 | //
|
---|
870 | //! This is the main loop, or what could be called the running state
|
---|
871 | //! machine. The flow diagram below shows what the loop is actually doing.
|
---|
872 | //! It's main purpose is to serialize command excecution and the main
|
---|
873 | //! loop in the state machine (e.g. the tracking loop)
|
---|
874 | //!
|
---|
875 | //! Leaving the loop can be forced by setting fExitRequested to another
|
---|
876 | //! value than zero. This is done automatically if dim's EXIT command
|
---|
877 | //! is received or can be forced by calling Stop().
|
---|
878 | //!
|
---|
879 | //! As long as no new command arrives the Execute() command is called
|
---|
880 | //! continously. This should implement the current action which
|
---|
881 | //! should be performed in the current state, e.g. calculating a
|
---|
882 | //! new command value and sending it to the hardware.
|
---|
883 | //!
|
---|
884 | //! If a command is received it is put into the fifo by the commandHandler().
|
---|
885 | //! The main loop now checks the fifo. If commands are in the fifo, it is
|
---|
886 | //! checked whether the command is valid ithin this state or not. If it is
|
---|
887 | //! not valid it is ignored. If it is valid the corresponding action
|
---|
888 | //! is performed. This can either be a call to Configure() (when no state
|
---|
889 | //! change is connected to the command) or Transition() (if the command
|
---|
890 | //! involves a state change).
|
---|
891 | //! In both cases areference to the received command (Command) is
|
---|
892 | //! passed to the function. Note that after the functions have finished
|
---|
893 | //! the command will go out of scope and be deleted.
|
---|
894 | //!
|
---|
895 | //! None of the commands should take to long for execution. Otherwise the
|
---|
896 | //! response time of the main loop will become too slow.
|
---|
897 | //!
|
---|
898 | //! Any of the three commands should usually return the current state
|
---|
899 | //! or (in case of the Transition() command) return the new state. However,
|
---|
900 | //! all three command can issue a state change by returning a new state.
|
---|
901 | //! However, this will just change the internal state. Any action which
|
---|
902 | //! is connected with the state change must have been executed already.
|
---|
903 | //!
|
---|
904 | //!
|
---|
905 | //!
|
---|
906 | //! \dot
|
---|
907 | //! digraph Run {
|
---|
908 | //! node [ shape=record, fontname=Helvetica, fontsize=10 ];
|
---|
909 | //! edge [ labelfontname=Helvetica, labelfontsize=8 ];
|
---|
910 | //! start0 [ label="Run()" style="rounded"];
|
---|
911 | //! start1 [ label="fExitRequested=0\nfRunning=true\nSetCurrentState(kSM_Ready)"];
|
---|
912 | //! cond1 [ label="Is fExitRequested==0?"];
|
---|
913 | //! exec [ label="HandleNewState(Execute())"];
|
---|
914 | //! fifo [ label="Any event in FIFO?"];
|
---|
915 | //! get [ label="Get event from FIFO\n Is event allowed within the current state?" ];
|
---|
916 | //! handle [ label="HandleEvent()" ];
|
---|
917 | //! exit1 [ label="fRunning=false\nSetCurrentState(kSM_FatalError)\n return -1" style="rounded"];
|
---|
918 | //! exit2 [ label="fRunning=false\nSetCurrentState(kSM_NotReady)\n return fExitRequested-1" style="rounded"];
|
---|
919 | //!
|
---|
920 | //! start0 -> start1 [ weight=8 ];
|
---|
921 | //! start1 -> cond1 [ weight=8 ];
|
---|
922 | //!
|
---|
923 | //! cond1:e -> exit2:n [ taillabel="true" ];
|
---|
924 | //! cond1 -> exec [ taillabel="false" weight=8 ];
|
---|
925 | //!
|
---|
926 | //! exec -> fifo [ taillabel="true" weight=8 ];
|
---|
927 | //! exec:e -> exit1:e [ taillabel="false" ];
|
---|
928 | //!
|
---|
929 | //! fifo -> cond1 [ taillabel="false" ];
|
---|
930 | //! fifo -> get [ taillabel="true" weight=8 ];
|
---|
931 | //!
|
---|
932 | //! get -> handle [ taillabel="true" ];
|
---|
933 | //!
|
---|
934 | //! handle:s -> exit1:n [ taillabel="false" weight=8 ];
|
---|
935 | //! handle -> cond1 [ taillabel="true" ];
|
---|
936 | //! }
|
---|
937 | //! \enddot
|
---|
938 | //!
|
---|
939 | //! @param dummy
|
---|
940 | //! If this parameter is set to treu then no action is executed
|
---|
941 | //! and now events are dispatched from the event list. It is usefull
|
---|
942 | //! if functions are assigned directly to any event to simulate
|
---|
943 | //! a running loop (e.g. block until Stop() was called or fExitRequested
|
---|
944 | //! was set by an EXIT command. If dummy==true, fRunning is not set
|
---|
945 | //! to true to allow handling events directly from the event handler.
|
---|
946 | //!
|
---|
947 | //! @returns
|
---|
948 | //! In the case of a a fatal error -1 is returned, fExitRequested-1 in all
|
---|
949 | //! other cases (This corresponds to the exit code either received by the
|
---|
950 | //! EXIT event or given to the Stop() function)
|
---|
951 | //!
|
---|
952 | //! @todo Fix docu (kSM_SetReady, HandleEvent)
|
---|
953 | //
|
---|
954 | int StateMachineImp::Run(bool dummy)
|
---|
955 | {
|
---|
956 | if (fCurrentState>=kSM_Ready)
|
---|
957 | {
|
---|
958 | Error("Run() can only be called in the NotReady state.");
|
---|
959 | return -1;
|
---|
960 | }
|
---|
961 |
|
---|
962 | if (!fExitRequested)
|
---|
963 | {
|
---|
964 | fRunning = !dummy;
|
---|
965 |
|
---|
966 | SetCurrentState(kSM_Ready, "by Run()");
|
---|
967 |
|
---|
968 | while (!fExitRequested)
|
---|
969 | {
|
---|
970 | usleep(1);
|
---|
971 | if (dummy)
|
---|
972 | continue;
|
---|
973 |
|
---|
974 | // Execute a step in the current state of the state machine
|
---|
975 | if (!HandleNewState(Execute(), 0, "by Execute-command"))
|
---|
976 | break;
|
---|
977 |
|
---|
978 | // If the command stack is empty go on with processing in the
|
---|
979 | // current state
|
---|
980 | if (IsQueueEmpty())
|
---|
981 | continue;
|
---|
982 |
|
---|
983 | // Pop the next command which arrived from the stack
|
---|
984 | const auto_ptr<Event> cmd(PopEvent());
|
---|
985 |
|
---|
986 | if (!HandleEvent(*cmd))
|
---|
987 | break;
|
---|
988 | }
|
---|
989 |
|
---|
990 | fRunning = false;
|
---|
991 |
|
---|
992 | if (!fExitRequested)
|
---|
993 | {
|
---|
994 | Fatal("Fatal Error occured... shutting down.");
|
---|
995 | return -1;
|
---|
996 | }
|
---|
997 |
|
---|
998 | SetCurrentState(kSM_NotReady, "due to return from Run().");
|
---|
999 | }
|
---|
1000 |
|
---|
1001 | const int exitcode = fExitRequested-1;
|
---|
1002 |
|
---|
1003 | // Prepare for next call
|
---|
1004 | fExitRequested = 0;
|
---|
1005 |
|
---|
1006 | return exitcode;
|
---|
1007 | }
|
---|
1008 |
|
---|
1009 | // --------------------------------------------------------------------------
|
---|
1010 | //
|
---|
1011 | //! This function can be called to stop the loop of a running state machine.
|
---|
1012 | //! Run() will then return with a return value corresponding to the value
|
---|
1013 | //! given as argument.
|
---|
1014 | //!
|
---|
1015 | //! Note that this is a dangerous operation, because as soon as one of the
|
---|
1016 | //! three state machine commands returns (Execute(), Configure() and
|
---|
1017 | //! Transition()) the loop will be left and Run(9 will return. The program
|
---|
1018 | //! is then responsible of correctly cleaning up the mess which might be left
|
---|
1019 | //! behind.
|
---|
1020 | //!
|
---|
1021 | //! @param code
|
---|
1022 | //! int with which Run() should return when returning.
|
---|
1023 | //
|
---|
1024 | void StateMachineImp::Stop(int code)
|
---|
1025 | {
|
---|
1026 | fExitRequested = code+1;
|
---|
1027 | }
|
---|