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

Last change on this file since 10312 was 10312, checked in by tbretz, 14 years ago
Some updates to the output of help messages.
File size: 7.4 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#include "ReadlineColor.h"
106
107template <class T>
108class RemoteControl : public T, public RemoteControlImp
109{
110private:
111 std::ostream &lout; /// Output stream for local synchrounous output
112
113 // This funtion defines which generator should be called.
114 // If it returns 0 the standard reaqdline generator are called.
115 // Otherwise set the right generator with rl_completion_matches.
116 char **Completion(const char *text, int start, int)
117 {
118 // Get the whole buffer before the tab-position
119 const string s = string(T::GetBuffer(), 0, start);
120 const string l = T::TrimSpaces(s.c_str());
121 if (l.empty())
122 {
123 if (fCurrentServer.empty())
124 return T::Complete(fCommandList.GetServerList(), text);
125 else
126 return T::Complete(fCommandList.GetServiceList(fCurrentServer), text);
127 }
128 return T::Complete(fCommandList.GetServiceList(l), text);
129 }
130
131 void infoHandler()
132 {
133 RemoteControlImp::infoHandler();
134 if (!fCurrentServer.empty() && !fCommandList.HasServer(fCurrentServer))
135 {
136 fCurrentServer = "";
137 T::UpdatePrompt();
138 }
139 }
140
141public:
142 // Redirect asynchronous output to the output window
143 RemoteControl(const char *name) : T(name),
144 RemoteControlImp(T::GetStreamOut(), T::GetStreamIn()),
145 lout(T::GetStreamIn())
146 {
147 }
148
149
150 bool PrintGeneralHelp()
151 {
152 T::PrintGeneralHelp();
153 lout << " " << kUnderline << "Specific commands:" << endl;
154 lout << kBold << " s,servers " << kReset << "List all servers which are connected." << endl;
155 lout << kBold << " svc,services " << kReset << "List all services in the network." << endl;
156 lout << endl;
157 return true;
158 }
159
160 bool PrintCommands()
161 {
162 lout << endl << kBold << "List of commands:" << endl;
163 fCommandList.PrintDescription(lout);
164 return true;
165 }
166
167 // returns whether a command should be put into the history
168 bool Process(const std::string &str)
169 {
170 if (ReadlineColor::Process(lout, str))
171 return true;
172
173 if (T::Process(str))
174 return true;
175
176 if (str=="servers" || str=="s")
177 {
178 fCommandList.PrintServerList(lout);
179 return true;
180 }
181
182 if (str=="services" || str=="svc")
183 {
184 fServiceList.PrintDescription(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.