source: trunk/FACT++/src/LocalControl.h@ 13178

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