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

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