source: trunk/FACT++/src/RemoteControl.h@ 10494

Last change on this file since 10494 was 10494, checked in by tbretz, 10 years ago
The new convention is to return always the server name in GetCommandList - removed the server name from the list.
File size: 7.3 KB
Line 
1#ifndef FACT_RemoteControl
2#define FACT_RemoteControl
3
4// **************************************************************************
5/** @class RemoteControlImp
6
7@brief This implements the basic functions of a remote control via dim
8
9Through a ServiceList object this object subscribes to all available
10SERVICE_LISTs in the dim network. This allows to keep an up-to-date
11list of all servers and services. Its ProcessCommand member function
12allows to emit commands according to the services found in the network.
13Its infoHandler() is called as an update notifier from the ClientList
14object.
15
16**/
17// **************************************************************************
18#include "DimNetwork.h"
19
20class RemoteControlImp : public DimNetwork
21{
22protected:
23 std::ostream &lout; /// Output stream for local synchrounous output
24
25 std::string fCurrentServer; /// The server to which we currently cd'ed
26
27protected:
28 // Redirect asynchronous output to the output window
29 RemoteControlImp(std::ostream &out, std::ostream &in) :
30 DimNetwork(out), lout(in)
31 {
32 }
33 bool ProcessCommand(const std::string &str);
34};
35
36
37
38// **************************************************************************
39/** @class RemoteControl
40
41@brief Implements a remote control based on a Readline class for the dim network
42
43This template implements all functions which overwrite any function from the
44Readline class. Since several derivatives of the Readline class implement
45different kind of Readline access, this class can be derived by any of them
46due to its template argument. However, the normal case will be
47deriving it from either Console or Shell.
48
49@tparam T
50 The base class for RemoteControl. Either Readlien or a class
51 deriving from it. This is usually either Console or Shell.
52
53**/
54// **************************************************************************
55#include "WindowLog.h"
56#include "ReadlineColor.h"
57#include "tools.h"
58
59template <class T>
60class RemoteControl : public T, public RemoteControlImp
61{
62private:
63 static void append(std::string &str)
64 {
65 str.append("/");
66 }
67 static void chop(std::string &str)
68 {
69 const size_t p = str.find_first_of('/');
70 if (p!=string::npos)
71 str = str.substr(p+1);
72 }
73
74 // This funtion defines which generator should be called.
75 // If it returns 0 the standard reaqdline generator are called.
76 // Otherwise set the right generator with rl_completion_matches.
77 char **Completion(const char *text, int start, int)
78 {
79 // Get the whole buffer before the tab-position
80 const string b = string(T::GetBuffer());
81 const string s = b.substr(0, start);
82 const string l = Tools::Trim(s.c_str());
83 if (l.empty())
84 {
85 if (fCurrentServer.empty())
86 {
87 const size_t p1 = b.find_first_of(' ');
88 const size_t p2 = b.find_first_of('/');
89
90 if (p1==string::npos && p2!=string::npos)
91 return T::Complete(GetCommandList(), text);
92
93 std::vector<std::string> v = GetServerList();
94 for_each(v.begin(), v.end(), RemoteControl::append);
95 return T::Complete(v, text);
96 }
97 else
98 {
99 std::vector<std::string> v = GetCommandList(fCurrentServer);
100 for_each(v.begin(), v.end(), RemoteControl::chop);
101 return T::Complete(v, text);
102 }
103 }
104 return T::Complete(GetCommandList(l), text);
105 }
106
107 void infoHandler()
108 {
109 RemoteControlImp::infoHandler();
110 if (!fCurrentServer.empty() && !HasServer(fCurrentServer))
111 {
112 fCurrentServer = "";
113 T::UpdatePrompt();
114 }
115 }
116
117public:
118 // Redirect asynchronous output to the output window
119 RemoteControl(const char *name) : T(name),
120 RemoteControlImp(T::GetStreamOut(), T::GetStreamIn())
121 {
122 }
123
124 bool PrintGeneralHelp()
125 {
126 T::PrintGeneralHelp();
127 lout << " " << kUnderline << "Specific commands:" << endl;
128 lout << kBold << " h,help <arg> " << kReset << "List help text for given server or command." << endl;
129// lout << kBold << " s,servers " << kReset << "List all servers which are connected." << endl;
130 lout << kBold << " svc,services " << kReset << "List all services in the network." << endl;
131 lout << kBold << " st,states " << kReset << "List all states in the network." << endl;
132 lout << endl;
133 return true;
134 }
135
136 bool PrintCommands()
137 {
138 lout << endl << kBold << "List of commands:" << endl;
139 PrintDescription(lout, true);
140 return true;
141 }
142
143 // returns whether a command should be put into the history
144 bool Process(const std::string &str)
145 {
146 if (str.substr(0, 2)=="h " || str.substr(0, 5)=="help ")
147 {
148 const size_t p1 = str.find_first_of(' ');
149 const string svc = str.substr(p1+1);
150
151 const size_t p3 = svc.find_first_of('/');
152 const string s = svc.substr(0, p3);
153 const string c = p3==string::npos?"":svc.substr(p3+1);
154
155 lout << endl;
156 if (!fCurrentServer.empty())
157 {
158 if (PrintDescription(lout, true, fCurrentServer, svc)==0)
159 lout << " " << svc << ": <not found>" << endl;
160 }
161 else
162 {
163 if (PrintDescription(lout, true, s, c)==0)
164 lout << " <no matches found>" <<endl;
165 }
166
167 return true;
168 }
169
170 if (ReadlineColor::Process(lout, str))
171 return true;
172
173 if (T::Process(str))
174 return true;
175
176 if (str=="services" || str=="svc")
177 {
178 PrintDescription(lout, false);
179 return true;
180 }
181
182 if (str=="states" || str=="st")
183 {
184 PrintStates(lout);
185 return true;
186 }
187
188 return ProcessCommand(str);
189 }
190};
191
192
193
194// **************************************************************************
195/** @class RemoteConsole
196
197@brief Derives the RemoteControl from Control and adds a proper prompt
198
199This is basically a RemoteControl, which derives through the template
200argument from the Console class. It enhances the functionality of
201the remote control with a proper updated prompt.
202
203 */
204// **************************************************************************
205#include "Console.h"
206
207class RemoteConsole : public RemoteControl<Console>
208{
209public:
210 RemoteConsole(const char *name, bool continous=false) :
211 RemoteControl<Console>(name)
212 {
213 SetContinous(continous);
214 }
215 string GetUpdatePrompt() const;
216};
217
218// **************************************************************************
219/** @class RemoteShell
220
221@brief Derives the RemoteControl from Shell and adds colored prompt
222
223This is basically a RemoteControl, which derives through the template
224argument from the Shell class. It enhances the functionality of
225the local control with a proper updated prompt.
226
227 */
228// **************************************************************************
229#include "Shell.h"
230
231class RemoteShell : public RemoteControl<Shell>
232{
233public:
234 RemoteShell(const char *name, bool = false) :
235 RemoteControl<Shell>(name)
236 {
237 }
238 string GetUpdatePrompt() const;
239};
240
241#endif
Note: See TracBrowser for help on using the repository browser.