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