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

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