| 1 | #ifndef FACT_MAIN
|
|---|
| 2 | #define FACT_MAIN
|
|---|
| 3 |
|
|---|
| 4 | #if BOOST_VERSION < 104400
|
|---|
| 5 | #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 4))
|
|---|
| 6 | #undef BOOST_HAS_RVALUE_REFS
|
|---|
| 7 | #endif
|
|---|
| 8 | #endif
|
|---|
| 9 | #include <boost/thread.hpp>
|
|---|
| 10 |
|
|---|
| 11 | #include "LocalControl.h"
|
|---|
| 12 |
|
|---|
| 13 | void MainThread(StateMachineImp *io_service, bool dummy)
|
|---|
| 14 | {
|
|---|
| 15 | // This is necessary so that the StateMachien Thread can signal the
|
|---|
| 16 | // Readline to exit
|
|---|
| 17 | io_service->Run(dummy);
|
|---|
| 18 | Readline::Stop();
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | /*
|
|---|
| 22 | template<class S>
|
|---|
| 23 | int RunDim(Configuration &conf)
|
|---|
| 24 | {
|
|---|
| 25 | WindowLog wout;
|
|---|
| 26 |
|
|---|
| 27 | ReadlineColor::PrintBootMsg(wout, conf.GetName(), false);
|
|---|
| 28 |
|
|---|
| 29 | //log.SetWindow(stdscr);
|
|---|
| 30 | if (conf.Has("log"))
|
|---|
| 31 | if (!wout.OpenLogFile(conf.Get<string>("log")))
|
|---|
| 32 | wout << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
|
|---|
| 33 |
|
|---|
| 34 | // Start io_service.Run to use the StateMachineImp::Run() loop
|
|---|
| 35 | // Start io_service.run to only use the commandHandler command detaching
|
|---|
| 36 | AutoScheduler<S> io_service(wout);
|
|---|
| 37 | if (!io_service.EvalConfiguration(conf))
|
|---|
| 38 | return -1;
|
|---|
| 39 |
|
|---|
| 40 | io_service.Run();
|
|---|
| 41 |
|
|---|
| 42 | return 0;
|
|---|
| 43 | }
|
|---|
| 44 | */
|
|---|
| 45 |
|
|---|
| 46 | template<class T, class S>
|
|---|
| 47 | int Main(const Configuration &conf, bool dummy=false)
|
|---|
| 48 | {
|
|---|
| 49 | static T shell(conf.GetName().c_str(), conf.Get<int>("console")!=1);
|
|---|
| 50 |
|
|---|
| 51 | WindowLog &win = shell.GetStreamIn();
|
|---|
| 52 | WindowLog &wout = shell.GetStreamOut();
|
|---|
| 53 |
|
|---|
| 54 | if (conf.Has("log"))
|
|---|
| 55 | if (!wout.OpenLogFile(conf.Get<string>("log")))
|
|---|
| 56 | win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
|
|---|
| 57 |
|
|---|
| 58 | S io_service(wout);
|
|---|
| 59 | const int rc = io_service.EvalConfiguration(conf);
|
|---|
| 60 | if (rc>=0)
|
|---|
| 61 | return rc;
|
|---|
| 62 |
|
|---|
| 63 | shell.SetReceiver(io_service);
|
|---|
| 64 |
|
|---|
| 65 | // boost::thread t(boost::bind(&AutoScheduler<S>::Run, &io_service));
|
|---|
| 66 | boost::thread t(boost::bind(MainThread, &io_service, dummy));
|
|---|
| 67 |
|
|---|
| 68 | if (conf.Has("cmd"))
|
|---|
| 69 | {
|
|---|
| 70 | const vector<string> v = conf.Get<vector<string>>("cmd");
|
|---|
| 71 | for (vector<string>::const_iterator it=v.begin(); it!=v.end(); it++)
|
|---|
| 72 | shell.ProcessLine(*it);
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | if (conf.Has("exec"))
|
|---|
| 76 | {
|
|---|
| 77 | const vector<string> v = conf.Get<vector<string>>("exec");
|
|---|
| 78 | for (vector<string>::const_iterator it=v.begin(); it!=v.end(); it++)
|
|---|
| 79 | shell.Execute(*it);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | if (conf.Get<bool>("quit"))
|
|---|
| 83 | shell.Stop();
|
|---|
| 84 |
|
|---|
| 85 | shell.Run(); // Run the shell
|
|---|
| 86 | io_service.Stop(); // Signal Loop-thread to stop
|
|---|
| 87 | // io_service.Close(); // Obsolete, done by the destructor
|
|---|
| 88 | // wout << "join: " << t.timed_join(boost::posix_time::milliseconds(0)) << endl;
|
|---|
| 89 |
|
|---|
| 90 | // Wait until the StateMachine has finished its thread
|
|---|
| 91 | // before returning and destroying the dim objects which might
|
|---|
| 92 | // still be in use.
|
|---|
| 93 | t.join();
|
|---|
| 94 |
|
|---|
| 95 | return 0;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | #endif
|
|---|