1 | #ifndef FACT_MAIN
|
---|
2 | #define FACT_MAIN
|
---|
3 |
|
---|
4 | #include <thread>
|
---|
5 | #include <functional>
|
---|
6 |
|
---|
7 | #include "LocalControl.h"
|
---|
8 |
|
---|
9 | void MainThread(StateMachineImp *io_service, bool dummy)
|
---|
10 | {
|
---|
11 | // This is necessary so that the StateMachien Thread can signal the
|
---|
12 | // Readline to exit
|
---|
13 | io_service->Run(dummy);
|
---|
14 | Readline::Stop();
|
---|
15 | }
|
---|
16 |
|
---|
17 | /*
|
---|
18 | template<class S>
|
---|
19 | int RunDim(Configuration &conf)
|
---|
20 | {
|
---|
21 | WindowLog wout;
|
---|
22 |
|
---|
23 | ReadlineColor::PrintBootMsg(wout, conf.GetName(), false);
|
---|
24 |
|
---|
25 | //log.SetWindow(stdscr);
|
---|
26 | if (conf.Has("log"))
|
---|
27 | if (!wout.OpenLogFile(conf.Get<string>("log")))
|
---|
28 | wout << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
|
---|
29 |
|
---|
30 | // Start io_service.Run to use the StateMachineImp::Run() loop
|
---|
31 | // Start io_service.run to only use the commandHandler command detaching
|
---|
32 | AutoScheduler<S> io_service(wout);
|
---|
33 | if (!io_service.EvalConfiguration(conf))
|
---|
34 | return -1;
|
---|
35 |
|
---|
36 | io_service.Run();
|
---|
37 |
|
---|
38 | return 0;
|
---|
39 | }
|
---|
40 | */
|
---|
41 |
|
---|
42 | template<class T, class S>
|
---|
43 | int Main(const Configuration &conf, bool dummy=false)
|
---|
44 | {
|
---|
45 | static T shell(conf.GetName().c_str(),
|
---|
46 | conf.Has("console") ? conf.Get<int>("console")!=1 : 0);
|
---|
47 |
|
---|
48 | WindowLog &win = shell.GetStreamIn();
|
---|
49 | WindowLog &wout = shell.GetStreamOut();
|
---|
50 |
|
---|
51 | if (conf.Has("log"))
|
---|
52 | if (!wout.OpenLogFile(conf.Get<string>("log")))
|
---|
53 | win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
|
---|
54 |
|
---|
55 | S io_service(wout);
|
---|
56 |
|
---|
57 | const boost::filesystem::path path(conf.GetName());
|
---|
58 |
|
---|
59 | const string pname = path.parent_path().string();
|
---|
60 | const string fname = path.filename();
|
---|
61 |
|
---|
62 | const Time now;
|
---|
63 | io_service.Write(now, "/----------------------- Program ------------------------");
|
---|
64 | io_service.Write(now, "| Program: "PACKAGE_STRING" ("+fname+")");
|
---|
65 | io_service.Write(now, "| CallPath: "+pname);
|
---|
66 | io_service.Write(now, "| Compiled: "__DATE__" "__TIME__);
|
---|
67 | io_service.Write(now, "| Revision: "REVISION);
|
---|
68 | io_service.Write(now, "| Contact: "PACKAGE_BUGREPORT);
|
---|
69 | io_service.Write(now, "| URL: "PACKAGE_URL);
|
---|
70 | io_service.Write(now, "| Start: "+now.GetAsStr("%c"));
|
---|
71 | io_service.Write(now, "\\----------------------- Options ------------------------");
|
---|
72 | const multimap<string,string> map = conf.GetOptions();
|
---|
73 | for (multimap<string,string>::const_iterator it=map.begin(); it!=map.end(); it++)
|
---|
74 | io_service.Write(now, ": "+it->first+(it->second.empty()?"":" = ")+it->second);
|
---|
75 | io_service.Write(now, "\\------------------- Evaluating options -----------------");
|
---|
76 |
|
---|
77 | const int rc = io_service.EvalConfiguration(conf);
|
---|
78 | io_service.Message("==================== Starting main loop =================");
|
---|
79 | if (rc>=0)
|
---|
80 | return rc;
|
---|
81 |
|
---|
82 | shell.SetReceiver(io_service);
|
---|
83 |
|
---|
84 | // boost::thread t(boost::bind(&AutoScheduler<S>::Run, &io_service));
|
---|
85 | thread t(bind(MainThread, &io_service, dummy));
|
---|
86 |
|
---|
87 | const vector<string> v1 = conf.Vec<string>("cmd");
|
---|
88 | for (vector<string>::const_iterator it=v1.begin(); it!=v1.end(); it++)
|
---|
89 | shell.ProcessLine(*it);
|
---|
90 |
|
---|
91 | const vector<string> v2 = conf.Vec<string>("exec");
|
---|
92 | for (vector<string>::const_iterator it=v2.begin(); it!=v2.end(); it++)
|
---|
93 | shell.Execute(*it);
|
---|
94 |
|
---|
95 | if (conf.Get<bool>("quit"))
|
---|
96 | shell.Stop();
|
---|
97 |
|
---|
98 | shell.Run(); // Run the shell
|
---|
99 | io_service.Stop(); // Signal Loop-thread to stop
|
---|
100 | // io_service.Close(); // Obsolete, done by the destructor
|
---|
101 | // wout << "join: " << t.timed_join(boost::posix_time::milliseconds(0)) << endl;
|
---|
102 |
|
---|
103 | // Wait until the StateMachine has finished its thread
|
---|
104 | // before returning and destroying the dim objects which might
|
---|
105 | // still be in use.
|
---|
106 | t.join();
|
---|
107 |
|
---|
108 | return 0;
|
---|
109 | }
|
---|
110 |
|
---|
111 | #endif
|
---|