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