source: trunk/FACT++/src/fscctrl.cc@ 11059

Last change on this file since 11059 was 11057, checked in by tbretz, 13 years ago
Removed RunDim and replaces it with RunShell and LocalStream
File size: 18.4 KB
Line 
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
26namespace ba = boost::asio;
27namespace bs = boost::system;
28namespace dummy = ba::placeholders;
29
30using namespace std;
31
32// ------------------------------------------------------------------------
33
34class ConnectionFSC : public Connection
35{
36 boost::asio::streambuf fBuffer;
37
38 bool fIsVerbose;
39
40protected:
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
71private:
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
232public:
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
249class ConnectionDimFSC : public ConnectionFSC
250{
251private:
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
285public:
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
300template <class T, class S>
301class 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
314private:
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
388public:
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
443void 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/*
452template<class S, class T>
453int RunDim(Configuration &conf)
454{
455 WindowLog wout;
456
457 ReadlineColor::PrintBootMsg(wout, conf.GetName(), false);
458
459
460 if (conf.Has("log"))
461 if (!wout.OpenLogFile(conf.Get<string>("log")))
462 wout << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
463
464 // Start io_service.Run to use the StateMachineImp::Run() loop
465 // Start io_service.run to only use the commandHandler command detaching
466 StateMachineFSC<S, T> io_service(wout);
467 if (!io_service.SetConfiguration(conf))
468 return -1;
469
470 io_service.Run();
471
472 return 0;
473}
474*/
475
476template<class T, class S, class R>
477int RunShell(Configuration &conf)
478{
479 static T shell(conf.GetName().c_str(), conf.Get<int>("console")!=1);
480
481 WindowLog &win = shell.GetStreamIn();
482 WindowLog &wout = shell.GetStreamOut();
483
484 if (conf.Has("log"))
485 if (!wout.OpenLogFile(conf.Get<string>("log")))
486 win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
487
488 StateMachineFSC<S, R> io_service(wout);
489 if (!io_service.SetConfiguration(conf))
490 return -1;
491
492 shell.SetReceiver(io_service);
493
494 boost::thread t(boost::bind(RunThread, &io_service));
495 // boost::thread t(boost::bind(&StateMachineFSC<S>::Run, &io_service));
496
497 if (conf.Has("exec"))
498 {
499 const vector<string> v = conf.Get<vector<string>>("exec");
500 for (vector<string>::const_iterator it=v.begin(); it!=v.end(); it++)
501 shell.Execute(*it);
502 }
503
504 shell.Run(); // Run the shell
505 io_service.Stop(); // Signal Loop-thread to stop
506 // io_service.Close(); // Obsolete, done by the destructor
507
508 // Wait until the StateMachine has finished its thread
509 // before returning and destroying the dim objects which might
510 // still be in use.
511 t.join();
512
513 return 0;
514}
515
516void SetupConfiguration(Configuration &conf)
517{
518 const string n = conf.GetName()+".log";
519
520 po::options_description config("Program options");
521 config.add_options()
522 ("dns", var<string>("localhost"), "Dim nameserver host name (Overwites DIM_DNS_NODE environment variable)")
523 ("log,l", var<string>(n), "Write log-file")
524 ("no-dim,d", po_bool(), "Disable dim services")
525 ("console,c", var<int>(), "Use console (0=shell, 1=simple buffered, X=simple unbuffered)")
526 ("exec,e", vars<string>(), "Execute one or more scrips at startup")
527 ;
528
529 po::options_description control("FTM control options");
530 control.add_options()
531 ("addr,a", var<string>("localhost:5000"), "Network address of FTM")
532 ("quiet,q", po_bool(), "Disable printing contents of all received messages (except dynamic data) in clear text.")
533 ;
534
535 conf.AddEnv("dns", "DIM_DNS_NODE");
536
537 conf.AddOptions(config);
538 conf.AddOptions(control);
539}
540
541/*
542 Extract usage clause(s) [if any] for SYNOPSIS.
543 Translators: "Usage" and "or" here are patterns (regular expressions) which
544 are used to match the usage synopsis in program output. An example from cp
545 (GNU coreutils) which contains both strings:
546 Usage: cp [OPTION]... [-T] SOURCE DEST
547 or: cp [OPTION]... SOURCE... DIRECTORY
548 or: cp [OPTION]... -t DIRECTORY SOURCE...
549 */
550void PrintUsage()
551{
552 cout <<
553 "The ftmctrl controls the FTM (FACT Trigger Master) board.\n"
554 "\n"
555 "The default is that the program is started without user intercation. "
556 "All actions are supposed to arrive as DimCommands. Using the -c "
557 "option, a local shell can be initialized. With h or help a short "
558 "help message about the usuage can be brought to the screen.\n"
559 "\n"
560 "Usage: fscctrl [-c type] [OPTIONS]\n"
561 " or: fscctrl [OPTIONS]\n";
562 cout << endl;
563}
564
565void PrintHelp()
566{
567 /* Additional help text which is printed after the configuration
568 options goes here */
569
570 /*
571 cout << "bla bla bla" << endl << endl;
572 cout << endl;
573 cout << "Environment:" << endl;
574 cout << "environment" << endl;
575 cout << endl;
576 cout << "Examples:" << endl;
577 cout << "test exam" << endl;
578 cout << endl;
579 cout << "Files:" << endl;
580 cout << "files" << endl;
581 cout << endl;
582 */
583}
584
585int main(int argc, const char* argv[])
586{
587 Configuration conf(argv[0]);
588 conf.SetPrintUsage(PrintUsage);
589 SetupConfiguration(conf);
590
591 po::variables_map vm;
592 try
593 {
594 vm = conf.Parse(argc, argv);
595 }
596#if BOOST_VERSION > 104000
597 catch (po::multiple_occurrences &e)
598 {
599 cerr << "Program options invalid due to: " << e.what() << " of '" << e.get_option_name() << "'." << endl;
600 return -1;
601 }
602#endif
603 catch (exception& e)
604 {
605 cerr << "Program options invalid due to: " << e.what() << endl;
606 return -1;
607 }
608
609 if (conf.HasVersion() || conf.HasPrint())
610 return -1;
611
612 if (conf.HasHelp())
613 {
614 PrintHelp();
615 return -1;
616 }
617
618 Dim::Setup(conf.Get<string>("dns"));
619
620 //try
621 {
622 // No console access at all
623 if (!conf.Has("console"))
624 {
625 if (conf.Get<bool>("no-dim"))
626 return RunShell<LocalStream, StateMachine, ConnectionFSC>(conf);
627 else
628 return RunShell<LocalStream, StateMachineDim, ConnectionDimFSC>(conf);
629 }
630 // Cosole access w/ and w/o Dim
631 if (conf.Get<bool>("no-dim"))
632 {
633 if (conf.Get<int>("console")==0)
634 return RunShell<LocalShell, StateMachine, ConnectionFSC>(conf);
635 else
636 return RunShell<LocalConsole, StateMachine, ConnectionFSC>(conf);
637 }
638 else
639 {
640 if (conf.Get<int>("console")==0)
641 return RunShell<LocalShell, StateMachineDim, ConnectionDimFSC>(conf);
642 else
643 return RunShell<LocalConsole, StateMachineDim, ConnectionDimFSC>(conf);
644 }
645 }
646 /*catch (std::exception& e)
647 {
648 cerr << "Exception: " << e.what() << endl;
649 return -1;
650 }*/
651
652 return 0;
653}
Note: See TracBrowser for help on using the repository browser.