1 | #ifndef FACT_LocalControl
|
---|
2 | #define FACT_LocalControl
|
---|
3 |
|
---|
4 | #include <ostream>
|
---|
5 |
|
---|
6 | class StateMachineImp;
|
---|
7 |
|
---|
8 | // **************************************************************************
|
---|
9 | /** @class LocalControl
|
---|
10 |
|
---|
11 | @brief Implements a local control for a StateMachine based on a Readline class
|
---|
12 |
|
---|
13 | This template implements all functions which overwrite any function from the
|
---|
14 | Readline class needed for a local control of a state machien. Since
|
---|
15 | several derivatives of the Readline class implement different kind of
|
---|
16 | Readline access, this class can be derived by any of them due to its
|
---|
17 | template argument. However, the normal case will be deriving it from
|
---|
18 | either Console or Shell.
|
---|
19 |
|
---|
20 | @tparam T
|
---|
21 | The base class for RemoteControl. Either Readlien or a class
|
---|
22 | deriving from it. This is usually either Console or Shell.
|
---|
23 |
|
---|
24 | **/
|
---|
25 | // **************************************************************************
|
---|
26 | #include <boost/filesystem.hpp>
|
---|
27 |
|
---|
28 | #include "WindowLog.h"
|
---|
29 | #include "StateMachineImp.h"
|
---|
30 |
|
---|
31 | using namespace std;
|
---|
32 |
|
---|
33 | template <class T>
|
---|
34 | class LocalControl : public T
|
---|
35 | {
|
---|
36 | private:
|
---|
37 | char **Completion(const char *text, int pos, int)
|
---|
38 | {
|
---|
39 | return pos>0 ? 0 : T::Complete(fStateMachine->GetEventNames(), text);
|
---|
40 | }
|
---|
41 |
|
---|
42 | protected:
|
---|
43 | StateMachineImp *fStateMachine;
|
---|
44 |
|
---|
45 | std::ostream &lout;
|
---|
46 |
|
---|
47 | std::string fName;
|
---|
48 |
|
---|
49 | LocalControl(const char *name) : T(name),
|
---|
50 | fStateMachine(0), lout(T::GetStreamIn()),
|
---|
51 | fName(boost::filesystem::path(name).filename().c_str())
|
---|
52 | { }
|
---|
53 |
|
---|
54 | bool PrintGeneralHelp()
|
---|
55 | {
|
---|
56 | T::PrintGeneralHelp();
|
---|
57 | lout << " " << kUnderline << "Specific commands:" << endl;
|
---|
58 | lout << kBold << " st,states " << kReset << "Display a list of the available states with description." << endl;
|
---|
59 | lout << kBold << " .s " << kReset << "Wait for the state-machine to change to the given state." << endl;
|
---|
60 | lout << " " " .s <state> [<timeout>]" << endl;
|
---|
61 | lout << " " "<state> The state id (see 'states') for which to wait (e.g. 3)" << endl;
|
---|
62 | lout << " " "<imeout> A timeout in millisenconds how long to wait (e.g. 500)" << endl;
|
---|
63 | lout << endl;
|
---|
64 | return true;
|
---|
65 | }
|
---|
66 | bool PrintCommands()
|
---|
67 | {
|
---|
68 | lout << endl << kBold << "List of commands:" << endl;
|
---|
69 | fStateMachine->PrintListOfEvents(lout);
|
---|
70 | lout << endl;
|
---|
71 |
|
---|
72 | return true;
|
---|
73 | }
|
---|
74 |
|
---|
75 | bool Process(const std::string &str)
|
---|
76 | {
|
---|
77 | if (str.substr(0, 2)=="h " || str.substr(0, 5)=="help ")
|
---|
78 | {
|
---|
79 | lout << endl;
|
---|
80 | fStateMachine->PrintListOfEvents(lout, str.substr(str.find_first_of(' ')+1));
|
---|
81 | lout << endl;
|
---|
82 |
|
---|
83 | return true;
|
---|
84 | }
|
---|
85 | if (str=="states" || str=="st")
|
---|
86 | {
|
---|
87 | if (fStateMachine)
|
---|
88 | fStateMachine->PrintListOfStates(lout);
|
---|
89 | return true;
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (str.substr(0, 3)==".s ")
|
---|
93 | {
|
---|
94 | istringstream in(str.substr(3));
|
---|
95 |
|
---|
96 | int state=-100, ms=0;
|
---|
97 | in >> state >> ms;
|
---|
98 |
|
---|
99 | if (state==-100)
|
---|
100 | {
|
---|
101 | lout << kRed << "Couldn't parse state id." << endl;
|
---|
102 | return true;
|
---|
103 | }
|
---|
104 |
|
---|
105 | const Time timeout = Time()+boost::posix_time::millisec(ms);
|
---|
106 |
|
---|
107 | const int target = atoi(str.c_str()+3);
|
---|
108 | while (fStateMachine->GetCurrentState()!=target && timeout>Time())
|
---|
109 | usleep(1);
|
---|
110 |
|
---|
111 | return true;
|
---|
112 | }
|
---|
113 |
|
---|
114 | if (T::Process(str))
|
---|
115 | return true;
|
---|
116 |
|
---|
117 | return !fStateMachine->PostEvent(lout, str);
|
---|
118 | }
|
---|
119 |
|
---|
120 | public:
|
---|
121 |
|
---|
122 | void SetReceiver(StateMachineImp &imp) { fStateMachine = &imp; }
|
---|
123 | };
|
---|
124 |
|
---|
125 | // **************************************************************************
|
---|
126 | /** @class LocalConsole
|
---|
127 |
|
---|
128 | @brief Derives the LocalControl from Control and adds prompt
|
---|
129 |
|
---|
130 | This is basically a LocalControl, which derives through the template
|
---|
131 | argument from the Console class. It enhances the functionality of
|
---|
132 | the local control with a proper updated prompt.
|
---|
133 |
|
---|
134 | */
|
---|
135 | // **************************************************************************
|
---|
136 | #include "Console.h"
|
---|
137 | #include "tools.h"
|
---|
138 |
|
---|
139 | class LocalConsole : public LocalControl<Console>
|
---|
140 | {
|
---|
141 | public:
|
---|
142 | LocalConsole(const char *name, bool continous=false)
|
---|
143 | : LocalControl<Console>(name)
|
---|
144 | {
|
---|
145 | SetContinous(continous);
|
---|
146 | }
|
---|
147 |
|
---|
148 | string GetUpdatePrompt() const
|
---|
149 | {
|
---|
150 | return GetLinePrompt()+" "
|
---|
151 | "\033[34m\033[1m"+fName+"\033[0m:"
|
---|
152 | "\033[32m\033[1m"+fStateMachine->GetStateName()+"\033[0m> ";
|
---|
153 | }
|
---|
154 | };
|
---|
155 |
|
---|
156 | // **************************************************************************
|
---|
157 | /** @class LocalShell
|
---|
158 |
|
---|
159 | @brief Derives the LocalControl from Shell and adds a colored prompt
|
---|
160 |
|
---|
161 | This is basically a LocalControl, which derives through the template
|
---|
162 | argument from the Shell class. It enhances the functionality of
|
---|
163 | the local control with a proper updated prompt.
|
---|
164 |
|
---|
165 | */
|
---|
166 | // **************************************************************************
|
---|
167 | #include "Shell.h"
|
---|
168 |
|
---|
169 | class LocalShell : public LocalControl<Shell>
|
---|
170 | {
|
---|
171 | public:
|
---|
172 | LocalShell(const char *name, bool = false)
|
---|
173 | : LocalControl<Shell>(name) { }
|
---|
174 |
|
---|
175 | string GetUpdatePrompt() const
|
---|
176 | {
|
---|
177 | return GetLinePrompt()+' '+fName+':'+fStateMachine->GetStateName()+"> ";
|
---|
178 | }
|
---|
179 | };
|
---|
180 |
|
---|
181 | #endif
|
---|