1 | #include <boost/bind.hpp>
|
---|
2 | #include <boost/array.hpp>
|
---|
3 | #if BOOST_VERSION < 104400
|
---|
4 | #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 4))
|
---|
5 | #undef BOOST_HAS_RVALUE_REFS
|
---|
6 | #endif
|
---|
7 | #endif
|
---|
8 | #include <boost/thread.hpp>
|
---|
9 | #include <boost/asio/error.hpp>
|
---|
10 | #include <boost/asio/deadline_timer.hpp>
|
---|
11 |
|
---|
12 | #include "Dim.h"
|
---|
13 | #include "Event.h"
|
---|
14 | #include "Shell.h"
|
---|
15 | #include "StateMachineDim.h"
|
---|
16 | #include "Connection.h"
|
---|
17 | #include "Configuration.h"
|
---|
18 | #include "Console.h"
|
---|
19 | #include "Converter.h"
|
---|
20 |
|
---|
21 | #include "tools.h"
|
---|
22 |
|
---|
23 | #include "LocalControl.h"
|
---|
24 |
|
---|
25 |
|
---|
26 | namespace ba = boost::asio;
|
---|
27 | namespace bs = boost::system;
|
---|
28 | namespace dummy = ba::placeholders;
|
---|
29 |
|
---|
30 | using namespace std;
|
---|
31 |
|
---|
32 | // ------------------------------------------------------------------------
|
---|
33 |
|
---|
34 | class ConnectionFSC : public Connection
|
---|
35 | {
|
---|
36 | boost::asio::streambuf fBuffer;
|
---|
37 |
|
---|
38 | bool fIsVerbose;
|
---|
39 |
|
---|
40 | protected:
|
---|
41 |
|
---|
42 | virtual void UpdateTemp(float, const vector<float> &)
|
---|
43 | {
|
---|
44 | }
|
---|
45 |
|
---|
46 | virtual void UpdateHum(float, const vector<float>&)
|
---|
47 | {
|
---|
48 | }
|
---|
49 |
|
---|
50 | virtual void UpdateVolt(float, const vector<float>&)
|
---|
51 | {
|
---|
52 | }
|
---|
53 |
|
---|
54 | virtual void UpdateCur(float, const vector<float>&)
|
---|
55 | {
|
---|
56 | }
|
---|
57 |
|
---|
58 | /*
|
---|
59 | virtual void UpdateError()
|
---|
60 | {
|
---|
61 | if (!fIsVerbose)
|
---|
62 | return;
|
---|
63 |
|
---|
64 | Out() << endl << kRed << "Error received:" << endl;
|
---|
65 | Out() << fError;
|
---|
66 | if (fIsHexOutput)
|
---|
67 | Out() << Converter::GetHex<uint16_t>(fError, 16) << endl;
|
---|
68 | }
|
---|
69 | */
|
---|
70 |
|
---|
71 | private:
|
---|
72 | void HandleReceivedData(const bs::error_code& err, size_t bytes_received, int /*type*/)
|
---|
73 | {
|
---|
74 | // Do not schedule a new read if the connection failed.
|
---|
75 | if (bytes_received==0 || err)
|
---|
76 | {
|
---|
77 | if (err==ba::error::eof)
|
---|
78 | Warn("Connection closed by remote host (FTM).");
|
---|
79 |
|
---|
80 | // 107: Transport endpoint is not connected (bs::error_code(107, bs::system_category))
|
---|
81 | // 125: Operation canceled
|
---|
82 | if (err && err!=ba::error::eof && // Connection closed by remote host
|
---|
83 | err!=ba::error::basic_errors::not_connected && // Connection closed by remote host
|
---|
84 | err!=ba::error::basic_errors::operation_aborted) // Connection closed by us
|
---|
85 | {
|
---|
86 | ostringstream str;
|
---|
87 | str << "Reading from " << URL() << ": " << err.message() << " (" << err << ")";// << endl;
|
---|
88 | Error(str);
|
---|
89 | }
|
---|
90 | PostClose(err!=ba::error::basic_errors::operation_aborted);
|
---|
91 | return;
|
---|
92 | }
|
---|
93 |
|
---|
94 | if (fIsVerbose)
|
---|
95 | Out() << kBold << "Received:" << endl;
|
---|
96 |
|
---|
97 | istream is(&fBuffer);
|
---|
98 |
|
---|
99 | int state = 0;
|
---|
100 | bool values = false;
|
---|
101 | int offset = 0;
|
---|
102 | /*
|
---|
103 | string buffer;
|
---|
104 | while (getline(is, buffer, '\n'))
|
---|
105 | {
|
---|
106 | if (fIsVerbose)
|
---|
107 | Out() << buffer << endl;
|
---|
108 |
|
---|
109 | buffer = Tools::Trim(buffer);
|
---|
110 |
|
---|
111 | if (buffer.empty())
|
---|
112 | continue;
|
---|
113 |
|
---|
114 | if (buffer.substr(0, 4)=="end.")
|
---|
115 | break;
|
---|
116 |
|
---|
117 | if (buffer.substr(0, 8)=="status: ")
|
---|
118 | {
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (buffer.substr(0, 8)=="time_s: ")
|
---|
122 | {
|
---|
123 | }
|
---|
124 |
|
---|
125 | if (buffer.substr(0, 8)=="VOLTAGES")
|
---|
126 | state = 1;
|
---|
127 |
|
---|
128 | if (buffer.substr(0, 11)=="RESISTANCES")
|
---|
129 | state = 2;
|
---|
130 |
|
---|
131 | if (state==1 && buffer.substr(0, 7)=="values:")
|
---|
132 | {
|
---|
133 | }
|
---|
134 |
|
---|
135 | if (state==2 && buffer.substr(0, 7)=="values:")
|
---|
136 | {
|
---|
137 | values = true;
|
---|
138 | continue;
|
---|
139 | }
|
---|
140 |
|
---|
141 | istringtream str(buffer);
|
---|
142 | for (int i=0; i<8; i++)
|
---|
143 | {
|
---|
144 | float f;
|
---|
145 | str >> f;
|
---|
146 | offset += 8;
|
---|
147 | }
|
---|
148 | }
|
---|
149 | */
|
---|
150 | /*
|
---|
151 | "status: 00000538 \n"
|
---|
152 | "time_s: 764.755 \n"
|
---|
153 | "VOLTAGES \n"
|
---|
154 | " \n"
|
---|
155 | "enable:11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 00001111 \n"
|
---|
156 | " done:11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 00001111 \n"
|
---|
157 | "values:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 00 0 0 0 0 0 0 0 0 \n"
|
---|
158 | "RESISTANCES \n"
|
---|
159 | " \n"
|
---|
160 | "enable:11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 \n"
|
---|
161 | " done:11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111 \n"
|
---|
162 | "values: \n"
|
---|
163 | "1000.16 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n"
|
---|
164 | "3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n"
|
---|
165 | "1197.07 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n"
|
---|
166 | " 558.59 677.92 817.26 989.39 1200.35 1503.06 1799.90 2204.18 \n"
|
---|
167 | "3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n"
|
---|
168 | "3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n"
|
---|
169 | "3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n"
|
---|
170 | "3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 3199.99 \n"
|
---|
171 | "end.\n";
|
---|
172 |
|
---|
173 | */
|
---|
174 | StartRead();
|
---|
175 | }
|
---|
176 |
|
---|
177 | void StartRead()
|
---|
178 | {
|
---|
179 | ba::async_read_until(*this, fBuffer, "end.\n",
|
---|
180 | boost::bind(&ConnectionFSC::HandleReceivedData, this,
|
---|
181 | dummy::error, dummy::bytes_transferred, 0));
|
---|
182 |
|
---|
183 | // FIXME: Add timeout here
|
---|
184 | }
|
---|
185 |
|
---|
186 | // This is called when a connection was established
|
---|
187 | void ConnectionEstablished()
|
---|
188 | {
|
---|
189 | PostMessage("m", 1);
|
---|
190 |
|
---|
191 | fBuffer.prepare(10000);
|
---|
192 | StartRead();
|
---|
193 | }
|
---|
194 |
|
---|
195 | /*
|
---|
196 | void HandleReadTimeout(const bs::error_code &error)
|
---|
197 | {
|
---|
198 | if (error==ba::error::basic_errors::operation_aborted)
|
---|
199 | return;
|
---|
200 |
|
---|
201 | if (error)
|
---|
202 | {
|
---|
203 | ostringstream str;
|
---|
204 | str << "Read timeout of " << URL() << ": " << error.message() << " (" << error << ")";// << endl;
|
---|
205 | Error(str);
|
---|
206 |
|
---|
207 | PostClose();
|
---|
208 | return;
|
---|
209 |
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (!is_open())
|
---|
213 | {
|
---|
214 | // For example: Here we could schedule a new accept if we
|
---|
215 | // would not want to allow two connections at the same time.
|
---|
216 | return;
|
---|
217 | }
|
---|
218 |
|
---|
219 | // Check whether the deadline has passed. We compare the deadline
|
---|
220 | // against the current time since a new asynchronous operation
|
---|
221 | // may have moved the deadline before this actor had a chance
|
---|
222 | // to run.
|
---|
223 | if (fInTimeout.expires_at() > ba::deadline_timer::traits_type::now())
|
---|
224 | return;
|
---|
225 |
|
---|
226 | Error("Timeout reading data from "+URL());
|
---|
227 |
|
---|
228 | PostClose();
|
---|
229 | }
|
---|
230 | */
|
---|
231 |
|
---|
232 | public:
|
---|
233 | ConnectionFSC(ba::io_service& ioservice, MessageImp &imp) : Connection(ioservice, imp()),
|
---|
234 | fIsVerbose(true)
|
---|
235 | {
|
---|
236 | SetLogStream(&imp);
|
---|
237 | }
|
---|
238 |
|
---|
239 | void SetVerbose(bool b)
|
---|
240 | {
|
---|
241 | fIsVerbose = b;
|
---|
242 | }
|
---|
243 | };
|
---|
244 |
|
---|
245 | // ------------------------------------------------------------------------
|
---|
246 |
|
---|
247 | #include "DimDescriptionService.h"
|
---|
248 |
|
---|
249 | class ConnectionDimFSC : public ConnectionFSC
|
---|
250 | {
|
---|
251 | private:
|
---|
252 |
|
---|
253 | DimDescribedService fDimTemp;
|
---|
254 | DimDescribedService fDimHum;
|
---|
255 | DimDescribedService fDimVolt;
|
---|
256 | DimDescribedService fDimCurrent;
|
---|
257 |
|
---|
258 | void Update(DimDescribedService &svc, vector<float> data, float time) const
|
---|
259 | {
|
---|
260 | data.insert(data.begin(), time);
|
---|
261 | svc.setData(data.data(), data.size()*sizeof(float));
|
---|
262 | svc.updateService();
|
---|
263 | }
|
---|
264 |
|
---|
265 | void UpdateTemp(float time, const vector<float> &temp)
|
---|
266 | {
|
---|
267 | Update(fDimTemp, temp, time);
|
---|
268 | }
|
---|
269 |
|
---|
270 | void UpdateHum(float time, const vector<float> &hum)
|
---|
271 | {
|
---|
272 | Update(fDimHum, hum, time);
|
---|
273 | }
|
---|
274 |
|
---|
275 | void UpdateVolt(float time, const vector<float> &volt)
|
---|
276 | {
|
---|
277 | Update(fDimVolt, volt, time);
|
---|
278 | }
|
---|
279 |
|
---|
280 | void UpdateCur(float time, const vector<float> &curr)
|
---|
281 | {
|
---|
282 | Update(fDimCurrent, curr, time);
|
---|
283 | }
|
---|
284 |
|
---|
285 | public:
|
---|
286 | ConnectionDimFSC(ba::io_service& ioservice, MessageImp &imp) :
|
---|
287 | ConnectionFSC(ioservice, imp),
|
---|
288 | fDimTemp ("FSC_CONTROL/TEMPERATURE", "F:1;F:64", ""),
|
---|
289 | fDimHum ("FSC_CONTROL/HUMIDITY", "F:1;F:40", ""),
|
---|
290 | fDimVolt ("FSC_CONTROL/VOLTAGE", "F:1;F:40", ""),
|
---|
291 | fDimCurrent("FSC_CONTROL/CURRENT", "F:1;F:4", "")
|
---|
292 | {
|
---|
293 | }
|
---|
294 |
|
---|
295 | // A B [C] [D] E [F] G H [I] J K [L] M N O P Q R [S] T U V W [X] Y Z
|
---|
296 | };
|
---|
297 |
|
---|
298 | // ------------------------------------------------------------------------
|
---|
299 |
|
---|
300 | template <class T, class S>
|
---|
301 | class StateMachineFSC : public T, public ba::io_service, public ba::io_service::work
|
---|
302 | {
|
---|
303 | int Wrap(boost::function<void()> f)
|
---|
304 | {
|
---|
305 | f();
|
---|
306 | return T::GetCurrentState();
|
---|
307 | }
|
---|
308 |
|
---|
309 | boost::function<int(const EventImp &)> Wrapper(boost::function<void()> func)
|
---|
310 | {
|
---|
311 | return boost::bind(&StateMachineFSC::Wrap, this, func);
|
---|
312 | }
|
---|
313 |
|
---|
314 | private:
|
---|
315 | S fFSC;
|
---|
316 |
|
---|
317 | enum states_t
|
---|
318 | {
|
---|
319 | kStateDisconnected = 1,
|
---|
320 | kStateConnected = 2,
|
---|
321 | };
|
---|
322 |
|
---|
323 | int Disconnect()
|
---|
324 | {
|
---|
325 | // Close all connections
|
---|
326 | fFSC.PostClose(false);
|
---|
327 |
|
---|
328 | /*
|
---|
329 | // Now wait until all connection have been closed and
|
---|
330 | // all pending handlers have been processed
|
---|
331 | poll();
|
---|
332 | */
|
---|
333 |
|
---|
334 | return T::GetCurrentState();
|
---|
335 | }
|
---|
336 |
|
---|
337 | int Reconnect(const EventImp &evt)
|
---|
338 | {
|
---|
339 | // Close all connections to supress the warning in SetEndpoint
|
---|
340 | fFSC.PostClose(false);
|
---|
341 |
|
---|
342 | // Now wait until all connection have been closed and
|
---|
343 | // all pending handlers have been processed
|
---|
344 | poll();
|
---|
345 |
|
---|
346 | if (evt.GetBool())
|
---|
347 | fFSC.SetEndpoint(evt.GetString());
|
---|
348 |
|
---|
349 | // Now we can reopen the connection
|
---|
350 | fFSC.PostClose(true);
|
---|
351 |
|
---|
352 | return T::GetCurrentState();
|
---|
353 | }
|
---|
354 |
|
---|
355 | int Execute()
|
---|
356 | {
|
---|
357 | // Dispatch (execute) at most one handler from the queue. In contrary
|
---|
358 | // to run_one(), it doesn't wait until a handler is available
|
---|
359 | // which can be dispatched, so poll_one() might return with 0
|
---|
360 | // handlers dispatched. The handlers are always dispatched/executed
|
---|
361 | // synchronously, i.e. within the call to poll_one()
|
---|
362 | poll_one();
|
---|
363 |
|
---|
364 | return fFSC.IsConnected() ? kStateConnected : kStateDisconnected;
|
---|
365 | }
|
---|
366 |
|
---|
367 | bool CheckEventSize(size_t has, const char *name, size_t size)
|
---|
368 | {
|
---|
369 | if (has==size)
|
---|
370 | return true;
|
---|
371 |
|
---|
372 | ostringstream msg;
|
---|
373 | msg << name << " - Received event has " << has << " bytes, but expected " << size << ".";
|
---|
374 | T::Fatal(msg);
|
---|
375 | return false;
|
---|
376 | }
|
---|
377 |
|
---|
378 | int SetVerbosity(const EventImp &evt)
|
---|
379 | {
|
---|
380 | if (!CheckEventSize(evt.GetSize(), "SetVerbosity", 1))
|
---|
381 | return T::kSM_FatalError;
|
---|
382 |
|
---|
383 | fFSC.SetVerbose(evt.GetBool());
|
---|
384 |
|
---|
385 | return T::GetCurrentState();
|
---|
386 | }
|
---|
387 |
|
---|
388 | public:
|
---|
389 | StateMachineFSC(ostream &out=cout) :
|
---|
390 | T(out, "FSC_CONTROL"), ba::io_service::work(static_cast<ba::io_service&>(*this)),
|
---|
391 | fFSC(*this, *this)
|
---|
392 | {
|
---|
393 | // ba::io_service::work is a kind of keep_alive for the loop.
|
---|
394 | // It prevents the io_service to go to stopped state, which
|
---|
395 | // would prevent any consecutive calls to run()
|
---|
396 | // or poll() to do nothing. reset() could also revoke to the
|
---|
397 | // previous state but this might introduce some overhead of
|
---|
398 | // deletion and creation of threads and more.
|
---|
399 |
|
---|
400 | // State names
|
---|
401 | AddStateName(kStateDisconnected, "Disconnected",
|
---|
402 | "FSC board not connected via ethernet.");
|
---|
403 |
|
---|
404 | AddStateName(kStateConnected, "Connected",
|
---|
405 | "Ethernet connection to FSC established.");
|
---|
406 |
|
---|
407 | // Verbosity commands
|
---|
408 | T::AddEvent("SET_VERBOSE", "B")
|
---|
409 | (boost::bind(&StateMachineFSC::SetVerbosity, this, _1))
|
---|
410 | ("set verbosity state"
|
---|
411 | "|verbosity[bool]:disable or enable verbosity for received data (yes/no), except dynamic data");
|
---|
412 |
|
---|
413 | // Conenction commands
|
---|
414 | AddEvent("DISCONNECT", kStateConnected)
|
---|
415 | (boost::bind(&StateMachineFSC::Disconnect, this))
|
---|
416 | ("disconnect from ethernet");
|
---|
417 |
|
---|
418 | AddEvent("RECONNECT", "O", kStateDisconnected, kStateConnected)
|
---|
419 | (boost::bind(&StateMachineFSC::Reconnect, this, _1))
|
---|
420 | ("(Re)connect ethernet connection to FTM, a new address can be given"
|
---|
421 | "|[host][string]:new ethernet address in the form <host:port>");
|
---|
422 |
|
---|
423 | fFSC.StartConnect();
|
---|
424 | }
|
---|
425 |
|
---|
426 | void SetEndpoint(const string &url)
|
---|
427 | {
|
---|
428 | fFSC.SetEndpoint(url);
|
---|
429 | }
|
---|
430 |
|
---|
431 | bool SetConfiguration(const Configuration &conf)
|
---|
432 | {
|
---|
433 | SetEndpoint(conf.Get<string>("addr"));
|
---|
434 |
|
---|
435 | fFSC.SetVerbose(!conf.Get<bool>("quiet"));
|
---|
436 |
|
---|
437 | return true;
|
---|
438 | }
|
---|
439 | };
|
---|
440 |
|
---|
441 | // ------------------------------------------------------------------------
|
---|
442 |
|
---|
443 | void RunThread(StateMachineImp *io_service)
|
---|
444 | {
|
---|
445 | // This is necessary so that the StateMachien Thread can signal the
|
---|
446 | // Readline to exit
|
---|
447 | io_service->Run();
|
---|
448 | Readline::Stop();
|
---|
449 | }
|
---|
450 |
|
---|
451 | template<class S, class T>
|
---|
452 | int RunDim(Configuration &conf)
|
---|
453 | {
|
---|
454 | WindowLog wout;
|
---|
455 |
|
---|
456 | /*
|
---|
457 | static Test shell(conf.GetName().c_str(), conf.Get<int>("console")!=1);
|
---|
458 |
|
---|
459 | WindowLog &win = shell.GetStreamIn();
|
---|
460 | WindowLog &wout = shell.GetStreamOut();
|
---|
461 | */
|
---|
462 |
|
---|
463 | ReadlineColor::PrintBootMsg(wout, conf.GetName(), false);
|
---|
464 |
|
---|
465 |
|
---|
466 | if (conf.Has("log"))
|
---|
467 | if (!wout.OpenLogFile(conf.Get<string>("log")))
|
---|
468 | wout << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
|
---|
469 |
|
---|
470 | // Start io_service.Run to use the StateMachineImp::Run() loop
|
---|
471 | // Start io_service.run to only use the commandHandler command detaching
|
---|
472 | StateMachineFSC<S, T> io_service(wout);
|
---|
473 | if (!io_service.SetConfiguration(conf))
|
---|
474 | return -1;
|
---|
475 |
|
---|
476 | io_service.Run();
|
---|
477 |
|
---|
478 | /*
|
---|
479 | shell.SetReceiver(io_service);
|
---|
480 |
|
---|
481 | boost::thread t(boost::bind(RunThread, &io_service));
|
---|
482 | // boost::thread t(boost::bind(&StateMachineFSC<S>::Run, &io_service));
|
---|
483 |
|
---|
484 | shell.Run(); // Run the shell
|
---|
485 | io_service.Stop(); // Signal Loop-thread to stop
|
---|
486 | // io_service.Close(); // Obsolete, done by the destructor
|
---|
487 |
|
---|
488 | // Wait until the StateMachine has finished its thread
|
---|
489 | // before returning and destroying the dim objects which might
|
---|
490 | // still be in use.
|
---|
491 | t.join();
|
---|
492 | */
|
---|
493 |
|
---|
494 | return 0;
|
---|
495 | }
|
---|
496 |
|
---|
497 | template<class T, class S, class R>
|
---|
498 | int RunShell(Configuration &conf)
|
---|
499 | {
|
---|
500 | static T shell(conf.GetName().c_str(), conf.Get<int>("console")!=1);
|
---|
501 |
|
---|
502 | WindowLog &win = shell.GetStreamIn();
|
---|
503 | WindowLog &wout = shell.GetStreamOut();
|
---|
504 |
|
---|
505 | if (conf.Has("log"))
|
---|
506 | if (!wout.OpenLogFile(conf.Get<string>("log")))
|
---|
507 | win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
|
---|
508 |
|
---|
509 | StateMachineFSC<S, R> io_service(wout);
|
---|
510 | if (!io_service.SetConfiguration(conf))
|
---|
511 | return -1;
|
---|
512 |
|
---|
513 | shell.SetReceiver(io_service);
|
---|
514 |
|
---|
515 | boost::thread t(boost::bind(RunThread, &io_service));
|
---|
516 | // boost::thread t(boost::bind(&StateMachineFSC<S>::Run, &io_service));
|
---|
517 |
|
---|
518 | if (conf.Has("exec"))
|
---|
519 | {
|
---|
520 | const vector<string> v = conf.Get<vector<string>>("exec");
|
---|
521 | for (vector<string>::const_iterator it=v.begin(); it!=v.end(); it++)
|
---|
522 | shell.Execute(*it);
|
---|
523 | }
|
---|
524 |
|
---|
525 | shell.Run(); // Run the shell
|
---|
526 | io_service.Stop(); // Signal Loop-thread to stop
|
---|
527 | // io_service.Close(); // Obsolete, done by the destructor
|
---|
528 |
|
---|
529 | // Wait until the StateMachine has finished its thread
|
---|
530 | // before returning and destroying the dim objects which might
|
---|
531 | // still be in use.
|
---|
532 | t.join();
|
---|
533 |
|
---|
534 | return 0;
|
---|
535 | }
|
---|
536 |
|
---|
537 | void SetupConfiguration(Configuration &conf)
|
---|
538 | {
|
---|
539 | const string n = conf.GetName()+".log";
|
---|
540 |
|
---|
541 | po::options_description config("Program options");
|
---|
542 | config.add_options()
|
---|
543 | ("dns", var<string>("localhost"), "Dim nameserver host name (Overwites DIM_DNS_NODE environment variable)")
|
---|
544 | ("log,l", var<string>(n), "Write log-file")
|
---|
545 | ("no-dim,d", po_bool(), "Disable dim services")
|
---|
546 | ("console,c", var<int>(), "Use console (0=shell, 1=simple buffered, X=simple unbuffered)")
|
---|
547 | ("exec,e", vars<string>(), "Execute one or more scrips at startup")
|
---|
548 | ;
|
---|
549 |
|
---|
550 | po::options_description control("FTM control options");
|
---|
551 | control.add_options()
|
---|
552 | ("addr,a", var<string>("localhost:5000"), "Network address of FTM")
|
---|
553 | ("quiet,q", po_bool(), "Disable printing contents of all received messages (except dynamic data) in clear text.")
|
---|
554 | ;
|
---|
555 |
|
---|
556 | conf.AddEnv("dns", "DIM_DNS_NODE");
|
---|
557 |
|
---|
558 | conf.AddOptions(config);
|
---|
559 | conf.AddOptions(control);
|
---|
560 | }
|
---|
561 |
|
---|
562 | /*
|
---|
563 | Extract usage clause(s) [if any] for SYNOPSIS.
|
---|
564 | Translators: "Usage" and "or" here are patterns (regular expressions) which
|
---|
565 | are used to match the usage synopsis in program output. An example from cp
|
---|
566 | (GNU coreutils) which contains both strings:
|
---|
567 | Usage: cp [OPTION]... [-T] SOURCE DEST
|
---|
568 | or: cp [OPTION]... SOURCE... DIRECTORY
|
---|
569 | or: cp [OPTION]... -t DIRECTORY SOURCE...
|
---|
570 | */
|
---|
571 | void PrintUsage()
|
---|
572 | {
|
---|
573 | cout <<
|
---|
574 | "The ftmctrl controls the FTM (FACT Trigger Master) board.\n"
|
---|
575 | "\n"
|
---|
576 | "The default is that the program is started without user intercation. "
|
---|
577 | "All actions are supposed to arrive as DimCommands. Using the -c "
|
---|
578 | "option, a local shell can be initialized. With h or help a short "
|
---|
579 | "help message about the usuage can be brought to the screen.\n"
|
---|
580 | "\n"
|
---|
581 | "Usage: fscctrl [-c type] [OPTIONS]\n"
|
---|
582 | " or: fscctrl [OPTIONS]\n";
|
---|
583 | cout << endl;
|
---|
584 | }
|
---|
585 |
|
---|
586 | void PrintHelp()
|
---|
587 | {
|
---|
588 | /* Additional help text which is printed after the configuration
|
---|
589 | options goes here */
|
---|
590 |
|
---|
591 | /*
|
---|
592 | cout << "bla bla bla" << endl << endl;
|
---|
593 | cout << endl;
|
---|
594 | cout << "Environment:" << endl;
|
---|
595 | cout << "environment" << endl;
|
---|
596 | cout << endl;
|
---|
597 | cout << "Examples:" << endl;
|
---|
598 | cout << "test exam" << endl;
|
---|
599 | cout << endl;
|
---|
600 | cout << "Files:" << endl;
|
---|
601 | cout << "files" << endl;
|
---|
602 | cout << endl;
|
---|
603 | */
|
---|
604 | }
|
---|
605 |
|
---|
606 | int main(int argc, const char* argv[])
|
---|
607 | {
|
---|
608 | Configuration conf(argv[0]);
|
---|
609 | conf.SetPrintUsage(PrintUsage);
|
---|
610 | SetupConfiguration(conf);
|
---|
611 |
|
---|
612 | po::variables_map vm;
|
---|
613 | try
|
---|
614 | {
|
---|
615 | vm = conf.Parse(argc, argv);
|
---|
616 | }
|
---|
617 | #if BOOST_VERSION > 104000
|
---|
618 | catch (po::multiple_occurrences &e)
|
---|
619 | {
|
---|
620 | cerr << "Program options invalid due to: " << e.what() << " of '" << e.get_option_name() << "'." << endl;
|
---|
621 | return -1;
|
---|
622 | }
|
---|
623 | #endif
|
---|
624 | catch (exception& e)
|
---|
625 | {
|
---|
626 | cerr << "Program options invalid due to: " << e.what() << endl;
|
---|
627 | return -1;
|
---|
628 | }
|
---|
629 |
|
---|
630 | if (conf.HasVersion() || conf.HasPrint())
|
---|
631 | return -1;
|
---|
632 |
|
---|
633 | if (conf.HasHelp())
|
---|
634 | {
|
---|
635 | PrintHelp();
|
---|
636 | return -1;
|
---|
637 | }
|
---|
638 |
|
---|
639 | Dim::Setup(conf.Get<string>("dns"));
|
---|
640 |
|
---|
641 | //try
|
---|
642 | {
|
---|
643 | // No console access at all
|
---|
644 | if (!conf.Has("console"))
|
---|
645 | {
|
---|
646 | if (conf.Get<bool>("no-dim"))
|
---|
647 | return RunDim<StateMachine, ConnectionFSC>(conf);
|
---|
648 | else
|
---|
649 | return RunDim<StateMachineDim, ConnectionDimFSC>(conf);
|
---|
650 | }
|
---|
651 | // Cosole access w/ and w/o Dim
|
---|
652 | if (conf.Get<bool>("no-dim"))
|
---|
653 | {
|
---|
654 | if (conf.Get<int>("console")==0)
|
---|
655 | return RunShell<LocalShell, StateMachine, ConnectionFSC>(conf);
|
---|
656 | else
|
---|
657 | return RunShell<LocalConsole, StateMachine, ConnectionFSC>(conf);
|
---|
658 | }
|
---|
659 | else
|
---|
660 | {
|
---|
661 | if (conf.Get<int>("console")==0)
|
---|
662 | return RunShell<LocalShell, StateMachineDim, ConnectionDimFSC>(conf);
|
---|
663 | else
|
---|
664 | return RunShell<LocalConsole, StateMachineDim, ConnectionDimFSC>(conf);
|
---|
665 | }
|
---|
666 | }
|
---|
667 | /*catch (std::exception& e)
|
---|
668 | {
|
---|
669 | cerr << "Exception: " << e.what() << endl;
|
---|
670 | return -1;
|
---|
671 | }*/
|
---|
672 |
|
---|
673 | return 0;
|
---|
674 | }
|
---|