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 | const bool backlog = wout.GetBacklog();
|
---|
53 | const bool null = wout.GetNullOutput();
|
---|
54 | wout.SetBacklog(false);
|
---|
55 | wout.SetNullOutput(false);
|
---|
56 | wout.Display(true);
|
---|
57 |
|
---|
58 | if (conf.Has("log"))
|
---|
59 | if (!wout.OpenLogFile(conf.Get<string>("log")))
|
---|
60 | win << kRed << "ERROR - Couldn't open log-file " << conf.Get<string>("log") << ": " << strerror(errno) << endl;
|
---|
61 |
|
---|
62 | S io_service(wout);
|
---|
63 |
|
---|
64 | const boost::filesystem::path path(conf.GetName());
|
---|
65 |
|
---|
66 | const string pname = path.parent_path().string();
|
---|
67 | const string fname = path.filename();
|
---|
68 |
|
---|
69 | const Time now;
|
---|
70 | io_service.Write(now, "/----------------------- Program ------------------------");
|
---|
71 | io_service.Write(now, "| Program: "PACKAGE_STRING" ("+fname+")");
|
---|
72 | io_service.Write(now, "| CallPath: "+pname);
|
---|
73 | io_service.Write(now, "| Compiled: "__DATE__" "__TIME__);
|
---|
74 | io_service.Write(now, "| Revision: "REVISION);
|
---|
75 | io_service.Write(now, "| Contact: "PACKAGE_BUGREPORT);
|
---|
76 | io_service.Write(now, "| URL: "PACKAGE_URL);
|
---|
77 | io_service.Write(now, "| Start: "+now.GetAsStr("%c"));
|
---|
78 | io_service.Write(now, "\\----------------------- Options ------------------------");
|
---|
79 | const multimap<string,string> mmap = conf.GetOptions();
|
---|
80 | for (multimap<string,string>::const_iterator it=mmap.begin(); it!=mmap.end(); it++)
|
---|
81 | io_service.Write(now, ": "+it->first+(it->second.empty()?"":" = ")+it->second);
|
---|
82 | io_service.Write(now, "\\------------------- Evaluating options -----------------");
|
---|
83 | const int rc = io_service.EvalOptions(conf);
|
---|
84 | if (rc>=0)
|
---|
85 | {
|
---|
86 | ostringstream str;
|
---|
87 | str << "Exit triggered by EvalOptions with rc=" << rc;
|
---|
88 | io_service.Write(now, str.str(), MessageImp::kError);
|
---|
89 | return rc;
|
---|
90 | }
|
---|
91 |
|
---|
92 | const map<string,string> &wco = conf.GetWildcardOptions();
|
---|
93 | if (wco.size()>0)
|
---|
94 | {
|
---|
95 | io_service.Write(now, "------------- Unrecognized wildcard options -------------", MessageImp::kWarn);
|
---|
96 |
|
---|
97 | size_t max = 0;
|
---|
98 | for (map<string,string>::const_iterator it=wco.begin(); it!=wco.end(); it++)
|
---|
99 | if (it->second.length()>max)
|
---|
100 | max = it->second.length();
|
---|
101 |
|
---|
102 | for (map<string,string>::const_iterator it=wco.begin(); it!=wco.end(); it++)
|
---|
103 | {
|
---|
104 | ostringstream str;
|
---|
105 | str.setf(ios_base::left);
|
---|
106 | str << setw(max+1) << it->second << " : " << it->first;
|
---|
107 | io_service.Write(now, str.str(), MessageImp::kWarn);
|
---|
108 | }
|
---|
109 | io_service.Write(now, "Unrecognized options found, will exit with rc=127", MessageImp::kError);
|
---|
110 | return 127;
|
---|
111 | }
|
---|
112 |
|
---|
113 | io_service.Message("==================== Starting main loop =================");
|
---|
114 |
|
---|
115 | wout.SetNullOutput(null);
|
---|
116 | wout.SetBacklog(backlog);
|
---|
117 |
|
---|
118 | shell.SetReceiver(io_service);
|
---|
119 |
|
---|
120 | // boost::thread t(boost::bind(&AutoScheduler<S>::Run, &io_service));
|
---|
121 | thread t(bind(MainThread, &io_service, dummy));
|
---|
122 |
|
---|
123 | const vector<string> v1 = conf.Vec<string>("cmd");
|
---|
124 | for (vector<string>::const_iterator it=v1.begin(); it!=v1.end(); it++)
|
---|
125 | shell.ProcessLine(*it);
|
---|
126 |
|
---|
127 | const vector<string> v2 = conf.Vec<string>("exec");
|
---|
128 | for (vector<string>::const_iterator it=v2.begin(); it!=v2.end(); it++)
|
---|
129 | shell.Execute(*it);
|
---|
130 |
|
---|
131 | if (conf.Get<bool>("quit"))
|
---|
132 | shell.Stop();
|
---|
133 |
|
---|
134 | shell.Run(); // Run the shell
|
---|
135 | io_service.Stop(); // Signal Loop-thread to stop
|
---|
136 | // io_service.Close(); // Obsolete, done by the destructor
|
---|
137 | // wout << "join: " << t.timed_join(boost::posix_time::milliseconds(0)) << endl;
|
---|
138 |
|
---|
139 | // Wait until the StateMachine has finished its thread
|
---|
140 | // before returning and destroying the dim objects which might
|
---|
141 | // still be in use.
|
---|
142 | t.join();
|
---|
143 |
|
---|
144 | return 0;
|
---|
145 | }
|
---|
146 |
|
---|
147 | #endif
|
---|