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(),
|
---|
50 | conf.Has("console") ? conf.Get<int>("console")!=1 : 0);
|
---|
51 |
|
---|
52 | WindowLog &win = shell.GetStreamIn();
|
---|
53 | WindowLog &wout = shell.GetStreamOut();
|
---|
54 |
|
---|
55 | if (conf.Has("log"))
|
---|
56 | if (!wout.OpenLogFile(conf.Get<string>("log")))
|
---|
57 | win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
|
---|
58 |
|
---|
59 |
|
---|
60 | S io_service(wout);
|
---|
61 |
|
---|
62 | const Time now;
|
---|
63 | io_service.Write(now, "/--------------------------- Program ----------------------------");
|
---|
64 | io_service.Write(now, "| Program: "PACKAGE_STRING" ("+conf.GetName()+")");
|
---|
65 | io_service.Write(now, "| Compiled: "__DATE__" "__TIME__);
|
---|
66 | io_service.Write(now, "| Revision: "REVISION);
|
---|
67 | io_service.Write(now, "| Contact: "PACKAGE_BUGREPORT);
|
---|
68 | io_service.Write(now, "| URL: "PACKAGE_URL);
|
---|
69 | io_service.Write(now, "| Start: "+now.GetAsStr("%c"));
|
---|
70 | io_service.Write(now, "\\--------------------------- Options ----------------------------");
|
---|
71 | const multimap<string,string> map = conf.GetOptions();
|
---|
72 | for (multimap<string,string>::const_iterator it=map.begin(); it!=map.end(); it++)
|
---|
73 | io_service.Write(now, ": "+it->first+(it->second.empty()?"":" = ")+it->second);
|
---|
74 | io_service.Write(now, "\\----------------------- Evaluating options ---------------------");
|
---|
75 |
|
---|
76 | const int rc = io_service.EvalConfiguration(conf);
|
---|
77 | io_service.Message("======================== Starting main loop =====================");
|
---|
78 | if (rc>=0)
|
---|
79 | return rc;
|
---|
80 |
|
---|
81 | shell.SetReceiver(io_service);
|
---|
82 |
|
---|
83 | // boost::thread t(boost::bind(&AutoScheduler<S>::Run, &io_service));
|
---|
84 | boost::thread t(boost::bind(MainThread, &io_service, dummy));
|
---|
85 |
|
---|
86 | const vector<string> v1 = conf.Vec<string>("cmd");
|
---|
87 | for (vector<string>::const_iterator it=v1.begin(); it!=v1.end(); it++)
|
---|
88 | shell.ProcessLine(*it);
|
---|
89 |
|
---|
90 | const vector<string> v2 = conf.Vec<string>("exec");
|
---|
91 | for (vector<string>::const_iterator it=v2.begin(); it!=v2.end(); it++)
|
---|
92 | shell.Execute(*it);
|
---|
93 |
|
---|
94 | if (conf.Get<bool>("quit"))
|
---|
95 | shell.Stop();
|
---|
96 |
|
---|
97 | shell.Run(); // Run the shell
|
---|
98 | io_service.Stop(); // Signal Loop-thread to stop
|
---|
99 | // io_service.Close(); // Obsolete, done by the destructor
|
---|
100 | // wout << "join: " << t.timed_join(boost::posix_time::milliseconds(0)) << endl;
|
---|
101 |
|
---|
102 | // Wait until the StateMachine has finished its thread
|
---|
103 | // before returning and destroying the dim objects which might
|
---|
104 | // still be in use.
|
---|
105 | t.join();
|
---|
106 |
|
---|
107 | return 0;
|
---|
108 | }
|
---|
109 |
|
---|
110 | #endif
|
---|