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

Last change on this file since 10183 was 10183, checked in by tbretz, 9 years ago
New import.
File size: 7.3 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 fServiceList;    /// An up-to-date list of all available commands
67    ClientList  fClientList;     /// A list with all MESSAGE services to which we subscribed
68
69    std::string fCurrentServer;  /// The server to which we currently cd'ed
70
71    void infoHandler();
72
73protected:
74    // Redirect asynchronous output to the output window
75    RemoteControlImp(std::ostream &out, std::ostream &in) :
76        MessageImp(out),
77        DimErrorRedirecter(static_cast<MessageImp&>(*this)),
78        lout(in), fServiceList("CMD", out)
79    {
80        fServiceList.SetHandler(this);
81    }
82    bool ProcessCommand(const std::string &str);
83};
84
85// **************************************************************************
86/** @class RemoteControl
87
88@brief Implements a remote control based on a Readline class for the dim network
89
90This template implements all functions which overwrite any function from the
91Readline class. Since several derivatives of the Readline class implement
92different kind of Readline access, this class can be derived by any of them
93due to its template argument. However, the normal case will be
94deriving it from either Console or Shell.
95
96@tparam T
97   The base class for RemoteControl. Either Readlien or a class
98    deriving from it. This is usually either Console or Shell.
99
100**/
101// **************************************************************************
102#include "WindowLog.h"
103
104template <class T>
105class RemoteControl : public T, public RemoteControlImp
106{
107private:
108    std::ostream &lout; /// Output stream for local synchrounous output
109
110    // This funtion defines which generator should be called.
111    // If it returns 0 the standard reaqdline generator are called.
112    // Otherwise set the right generator with rl_completion_matches.
113    char **Completion(const char *text, int start, int)
114    {
115        // Get the whole buffer before the tab-position
116        const string s = string(T::GetBuffer(), 0, start);
117        const string l = T::TrimSpaces(s.c_str());
118        if (l.empty())
119        {
120            if (fCurrentServer.empty())
121                return T::Complete(fServiceList.GetServerList(), text);
122            else
123                return T::Complete(fServiceList.GetServiceList(fCurrentServer), text);
124        }
125        return T::Complete(fServiceList.GetServiceList(l), text);
126    }
127
128    void infoHandler()
129    {
130        RemoteControlImp::infoHandler();
131        if (!fCurrentServer.empty() && !fServiceList.HasServer(fCurrentServer))
132        {
133            fCurrentServer = "";
134            T::UpdatePrompt();
135        }
136    }
137
138public:
139    // Redirect asynchronous output to the output window
140    RemoteControl(const char *name) : T(name),
141        RemoteControlImp(T::GetStreamOut(), T::GetStreamIn()),
142        lout(T::GetStreamIn())
143    {
144        fServiceList.SetHandler(this);
145    }
146
147
148    bool PrintGeneralHelp()
149    {
150        T::PrintGeneralHelp();
151        lout << " " << kUnderline << "Specific commands:" << endl;
152        lout << kBold << "  s,servers   " << kReset << "List all servers which are connected." << endl;
153        lout << endl;
154        return true;
155    }
156
157    bool PrintCommands()
158    {
159        lout << endl << kBold << "List of commands:" << endl;
160        fServiceList.PrintServiceList(lout);
161        return true;
162    }
163
164    // returns whether a command should be put into the history
165    bool Process(const std::string &str)
166    {
167        if (T::Process(str))
168            return true;
169
170        if (str=="servers" || str=="s")
171        {
172            fServiceList.PrintServerList(lout);
173            return true;
174        }
175
176        return ProcessCommand(str);
177    }
178    void Run(const char * = 0)
179    {
180        lout << endl;
181        lout << kBlue << kBold << "You are on the terminal of the MCP -" << endl;
182        lout << kBlue << kBold << "the Master Control Program." << endl;
183        lout << endl;
184        lout << kBlue << kBold << "Hello Flynn..." << endl;
185        lout << endl;
186
187        T::Run();
188    }
189};
190
191
192
193// **************************************************************************
194/** @class RemoteConsole
195
196@brief Derives the RemoteControl from Control and adds a proper prompt
197
198This is basically a RemoteControl, which derives through the template
199argument from the Console class. It enhances the functionality of
200the remote control with a proper updated prompt.
201
202 */
203// **************************************************************************
204#include "Console.h"
205
206class RemoteConsole : public RemoteControl<Console>
207{
208public:
209    RemoteConsole(const char *name, bool continous=false) :
210        RemoteControl<Console>(name)
211    {
212        SetContinous(continous);
213    }
214    string GetUpdatePrompt() const;
215};
216
217// **************************************************************************
218/** @class RemoteShell
219
220@brief Derives the RemoteControl from Shell and adds colored prompt
221
222This is basically a RemoteControl, which derives through the template
223argument from the Shell class. It enhances the functionality of
224the local control with a proper updated prompt.
225
226 */
227// **************************************************************************
228#include "Shell.h"
229
230class RemoteShell : public RemoteControl<Shell>
231{
232public:
233    RemoteShell(const char *name, bool = false) :
234        RemoteControl<Shell>(name)
235    {
236    }
237    string GetUpdatePrompt() const;
238};
239
240#endif
Note: See TracBrowser for help on using the repository browser.