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