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

Last change on this file since 15462 was 13897, checked in by tbretz, 12 years ago
Updated the handling of labels and how a script is stopped.
File size: 7.1 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.\n";
67 lout << " " " .s <server> [<state> [<timeout> [<label>]]]\n";
68 lout << " " "<server> The server for which state to wait (e.g. FTM_CONTROL)\n";
69 lout << " " "<state> The state id (see 'states') for which to wait (e.g. 3)\n";
70 lout << " " "<imeout> A timeout in millisenconds how long to wait (e.g. 500)\n";
71 lout << " " "<label> A label until which everything is skipped in case of timeout\n";
72 lout << endl;
73 return true;
74 }
75 bool PrintCommands()
76 {
77 lout << endl << kBold << "List of commands:" << endl;
78 fStateMachine->PrintListOfEvents(lout);
79 lout << endl;
80
81 return true;
82 }
83
84 bool Process(const std::string &str)
85 {
86 if (str.substr(0, 2)=="h " || str.substr(0, 5)=="help ")
87 {
88 lout << endl;
89 fStateMachine->PrintListOfEvents(lout, str.substr(str.find_first_of(' ')+1));
90 lout << endl;
91
92 return true;
93 }
94 if (str=="states" || str=="st")
95 {
96 fStateMachine->PrintListOfStates(lout);
97 return true;
98 }
99 if (str=="allowed" || str=="ac")
100 {
101 lout << endl << kBold << "List of commands allowed in current state:" << endl;
102 fStateMachine->PrintListOfAllowedEvents(lout);
103 lout << endl;
104 return true;
105 }
106
107 if (str.substr(0, 3)==".s ")
108 {
109 istringstream in(str.substr(3));
110
111 int state=-100, ms=0;
112 in >> state >> ms;
113
114 if (state==-100)
115 {
116 lout << kRed << "Couldn't parse state id in '" << str.substr(3) << "'" << endl;
117 return true;
118 }
119
120 const Time timeout = ms<=0 ? Time(Time::none) : Time()+boost::posix_time::millisec(ms);
121
122 const int target = stoi(str.substr(3));
123 while (fStateMachine->GetCurrentState()!=target && timeout>Time() && !T::IsScriptStopped())
124 usleep(1);
125
126 if (fStateMachine->GetCurrentState()==target)
127 return true;
128
129 int label = -1;
130 in >> label;
131 if (in.fail() && !in.eof())
132 {
133 lout << kRed << "Invalid label in '" << str.substr(3) << "'" << endl;
134 T::StopScript();
135 return true;
136 }
137 T::SetLabel(label);
138
139 return true;
140 }
141
142 if (str[0]=='>')
143 {
144 fStateMachine->Comment(Tools::Trim(str.substr(1)));
145 return true;
146 }
147
148 if (T::Process(str))
149 return true;
150
151 return !fStateMachine->PostEvent(lout, str);
152 }
153
154public:
155
156 void SetReceiver(StateMachineImp &imp) { fStateMachine = &imp; }
157};
158
159// **************************************************************************
160/** @class LocalStream
161
162@brief Derives the LocalControl from ConsoleStream
163
164This is basically a LocalControl, which derives through the template
165argument from the ConsoleStream class.
166
167 */
168// **************************************************************************
169#include "Console.h"
170
171class LocalStream : public LocalControl<ConsoleStream>
172{
173public:
174 LocalStream(const char *name, bool null = false)
175 : LocalControl<ConsoleStream>(name) { SetNullOutput(null); }
176};
177
178// **************************************************************************
179/** @class LocalConsole
180
181@brief Derives the LocalControl from Control and adds prompt
182
183This is basically a LocalControl, which derives through the template
184argument from the Console class. It enhances the functionality of
185the local control with a proper updated prompt.
186
187 */
188// **************************************************************************
189#include "tools.h"
190
191class LocalConsole : public LocalControl<Console>
192{
193public:
194 LocalConsole(const char *name, bool continous=false)
195 : LocalControl<Console>(name)
196 {
197 SetContinous(continous);
198 }
199
200 string GetUpdatePrompt() const
201 {
202 return GetLinePrompt()+" "
203 "\033[34m\033[1m"+fName+"\033[0m:"
204 "\033[32m\033[1m"+fStateMachine->GetStateName()+"\033[0m> ";
205 }
206};
207
208// **************************************************************************
209/** @class LocalShell
210
211@brief Derives the LocalControl from Shell and adds a colored prompt
212
213This is basically a LocalControl, which derives through the template
214argument from the Shell class. It enhances the functionality of
215the local control with a proper updated prompt.
216
217 */
218// **************************************************************************
219#include "Shell.h"
220
221class LocalShell : public LocalControl<Shell>
222{
223public:
224 LocalShell(const char *name, bool = false)
225 : LocalControl<Shell>(name) { }
226
227 string GetUpdatePrompt() const
228 {
229 return GetLinePrompt()+' '+fName+':'+fStateMachine->GetStateName()+"> ";
230 }
231};
232
233#endif
Note: See TracBrowser for help on using the repository browser.