FACT++  1.0
LocalControl.h
Go to the documentation of this file.
1 #ifndef FACT_LocalControl
2 #define FACT_LocalControl
3 
4 #include <ostream>
5 
6 // **************************************************************************
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 
32 using namespace std;
33 
34 template <class T>
35 class LocalControl : public T
36 {
37 private:
38  char **Completion(const char *text, int pos, int)
39  {
40  return pos>0 ? 0 : T::Complete(fStateMachine->GetEventNames(), text);
41  }
42 
43 protected:
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 
60  {
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  }
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  while (fStateMachine->GetCurrentState()!=state && timeout>Time() && !T::IsScriptStopped())
123  usleep(1);
124 
125  if (fStateMachine->GetCurrentState()==state)
126  return true;
127 
128  int label = -1;
129  in >> label;
130  if (in.fail() && !in.eof())
131  {
132  lout << kRed << "Invalid label in '" << str.substr(3) << "'" << endl;
133  T::StopScript();
134  return true;
135  }
136  T::SetLabel(label);
137 
138  return true;
139  }
140 
141  if (str[0]=='>')
142  {
143  fStateMachine->Comment(Tools::Trim(str.substr(1)));
144  return true;
145  }
146 
147  if (T::Process(str))
148  return true;
149 
150  return !fStateMachine->PostEvent(lout, str);
151  }
152 
153 public:
154 
155  void SetReceiver(StateMachineImp &imp) { fStateMachine = &imp; }
156 };
157 
158 // **************************************************************************
167 // **************************************************************************
168 #include "Console.h"
169 
170 class LocalStream : public LocalControl<ConsoleStream>
171 {
172 public:
173  LocalStream(const char *name, bool null = false)
174  : LocalControl<ConsoleStream>(name) { SetNullOutput(null); }
175 };
176 
177 // **************************************************************************
187 // **************************************************************************
188 #include "tools.h"
189 
190 class LocalConsole : public LocalControl<Console>
191 {
192 public:
193  LocalConsole(const char *name, bool continous=false)
194  : LocalControl<Console>(name)
195  {
196  SetContinous(continous);
197  }
198 
199  string GetUpdatePrompt() const
200  {
201  return GetLinePrompt()+" "
202  "\033[34m\033[1m"+fName+"\033[0m:"
203  "\033[32m\033[1m"+fStateMachine->GetStateName()+"\033[0m> ";
204  }
205 };
206 
207 // **************************************************************************
217 // **************************************************************************
218 #include "Shell.h"
219 
220 class LocalShell : public LocalControl<Shell>
221 {
222 public:
223  LocalShell(const char *name, bool = false)
224  : LocalControl<Shell>(name) { }
225 
226  string GetUpdatePrompt() const
227  {
228  return GetLinePrompt()+' '+fName+':'+fStateMachine->GetStateName()+"> ";
229  }
230 };
231 
232 #endif
int Comment(const std::string &str)
Definition: MessageImp.h:52
Derives the LocalControl from Control and adds prompt.
Definition: LocalControl.h:190
This is an extension to the Readline class provding a colored output.
Definition: Console.h:7
void PrintListOfStates(std::ostream &out) const
string GetUpdatePrompt() const
Definition: LocalControl.h:226
bool PrintGeneralHelp(std::ostream &out, const std::string &name)
std::ostream & lout
Definition: LocalControl.h:46
int GetCurrentState() const
return the current state of the machine
Base class for a state machine implementation.
bool PostEvent(std::ostream &lout, const std::string &str)
Post an event to the event queue.
This is an extension to the Readline class provding buffered output.
Definition: Console.h:34
void SetReceiver(StateMachineImp &imp)
Definition: LocalControl.h:155
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
char str[80]
Definition: test_client.c:7
bool PrintGeneralHelp()
Definition: LocalControl.h:59
Set color Red.
Definition: WindowLog.h:17
STL namespace.
char ** Completion(const char *text, int pos, int)
Definition: LocalControl.h:38
Derives the LocalControl from Shell and adds a colored prompt.
Definition: LocalControl.h:220
bool Process(const std::string &str)
Definition: LocalControl.h:84
std::string fName
Definition: LocalControl.h:48
Set attribute Underline.
Definition: WindowLog.h:32
LocalStream(const char *name, bool null=false)
Definition: LocalControl.h:173
StateMachineImp * fStateMachine
Definition: LocalControl.h:44
LocalShell(const char *name, bool=false)
Definition: LocalControl.h:223
LocalConsole(const char *name, bool continous=false)
Definition: LocalControl.h:193
LocalControl(const char *name)
Definition: LocalControl.h:50
bool Process(std::ostream &out, const std::string &str)
Implements a local control for a StateMachine based on a Readline class.
Definition: LocalControl.h:35
void PrintListOfEvents(std::ostream &out, const std::string &evt="")
string GetUpdatePrompt() const
Definition: LocalControl.h:199
void PrintListOfAllowedEvents(std::ostream &out)
if(extraDns) new Dns
std::string Trim(const std::string &str)
Definition: tools.cc:68
Derives the LocalControl from ConsoleStream.
Definition: LocalControl.h:170
Reset all attributes.
Definition: WindowLog.h:29
Do not initialize the time.
Definition: Time.h:51
bool PrintCommands()
Definition: LocalControl.h:75
Set attribute Bold.
Definition: WindowLog.h:36
Implementation of a console based user shell with an input and output window.
Definition: Shell.h:12