source: trunk/FACT++/src/StateMachineImp.cc@ 11323

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