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

Last change on this file since 10429 was 10429, checked in by tbretz, 9 years ago
Moved the tools function into their own namespace to get rid of problems whenlinking with root.
File size: 7.0 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
68    // This funtion defines which generator should be called.
69    // If it returns 0 the standard reaqdline generator are called.
70    // Otherwise set the right generator with rl_completion_matches.
71    char **Completion(const char *text, int start, int)
72    {
73        // Get the whole buffer before the tab-position
74        const string b = string(T::GetBuffer());
75        const string s = b.substr(0, start);
76        const string l = Tools::Trim(s.c_str());
77        if (l.empty())
78        {
79            if (fCurrentServer.empty())
80            {
81                const size_t p1 = b.find_first_of(' ');
82                const size_t p2 = b.find_first_of('/');
83
84                if (p1==string::npos && p2!=string::npos)
85                    return T::Complete(GetCommandList(), text);
86
87                std::vector<std::string> v = GetServerList();
88                for_each(v.begin(), v.end(), RemoteControl::append);
89                return T::Complete(v, text);
90            }
91            else
92                return T::Complete(GetCommandList(fCurrentServer), text);
93        }
94        return T::Complete(GetCommandList(l), text);
95    }
96
97    void infoHandler()
98    {
99        RemoteControlImp::infoHandler();
100        if (!fCurrentServer.empty() && !HasServer(fCurrentServer))
101        {
102            fCurrentServer = "";
103            T::UpdatePrompt();
104        }
105    }
106
107public:
108    // Redirect asynchronous output to the output window
109    RemoteControl(const char *name) : T(name),
110        RemoteControlImp(T::GetStreamOut(), T::GetStreamIn())
111    {
112    }
113
114    bool PrintGeneralHelp()
115    {
116        T::PrintGeneralHelp();
117        lout << " " << kUnderline << "Specific commands:" << endl;
118        lout << kBold << "   h,help <arg> " << kReset << "List help text for given server or command." << endl;
119//        lout << kBold << "   s,servers    " << kReset << "List all servers which are connected." << endl;
120        lout << kBold << "   svc,services " << kReset << "List all services in the network." << endl;
121        lout << kBold << "   st,states    " << kReset << "List all states in the network." << endl;
122        lout << endl;
123        return true;
124    }
125
126    bool PrintCommands()
127    {
128        lout << endl << kBold << "List of commands:" << endl;
129        PrintDescription(lout, true);
130        return true;
131    }
132
133    // returns whether a command should be put into the history
134    bool Process(const std::string &str)
135    {
136        if (str.substr(0, 2)=="h " || str.substr(0, 5)=="help ")
137        {
138            const size_t p1 = str.find_first_of(' ');
139            const string svc = str.substr(p1+1);
140
141            const size_t p3 = svc.find_first_of('/');
142            const string s = svc.substr(0, p3);
143            const string c = p3==string::npos?"":svc.substr(p3+1);
144
145            lout << endl;
146            if (!fCurrentServer.empty())
147            {
148                if (PrintDescription(lout, true, fCurrentServer, svc)==0)
149                    lout << "   " << svc << ": <not found>" << endl;
150            }
151            else
152            {
153                if (PrintDescription(lout, true, s, c)==0)
154                    lout << "   <no matches found>" <<endl;
155            }
156
157            return true;
158        }
159
160        if (ReadlineColor::Process(lout, str))
161            return true;
162
163        if (T::Process(str))
164            return true;
165
166        if (str=="services" || str=="svc")
167        {
168            PrintDescription(lout, false);
169            return true;
170        }
171
172        if (str=="states" || str=="st")
173        {
174            PrintStates(lout);
175            return true;
176        }
177
178        return ProcessCommand(str);
179    }
180};
181
182
183
184// **************************************************************************
185/** @class RemoteConsole
186
187@brief Derives the RemoteControl from Control and adds a proper prompt
188
189This is basically a RemoteControl, which derives through the template
190argument from the Console class. It enhances the functionality of
191the remote control with a proper updated prompt.
192
193 */
194// **************************************************************************
195#include "Console.h"
196
197class RemoteConsole : public RemoteControl<Console>
198{
199public:
200    RemoteConsole(const char *name, bool continous=false) :
201        RemoteControl<Console>(name)
202    {
203        SetContinous(continous);
204    }
205    string GetUpdatePrompt() const;
206};
207
208// **************************************************************************
209/** @class RemoteShell
210
211@brief Derives the RemoteControl from Shell and adds colored prompt
212
213This is basically a RemoteControl, which derives through the template
214argument from the Shell class. It enhances the functionality of
215the local control with a proper updated prompt.
216
217 */
218// **************************************************************************
219#include "Shell.h"
220
221class RemoteShell : public RemoteControl<Shell>
222{
223public:
224    RemoteShell(const char *name, bool = false) :
225        RemoteControl<Shell>(name)
226    {
227    }
228    string GetUpdatePrompt() const;
229};
230
231#endif
Note: See TracBrowser for help on using the repository browser.