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 "tools.h"
|
---|
29 |
|
---|
30 | #include "WindowLog.h"
|
---|
31 | #include "StateMachineImp.h"
|
---|
32 |
|
---|
33 | using namespace std;
|
---|
34 |
|
---|
35 | template <class T>
|
---|
36 | class LocalControl : public T
|
---|
37 | {
|
---|
38 | private:
|
---|
39 | char **Completion(const char *text, int pos, int)
|
---|
40 | {
|
---|
41 | return pos>0 ? 0 : T::Complete(fStateMachine->GetEventNames(), text);
|
---|
42 | }
|
---|
43 |
|
---|
44 | protected:
|
---|
45 | StateMachineImp *fStateMachine;
|
---|
46 |
|
---|
47 | std::ostream &lout;
|
---|
48 |
|
---|
49 | std::string fName;
|
---|
50 |
|
---|
51 | LocalControl(const char *name) : T(name),
|
---|
52 | fStateMachine(0), lout(T::GetStreamIn()),
|
---|
53 | fName(boost::filesystem::path(name).filename())
|
---|
54 | { }
|
---|
55 |
|
---|
56 | bool PrintGeneralHelp()
|
---|
57 | {
|
---|
58 | T::PrintGeneralHelp();
|
---|
59 | lout << " " << kUnderline << "Specific commands:" << endl;
|
---|
60 | lout << kBold << " ac,allowed " << kReset << "Display a list of all currently allowed commands." << endl;
|
---|
61 | lout << kBold << " st,states " << kReset << "Display a list of the available states with description." << endl;
|
---|
62 | lout << kBold << " # <text> " << kReset << "Echo <text> to the output stream" << endl;
|
---|
63 | lout << kBold << " .s " << kReset << "Wait for the state-machine to change to the given state." << endl;
|
---|
64 | lout << " " " .s <state> [<timeout>]" << endl;
|
---|
65 | lout << " " "<state> The state id (see 'states') for which to wait (e.g. 3)" << endl;
|
---|
66 | lout << " " "<imeout> A timeout in millisenconds how long to wait (e.g. 500)" << endl;
|
---|
67 | lout << endl;
|
---|
68 | return true;
|
---|
69 | }
|
---|
70 | bool PrintCommands()
|
---|
71 | {
|
---|
72 | lout << endl << kBold << "List of commands:" << endl;
|
---|
73 | fStateMachine->PrintListOfEvents(lout);
|
---|
74 | lout << endl;
|
---|
75 |
|
---|
76 | return true;
|
---|
77 | }
|
---|
78 |
|
---|
79 | bool Process(const std::string &str)
|
---|
80 | {
|
---|
81 | if (str.substr(0, 2)=="h " || str.substr(0, 5)=="help ")
|
---|
82 | {
|
---|
83 | lout << endl;
|
---|
84 | fStateMachine->PrintListOfEvents(lout, str.substr(str.find_first_of(' ')+1));
|
---|
85 | lout << endl;
|
---|
86 |
|
---|
87 | return true;
|
---|
88 | }
|
---|
89 | if (str=="states" || str=="st")
|
---|
90 | {
|
---|
91 | fStateMachine->PrintListOfStates(lout);
|
---|
92 | return true;
|
---|
93 | }
|
---|
94 | if (str=="allowed" || str=="ac")
|
---|
95 | {
|
---|
96 | lout << endl << kBold << "List of commands allowed in current state:" << endl;
|
---|
97 | fStateMachine->PrintListOfAllowedEvents(lout);
|
---|
98 | lout << endl;
|
---|
99 | return true;
|
---|
100 | }
|
---|
101 |
|
---|
102 | if (str.substr(0, 3)==".s ")
|
---|
103 | {
|
---|
104 | istringstream in(str.substr(3));
|
---|
105 |
|
---|
106 | int state=-100, ms=0;
|
---|
107 | in >> state >> ms;
|
---|
108 |
|
---|
109 | if (state==-100)
|
---|
110 | {
|
---|
111 | lout << kRed << "Couldn't parse state id." << endl;
|
---|
112 | return true;
|
---|
113 | }
|
---|
114 |
|
---|
115 | const Time timeout = ms<=0 ? Time(Time::none) : Time()+boost::posix_time::millisec(ms);
|
---|
116 |
|
---|
117 | const int target = stoi(str.substr(3));
|
---|
118 | while (fStateMachine->GetCurrentState()!=target && timeout>Time())
|
---|
119 | usleep(1);
|
---|
120 |
|
---|
121 | return true;
|
---|
122 | }
|
---|
123 |
|
---|
124 | if (str[0]=='#')
|
---|
125 | {
|
---|
126 | lout << Tools::Trim(str.substr(1)) << endl;
|
---|
127 | return true;
|
---|
128 | }
|
---|
129 |
|
---|
130 | if (T::Process(str))
|
---|
131 | return true;
|
---|
132 |
|
---|
133 | return !fStateMachine->PostEvent(lout, str);
|
---|
134 | }
|
---|
135 |
|
---|
136 | public:
|
---|
137 |
|
---|
138 | void SetReceiver(StateMachineImp &imp) { fStateMachine = &imp; }
|
---|
139 | };
|
---|
140 |
|
---|
141 | // **************************************************************************
|
---|
142 | /** @class LocalStream
|
---|
143 |
|
---|
144 | @brief Derives the LocalControl from ConsoleStream
|
---|
145 |
|
---|
146 | This is basically a LocalControl, which derives through the template
|
---|
147 | argument from the ConsoleStream class.
|
---|
148 |
|
---|
149 | */
|
---|
150 | // **************************************************************************
|
---|
151 | #include "Console.h"
|
---|
152 |
|
---|
153 | class LocalStream : public LocalControl<ConsoleStream>
|
---|
154 | {
|
---|
155 | public:
|
---|
156 | LocalStream(const char *name, bool null = false)
|
---|
157 | : LocalControl<ConsoleStream>(name) { SetNullOutput(null); }
|
---|
158 | };
|
---|
159 |
|
---|
160 | // **************************************************************************
|
---|
161 | /** @class LocalConsole
|
---|
162 |
|
---|
163 | @brief Derives the LocalControl from Control and adds prompt
|
---|
164 |
|
---|
165 | This is basically a LocalControl, which derives through the template
|
---|
166 | argument from the Console class. It enhances the functionality of
|
---|
167 | the local control with a proper updated prompt.
|
---|
168 |
|
---|
169 | */
|
---|
170 | // **************************************************************************
|
---|
171 | #include "tools.h"
|
---|
172 |
|
---|
173 | class LocalConsole : public LocalControl<Console>
|
---|
174 | {
|
---|
175 | public:
|
---|
176 | LocalConsole(const char *name, bool continous=false)
|
---|
177 | : LocalControl<Console>(name)
|
---|
178 | {
|
---|
179 | SetContinous(continous);
|
---|
180 | }
|
---|
181 |
|
---|
182 | string GetUpdatePrompt() const
|
---|
183 | {
|
---|
184 | return GetLinePrompt()+" "
|
---|
185 | "\033[34m\033[1m"+fName+"\033[0m:"
|
---|
186 | "\033[32m\033[1m"+fStateMachine->GetStateName()+"\033[0m> ";
|
---|
187 | }
|
---|
188 | };
|
---|
189 |
|
---|
190 | // **************************************************************************
|
---|
191 | /** @class LocalShell
|
---|
192 |
|
---|
193 | @brief Derives the LocalControl from Shell and adds a colored prompt
|
---|
194 |
|
---|
195 | This is basically a LocalControl, which derives through the template
|
---|
196 | argument from the Shell class. It enhances the functionality of
|
---|
197 | the local control with a proper updated prompt.
|
---|
198 |
|
---|
199 | */
|
---|
200 | // **************************************************************************
|
---|
201 | #include "Shell.h"
|
---|
202 |
|
---|
203 | class LocalShell : public LocalControl<Shell>
|
---|
204 | {
|
---|
205 | public:
|
---|
206 | LocalShell(const char *name, bool = false)
|
---|
207 | : LocalControl<Shell>(name) { }
|
---|
208 |
|
---|
209 | string GetUpdatePrompt() const
|
---|
210 | {
|
---|
211 | return GetLinePrompt()+' '+fName+':'+fStateMachine->GetStateName()+"> ";
|
---|
212 | }
|
---|
213 | };
|
---|
214 |
|
---|
215 | #endif
|
---|