1 | #ifndef FACT_DimCtrl
|
---|
2 | #define FACT_DimCtrl
|
---|
3 |
|
---|
4 | #include "Main.h"
|
---|
5 | #include "tools.h"
|
---|
6 | #include "MessageDim.h"
|
---|
7 | #include "RemoteControl.h"
|
---|
8 |
|
---|
9 | using namespace std;
|
---|
10 |
|
---|
11 | // ========================================================================
|
---|
12 |
|
---|
13 |
|
---|
14 | /*
|
---|
15 | Extract usage clause(s) [if any] for SYNOPSIS.
|
---|
16 | Translators: "Usage" and "or" here are patterns (regular expressions) which
|
---|
17 | are used to match the usage synopsis in program output. An example from cp
|
---|
18 | (GNU coreutils) which contains both strings:
|
---|
19 | Usage: cp [OPTION]... [-T] SOURCE DEST
|
---|
20 | or: cp [OPTION]... SOURCE... DIRECTORY
|
---|
21 | or: cp [OPTION]... -t DIRECTORY SOURCE...
|
---|
22 | */
|
---|
23 | void PrintUsage()
|
---|
24 | {
|
---|
25 | cout << "\n"
|
---|
26 | "The console connects to all available Dim Servers and allows to "
|
---|
27 | "easily access all of their commands.\n"
|
---|
28 | "\n"
|
---|
29 | "Usage: dimctrl [-c type] [OPTIONS]\n"
|
---|
30 | " or: dimctrl [OPTIONS]\n\n";
|
---|
31 | cout << endl;
|
---|
32 | }
|
---|
33 |
|
---|
34 | void PrintHelp()
|
---|
35 | {
|
---|
36 | Main::PrintUsage();
|
---|
37 | }
|
---|
38 |
|
---|
39 | // A simple dummy state machine
|
---|
40 | class DimCtrl : public MainImp, public DimCommandHandler, public MessageDimTX
|
---|
41 | {
|
---|
42 | int fLabel;
|
---|
43 | int fStop;
|
---|
44 | int fVerbosity;
|
---|
45 | bool fDebug;
|
---|
46 | bool fIsServer;
|
---|
47 |
|
---|
48 | DimDescribedService fSrvState;
|
---|
49 | DimCommand fDimStart;
|
---|
50 | DimCommand fDimStop;
|
---|
51 |
|
---|
52 | map<string,string> fData;
|
---|
53 | string fScript;
|
---|
54 |
|
---|
55 | void ProcessStart()
|
---|
56 | {
|
---|
57 | if (!fScript.empty() || fLabel>=0)
|
---|
58 | {
|
---|
59 | Error("Script execution still in progress.");
|
---|
60 | return;
|
---|
61 | }
|
---|
62 |
|
---|
63 | string opt(fDimStart.getString());
|
---|
64 |
|
---|
65 | fData = Tools::Split(opt);
|
---|
66 |
|
---|
67 | if (opt.size()>0)
|
---|
68 | Debug("Start '"+opt+"' received.");
|
---|
69 |
|
---|
70 | if (fDebug)
|
---|
71 | Debug("Received data: "+string(fDimStart.getString()));
|
---|
72 |
|
---|
73 | if (opt.size()==0)
|
---|
74 | {
|
---|
75 | if (fData.size()==0)
|
---|
76 | Error("File name missing in DIM_CONTROL/START");
|
---|
77 | else
|
---|
78 | Error("Equal sign missing in argument '"+fData.begin()->first+"'");
|
---|
79 |
|
---|
80 | return;
|
---|
81 | }
|
---|
82 |
|
---|
83 | if (fDebug)
|
---|
84 | {
|
---|
85 | for (auto it=fData.begin(); it!=fData.end(); it++)
|
---|
86 | Debug(" Arg: "+it->first+" = "+it->second);
|
---|
87 | }
|
---|
88 |
|
---|
89 | fScript = opt;
|
---|
90 | }
|
---|
91 |
|
---|
92 | void commandHandler()
|
---|
93 | {
|
---|
94 | if (getCommand()==&fDimStop)
|
---|
95 | {
|
---|
96 | Debug("Stop received");
|
---|
97 | Readline::SetLabel(0);
|
---|
98 | }
|
---|
99 |
|
---|
100 | if (getCommand()==&fDimStart)
|
---|
101 | ProcessStart();
|
---|
102 | }
|
---|
103 |
|
---|
104 | public:
|
---|
105 | DimCtrl(ostream &out=cout) : MessageDimTX("DIM_CONTROL", out),
|
---|
106 | fLabel(-3), fStop(false), fVerbosity(0), fDebug(false), fIsServer(false),
|
---|
107 | fSrvState("DIM_CONTROL/STATE", "C",
|
---|
108 | "Provides the state of the state machine as quality of service."
|
---|
109 | "|Text[string]:A human readable string sent by the last state change."),
|
---|
110 | fDimStart("DIM_CONTROL/START", "C", this),
|
---|
111 | fDimStop("DIM_CONTROL/STOP", "", this)
|
---|
112 | {
|
---|
113 | }
|
---|
114 | ~DimCtrl()
|
---|
115 | {
|
---|
116 | DimServer::stop();
|
---|
117 | }
|
---|
118 |
|
---|
119 | bool check(string &str)
|
---|
120 | {
|
---|
121 | for (auto c=str.begin(); c<str.end(); c++)
|
---|
122 | {
|
---|
123 | if ((*c>='A' && *c<='Z') || *c=='_')
|
---|
124 | continue;
|
---|
125 |
|
---|
126 | if (*c++!=':')
|
---|
127 | return false;
|
---|
128 |
|
---|
129 | if (c==str.end())
|
---|
130 | return false;
|
---|
131 |
|
---|
132 | if (*c!=' ')
|
---|
133 | return false;
|
---|
134 |
|
---|
135 | str = string(c+1, str.end());
|
---|
136 | return true;
|
---|
137 | }
|
---|
138 |
|
---|
139 | return false;
|
---|
140 | }
|
---|
141 |
|
---|
142 | string Line(const string &txt, char fill)
|
---|
143 | {
|
---|
144 | const int n = (55-txt.length())/2;
|
---|
145 |
|
---|
146 | ostringstream out;
|
---|
147 | out << setfill(fill);
|
---|
148 | out << setw(n) << fill << ' ';
|
---|
149 | out << txt;
|
---|
150 | out << ' ' << setw(n) << fill;
|
---|
151 |
|
---|
152 | if (2*n+txt.length()+2 != 57)
|
---|
153 | out << fill;
|
---|
154 |
|
---|
155 | return out.str();
|
---|
156 | }
|
---|
157 |
|
---|
158 | int Write(const Time &time, const std::string &txt, int qos=kMessage)
|
---|
159 | {
|
---|
160 | if (txt=="")
|
---|
161 | {
|
---|
162 | fLabel = qos;
|
---|
163 |
|
---|
164 | if (fDebug)
|
---|
165 | {
|
---|
166 | string msg;
|
---|
167 | if (fLabel<0)
|
---|
168 | {
|
---|
169 | if (fLabel==-1)
|
---|
170 | msg = Line("Begin ["+fScript+"]", '=');
|
---|
171 | else
|
---|
172 | msg = Line("End ["+fScript+"]", '=');
|
---|
173 | }
|
---|
174 | else
|
---|
175 | {
|
---|
176 | ostringstream out;
|
---|
177 | out << "Label " << fLabel << " [" << fScript << "]";
|
---|
178 | msg = Line(out.str(), '-');
|
---|
179 | }
|
---|
180 |
|
---|
181 | MessageDimTX::Write(time, msg, 90);
|
---|
182 | }
|
---|
183 |
|
---|
184 | fSrvState.setQuality(fLabel);
|
---|
185 | switch (fLabel)
|
---|
186 | {
|
---|
187 | case -2: return fSrvState.Update(fScript+" [end]");
|
---|
188 | case -1: return fSrvState.Update(fScript+" [load]");
|
---|
189 | case 0: return fSrvState.Update(fScript+" [start]");
|
---|
190 | }
|
---|
191 |
|
---|
192 | ostringstream msg;
|
---|
193 | msg << fScript << " [label=" << fLabel << "]";
|
---|
194 | return fSrvState.Update(msg.str());
|
---|
195 | }
|
---|
196 |
|
---|
197 | if (qos<fVerbosity)
|
---|
198 | return 0;
|
---|
199 |
|
---|
200 | // Avoid recursions
|
---|
201 | if (fIsServer && txt.substr(0, 13)=="DIM_CONTROL: ")
|
---|
202 | return 0;
|
---|
203 |
|
---|
204 | // Don't send received messages via dim
|
---|
205 | string cpy(txt);
|
---|
206 | if (fIsServer && check(cpy))
|
---|
207 | return MessageImp::Write(time, cpy, qos);
|
---|
208 |
|
---|
209 | // Send all of our own messages via dim
|
---|
210 | return MessageDimTX::Write(time, txt, qos);
|
---|
211 | }
|
---|
212 |
|
---|
213 | int EvalOptions(Configuration &conf)
|
---|
214 | {
|
---|
215 | fVerbosity = 90;
|
---|
216 |
|
---|
217 | if (conf.Get<bool>("stop"))
|
---|
218 | return Dim::SendCommand("DIM_CONTROL/STOP") + 1;
|
---|
219 |
|
---|
220 | if (conf.Has("start"))
|
---|
221 | return Dim::SendCommand("DIM_CONTROL/START", conf.Get<string>("start")) + 1;
|
---|
222 |
|
---|
223 | fVerbosity = 40;
|
---|
224 |
|
---|
225 | fIsServer = conf.Get<bool>("server");
|
---|
226 | if (fIsServer)
|
---|
227 | {
|
---|
228 | // Sleep needed to ensure that the server can send
|
---|
229 | // an EXIT if another instance is already running
|
---|
230 | DimServer::start("DIM_CONTROL");
|
---|
231 | sleep(1);
|
---|
232 | }
|
---|
233 |
|
---|
234 | if (conf.Has("verbosity"))
|
---|
235 | fVerbosity = conf.Get<uint32_t>("verbosity");
|
---|
236 |
|
---|
237 | if (conf.Get<bool>("quiet"))
|
---|
238 | fVerbosity = 90;
|
---|
239 |
|
---|
240 | fDebug = conf.Get<bool>("debug");
|
---|
241 |
|
---|
242 | return -1;
|
---|
243 | }
|
---|
244 |
|
---|
245 | void Stop(int stop=1) { fStop = stop; Readline::SetLabel(0); }
|
---|
246 | int Run(bool)
|
---|
247 | {
|
---|
248 | while (!fStop)
|
---|
249 | {
|
---|
250 | const string s = fScript;
|
---|
251 | if (!s.empty())
|
---|
252 | {
|
---|
253 | Readline::Instance()->Execute(s, fData);
|
---|
254 | fScript = "";
|
---|
255 | }
|
---|
256 |
|
---|
257 | usleep(1000);
|
---|
258 | }
|
---|
259 | return fStop-1; }
|
---|
260 | };
|
---|
261 |
|
---|
262 | void SetupConfiguration(Configuration &conf)
|
---|
263 | {
|
---|
264 | po::options_description control("Dim Control");
|
---|
265 | control.add_options()
|
---|
266 | ("server", po_bool(false), "Start dimctrl as a dim server")
|
---|
267 | ("verbosity,v", var<uint32_t>()->implicit_value(0), "Set a new verbosity level (see MessageImp)")
|
---|
268 | ("quiet,q", po_bool(false), "Suppress all output except comments (log-level>=90)")
|
---|
269 | ("debug", po_bool(false), "Print the labels for debugging purpose")
|
---|
270 | ("start", var<string>(), "")
|
---|
271 | ("stop", po_switch(), "")
|
---|
272 | ;
|
---|
273 |
|
---|
274 | conf.AddOptions(control);
|
---|
275 | }
|
---|
276 |
|
---|
277 | int main(int argc, const char *argv[])
|
---|
278 | {
|
---|
279 | Configuration conf(argv[0]);
|
---|
280 | conf.SetPrintUsage(PrintUsage);
|
---|
281 | Main::SetupConfiguration(conf);
|
---|
282 | SetupConfiguration(conf);
|
---|
283 |
|
---|
284 | if (!conf.DoParse(argc, argv, PrintHelp))
|
---|
285 | return -1;
|
---|
286 |
|
---|
287 | if (!conf.Has("console"))
|
---|
288 | return Main::execute<RemoteStream, DimCtrl>(conf);
|
---|
289 |
|
---|
290 | if (conf.Get<int>("console")==0)
|
---|
291 | return Main::execute<RemoteShell, DimCtrl>(conf);
|
---|
292 | else
|
---|
293 | return Main::execute<RemoteConsole, DimCtrl>(conf);
|
---|
294 |
|
---|
295 | return 0;
|
---|
296 | }
|
---|
297 | #endif
|
---|