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

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